OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CollectionsSTL_h | 5 #ifndef CollectionsSTL_h |
6 #define CollectionsSTL_h | 6 #define CollectionsSTL_h |
7 | 7 |
8 #include "wtf/Allocator.h" | 8 #include "platform/inspector_protocol/String16.h" |
9 #include "wtf/HashMap.h" | 9 #include "wtf/OwnPtr.h" |
10 #include "wtf/PassOwnPtr.h" | 10 #include "wtf/PassOwnPtr.h" |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <unordered_map> | 13 #include <unordered_map> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 namespace std { | 16 namespace std { |
17 template<> | 17 template<> |
18 struct hash<String> { | 18 struct hash<String16> { |
19 std::size_t operator()(const String& k) const { return StringHash::hash(k); } | 19 std::size_t operator()(const String16& k) const |
20 { | |
21 size_t hash = 0; | |
kozy
2016/04/12 17:09:59
StringHash::hash caches result. Can we add cache h
| |
22 for (size_t i = 0; i < k.length(); ++i) | |
23 hash += 31 * k[i]; | |
alph
2016/04/12 19:05:25
looks like a very bad hash. This is basically just
kozy
2016/04/12 19:07:50
Or we can reuse calculateHash from V8DebuggerAgent
| |
24 return hash; | |
25 } | |
20 }; | 26 }; |
21 } | 27 } |
22 | 28 |
23 namespace blink { | 29 namespace blink { |
24 namespace protocol { | 30 namespace protocol { |
25 | 31 |
26 template <typename T> | 32 template <typename T> |
27 class Vector { | 33 class Vector { |
28 public: | 34 public: |
29 Vector() { } | 35 Vector() { } |
(...skipping 25 matching lines...) Expand all Loading... | |
55 private: | 61 private: |
56 std::vector<T> m_impl; | 62 std::vector<T> m_impl; |
57 }; | 63 }; |
58 | 64 |
59 template <typename T> | 65 template <typename T> |
60 class Vector<OwnPtr<T>> { | 66 class Vector<OwnPtr<T>> { |
61 WTF_MAKE_NONCOPYABLE(Vector); | 67 WTF_MAKE_NONCOPYABLE(Vector); |
62 public: | 68 public: |
63 Vector() { } | 69 Vector() { } |
64 Vector(size_t capacity) : m_impl(capacity) { } | 70 Vector(size_t capacity) : m_impl(capacity) { } |
71 Vector(Vector&& other) : m_impl(std::move(other.m_impl)) { } | |
65 ~Vector() { } | 72 ~Vector() { } |
66 | 73 |
67 typedef typename std::vector<OwnPtr<T>>::iterator iterator; | 74 typedef typename std::vector<OwnPtr<T>>::iterator iterator; |
68 typedef typename std::vector<OwnPtr<T>>::const_iterator const_iterator; | 75 typedef typename std::vector<OwnPtr<T>>::const_iterator const_iterator; |
69 | 76 |
70 iterator begin() { return m_impl.begin(); } | 77 iterator begin() { return m_impl.begin(); } |
71 iterator end() { return m_impl.end(); } | 78 iterator end() { return m_impl.end(); } |
72 const_iterator begin() const { return m_impl.begin(); } | 79 const_iterator begin() const { return m_impl.begin(); } |
73 const_iterator end() const { return m_impl.end(); } | 80 const_iterator end() const { return m_impl.end(); } |
74 | 81 |
75 void resize(size_t s) { m_impl.resize(s); } | 82 void resize(size_t s) { m_impl.resize(s); } |
76 size_t size() const { return m_impl.size(); } | 83 size_t size() const { return m_impl.size(); } |
77 bool isEmpty() const { return !m_impl.size(); } | 84 bool isEmpty() const { return !m_impl.size(); } |
78 OwnPtr<T>& operator[](size_t i) { return at(i); } | 85 OwnPtr<T>& operator[](size_t i) { return at(i); } |
79 const OwnPtr<T>& operator[](size_t i) const { return at(i); } | 86 const OwnPtr<T>& operator[](size_t i) const { return at(i); } |
80 OwnPtr<T>& at(size_t i) { return m_impl[i]; } | 87 OwnPtr<T>& at(size_t i) { return m_impl[i]; } |
81 const OwnPtr<T>& at(size_t i) const { return m_impl.at(i); } | 88 const OwnPtr<T>& at(size_t i) const { return m_impl.at(i); } |
82 OwnPtr<T>& last() { return m_impl[m_impl.size() - 1]; } | 89 OwnPtr<T>& last() { return m_impl[m_impl.size() - 1]; } |
83 const OwnPtr<T>& last() const { return m_impl[m_impl.size() - 1]; } | 90 const OwnPtr<T>& last() const { return m_impl[m_impl.size() - 1]; } |
84 void append(const PassOwnPtr<T>& t) { m_impl.push_back(t); } | 91 void append(const PassOwnPtr<T>& t) { m_impl.push_back(t); } |
85 void prepend(const PassOwnPtr<T>& t) { m_impl.insert(m_impl.begin(), t); } | 92 void prepend(const PassOwnPtr<T>& t) { m_impl.insert(m_impl.begin(), t); } |
86 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } | 93 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } |
87 void clear() { m_impl.clear(); } | 94 void clear() { m_impl.clear(); } |
88 void swap(Vector& other) { m_impl.swap(other.m_impl); } | 95 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
96 void swap(Vector&& other) { m_impl.swap(other.m_impl); } | |
89 void removeLast() { m_impl.pop_back(); } | 97 void removeLast() { m_impl.pop_back(); } |
90 | 98 |
91 private: | 99 private: |
92 std::vector<OwnPtr<T>> m_impl; | 100 std::vector<OwnPtr<T>> m_impl; |
93 }; | 101 }; |
94 | 102 |
95 template <typename K, typename V, typename I> | 103 template <typename K, typename V, typename I> |
96 class HashMapIterator { | 104 class HashMapIterator { |
97 STACK_ALLOCATED(); | 105 STACK_ALLOCATED(); |
98 public: | 106 public: |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 template <typename K> | 221 template <typename K> |
214 class HashSet : public protocol::HashMap<K, K> { | 222 class HashSet : public protocol::HashMap<K, K> { |
215 public: | 223 public: |
216 void add(const K& k) { this->set(k, k); } | 224 void add(const K& k) { this->set(k, k); } |
217 }; | 225 }; |
218 | 226 |
219 } // namespace platform | 227 } // namespace platform |
220 } // namespace blink | 228 } // namespace blink |
221 | 229 |
222 #endif // !defined(CollectionsSTL_h) | 230 #endif // !defined(CollectionsSTL_h) |
OLD | NEW |