1 // Copyright (c) 2013 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.
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.
9 // This class provides no thread safety guarantees, beyond what you would
10 // normally see with std::list.
12 // Iterators should be stable in the face of mutations, except for an
13 // iterator pointing to an element that was just deleted.
15 #ifndef UTIL_GTL_LINKED_HASH_MAP_H_
16 #define UTIL_GTL_LINKED_HASH_MAP_H_
21 #include "base/containers/hash_tables.h"
22 #include "base/logging.h"
23 #include "base/macros.h"
25 // This holds a list of pair<Key, Value> items. This list is what gets
26 // traversed, and it's iterators from this list that we return from
29 // We also keep a map<Key, list::iterator> for find. Since std::list is a
30 // doubly-linked list, the iterators should remain stable.
31 template<class Key
, class Value
>
32 class linked_hash_map
{
34 typedef std::list
<std::pair
<Key
, Value
> > ListType
;
35 typedef base::hash_map
<Key
, typename
ListType::iterator
> MapType
;
38 typedef typename
ListType::iterator iterator
;
39 typedef typename
ListType::reverse_iterator reverse_iterator
;
40 typedef typename
ListType::const_iterator const_iterator
;
41 typedef typename
ListType::const_reverse_iterator const_reverse_iterator
;
42 typedef typename
MapType::key_type key_type
;
43 typedef typename
ListType::value_type value_type
;
44 typedef typename
ListType::size_type size_type
;
46 linked_hash_map() : map_(), list_() {
49 // Returns an iterator to the first (insertion-ordered) element. Like a map,
50 // this can be dereferenced to a pair<Key, Value>.
54 const_iterator
begin() const {
58 // Returns an iterator beyond the last element.
62 const_iterator
end() const {
66 // Returns an iterator to the last (insertion-ordered) element. Like a map,
67 // this can be dereferenced to a pair<Key, Value>.
68 reverse_iterator
rbegin() {
69 return list_
.rbegin();
71 const_reverse_iterator
rbegin() const {
72 return list_
.rbegin();
75 // Returns an iterator beyond the first element.
76 reverse_iterator
rend() {
79 const_reverse_iterator
rend() const {
83 // Front and back accessors common to many stl containers.
85 // Returns the earliest-inserted element
86 const value_type
& front() const {
90 // Returns the earliest-inserted element.
95 // Returns the most-recently-inserted element.
96 const value_type
& back() const {
100 // Returns the most-recently-inserted element.
105 // Clears the map of all values.
111 // Returns true iff the map is empty.
113 return list_
.empty();
116 // Erases values with the provided key. Returns the number of elements
117 // erased. In this implementation, this will be 0 or 1.
118 size_type
erase(const Key
& key
) {
119 typename
MapType::iterator found
= map_
.find(key
);
120 if (found
== map_
.end()) return 0;
122 list_
.erase(found
->second
);
128 // Erases the item that 'position' points to. Returns an iterator that points
129 // to the item that comes immediately after the deleted item in the list, or
131 // If the provided iterator is invalid or there is inconsistency between the
132 // map and list, a CHECK() error will occur.
133 iterator
erase(iterator position
) {
134 typename
MapType::iterator found
= map_
.find(position
->first
);
135 CHECK(found
->second
== position
)
136 << "Inconsisent iterator for map and list, or the iterator is invalid.";
139 return list_
.erase(position
);
142 // Erases all the items in the range [first, last). Returns an iterator that
143 // points to the item that comes immediately after the last deleted item in
144 // the list, or end().
145 iterator
erase(iterator first
, iterator last
) {
146 while (first
!= last
&& first
!= end()) {
147 first
= erase(first
);
152 // Finds the element with the given key. Returns an iterator to the
153 // value found, or to end() if the value was not found. Like a map, this
154 // iterator points to a pair<Key, Value>.
155 iterator
find(const Key
& key
) {
156 typename
MapType::iterator found
= map_
.find(key
);
157 if (found
== map_
.end()) {
160 return found
->second
;
163 const_iterator
find(const Key
& key
) const {
164 typename
MapType::const_iterator found
= map_
.find(key
);
165 if (found
== map_
.end()) {
168 return found
->second
;
171 // Returns the bounds of a range that includes all the elements in the
172 // container with a key that compares equal to x.
173 std::pair
<iterator
, iterator
> equal_range(const key_type
& key
) {
174 std::pair
<typename
MapType::iterator
, typename
MapType::iterator
> eq_range
=
175 map_
.equal_range(key
);
177 return std::make_pair(eq_range
.first
->second
, eq_range
.second
->second
);
180 std::pair
<const_iterator
, const_iterator
> equal_range(
181 const key_type
& key
) const {
182 std::pair
<typename
MapType::const_iterator
,
183 typename
MapType::const_iterator
> eq_range
=
184 map_
.equal_range(key
);
185 const const_iterator
& start_iter
= eq_range
.first
!= map_
.end() ?
186 eq_range
.first
->second
: end();
187 const const_iterator
& end_iter
= eq_range
.second
!= map_
.end() ?
188 eq_range
.second
->second
: end();
190 return std::make_pair(start_iter
, end_iter
);
193 // Returns the value mapped to key, or an inserted iterator to that position
195 Value
& operator[](const key_type
& key
) {
196 return (*((this->insert(std::make_pair(key
, Value()))).first
)).second
;
199 // Inserts an element into the map
200 std::pair
<iterator
, bool> insert(const std::pair
<Key
, Value
>& pair
) {
201 // First make sure the map doesn't have a key with this value. If it does,
202 // return a pair with an iterator to it, and false indicating that we
203 // didn't insert anything.
204 typename
MapType::iterator found
= map_
.find(pair
.first
);
205 if (found
!= map_
.end()) return std::make_pair(found
->second
, false);
207 // Otherwise, insert into the list first.
208 list_
.push_back(pair
);
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();
215 CHECK(map_
.insert(std::make_pair(pair
.first
, last
)).second
)
216 << "Map and list are inconsistent";
218 return std::make_pair(last
, true);
221 size_type
size() const {
225 void swap(linked_hash_map
& other
) {
226 map_
.swap(other
.map_
);
227 list_
.swap(other
.list_
);
231 // The map component, used for speedy lookups
234 // The list component, used for maintaining insertion order
237 // |map_| contains iterators to |list_|, therefore a default copy constructor
238 // or copy assignment operator would result in an inconsistent state.
239 DISALLOW_COPY_AND_ASSIGN(linked_hash_map
);
242 #endif // UTIL_GTL_LINKED_HASH_MAP_H_