| 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 CollectionsWTF_h |
| 6 #define CollectionsWTF_h |
| 7 |
| 8 #include "wtf/Allocator.h" |
| 9 #include "wtf/HashMap.h" |
| 10 #include "wtf/PassOwnPtr.h" |
| 11 #include "wtf/Vector.h" |
| 12 #include "wtf/VectorTraits.h" |
| 13 |
| 14 namespace blink { |
| 15 namespace protocol { |
| 16 |
| 17 template <typename T> |
| 18 class Vector { |
| 19 public: |
| 20 Vector() { } |
| 21 Vector(size_t capacity) : m_impl(capacity) { } |
| 22 typedef T* iterator; |
| 23 typedef const T* const_iterator; |
| 24 |
| 25 iterator begin() { return m_impl.begin(); } |
| 26 iterator end() { return m_impl.end(); } |
| 27 const_iterator begin() const { return m_impl.begin(); } |
| 28 const_iterator end() const { return m_impl.end(); } |
| 29 |
| 30 void resize(size_t s) { m_impl.resize(s); } |
| 31 size_t find(const T& t) const { return m_impl.find(t); } |
| 32 size_t size() const { return m_impl.size(); } |
| 33 bool isEmpty() const { return m_impl.isEmpty(); } |
| 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.at(i); } |
| 37 const T& at(size_t i) const { return m_impl.at(i); } |
| 38 T& last() { return m_impl.last(); } |
| 39 const T& last() const { return m_impl.last(); } |
| 40 void append(const T& t) { m_impl.append(t); } |
| 41 void prepend(const T& t) { m_impl.prepend(t); } |
| 42 void remove(size_t i) { m_impl.remove(i); } |
| 43 void insert(size_t i, const T& t) { m_impl.insert(i, t); } |
| 44 void clear() { m_impl.clear(); } |
| 45 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
| 46 void removeLast() { m_impl.removeLast(); } |
| 47 |
| 48 private: |
| 49 WTF::Vector<T> m_impl; |
| 50 }; |
| 51 |
| 52 template <typename T> |
| 53 class Vector<OwnPtr<T>> { |
| 54 WTF_MAKE_NONCOPYABLE(Vector); |
| 55 public: |
| 56 Vector() { } |
| 57 Vector(size_t capacity) : m_impl(capacity) { } |
| 58 ~Vector() { } |
| 59 |
| 60 typedef OwnPtr<T>* iterator; |
| 61 typedef const OwnPtr<T>* const_iterator; |
| 62 |
| 63 iterator begin() { return m_impl.begin(); } |
| 64 iterator end() { return m_impl.end(); } |
| 65 const_iterator begin() const { return m_impl.begin(); } |
| 66 const_iterator end() const { return m_impl.end(); } |
| 67 |
| 68 void resize(size_t s) { m_impl.resize(s); } |
| 69 size_t find(const T* t) const { return m_impl.find(t); } |
| 70 size_t size() const { return m_impl.size(); } |
| 71 bool isEmpty() const { return m_impl.isEmpty(); } |
| 72 OwnPtr<T>& operator[](size_t i) { return m_impl.at(i); } |
| 73 const OwnPtr<T>& operator[](size_t i) const { return m_impl.at(i); } |
| 74 OwnPtr<T>& at(size_t i) { return m_impl.at(i); } |
| 75 const OwnPtr<T>& at(size_t i) const { return m_impl.at(i); } |
| 76 OwnPtr<T>& last() { return m_impl.last(); } |
| 77 const OwnPtr<T>& last() const { return m_impl.last(); } |
| 78 void append(PassOwnPtr<T> t) { m_impl.append(t); } |
| 79 void prepend(PassOwnPtr<T> t) { m_impl.prepend(t); } |
| 80 void remove(size_t i) { m_impl.remove(i); } |
| 81 void insert(size_t i, PassOwnPtr<T> t) { m_impl.insert(i, t); } |
| 82 void clear() { m_impl.clear(); } |
| 83 void swap(Vector& other) { m_impl.swap(other.m_impl); } |
| 84 void removeLast() { m_impl.removeLast(); } |
| 85 |
| 86 private: |
| 87 WTF::Vector<OwnPtr<T>> m_impl; |
| 88 }; |
| 89 |
| 90 template <typename K, typename V, typename I> |
| 91 class HashMapIterator { |
| 92 STACK_ALLOCATED(); |
| 93 public: |
| 94 HashMapIterator(const I& impl) : m_impl(impl) { } |
| 95 std::pair<K, V*>* get() const { m_pair = std::make_pair(m_impl->key, &m_impl
->value); return &m_pair; } |
| 96 std::pair<K, V*>& operator*() const { return *get(); } |
| 97 std::pair<K, V*>* operator->() const { return get(); } |
| 98 |
| 99 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; } |
| 101 |
| 102 HashMapIterator<K, V, I>& operator++() { ++m_impl; return *this; } |
| 103 |
| 104 private: |
| 105 mutable std::pair<K, V*> m_pair; |
| 106 I m_impl; |
| 107 }; |
| 108 |
| 109 template <typename K, typename V, typename I> |
| 110 class HashMapIterator<K, OwnPtr<V>, I> { |
| 111 STACK_ALLOCATED(); |
| 112 public: |
| 113 HashMapIterator(const I& impl) : m_impl(impl) { } |
| 114 std::pair<K, V*>* get() const { m_pair = std::make_pair(m_impl->key, m_impl-
>value.get()); return &m_pair; } |
| 115 std::pair<K, V*>& operator*() const { return *get(); } |
| 116 std::pair<K, V*>* operator->() const { return get(); } |
| 117 |
| 118 bool operator==(const HashMapIterator<K, OwnPtr<V>, I>& other) const { retur
n m_impl == other.m_impl; } |
| 119 bool operator!=(const HashMapIterator<K, OwnPtr<V>, I>& other) const { retur
n m_impl != other.m_impl; } |
| 120 |
| 121 HashMapIterator<K, OwnPtr<V>, I>& operator++() { ++m_impl; return *this; } |
| 122 |
| 123 private: |
| 124 mutable std::pair<K, V*> m_pair; |
| 125 I m_impl; |
| 126 }; |
| 127 |
| 128 template <typename K, typename V> |
| 129 class HashMap { |
| 130 public: |
| 131 HashMap() { } |
| 132 ~HashMap() { } |
| 133 |
| 134 using iterator = HashMapIterator<K, V, typename WTF::HashMap<K, V>::iterator
>; |
| 135 using const_iterator = HashMapIterator<K, const V, typename WTF::HashMap<K,
V>::const_iterator>; |
| 136 |
| 137 iterator begin() { return HashMapIterator<K, V, typename WTF::HashMap<K, V>:
:iterator>(m_impl.begin()); } |
| 138 iterator end() { return HashMapIterator<K, V, typename WTF::HashMap<K, V>::i
terator>(m_impl.end()); } |
| 139 iterator find(const K& k) { return HashMapIterator<K, V, typename WTF::HashM
ap<K, V>::iterator>(m_impl.find(k)); } |
| 140 const_iterator begin() const { return HashMapIterator<K, const V, typename W
TF::HashMap<K, V>::const_iterator>(m_impl.begin()); } |
| 141 const_iterator end() const { return HashMapIterator<K, const V, typename WTF
::HashMap<K, V>::const_iterator>(m_impl.end()); } |
| 142 const_iterator find(const K& k) const { return HashMapIterator<K, const V, t
ypename WTF::HashMap<K, V>::const_iterator>(m_impl.find(k)); } |
| 143 |
| 144 size_t size() const { return m_impl.size(); } |
| 145 bool isEmpty() const { return m_impl.isEmpty(); } |
| 146 bool set(const K& k, const V& v) { return m_impl.set(k, v).isNewEntry; } |
| 147 bool contains(const K& k) const { return m_impl.contains(k); } |
| 148 V get(const K& k) const { return m_impl.get(k); } |
| 149 void remove(const K& k) { m_impl.remove(k); } |
| 150 void clear() { m_impl.clear(); } |
| 151 V take(const K& k) { return m_impl.take(k); } |
| 152 |
| 153 private: |
| 154 WTF::HashMap<K, V> m_impl; |
| 155 }; |
| 156 |
| 157 template <typename K, typename V> |
| 158 class HashMap<K, OwnPtr<V>> { |
| 159 public: |
| 160 HashMap() { } |
| 161 ~HashMap() { } |
| 162 |
| 163 using iterator = HashMapIterator<K, OwnPtr<V>, typename WTF::HashMap<K, OwnP
tr<V>>::iterator>; |
| 164 using const_iterator = HashMapIterator<K, OwnPtr<V>, typename WTF::HashMap<K
, OwnPtr<V>>::const_iterator>; |
| 165 |
| 166 iterator begin() { return HashMapIterator<K, OwnPtr<V>, typename WTF::HashMa
p<K, OwnPtr<V>>::iterator>(m_impl.begin()); } |
| 167 iterator end() { return HashMapIterator<K, OwnPtr<V>, typename WTF::HashMap<
K, OwnPtr<V>>::iterator>(m_impl.end()); } |
| 168 iterator find(const K& k) { return HashMapIterator<K, OwnPtr<V>, typename WT
F::HashMap<K, OwnPtr<V>>::iterator>(m_impl.find(k)); } |
| 169 const_iterator begin() const { return HashMapIterator<K, OwnPtr<V>, typename
WTF::HashMap<K, OwnPtr<V>>::const_iterator>(m_impl.begin()); } |
| 170 const_iterator end() const { return HashMapIterator<K, OwnPtr<V>, typename W
TF::HashMap<K, OwnPtr<V>>::const_iterator>(m_impl.end()); } |
| 171 const_iterator find(const K& k) const { return HashMapIterator<K, OwnPtr<V>,
typename WTF::HashMap<K, OwnPtr<V>>::const_iterator>(m_impl.find(k)); } |
| 172 |
| 173 size_t size() const { return m_impl.size(); } |
| 174 bool isEmpty() const { return m_impl.isEmpty(); } |
| 175 bool set(const K& k, PassOwnPtr<V> v) { return m_impl.set(k, v).isNewEntry;
} |
| 176 bool contains(const K& k) const { return m_impl.contains(k); } |
| 177 V* get(const K& k) const { return m_impl.get(k); } |
| 178 PassOwnPtr<V> take(const K& k) { return m_impl.take(k); } |
| 179 void remove(const K& k) { m_impl.remove(k); } |
| 180 void clear() { m_impl.clear(); } |
| 181 |
| 182 private: |
| 183 WTF::HashMap<K, OwnPtr<V>> m_impl; |
| 184 }; |
| 185 |
| 186 template <typename K> |
| 187 class HashSet : public protocol::HashMap<K, K> { |
| 188 public: |
| 189 void add(const K& k) { this->set(k, k); } |
| 190 }; |
| 191 |
| 192 } // namespace platform |
| 193 } // namespace blink |
| 194 |
| 195 #endif // !defined(CollectionsWTF_h) |
| OLD | NEW |