| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CollectionsSTL_h | |
| 6 #define CollectionsSTL_h | |
| 7 | |
| 8 #include "platform/inspector_protocol/Platform.h" | |
| 9 #include "platform/inspector_protocol/String16.h" | |
| 10 | |
| 11 #include <algorithm> | |
| 12 #include <map> | |
| 13 #include <vector> | |
| 14 | |
| 15 namespace blink { | |
| 16 namespace protocol { | |
| 17 | |
| 18 template <typename T> | |
| 19 class Vector { | |
| 20 public: | |
| 21 Vector() { } | |
| 22 Vector(size_t capacity) : m_impl(capacity) { } | |
| 23 typedef typename std::vector<T>::iterator iterator; | |
| 24 typedef typename std::vector<T>::const_iterator const_iterator; | |
| 25 | |
| 26 iterator begin() { return m_impl.begin(); } | |
| 27 iterator end() { return m_impl.end(); } | |
| 28 const_iterator begin() const { return m_impl.begin(); } | |
| 29 const_iterator end() const { return m_impl.end(); } | |
| 30 | |
| 31 void resize(size_t s) { m_impl.resize(s); } | |
| 32 size_t size() const { return m_impl.size(); } | |
| 33 bool isEmpty() const { return !m_impl.size(); } | |
| 34 T& operator[](size_t i) { return at(i); } | |
| 35 const T& operator[](size_t i) const { return at(i); } | |
| 36 T& at(size_t i) { return m_impl[i]; } | |
| 37 const T& at(size_t i) const { return m_impl.at(i); } | |
| 38 T& last() { return m_impl[m_impl.size() - 1]; } | |
| 39 const T& last() const { return m_impl[m_impl.size() - 1]; } | |
| 40 void append(const T& t) { m_impl.push_back(t); } | |
| 41 void prepend(const T& t) { m_impl.insert(m_impl.begin(), t); } | |
| 42 void remove(size_t i) { m_impl.erase(m_impl.begin() + i); } | |
| 43 void clear() { m_impl.clear(); } | |
| 44 void swap(Vector& other) { m_impl.swap(other.m_impl); } | |
| 45 void removeLast() { m_impl.pop_back(); } | |
| 46 | |
| 47 private: | |
| 48 std::vector<T> m_impl; | |
| 49 }; | |
| 50 | |
| 51 template <typename T> | |
| 52 class Vector<std::unique_ptr<T>> { | |
| 53 public: | |
| 54 Vector() { } | |
| 55 Vector(size_t capacity) : m_impl(capacity) { } | |
| 56 Vector(Vector&& other) { m_impl.swap(other.m_impl); } | |
| 57 ~Vector() { clear(); } | |
| 58 | |
| 59 typedef typename std::vector<T*>::iterator iterator; | |
| 60 typedef typename std::vector<T*>::const_iterator const_iterator; | |
| 61 | |
| 62 iterator begin() { return m_impl.begin(); } | |
| 63 iterator end() { return m_impl.end(); } | |
| 64 const_iterator begin() const { return m_impl.begin(); } | |
| 65 const_iterator end() const { return m_impl.end(); } | |
| 66 | |
| 67 void resize(size_t s) { m_impl.resize(s); } | |
| 68 size_t size() const { return m_impl.size(); } | |
| 69 bool isEmpty() const { return !m_impl.size(); } | |
| 70 T* operator[](size_t i) { return at(i); } | |
| 71 const T* operator[](size_t i) const { return at(i); } | |
| 72 T* at(size_t i) { return m_impl[i]; } | |
| 73 const T* at(size_t i) const { return m_impl.at(i); } | |
| 74 T* last() { return m_impl[m_impl.size() - 1]; } | |
| 75 const T* last() const { return m_impl[m_impl.size() - 1]; } | |
| 76 void append(std::unique_ptr<T> t) { m_impl.push_back(t.release()); } | |
| 77 void prepend(std::unique_ptr<T> t) { m_impl.insert(m_impl.begin(), t.release
()); } | |
| 78 | |
| 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 | |
| 92 void swap(Vector& other) { m_impl.swap(other.m_impl); } | |
| 93 void swap(Vector&& other) { m_impl.swap(other.m_impl); } | |
| 94 void removeLast() | |
| 95 { | |
| 96 delete last(); | |
| 97 m_impl.pop_back(); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 Vector(const Vector&) = delete; | |
| 102 Vector& operator=(const Vector&) = delete; | |
| 103 std::vector<T*> m_impl; | |
| 104 }; | |
| 105 | |
| 106 template <typename K, typename V, typename I> | |
| 107 class HashMapIterator { | |
| 108 public: | |
| 109 HashMapIterator(const I& impl) : m_impl(impl) { } | |
| 110 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second
= &m_impl->second; return &m_pair; } | |
| 111 std::pair<K, V*>& operator*() const { return *get(); } | |
| 112 std::pair<K, V*>* operator->() const { return get(); } | |
| 113 | |
| 114 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; } | |
| 116 | |
| 117 HashMapIterator<K, V, I>& operator++() { ++m_impl; return *this; } | |
| 118 | |
| 119 private: | |
| 120 mutable std::pair<K, V*> m_pair; | |
| 121 I m_impl; | |
| 122 }; | |
| 123 | |
| 124 template <typename K, typename V, typename I> | |
| 125 class HashMapIterator<K, std::unique_ptr<V>, I> { | |
| 126 public: | |
| 127 HashMapIterator(const I& impl) : m_impl(impl) { } | |
| 128 std::pair<K, V*>* get() const { m_pair.first = m_impl->first; m_pair.second
= m_impl->second; return &m_pair; } | |
| 129 std::pair<K, V*>& operator*() const { return *get(); } | |
| 130 std::pair<K, V*>* operator->() const { return get(); } | |
| 131 | |
| 132 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; } | |
| 134 | |
| 135 HashMapIterator<K, std::unique_ptr<V>, I>& operator++() { ++m_impl; return *
this; } | |
| 136 | |
| 137 private: | |
| 138 mutable std::pair<K, V*> m_pair; | |
| 139 I m_impl; | |
| 140 }; | |
| 141 | |
| 142 template <typename K, typename V> | |
| 143 class HashMap { | |
| 144 public: | |
| 145 HashMap() { } | |
| 146 ~HashMap() { } | |
| 147 | |
| 148 using iterator = HashMapIterator<K, V, typename std::map<K, V>::iterator>; | |
| 149 using const_iterator = HashMapIterator<K, const V, typename std::map<K, V>::
const_iterator>; | |
| 150 | |
| 151 iterator begin() { return iterator(m_impl.begin()); } | |
| 152 iterator end() { return iterator(m_impl.end()); } | |
| 153 iterator find(const K& k) { return iterator(m_impl.find(k)); } | |
| 154 const_iterator begin() const { return const_iterator(m_impl.begin()); } | |
| 155 const_iterator end() const { return const_iterator(m_impl.end()); } | |
| 156 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k)
); } | |
| 157 | |
| 158 size_t size() const { return m_impl.size(); } | |
| 159 bool isEmpty() const { return !m_impl.size(); } | |
| 160 bool set(const K& k, const V& v) | |
| 161 { | |
| 162 bool isNew = m_impl.find(k) == m_impl.end(); | |
| 163 m_impl[k] = v; | |
| 164 return isNew; | |
| 165 } | |
| 166 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } | |
| 167 V get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end(
) ? V() : it->second; } | |
| 168 void remove(const K& k) { m_impl.erase(k); } | |
| 169 void clear() { m_impl.clear(); } | |
| 170 V take(const K& k) | |
| 171 { | |
| 172 V result = m_impl[k]; | |
| 173 m_impl.erase(k); | |
| 174 return result; | |
| 175 } | |
| 176 | |
| 177 private: | |
| 178 std::map<K, V> m_impl; | |
| 179 }; | |
| 180 | |
| 181 template <typename K, typename V> | |
| 182 class HashMap<K, std::unique_ptr<V>> { | |
| 183 public: | |
| 184 HashMap() { } | |
| 185 ~HashMap() { clear(); } | |
| 186 | |
| 187 using iterator = HashMapIterator<K, std::unique_ptr<V>, typename std::map<K,
V*>::iterator>; | |
| 188 using const_iterator = HashMapIterator<K, std::unique_ptr<V>, typename std::
map<K, V*>::const_iterator>; | |
| 189 | |
| 190 iterator begin() { return iterator(m_impl.begin()); } | |
| 191 iterator end() { return iterator(m_impl.end()); } | |
| 192 iterator find(const K& k) { return iterator(m_impl.find(k)); } | |
| 193 const_iterator begin() const { return const_iterator(m_impl.begin()); } | |
| 194 const_iterator end() const { return const_iterator(m_impl.end()); } | |
| 195 const_iterator find(const K& k) const { return const_iterator(m_impl.find(k)
); } | |
| 196 | |
| 197 size_t size() const { return m_impl.size(); } | |
| 198 bool isEmpty() const { return !m_impl.size(); } | |
| 199 bool set(const K& k, std::unique_ptr<V> v) | |
| 200 { | |
| 201 bool isNew = m_impl.find(k) == m_impl.end(); | |
| 202 if (!isNew) | |
| 203 delete m_impl[k]; | |
| 204 m_impl[k] = v.release(); | |
| 205 return isNew; | |
| 206 } | |
| 207 bool contains(const K& k) const { return m_impl.find(k) != m_impl.end(); } | |
| 208 V* get(const K& k) const { auto it = m_impl.find(k); return it == m_impl.end
() ? nullptr : it->second; } | |
| 209 std::unique_ptr<V> take(const K& k) | |
| 210 { | |
| 211 if (!contains(k)) | |
| 212 return nullptr; | |
| 213 std::unique_ptr<V> result(m_impl[k]); | |
| 214 delete m_impl[k]; | |
| 215 m_impl.erase(k); | |
| 216 return result; | |
| 217 } | |
| 218 void remove(const K& k) | |
| 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 } | |
| 230 | |
| 231 private: | |
| 232 std::map<K, V*> m_impl; | |
| 233 }; | |
| 234 | |
| 235 template <typename K> | |
| 236 class HashSet : public protocol::HashMap<K, K> { | |
| 237 public: | |
| 238 void add(const K& k) { this->set(k, k); } | |
| 239 }; | |
| 240 | |
| 241 } // namespace platform | |
| 242 } // namespace blink | |
| 243 | |
| 244 #endif // !defined(CollectionsSTL_h) | |
| OLD | NEW |