OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_BASE_PRIORITY_QUEUE_H_ | 5 #ifndef NET_BASE_PRIORITY_QUEUE_H_ |
6 #define NET_BASE_PRIORITY_QUEUE_H_ | 6 #define NET_BASE_PRIORITY_QUEUE_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 unsigned id = next_id_; | 132 unsigned id = next_id_; |
133 valid_ids_.insert(id); | 133 valid_ids_.insert(id); |
134 ++next_id_; | 134 ++next_id_; |
135 return Pointer(priority, list.insert(list.end(), | 135 return Pointer(priority, list.insert(list.end(), |
136 std::make_pair(id, value))); | 136 std::make_pair(id, value))); |
137 #else | 137 #else |
138 return Pointer(priority, list.insert(list.end(), value)); | 138 return Pointer(priority, list.insert(list.end(), value)); |
139 #endif | 139 #endif |
140 } | 140 } |
141 | 141 |
| 142 // Adds |value| with |priority| to the queue. Returns a pointer to the |
| 143 // created element. |
| 144 Pointer InsertAtFront(const T& value, Priority priority) { |
| 145 DCHECK(CalledOnValidThread()); |
| 146 DCHECK_LT(priority, lists_.size()); |
| 147 ++size_; |
| 148 List& list = lists_[priority]; |
| 149 #if !defined(NDEBUG) |
| 150 unsigned id = next_id_; |
| 151 valid_ids_.insert(id); |
| 152 ++next_id_; |
| 153 return Pointer(priority, list.insert(list.begin(), |
| 154 std::make_pair(id, value))); |
| 155 #else |
| 156 return Pointer(priority, list.insert(list.begin(), value)); |
| 157 #endif |
| 158 } |
| 159 |
142 // Removes the value pointed by |pointer| from the queue. All pointers to this | 160 // Removes the value pointed by |pointer| from the queue. All pointers to this |
143 // value including |pointer| become invalid. | 161 // value including |pointer| become invalid. |
144 void Erase(const Pointer& pointer) { | 162 void Erase(const Pointer& pointer) { |
145 DCHECK(CalledOnValidThread()); | 163 DCHECK(CalledOnValidThread()); |
146 DCHECK_LT(pointer.priority_, lists_.size()); | 164 DCHECK_LT(pointer.priority_, lists_.size()); |
147 DCHECK_GT(size_, 0u); | 165 DCHECK_GT(size_, 0u); |
148 | 166 |
149 #if !defined(NDEBUG) | 167 #if !defined(NDEBUG) |
150 DCHECK_EQ(1u, valid_ids_.erase(pointer.id_)); | 168 DCHECK_EQ(1u, valid_ids_.erase(pointer.id_)); |
151 DCHECK_EQ(pointer.iterator_->first, pointer.id_); | 169 DCHECK_EQ(pointer.iterator_->first, pointer.id_); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 | 247 |
230 ListVector lists_; | 248 ListVector lists_; |
231 size_t size_; | 249 size_t size_; |
232 | 250 |
233 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); | 251 DISALLOW_COPY_AND_ASSIGN(PriorityQueue); |
234 }; | 252 }; |
235 | 253 |
236 } // namespace net | 254 } // namespace net |
237 | 255 |
238 #endif // NET_BASE_PRIORITY_QUEUE_H_ | 256 #endif // NET_BASE_PRIORITY_QUEUE_H_ |
OLD | NEW |