| 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/String16.h" | 8 #include "platform/inspector_protocol/String16.h" |
| 9 #include "wtf/OwnPtr.h" | 9 #include "wtf/PtrUtil.h" |
| 10 #include "wtf/PassOwnPtr.h" | |
| 11 | 10 |
| 12 #include <algorithm> | 11 #include <algorithm> |
| 13 #include <unordered_map> | 12 #include <unordered_map> |
| 14 #include <vector> | 13 #include <vector> |
| 15 | 14 |
| 16 | 15 |
| 17 namespace blink { | 16 namespace blink { |
| 18 namespace protocol { | 17 namespace protocol { |
| 19 | 18 |
| 20 template <typename T> | 19 template <typename T> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 44 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } | 43 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } |
| 45 void clear() { m_impl.clear(); } | 44 void clear() { m_impl.clear(); } |
| 46 void swap(Vector& other) { m_impl.swap(other.m_impl); } | 45 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
| 47 void removeLast() { m_impl.pop_back(); } | 46 void removeLast() { m_impl.pop_back(); } |
| 48 | 47 |
| 49 private: | 48 private: |
| 50 std::vector<T> m_impl; | 49 std::vector<T> m_impl; |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 template <typename T> | 52 template <typename T> |
| 54 class Vector<OwnPtr<T>> { | 53 class Vector<std::unique_ptr<T>> { |
| 55 WTF_MAKE_NONCOPYABLE(Vector); | 54 WTF_MAKE_NONCOPYABLE(Vector); |
| 56 public: | 55 public: |
| 57 Vector() { } | 56 Vector() { } |
| 58 Vector(size_t capacity) : m_impl(capacity) { } | 57 Vector(size_t capacity) : m_impl(capacity) { } |
| 59 Vector(Vector&& other) : m_impl(std::move(other.m_impl)) { } | 58 Vector(Vector&& other) : m_impl(std::move(other.m_impl)) { } |
| 60 ~Vector() { } | 59 ~Vector() { } |
| 61 | 60 |
| 62 typedef typename std::vector<OwnPtr<T>>::iterator iterator; | 61 typedef typename std::vector<std::unique_ptr<T>>::iterator iterator; |
| 63 typedef typename std::vector<OwnPtr<T>>::const_iterator const_iterator; | 62 typedef typename std::vector<std::unique_ptr<T>>::const_iterator const_itera
tor; |
| 64 | 63 |
| 65 iterator begin() { return m_impl.begin(); } | 64 iterator begin() { return m_impl.begin(); } |
| 66 iterator end() { return m_impl.end(); } | 65 iterator end() { return m_impl.end(); } |
| 67 const_iterator begin() const { return m_impl.begin(); } | 66 const_iterator begin() const { return m_impl.begin(); } |
| 68 const_iterator end() const { return m_impl.end(); } | 67 const_iterator end() const { return m_impl.end(); } |
| 69 | 68 |
| 70 void resize(size_t s) { m_impl.resize(s); } | 69 void resize(size_t s) { m_impl.resize(s); } |
| 71 size_t size() const { return m_impl.size(); } | 70 size_t size() const { return m_impl.size(); } |
| 72 bool isEmpty() const { return !m_impl.size(); } | 71 bool isEmpty() const { return !m_impl.size(); } |
| 73 OwnPtr<T>& operator[](size_t i) { return at(i); } | 72 std::unique_ptr<T>& operator[](size_t i) { return at(i); } |
| 74 const OwnPtr<T>& operator[](size_t i) const { return at(i); } | 73 const std::unique_ptr<T>& operator[](size_t i) const { return at(i); } |
| 75 OwnPtr<T>& at(size_t i) { return m_impl[i]; } | 74 std::unique_ptr<T>& at(size_t i) { return m_impl[i]; } |
| 76 const OwnPtr<T>& at(size_t i) const { return m_impl.at(i); } | 75 const std::unique_ptr<T>& at(size_t i) const { return m_impl.at(i); } |
| 77 OwnPtr<T>& last() { return m_impl[m_impl.size() - 1]; } | 76 std::unique_ptr<T>& last() { return m_impl[m_impl.size() - 1]; } |
| 78 const OwnPtr<T>& last() const { return m_impl[m_impl.size() - 1]; } | 77 const std::unique_ptr<T>& last() const { return m_impl[m_impl.size() - 1]; } |
| 79 void append(PassOwnPtr<T> t) { m_impl.emplace_back(std::move(t)); } | 78 void append(std::unique_ptr<T> t) { m_impl.emplace_back(std::move(t)); } |
| 80 void prepend(PassOwnPtr<T> t) { m_impl.insert(m_impl.begin(), std::move(t));
} | 79 void prepend(std::unique_ptr<T> t) { m_impl.insert(m_impl.begin(), std::move
(t)); } |
| 81 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } | 80 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } |
| 82 void clear() { m_impl.clear(); } | 81 void clear() { m_impl.clear(); } |
| 83 void swap(Vector& other) { m_impl.swap(other.m_impl); } | 82 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
| 84 void swap(Vector&& other) { m_impl.swap(other.m_impl); } | 83 void swap(Vector&& other) { m_impl.swap(other.m_impl); } |
| 85 void removeLast() { m_impl.pop_back(); } | 84 void removeLast() { m_impl.pop_back(); } |
| 86 | 85 |
| 87 private: | 86 private: |
| 88 std::vector<OwnPtr<T>> m_impl; | 87 std::vector<std::unique_ptr<T>> m_impl; |
| 89 }; | 88 }; |
| 90 | 89 |
| 91 template <typename K, typename V, typename I> | 90 template <typename K, typename V, typename I> |
| 92 class HashMapIterator { | 91 class HashMapIterator { |
| 93 STACK_ALLOCATED(); | 92 STACK_ALLOCATED(); |
| 94 public: | 93 public: |
| 95 HashMapIterator(const I& impl) : m_impl(impl) { } | 94 HashMapIterator(const I& impl) : m_impl(impl) { } |
| 96 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second
= &m_impl->second; return &m_pair; } | 95 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second
= &m_impl->second; return &m_pair; } |
| 97 std::pair<K, V*>& operator*() const { return *get(); } | 96 std::pair<K, V*>& operator*() const { return *get(); } |
| 98 std::pair<K, V*>* operator->() const { return get(); } | 97 std::pair<K, V*>* operator->() const { return get(); } |
| 99 | 98 |
| 100 bool operator==(const HashMapIterator<K, V, I>& other) const { return m_impl
== other.m_impl; } | 99 bool operator==(const HashMapIterator<K, V, I>& other) const { return m_impl
== other.m_impl; } |
| 101 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; } |
| 102 | 101 |
| 103 HashMapIterator<K, V, I>& operator++() { ++m_impl; return *this; } | 102 HashMapIterator<K, V, I>& operator++() { ++m_impl; return *this; } |
| 104 | 103 |
| 105 private: | 104 private: |
| 106 mutable std::pair<K, V*> m_pair; | 105 mutable std::pair<K, V*> m_pair; |
| 107 I m_impl; | 106 I m_impl; |
| 108 }; | 107 }; |
| 109 | 108 |
| 110 template <typename K, typename V, typename I> | 109 template <typename K, typename V, typename I> |
| 111 class HashMapIterator<K, OwnPtr<V>, I> { | 110 class HashMapIterator<K, std::unique_ptr<V>, I> { |
| 112 STACK_ALLOCATED(); | 111 STACK_ALLOCATED(); |
| 113 public: | 112 public: |
| 114 HashMapIterator(const I& impl) : m_impl(impl) { } | 113 HashMapIterator(const I& impl) : m_impl(impl) { } |
| 115 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second
= m_impl->second.get(); return &m_pair; } | 114 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second
= m_impl->second.get(); return &m_pair; } |
| 116 std::pair<K, V*>& operator*() const { return *get(); } | 115 std::pair<K, V*>& operator*() const { return *get(); } |
| 117 std::pair<K, V*>* operator->() const { return get(); } | 116 std::pair<K, V*>* operator->() const { return get(); } |
| 118 | 117 |
| 119 bool operator==(const HashMapIterator<K, OwnPtr<V>, I>& other) const { retur
n m_impl == other.m_impl; } | 118 bool operator==(const HashMapIterator<K, std::unique_ptr<V>, I>& other) cons
t { return m_impl == other.m_impl; } |
| 120 bool operator!=(const HashMapIterator<K, OwnPtr<V>, I>& other) const { retur
n 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; } |
| 121 | 120 |
| 122 HashMapIterator<K, OwnPtr<V>, I>& operator++() { ++m_impl; return *this; } | 121 HashMapIterator<K, std::unique_ptr<V>, I>& operator++() { ++m_impl; return *
this; } |
| 123 | 122 |
| 124 private: | 123 private: |
| 125 mutable std::pair<K, V*> m_pair; | 124 mutable std::pair<K, V*> m_pair; |
| 126 I m_impl; | 125 I m_impl; |
| 127 }; | 126 }; |
| 128 | 127 |
| 129 template <typename K, typename V> | 128 template <typename K, typename V> |
| 130 class HashMap { | 129 class HashMap { |
| 131 public: | 130 public: |
| 132 HashMap() { } | 131 HashMap() { } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 159 V result = m_impl[k]; | 158 V result = m_impl[k]; |
| 160 m_impl.erase(k); | 159 m_impl.erase(k); |
| 161 return result; | 160 return result; |
| 162 } | 161 } |
| 163 | 162 |
| 164 private: | 163 private: |
| 165 std::unordered_map<K, V> m_impl; | 164 std::unordered_map<K, V> m_impl; |
| 166 }; | 165 }; |
| 167 | 166 |
| 168 template <typename K, typename V> | 167 template <typename K, typename V> |
| 169 class HashMap<K, OwnPtr<V>> { | 168 class HashMap<K, std::unique_ptr<V>> { |
| 170 public: | 169 public: |
| 171 HashMap() { } | 170 HashMap() { } |
| 172 ~HashMap() { } | 171 ~HashMap() { } |
| 173 | 172 |
| 174 using iterator = HashMapIterator<K, OwnPtr<V>, typename std::unordered_map<K
, OwnPtr<V>>::iterator>; | 173 using iterator = HashMapIterator<K, std::unique_ptr<V>, typename std::unorde
red_map<K, std::unique_ptr<V>>::iterator>; |
| 175 using const_iterator = HashMapIterator<K, OwnPtr<V>, typename std::unordered
_map<K, OwnPtr<V>>::const_iterator>; | 174 using const_iterator = HashMapIterator<K, std::unique_ptr<V>, typename std::
unordered_map<K, std::unique_ptr<V>>::const_iterator>; |
| 176 | 175 |
| 177 iterator begin() { return iterator(m_impl.begin()); } | 176 iterator begin() { return iterator(m_impl.begin()); } |
| 178 iterator end() { return iterator(m_impl.end()); } | 177 iterator end() { return iterator(m_impl.end()); } |
| 179 iterator find(const K& k) { return iterator(m_impl.find(k)); } | 178 iterator find(const K& k) { return iterator(m_impl.find(k)); } |
| 180 const_iterator begin() const { return const_iterator(m_impl.begin()); } | 179 const_iterator begin() const { return const_iterator(m_impl.begin()); } |
| 181 const_iterator end() const { return const_iterator(m_impl.end()); } | 180 const_iterator end() const { return const_iterator(m_impl.end()); } |
| 182 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k)
); } | 181 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k)
); } |
| 183 | 182 |
| 184 size_t size() const { return m_impl.size(); } | 183 size_t size() const { return m_impl.size(); } |
| 185 bool isEmpty() const { return !m_impl.size(); } | 184 bool isEmpty() const { return !m_impl.size(); } |
| 186 bool set(const K& k, PassOwnPtr<V> v) | 185 bool set(const K& k, std::unique_ptr<V> v) |
| 187 { | 186 { |
| 188 bool isNew = m_impl.find(k) == m_impl.end(); | 187 bool isNew = m_impl.find(k) == m_impl.end(); |
| 189 m_impl[k] = std::move(v); | 188 m_impl[k] = std::move(v); |
| 190 return isNew; | 189 return isNew; |
| 191 } | 190 } |
| 192 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } | 191 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } |
| 193 V* get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end
() ? nullptr : it->second.get(); } | 192 V* get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end
() ? nullptr : it->second.get(); } |
| 194 PassOwnPtr<V> take(const K& k) | 193 std::unique_ptr<V> take(const K& k) |
| 195 { | 194 { |
| 196 if (!contains(k)) | 195 if (!contains(k)) |
| 197 return nullptr; | 196 return nullptr; |
| 198 OwnPtr<V> result = std::move(m_impl[k]); | 197 std::unique_ptr<V> result = std::move(m_impl[k]); |
| 199 m_impl.erase(k); | 198 m_impl.erase(k); |
| 200 return result.release(); | 199 return result.release(); |
| 201 } | 200 } |
| 202 void remove(const K& k) { m_impl.erase(k); } | 201 void remove(const K& k) { m_impl.erase(k); } |
| 203 void clear() { m_impl.clear(); } | 202 void clear() { m_impl.clear(); } |
| 204 | 203 |
| 205 private: | 204 private: |
| 206 std::unordered_map<K, OwnPtr<V>> m_impl; | 205 std::unordered_map<K, std::unique_ptr<V>> m_impl; |
| 207 }; | 206 }; |
| 208 | 207 |
| 209 template <typename K> | 208 template <typename K> |
| 210 class HashSet : public protocol::HashMap<K, K> { | 209 class HashSet : public protocol::HashMap<K, K> { |
| 211 public: | 210 public: |
| 212 void add(const K& k) { this->set(k, k); } | 211 void add(const K& k) { this->set(k, k); } |
| 213 }; | 212 }; |
| 214 | 213 |
| 215 } // namespace platform | 214 } // namespace platform |
| 216 } // namespace blink | 215 } // namespace blink |
| 217 | 216 |
| 218 #endif // !defined(CollectionsSTL_h) | 217 #endif // !defined(CollectionsSTL_h) |
| OLD | NEW |