| OLD | NEW |
| 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. |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 return 1; | 132 return 1; |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Erases the item that 'position' points to. Returns an iterator that points | 135 // Erases the item that 'position' points to. Returns an iterator that points |
| 136 // to the item that comes immediately after the deleted item in the list, or | 136 // to the item that comes immediately after the deleted item in the list, or |
| 137 // end(). | 137 // end(). |
| 138 // If the provided iterator is invalid or there is inconsistency between the | 138 // If the provided iterator is invalid or there is inconsistency between the |
| 139 // map and list, a CHECK() error will occur. | 139 // map and list, a CHECK() error will occur. |
| 140 iterator erase(iterator position) { | 140 iterator erase(iterator position) { |
| 141 typename MapType::iterator found = map_.find(position->first); | 141 typename MapType::iterator found = map_.find(position->first); |
| 142 CHECK(found->second == position) | 142 // Inconsisent iterator for map and list, or the iterator is invalid. |
| 143 << "Inconsisent iterator for map and list, or the iterator is invalid."; | 143 CHECK(found->second == position); |
| 144 | 144 |
| 145 map_.erase(found); | 145 map_.erase(found); |
| 146 return list_.erase(position); | 146 return list_.erase(position); |
| 147 } | 147 } |
| 148 | 148 |
| 149 // Erases all the items in the range [first, last). Returns an iterator that | 149 // Erases all the items in the range [first, last). Returns an iterator that |
| 150 // points to the item that comes immediately after the last deleted item in | 150 // points to the item that comes immediately after the last deleted item in |
| 151 // the list, or end(). | 151 // the list, or end(). |
| 152 iterator erase(iterator first, iterator last) { | 152 iterator erase(iterator first, iterator last) { |
| 153 while (first != last && first != end()) { | 153 while (first != last && first != end()) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 if (found != map_.end()) return std::make_pair(found->second, false); | 212 if (found != map_.end()) return std::make_pair(found->second, false); |
| 213 | 213 |
| 214 // Otherwise, insert into the list first. | 214 // Otherwise, insert into the list first. |
| 215 list_.push_back(pair); | 215 list_.push_back(pair); |
| 216 | 216 |
| 217 // Obtain an iterator to the newly added element. We do -- instead of - | 217 // Obtain an iterator to the newly added element. We do -- instead of - |
| 218 // since list::iterator doesn't implement operator-(). | 218 // since list::iterator doesn't implement operator-(). |
| 219 typename ListType::iterator last = list_.end(); | 219 typename ListType::iterator last = list_.end(); |
| 220 --last; | 220 --last; |
| 221 | 221 |
| 222 CHECK(map_.insert(std::make_pair(pair.first, last)).second) | 222 // Map and list are inconsistent |
| 223 << "Map and list are inconsistent"; | 223 CHECK(map_.insert(std::make_pair(pair.first, last)).second); |
| 224 | 224 |
| 225 return std::make_pair(last, true); | 225 return std::make_pair(last, true); |
| 226 } | 226 } |
| 227 | 227 |
| 228 size_type size() const { | 228 size_type size() const { |
| 229 return list_.size(); | 229 return list_.size(); |
| 230 } | 230 } |
| 231 | 231 |
| 232 template <typename... Args> | 232 template <typename... Args> |
| 233 std::pair<iterator, bool> emplace(Args&&... args) { | 233 std::pair<iterator, bool> emplace(Args&&... args) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 255 ListType list_; | 255 ListType list_; |
| 256 | 256 |
| 257 // |map_| contains iterators to |list_|, therefore a default copy constructor | 257 // |map_| contains iterators to |list_|, therefore a default copy constructor |
| 258 // or copy assignment operator would result in an inconsistent state. | 258 // or copy assignment operator would result in an inconsistent state. |
| 259 DISALLOW_COPY_AND_ASSIGN(linked_hash_map); | 259 DISALLOW_COPY_AND_ASSIGN(linked_hash_map); |
| 260 }; | 260 }; |
| 261 | 261 |
| 262 } // namespace net | 262 } // namespace net |
| 263 | 263 |
| 264 #endif // UTIL_GTL_LINKED_HASH_MAP_H_ | 264 #endif // UTIL_GTL_LINKED_HASH_MAP_H_ |
| OLD | NEW |