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