Chromium Code Reviews| 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 "platform/inspector_protocol/Platform.h" | |
| 8 #include "platform/inspector_protocol/String16.h" | 9 #include "platform/inspector_protocol/String16.h" |
| 9 #include "wtf/PtrUtil.h" | |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <unordered_map> | 12 #include <map> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 | |
| 16 namespace blink { | 15 namespace blink { |
| 17 namespace protocol { | 16 namespace protocol { |
| 18 | 17 |
| 19 template <typename T> | 18 template <typename T> |
| 20 class Vector { | 19 class Vector { |
| 21 public: | 20 public: |
| 22 Vector() { } | 21 Vector() { } |
| 23 Vector(size_t capacity) : m_impl(capacity) { } | 22 Vector(size_t capacity) : m_impl(capacity) { } |
| 24 typedef typename std::vector<T>::iterator iterator; | 23 typedef typename std::vector<T>::iterator iterator; |
| 25 typedef typename std::vector<T>::const_iterator const_iterator; | 24 typedef typename std::vector<T>::const_iterator const_iterator; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 44 void clear() { m_impl.clear(); } | 43 void clear() { m_impl.clear(); } |
| 45 void swap(Vector& other) { m_impl.swap(other.m_impl); } | 44 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
| 46 void removeLast() { m_impl.pop_back(); } | 45 void removeLast() { m_impl.pop_back(); } |
| 47 | 46 |
| 48 private: | 47 private: |
| 49 std::vector<T> m_impl; | 48 std::vector<T> m_impl; |
| 50 }; | 49 }; |
| 51 | 50 |
| 52 template <typename T> | 51 template <typename T> |
| 53 class Vector<std::unique_ptr<T>> { | 52 class Vector<std::unique_ptr<T>> { |
| 54 WTF_MAKE_NONCOPYABLE(Vector); | |
| 55 public: | 53 public: |
| 56 Vector() { } | 54 Vector() { } |
| 57 Vector(size_t capacity) : m_impl(capacity) { } | 55 Vector(size_t capacity) : m_impl(capacity) { } |
| 58 Vector(Vector&& other) : m_impl(std::move(other.m_impl)) { } | 56 Vector(Vector&& other) { m_impl.swap(other.m_impl); } |
| 59 ~Vector() { } | 57 ~Vector() { clear(); } |
| 60 | 58 |
| 61 typedef typename std::vector<std::unique_ptr<T>>::iterator iterator; | 59 typedef typename std::vector<T*>::iterator iterator; |
| 62 typedef typename std::vector<std::unique_ptr<T>>::const_iterator const_itera tor; | 60 typedef typename std::vector<T*>::const_iterator const_iterator; |
| 63 | 61 |
| 64 iterator begin() { return m_impl.begin(); } | 62 iterator begin() { return m_impl.begin(); } |
| 65 iterator end() { return m_impl.end(); } | 63 iterator end() { return m_impl.end(); } |
| 66 const_iterator begin() const { return m_impl.begin(); } | 64 const_iterator begin() const { return m_impl.begin(); } |
| 67 const_iterator end() const { return m_impl.end(); } | 65 const_iterator end() const { return m_impl.end(); } |
| 68 | 66 |
| 69 void resize(size_t s) { m_impl.resize(s); } | 67 void resize(size_t s) { m_impl.resize(s); } |
| 70 size_t size() const { return m_impl.size(); } | 68 size_t size() const { return m_impl.size(); } |
| 71 bool isEmpty() const { return !m_impl.size(); } | 69 bool isEmpty() const { return !m_impl.size(); } |
| 72 std::unique_ptr<T>& operator[](size_t i) { return at(i); } | 70 T* operator[](size_t i) { return at(i); } |
| 73 const std::unique_ptr<T>& operator[](size_t i) const { return at(i); } | 71 const T* operator[](size_t i) const { return at(i); } |
| 74 std::unique_ptr<T>& at(size_t i) { return m_impl[i]; } | 72 T* at(size_t i) { return m_impl[i]; } |
| 75 const std::unique_ptr<T>& at(size_t i) const { return m_impl.at(i); } | 73 const T* at(size_t i) const { return m_impl.at(i); } |
| 76 std::unique_ptr<T>& last() { return m_impl[m_impl.size() - 1]; } | 74 T* last() { return m_impl[m_impl.size() - 1]; } |
| 77 const std::unique_ptr<T>& last() const { return m_impl[m_impl.size() - 1]; } | 75 const T* last() const { return m_impl[m_impl.size() - 1]; } |
| 78 void append(std::unique_ptr<T> t) { m_impl.emplace_back(std::move(t)); } | 76 void append(std::unique_ptr<T> t) { m_impl.push_back(t.release()); } |
| 79 void prepend(std::unique_ptr<T> t) { m_impl.insert(m_impl.begin(), std::move (t)); } | 77 void prepend(std::unique_ptr<T> t) { m_impl.insert(m_impl.begin(), t.release ()); } |
| 80 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } | 78 |
| 81 void clear() { m_impl.clear(); } | 79 void remove(size_t i) |
| 80 { | |
| 81 delete m_impl[i]; | |
| 82 m_impl.erase(m_impl.begin() + i); | |
| 83 } | |
| 84 | |
| 85 void clear() | |
| 86 { | |
| 87 for (auto t : m_impl) | |
| 88 delete t; | |
| 89 m_impl.clear(); | |
| 90 } | |
| 91 | |
| 82 void swap(Vector& other) { m_impl.swap(other.m_impl); } | 92 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
| 83 void swap(Vector&& other) { m_impl.swap(other.m_impl); } | 93 void swap(Vector&& other) { m_impl.swap(other.m_impl); } |
| 84 void removeLast() { m_impl.pop_back(); } | 94 void removeLast() |
| 95 { | |
| 96 delete last(); | |
| 97 m_impl.pop_back(); | |
| 98 } | |
| 85 | 99 |
| 86 private: | 100 private: |
| 87 std::vector<std::unique_ptr<T>> m_impl; | 101 Vector(const Vector&) = delete; |
| 102 Vector& operator=(const Vector&) = delete; | |
| 103 std::vector<T*> m_impl; | |
| 88 }; | 104 }; |
| 89 | 105 |
| 90 template <typename K, typename V, typename I> | 106 template <typename K, typename V, typename I> |
| 91 class HashMapIterator { | 107 class HashMapIterator { |
| 92 STACK_ALLOCATED(); | |
| 93 public: | 108 public: |
| 94 HashMapIterator(const I& impl) : m_impl(impl) { } | 109 HashMapIterator(const I& impl) : m_impl(impl) { } |
| 95 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second = &m_impl->second; return &m_pair; } | 110 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second = &m_impl->second; return &m_pair; } |
| 96 std::pair<K, V*>& operator*() const { return *get(); } | 111 std::pair<K, V*>& operator*() const { return *get(); } |
| 97 std::pair<K, V*>* operator->() const { return get(); } | 112 std::pair<K, V*>* operator->() const { return get(); } |
| 98 | 113 |
| 99 bool operator==(const HashMapIterator<K, V, I>& other) const { return m_impl == other.m_impl; } | 114 bool operator==(const HashMapIterator<K, V, I>& other) const { return m_impl == other.m_impl; } |
| 100 bool operator!=(const HashMapIterator<K, V, I>& other) const { return m_impl != other.m_impl; } | 115 bool operator!=(const HashMapIterator<K, V, I>& other) const { return m_impl != other.m_impl; } |
| 101 | 116 |
| 102 HashMapIterator<K, V, I>& operator++() { ++m_impl; return *this; } | 117 HashMapIterator<K, V, I>& operator++() { ++m_impl; return *this; } |
| 103 | 118 |
| 104 private: | 119 private: |
| 105 mutable std::pair<K, V*> m_pair; | 120 mutable std::pair<K, V*> m_pair; |
| 106 I m_impl; | 121 I m_impl; |
| 107 }; | 122 }; |
| 108 | 123 |
| 109 template <typename K, typename V, typename I> | 124 template <typename K, typename V, typename I> |
| 110 class HashMapIterator<K, std::unique_ptr<V>, I> { | 125 class HashMapIterator<K, std::unique_ptr<V>, I> { |
| 111 STACK_ALLOCATED(); | |
| 112 public: | 126 public: |
| 113 HashMapIterator(const I& impl) : m_impl(impl) { } | 127 HashMapIterator(const I& impl) : m_impl(impl) { } |
| 114 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second = m_impl->second.get(); return &m_pair; } | 128 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second = m_impl->second; return &m_pair; } |
| 115 std::pair<K, V*>& operator*() const { return *get(); } | 129 std::pair<K, V*>& operator*() const { return *get(); } |
| 116 std::pair<K, V*>* operator->() const { return get(); } | 130 std::pair<K, V*>* operator->() const { return get(); } |
| 117 | 131 |
| 118 bool operator==(const HashMapIterator<K, std::unique_ptr<V>, I>& other) cons t { return m_impl == other.m_impl; } | 132 bool operator==(const HashMapIterator<K, std::unique_ptr<V>, I>& other) cons t { return m_impl == other.m_impl; } |
| 119 bool operator!=(const HashMapIterator<K, std::unique_ptr<V>, I>& other) cons t { return m_impl != other.m_impl; } | 133 bool operator!=(const HashMapIterator<K, std::unique_ptr<V>, I>& other) cons t { return m_impl != other.m_impl; } |
| 120 | 134 |
| 121 HashMapIterator<K, std::unique_ptr<V>, I>& operator++() { ++m_impl; return * this; } | 135 HashMapIterator<K, std::unique_ptr<V>, I>& operator++() { ++m_impl; return * this; } |
| 122 | 136 |
| 123 private: | 137 private: |
| 124 mutable std::pair<K, V*> m_pair; | 138 mutable std::pair<K, V*> m_pair; |
| 125 I m_impl; | 139 I m_impl; |
| 126 }; | 140 }; |
| 127 | 141 |
| 128 template <typename K, typename V> | 142 template <typename K, typename V> |
| 129 class HashMap { | 143 class HashMap { |
| 130 public: | 144 public: |
| 131 HashMap() { } | 145 HashMap() { } |
| 132 ~HashMap() { } | 146 ~HashMap() { } |
| 133 | 147 |
| 134 using iterator = HashMapIterator<K, V, typename std::unordered_map<K, V>::it erator>; | 148 using iterator = HashMapIterator<K, V, typename std::map<K, V>::iterator>; |
| 135 using const_iterator = HashMapIterator<K, const V, typename std::unordered_m ap<K, V>::const_iterator>; | 149 using const_iterator = HashMapIterator<K, const V, typename std::map<K, V>:: const_iterator>; |
| 136 | 150 |
| 137 iterator begin() { return iterator(m_impl.begin()); } | 151 iterator begin() { return iterator(m_impl.begin()); } |
| 138 iterator end() { return iterator(m_impl.end()); } | 152 iterator end() { return iterator(m_impl.end()); } |
| 139 iterator find(const K& k) { return iterator(m_impl.find(k)); } | 153 iterator find(const K& k) { return iterator(m_impl.find(k)); } |
| 140 const_iterator begin() const { return const_iterator(m_impl.begin()); } | 154 const_iterator begin() const { return const_iterator(m_impl.begin()); } |
| 141 const_iterator end() const { return const_iterator(m_impl.end()); } | 155 const_iterator end() const { return const_iterator(m_impl.end()); } |
| 142 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k) ); } | 156 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k) ); } |
| 143 | 157 |
| 144 size_t size() const { return m_impl.size(); } | 158 size_t size() const { return m_impl.size(); } |
| 145 bool isEmpty() const { return !m_impl.size(); } | 159 bool isEmpty() const { return !m_impl.size(); } |
| 146 bool set(const K& k, const V& v) | 160 bool set(const K& k, const V& v) |
| 147 { | 161 { |
| 148 bool isNew = m_impl.find(k) == m_impl.end(); | 162 bool isNew = m_impl.find(k) == m_impl.end(); |
| 149 m_impl[k] = v; | 163 m_impl[k] = v; |
| 150 return isNew; | 164 return isNew; |
| 151 } | 165 } |
| 152 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } | 166 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } |
| 153 V get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end( ) ? V() : it->second; } | 167 V get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end( ) ? V() : it->second; } |
| 154 void remove(const K& k) { m_impl.erase(k); } | 168 void remove(const K& k) { m_impl.erase(k); } |
| 155 void clear() { m_impl.clear(); } | 169 void clear() { m_impl.clear(); } |
| 156 V take(const K& k) | 170 V take(const K& k) |
| 157 { | 171 { |
| 158 V result = m_impl[k]; | 172 V result = m_impl[k]; |
| 159 m_impl.erase(k); | 173 m_impl.erase(k); |
| 160 return result; | 174 return result; |
| 161 } | 175 } |
| 162 | 176 |
| 163 private: | 177 private: |
| 164 std::unordered_map<K, V> m_impl; | 178 std::map<K, V> m_impl; |
|
alph
2016/06/08 17:18:48
Why it became ordered? I don't see where ordering
| |
| 165 }; | 179 }; |
| 166 | 180 |
| 167 template <typename K, typename V> | 181 template <typename K, typename V> |
| 168 class HashMap<K, std::unique_ptr<V>> { | 182 class HashMap<K, std::unique_ptr<V>> { |
| 169 public: | 183 public: |
| 170 HashMap() { } | 184 HashMap() { } |
| 171 ~HashMap() { } | 185 ~HashMap() { clear(); } |
| 172 | 186 |
| 173 using iterator = HashMapIterator<K, std::unique_ptr<V>, typename std::unorde red_map<K, std::unique_ptr<V>>::iterator>; | 187 using iterator = HashMapIterator<K, std::unique_ptr<V>, typename std::map<K, V*>::iterator>; |
| 174 using const_iterator = HashMapIterator<K, std::unique_ptr<V>, typename std:: unordered_map<K, std::unique_ptr<V>>::const_iterator>; | 188 using const_iterator = HashMapIterator<K, std::unique_ptr<V>, typename std:: map<K, V*>::const_iterator>; |
| 175 | 189 |
| 176 iterator begin() { return iterator(m_impl.begin()); } | 190 iterator begin() { return iterator(m_impl.begin()); } |
| 177 iterator end() { return iterator(m_impl.end()); } | 191 iterator end() { return iterator(m_impl.end()); } |
| 178 iterator find(const K& k) { return iterator(m_impl.find(k)); } | 192 iterator find(const K& k) { return iterator(m_impl.find(k)); } |
| 179 const_iterator begin() const { return const_iterator(m_impl.begin()); } | 193 const_iterator begin() const { return const_iterator(m_impl.begin()); } |
| 180 const_iterator end() const { return const_iterator(m_impl.end()); } | 194 const_iterator end() const { return const_iterator(m_impl.end()); } |
| 181 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k) ); } | 195 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k) ); } |
| 182 | 196 |
| 183 size_t size() const { return m_impl.size(); } | 197 size_t size() const { return m_impl.size(); } |
| 184 bool isEmpty() const { return !m_impl.size(); } | 198 bool isEmpty() const { return !m_impl.size(); } |
| 185 bool set(const K& k, std::unique_ptr<V> v) | 199 bool set(const K& k, std::unique_ptr<V> v) |
| 186 { | 200 { |
| 187 bool isNew = m_impl.find(k) == m_impl.end(); | 201 bool isNew = m_impl.find(k) == m_impl.end(); |
| 188 m_impl[k] = std::move(v); | 202 if (!isNew) |
| 203 delete m_impl[k]; | |
| 204 m_impl[k] = v.release(); | |
| 189 return isNew; | 205 return isNew; |
| 190 } | 206 } |
| 191 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } | 207 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } |
| 192 V* get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end () ? nullptr : it->second.get(); } | 208 V* get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end () ? nullptr : it->second; } |
| 193 std::unique_ptr<V> take(const K& k) | 209 std::unique_ptr<V> take(const K& k) |
| 194 { | 210 { |
| 195 if (!contains(k)) | 211 if (!contains(k)) |
| 196 return nullptr; | 212 return nullptr; |
| 197 std::unique_ptr<V> result = std::move(m_impl[k]); | 213 std::unique_ptr<V> result(m_impl[k]); |
| 214 delete m_impl[k]; | |
| 198 m_impl.erase(k); | 215 m_impl.erase(k); |
| 199 return result.release(); | 216 return result; |
| 200 } | 217 } |
| 201 void remove(const K& k) { m_impl.erase(k); } | 218 void remove(const K& k) |
| 202 void clear() { m_impl.clear(); } | 219 { |
| 220 delete m_impl[k]; | |
| 221 m_impl.erase(k); | |
| 222 } | |
| 223 | |
| 224 void clear() | |
| 225 { | |
| 226 for (auto pair : m_impl) | |
| 227 delete pair.second; | |
| 228 m_impl.clear(); | |
| 229 } | |
| 203 | 230 |
| 204 private: | 231 private: |
| 205 std::unordered_map<K, std::unique_ptr<V>> m_impl; | 232 std::map<K, V*> m_impl; |
| 206 }; | 233 }; |
| 207 | 234 |
| 208 template <typename K> | 235 template <typename K> |
| 209 class HashSet : public protocol::HashMap<K, K> { | 236 class HashSet : public protocol::HashMap<K, K> { |
| 210 public: | 237 public: |
| 211 void add(const K& k) { this->set(k, k); } | 238 void add(const K& k) { this->set(k, k); } |
| 212 }; | 239 }; |
| 213 | 240 |
| 214 } // namespace platform | 241 } // namespace platform |
| 215 } // namespace blink | 242 } // namespace blink |
| 216 | 243 |
| 217 #endif // !defined(CollectionsSTL_h) | 244 #endif // !defined(CollectionsSTL_h) |
| OLD | NEW |