danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 1 | // Copyright (c) 2019 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 | // This is a simplistic insertion-ordered map. It behaves similarly to an STL |
| 6 | // map, but only implements a small subset of the map's methods. Internally, we |
| 7 | // just keep a map and a list going in parallel. |
| 8 | // |
| 9 | // This class provides no thread safety guarantees, beyond what you would |
| 10 | // normally see with std::list. |
| 11 | // |
| 12 | // Iterators point into the list and should be stable in the face of |
| 13 | // mutations, except for an iterator pointing to an element that was just |
| 14 | // deleted. |
| 15 | |
| 16 | #ifndef QUICHE_COMMON_SIMPLE_LINKED_HASH_MAP_H_ |
| 17 | #define QUICHE_COMMON_SIMPLE_LINKED_HASH_MAP_H_ |
| 18 | |
QUICHE team | bbce603 | 2020-01-03 07:42:10 -0800 | [diff] [blame] | 19 | #include <functional> |
danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 20 | #include <list> |
| 21 | #include <tuple> |
| 22 | #include <type_traits> |
| 23 | #include <utility> |
| 24 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 25 | #include "common/platform/api/quiche_logging.h" |
| 26 | #include "common/platform/api/quiche_unordered_containers.h" |
danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 27 | |
| 28 | namespace quiche { |
| 29 | |
| 30 | // This holds a list of pair<Key, Value> items. This list is what gets |
| 31 | // traversed, and it's iterators from this list that we return from |
| 32 | // begin/end/find. |
| 33 | // |
| 34 | // We also keep a set<list::iterator> for find. Since std::list is a |
| 35 | // doubly-linked list, the iterators should remain stable. |
| 36 | |
QUICHE team | bbce603 | 2020-01-03 07:42:10 -0800 | [diff] [blame] | 37 | template <class Key, |
| 38 | class Value, |
| 39 | class Hash = std::hash<Key>, |
| 40 | class Eq = std::equal_to<Key>> |
danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 41 | class SimpleLinkedHashMap { |
| 42 | private: |
| 43 | typedef std::list<std::pair<Key, Value>> ListType; |
QUICHE team | bbce603 | 2020-01-03 07:42:10 -0800 | [diff] [blame] | 44 | typedef QuicheUnorderedMap<Key, typename ListType::iterator, Hash, Eq> |
| 45 | MapType; |
danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 46 | |
| 47 | public: |
| 48 | typedef typename ListType::iterator iterator; |
| 49 | typedef typename ListType::reverse_iterator reverse_iterator; |
| 50 | typedef typename ListType::const_iterator const_iterator; |
| 51 | typedef typename ListType::const_reverse_iterator const_reverse_iterator; |
| 52 | typedef typename MapType::key_type key_type; |
| 53 | typedef typename ListType::value_type value_type; |
| 54 | typedef typename ListType::size_type size_type; |
| 55 | |
| 56 | SimpleLinkedHashMap() = default; |
| 57 | explicit SimpleLinkedHashMap(size_type bucket_count) : map_(bucket_count) {} |
| 58 | |
| 59 | SimpleLinkedHashMap(const SimpleLinkedHashMap& other) = delete; |
| 60 | SimpleLinkedHashMap& operator=(const SimpleLinkedHashMap& other) = delete; |
| 61 | SimpleLinkedHashMap(SimpleLinkedHashMap&& other) = default; |
| 62 | SimpleLinkedHashMap& operator=(SimpleLinkedHashMap&& other) = default; |
| 63 | |
| 64 | // Returns an iterator to the first (insertion-ordered) element. Like a map, |
| 65 | // this can be dereferenced to a pair<Key, Value>. |
| 66 | iterator begin() { return list_.begin(); } |
| 67 | const_iterator begin() const { return list_.begin(); } |
| 68 | |
| 69 | // Returns an iterator beyond the last element. |
| 70 | iterator end() { return list_.end(); } |
| 71 | const_iterator end() const { return list_.end(); } |
| 72 | |
| 73 | // Returns an iterator to the last (insertion-ordered) element. Like a map, |
| 74 | // this can be dereferenced to a pair<Key, Value>. |
| 75 | reverse_iterator rbegin() { return list_.rbegin(); } |
| 76 | const_reverse_iterator rbegin() const { return list_.rbegin(); } |
| 77 | |
| 78 | // Returns an iterator beyond the first element. |
| 79 | reverse_iterator rend() { return list_.rend(); } |
| 80 | const_reverse_iterator rend() const { return list_.rend(); } |
| 81 | |
| 82 | // Front and back accessors common to many stl containers. |
| 83 | |
| 84 | // Returns the earliest-inserted element |
| 85 | const value_type& front() const { return list_.front(); } |
| 86 | |
| 87 | // Returns the earliest-inserted element. |
| 88 | value_type& front() { return list_.front(); } |
| 89 | |
| 90 | // Returns the most-recently-inserted element. |
| 91 | const value_type& back() const { return list_.back(); } |
| 92 | |
| 93 | // Returns the most-recently-inserted element. |
| 94 | value_type& back() { return list_.back(); } |
| 95 | |
| 96 | // Clears the map of all values. |
| 97 | void clear() { |
| 98 | map_.clear(); |
| 99 | list_.clear(); |
| 100 | } |
| 101 | |
| 102 | // Returns true iff the map is empty. |
| 103 | bool empty() const { return list_.empty(); } |
| 104 | |
| 105 | // Removes the first element from the list. |
| 106 | void pop_front() { erase(begin()); } |
| 107 | |
| 108 | // Erases values with the provided key. Returns the number of elements |
| 109 | // erased. In this implementation, this will be 0 or 1. |
| 110 | size_type erase(const Key& key) { |
| 111 | typename MapType::iterator found = map_.find(key); |
| 112 | if (found == map_.end()) { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | list_.erase(found->second); |
| 117 | map_.erase(found); |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | // Erases the item that 'position' points to. Returns an iterator that points |
| 123 | // to the item that comes immediately after the deleted item in the list, or |
| 124 | // end(). |
| 125 | // If the provided iterator is invalid or there is inconsistency between the |
| 126 | // map and list, a CHECK() error will occur. |
| 127 | iterator erase(iterator position) { |
| 128 | typename MapType::iterator found = map_.find(position->first); |
| 129 | CHECK(found->second == position) |
| 130 | << "Inconsisent iterator for map and list, or the iterator is invalid."; |
| 131 | |
| 132 | map_.erase(found); |
| 133 | return list_.erase(position); |
| 134 | } |
| 135 | |
| 136 | // Erases all the items in the range [first, last). Returns an iterator that |
| 137 | // points to the item that comes immediately after the last deleted item in |
| 138 | // the list, or end(). |
| 139 | iterator erase(iterator first, iterator last) { |
| 140 | while (first != last && first != end()) { |
| 141 | first = erase(first); |
| 142 | } |
| 143 | return first; |
| 144 | } |
| 145 | |
| 146 | // Finds the element with the given key. Returns an iterator to the |
| 147 | // value found, or to end() if the value was not found. Like a map, this |
| 148 | // iterator points to a pair<Key, Value>. |
| 149 | iterator find(const Key& key) { |
| 150 | typename MapType::iterator found = map_.find(key); |
| 151 | if (found == map_.end()) { |
| 152 | return end(); |
| 153 | } |
| 154 | return found->second; |
| 155 | } |
| 156 | |
| 157 | const_iterator find(const Key& key) const { |
| 158 | typename MapType::const_iterator found = map_.find(key); |
| 159 | if (found == map_.end()) { |
| 160 | return end(); |
| 161 | } |
| 162 | return found->second; |
| 163 | } |
| 164 | |
| 165 | bool contains(const Key& key) const { return find(key) != end(); } |
| 166 | |
| 167 | // Returns the value mapped to key, or an inserted iterator to that position |
| 168 | // in the map. |
| 169 | Value& operator[](const key_type& key) { |
| 170 | return (*((this->insert(std::make_pair(key, Value()))).first)).second; |
| 171 | } |
| 172 | |
| 173 | // Inserts an element into the map |
| 174 | std::pair<iterator, bool> insert(const std::pair<Key, Value>& pair) { |
| 175 | // First make sure the map doesn't have a key with this value. If it does, |
| 176 | // return a pair with an iterator to it, and false indicating that we |
| 177 | // didn't insert anything. |
| 178 | typename MapType::iterator found = map_.find(pair.first); |
| 179 | if (found != map_.end()) { |
| 180 | return std::make_pair(found->second, false); |
| 181 | } |
| 182 | |
| 183 | // Otherwise, insert into the list first. |
| 184 | list_.push_back(pair); |
| 185 | |
| 186 | // Obtain an iterator to the newly added element. We do -- instead of - |
| 187 | // since list::iterator doesn't implement operator-(). |
| 188 | typename ListType::iterator last = list_.end(); |
| 189 | --last; |
| 190 | |
| 191 | CHECK(map_.insert(std::make_pair(pair.first, last)).second) |
| 192 | << "Map and list are inconsistent"; |
| 193 | |
| 194 | return std::make_pair(last, true); |
| 195 | } |
| 196 | |
| 197 | // Inserts an element into the map |
| 198 | std::pair<iterator, bool> insert(std::pair<Key, Value>&& pair) { |
| 199 | // First make sure the map doesn't have a key with this value. If it does, |
| 200 | // return a pair with an iterator to it, and false indicating that we |
| 201 | // didn't insert anything. |
| 202 | typename MapType::iterator found = map_.find(pair.first); |
| 203 | if (found != map_.end()) { |
| 204 | return std::make_pair(found->second, false); |
| 205 | } |
| 206 | |
| 207 | // Otherwise, insert into the list first. |
| 208 | list_.push_back(std::move(pair)); |
| 209 | |
| 210 | // Obtain an iterator to the newly added element. We do -- instead of - |
| 211 | // since list::iterator doesn't implement operator-(). |
| 212 | typename ListType::iterator last = list_.end(); |
| 213 | --last; |
| 214 | |
QUICHE team | ebc71c1 | 2020-09-10 08:00:10 -0700 | [diff] [blame] | 215 | CHECK(map_.insert(std::make_pair(last->first, last)).second) |
danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 216 | << "Map and list are inconsistent"; |
| 217 | return std::make_pair(last, true); |
| 218 | } |
| 219 | |
rch | c83b674 | 2019-07-01 17:41:32 -0700 | [diff] [blame] | 220 | // Derive size_ from map_, as list::size might be O(N). |
| 221 | size_type size() const { return map_.size(); } |
danzh | c3be2d4 | 2019-04-25 07:47:41 -0700 | [diff] [blame] | 222 | |
| 223 | template <typename... Args> |
| 224 | std::pair<iterator, bool> emplace(Args&&... args) { |
| 225 | ListType node_donor; |
| 226 | auto node_pos = |
| 227 | node_donor.emplace(node_donor.end(), std::forward<Args>(args)...); |
| 228 | const auto& k = node_pos->first; |
| 229 | auto ins = map_.insert({k, node_pos}); |
| 230 | if (!ins.second) { |
| 231 | return {ins.first->second, false}; |
| 232 | } |
| 233 | list_.splice(list_.end(), node_donor, node_pos); |
| 234 | return {ins.first->second, true}; |
| 235 | } |
| 236 | |
| 237 | void swap(SimpleLinkedHashMap& other) { |
| 238 | map_.swap(other.map_); |
| 239 | list_.swap(other.list_); |
| 240 | } |
| 241 | |
| 242 | private: |
| 243 | // The map component, used for speedy lookups |
| 244 | MapType map_; |
| 245 | |
| 246 | // The list component, used for maintaining insertion order |
| 247 | ListType list_; |
| 248 | }; |
| 249 | |
| 250 | } // namespace quiche |
| 251 | |
| 252 | #endif // QUICHE_COMMON_SIMPLE_LINKED_HASH_MAP_H_ |