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