Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(659)

Side by Side Diff: net/base/linked_hash_map.h

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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 // This is a simplistic insertion-ordered map. It behaves similarly to an STL 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 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. 7 // just keep a map and a list going in parallel.
8 // 8 //
9 // This class provides no thread safety guarantees, beyond what you would 9 // This class provides no thread safety guarantees, beyond what you would
10 // normally see with std::list. 10 // normally see with std::list.
11 // 11 //
12 // Iterators should be stable in the face of mutations, except for an 12 // Iterators should be stable in the face of mutations, except for an
13 // iterator pointing to an element that was just deleted. 13 // iterator pointing to an element that was just deleted.
14 14
15 #ifndef UTIL_GTL_LINKED_HASH_MAP_H_ 15 #ifndef UTIL_GTL_LINKED_HASH_MAP_H_
16 #define UTIL_GTL_LINKED_HASH_MAP_H_ 16 #define UTIL_GTL_LINKED_HASH_MAP_H_
17 17
18 #include <list> 18 #include <list>
19 #include <utility> 19 #include <utility>
20 20
21 #include "base/containers/hash_tables.h" 21 #include "base/containers/hash_tables.h"
22 #include "base/logging.h" 22 #include "base/logging.h"
23 23
24 // This holds a list of pair<Key, Value> items. This list is what gets 24 // This holds a list of pair<Key, Value> items. This list is what gets
25 // traversed, and it's iterators from this list that we return from 25 // traversed, and it's iterators from this list that we return from
26 // begin/end/find. 26 // begin/end/find.
27 // 27 //
28 // We also keep a map<Key, list::iterator> for find. Since std::list is a 28 // We also keep a map<Key, list::iterator> for find. Since std::list is a
29 // doubly-linked list, the iterators should remain stable. 29 // doubly-linked list, the iterators should remain stable.
30 template<class Key, class Value> 30 template <class Key, class Value>
31 class linked_hash_map { 31 class linked_hash_map {
32 private: 32 private:
33 typedef std::list<std::pair<Key, Value> > ListType; 33 typedef std::list<std::pair<Key, Value> > ListType;
34 typedef base::hash_map<Key, typename ListType::iterator> MapType; 34 typedef base::hash_map<Key, typename ListType::iterator> MapType;
35 35
36 public: 36 public:
37 typedef typename ListType::iterator iterator; 37 typedef typename ListType::iterator iterator;
38 typedef typename ListType::reverse_iterator reverse_iterator; 38 typedef typename ListType::reverse_iterator reverse_iterator;
39 typedef typename ListType::const_iterator const_iterator; 39 typedef typename ListType::const_iterator const_iterator;
40 typedef typename ListType::const_reverse_iterator const_reverse_iterator; 40 typedef typename ListType::const_reverse_iterator const_reverse_iterator;
41 typedef typename MapType::key_type key_type; 41 typedef typename MapType::key_type key_type;
42 typedef typename ListType::value_type value_type; 42 typedef typename ListType::value_type value_type;
43 typedef typename ListType::size_type size_type; 43 typedef typename ListType::size_type size_type;
44 44
45 linked_hash_map() : map_(), list_() { 45 linked_hash_map() : map_(), list_() {}
46 }
47 46
48 // Returns an iterator to the first (insertion-ordered) element. Like a map, 47 // Returns an iterator to the first (insertion-ordered) element. Like a map,
49 // this can be dereferenced to a pair<Key, Value>. 48 // this can be dereferenced to a pair<Key, Value>.
50 iterator begin() { 49 iterator begin() { return list_.begin(); }
51 return list_.begin(); 50 const_iterator begin() const { return list_.begin(); }
52 }
53 const_iterator begin() const {
54 return list_.begin();
55 }
56 51
57 // Returns an iterator beyond the last element. 52 // Returns an iterator beyond the last element.
58 iterator end() { 53 iterator end() { return list_.end(); }
59 return list_.end(); 54 const_iterator end() const { return list_.end(); }
60 }
61 const_iterator end() const {
62 return list_.end();
63 }
64 55
65 // Returns an iterator to the last (insertion-ordered) element. Like a map, 56 // Returns an iterator to the last (insertion-ordered) element. Like a map,
66 // this can be dereferenced to a pair<Key, Value>. 57 // this can be dereferenced to a pair<Key, Value>.
67 reverse_iterator rbegin() { 58 reverse_iterator rbegin() { return list_.rbegin(); }
68 return list_.rbegin(); 59 const_reverse_iterator rbegin() const { return list_.rbegin(); }
69 }
70 const_reverse_iterator rbegin() const {
71 return list_.rbegin();
72 }
73 60
74 // Returns an iterator beyond the first element. 61 // Returns an iterator beyond the first element.
75 reverse_iterator rend() { 62 reverse_iterator rend() { return list_.rend(); }
76 return list_.rend(); 63 const_reverse_iterator rend() const { return list_.rend(); }
77 }
78 const_reverse_iterator rend() const {
79 return list_.rend();
80 }
81 64
82 // Clears the map of all values. 65 // Clears the map of all values.
83 void clear() { 66 void clear() {
84 map_.clear(); 67 map_.clear();
85 list_.clear(); 68 list_.clear();
86 } 69 }
87 70
88 // Returns true iff the map is empty. 71 // Returns true iff the map is empty.
89 bool empty() const { 72 bool empty() const { return list_.empty(); }
90 return list_.empty();
91 }
92 73
93 // Erases values with the provided key. Returns the number of elements 74 // Erases values with the provided key. Returns the number of elements
94 // erased. In this implementation, this will be 0 or 1. 75 // erased. In this implementation, this will be 0 or 1.
95 size_type erase(const Key& key) { 76 size_type erase(const Key& key) {
96 typename MapType::iterator found = map_.find(key); 77 typename MapType::iterator found = map_.find(key);
97 if (found == map_.end()) return 0; 78 if (found == map_.end())
79 return 0;
98 80
99 list_.erase(found->second); 81 list_.erase(found->second);
100 map_.erase(found); 82 map_.erase(found);
101 83
102 return 1; 84 return 1;
103 } 85 }
104 86
105 // Erases values with the provided iterator. If the provided iterator is 87 // Erases values with the provided iterator. If the provided iterator is
106 // invalid or there is inconsistency between the map and list, a CHECK() error 88 // invalid or there is inconsistency between the map and list, a CHECK() error
107 // will occur. 89 // will occur.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 std::pair<iterator, iterator> equal_range(const key_type& key) { 127 std::pair<iterator, iterator> equal_range(const key_type& key) {
146 std::pair<typename MapType::iterator, typename MapType::iterator> eq_range = 128 std::pair<typename MapType::iterator, typename MapType::iterator> eq_range =
147 map_.equal_range(key); 129 map_.equal_range(key);
148 130
149 return std::make_pair(eq_range.first->second, eq_range.second->second); 131 return std::make_pair(eq_range.first->second, eq_range.second->second);
150 } 132 }
151 133
152 std::pair<const_iterator, const_iterator> equal_range( 134 std::pair<const_iterator, const_iterator> equal_range(
153 const key_type& key) const { 135 const key_type& key) const {
154 std::pair<typename MapType::const_iterator, 136 std::pair<typename MapType::const_iterator,
155 typename MapType::const_iterator> eq_range = 137 typename MapType::const_iterator> eq_range =
156 map_.equal_range(key); 138 map_.equal_range(key);
157 const const_iterator& start_iter = eq_range.first != map_.end() ? 139 const const_iterator& start_iter =
158 eq_range.first->second : end(); 140 eq_range.first != map_.end() ? eq_range.first->second : end();
159 const const_iterator& end_iter = eq_range.second != map_.end() ? 141 const const_iterator& end_iter =
160 eq_range.second->second : end(); 142 eq_range.second != map_.end() ? eq_range.second->second : end();
161 143
162 return std::make_pair(start_iter, end_iter); 144 return std::make_pair(start_iter, end_iter);
163 } 145 }
164 146
165 // Returns the value mapped to key, or an inserted iterator to that position 147 // Returns the value mapped to key, or an inserted iterator to that position
166 // in the map. 148 // in the map.
167 Value& operator[](const key_type& key) { 149 Value& operator[](const key_type& key) {
168 return (*((this->insert(std::make_pair(key, Value()))).first)).second; 150 return (*((this->insert(std::make_pair(key, Value()))).first)).second;
169 } 151 }
170 152
171 // Inserts an element into the map 153 // Inserts an element into the map
172 std::pair<iterator, bool> insert(const std::pair<Key, Value>& pair) { 154 std::pair<iterator, bool> insert(const std::pair<Key, Value>& pair) {
173 // First make sure the map doesn't have a key with this value. If it does, 155 // First make sure the map doesn't have a key with this value. If it does,
174 // return a pair with an iterator to it, and false indicating that we 156 // return a pair with an iterator to it, and false indicating that we
175 // didn't insert anything. 157 // didn't insert anything.
176 typename MapType::iterator found = map_.find(pair.first); 158 typename MapType::iterator found = map_.find(pair.first);
177 if (found != map_.end()) return std::make_pair(found->second, false); 159 if (found != map_.end())
160 return std::make_pair(found->second, false);
178 161
179 // Otherwise, insert into the list first. 162 // Otherwise, insert into the list first.
180 list_.push_back(pair); 163 list_.push_back(pair);
181 164
182 // Obtain an iterator to the newly added element. We do -- instead of - 165 // Obtain an iterator to the newly added element. We do -- instead of -
183 // since list::iterator doesn't implement operator-(). 166 // since list::iterator doesn't implement operator-().
184 typename ListType::iterator last = list_.end(); 167 typename ListType::iterator last = list_.end();
185 --last; 168 --last;
186 169
187 CHECK(map_.insert(std::make_pair(pair.first, last)).second) 170 CHECK(map_.insert(std::make_pair(pair.first, last)).second)
188 << "Map and list are inconsistent"; 171 << "Map and list are inconsistent";
189 172
190 return std::make_pair(last, true); 173 return std::make_pair(last, true);
191 } 174 }
192 175
193 size_type size() const { 176 size_type size() const { return list_.size(); }
194 return list_.size();
195 }
196 177
197 void swap(linked_hash_map& other) { 178 void swap(linked_hash_map& other) {
198 map_.swap(other.map_); 179 map_.swap(other.map_);
199 list_.swap(other.list_); 180 list_.swap(other.list_);
200 } 181 }
201 182
202 private: 183 private:
203 // The map component, used for speedy lookups 184 // The map component, used for speedy lookups
204 MapType map_; 185 MapType map_;
205 186
206 // The list component, used for maintaining insertion order 187 // The list component, used for maintaining insertion order
207 ListType list_; 188 ListType list_;
208 }; 189 };
209 190
210 #endif // UTIL_GTL_LINKED_HASH_MAP_H_ 191 #endif // UTIL_GTL_LINKED_HASH_MAP_H_
OLDNEW
« no previous file with comments | « net/base/keygen_handler_win.cc ('k') | net/base/load_flags.h » ('j') | net/base/mime_sniffer.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698