Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_PRIORITY_QUEUE_H_ |
| 6 | #define NET_BASE_PRIORITY_QUEUE_H_ |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 11 | #include <list> |
David Bienvenu | a03ac8c | 2020-11-06 15:55:39 | [diff] [blame] | 12 | #include <utility> |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Hans Wennborg | 0cb546b | 2020-06-23 18:00:09 | [diff] [blame] | 15 | #include "base/check_op.h" |
Avi Drissman | 41c4a41 | 2023-01-11 22:45:37 | [diff] [blame] | 16 | #include "base/functional/bind.h" |
| 17 | #include "base/functional/callback.h" |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 19 | |
| 20 | #if !defined(NDEBUG) |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 21 | #include <unordered_set> |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | namespace net { |
| 25 | |
| 26 | // A simple priority queue. The order of values is by priority and then FIFO. |
| 27 | // Unlike the std::priority_queue, this implementation allows erasing elements |
| 28 | // from the queue, and all operations are O(p) time for p priority levels. |
| 29 | // The queue is agnostic to priority ordering (whether 0 precedes 1). |
| 30 | // If the highest priority is 0, FirstMin() returns the first in order. |
| 31 | // |
| 32 | // In debug-mode, the internal queues store (id, value) pairs where id is used |
| 33 | // to validate Pointers. |
| 34 | // |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 35 | template <typename T> |
| 36 | class PriorityQueue { |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 37 | private: |
| 38 | // This section is up-front for Pointer only. |
| 39 | #if !defined(NDEBUG) |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 40 | typedef std::list<std::pair<unsigned, T> > List; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 41 | #else |
| 42 | typedef std::list<T> List; |
| 43 | #endif |
| 44 | |
| 45 | public: |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 46 | typedef uint32_t Priority; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 47 | |
| 48 | // A pointer to a value stored in the queue. The pointer becomes invalid |
| 49 | // when the queue is destroyed or cleared, or the value is erased. |
| 50 | class Pointer { |
| 51 | public: |
| 52 | // Constructs a null pointer. |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 53 | Pointer() : priority_(kNullPriority) { |
| 54 | #if !defined(NDEBUG) |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 55 | id_ = static_cast<unsigned>(-1); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 56 | #endif |
[email protected] | d826a97b | 2013-11-20 00:35:14 | [diff] [blame] | 57 | // TODO(syzm) |
| 58 | // An uninitialized iterator behaves like an uninitialized pointer as per |
| 59 | // the STL docs. The fix below is ugly and should possibly be replaced |
| 60 | // with a better approach. |
| 61 | iterator_ = dummy_empty_list_.end(); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 62 | } |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 63 | |
[email protected] | d826a97b | 2013-11-20 00:35:14 | [diff] [blame] | 64 | Pointer(const Pointer& p) |
| 65 | : priority_(p.priority_), |
| 66 | iterator_(p.iterator_) { |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 67 | #if !defined(NDEBUG) |
| 68 | id_ = p.id_; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | Pointer& operator=(const Pointer& p) { |
| 73 | // Self-assignment is benign. |
| 74 | priority_ = p.priority_; |
| 75 | iterator_ = p.iterator_; |
| 76 | #if !defined(NDEBUG) |
| 77 | id_ = p.id_; |
| 78 | #endif |
| 79 | return *this; |
| 80 | } |
| 81 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 82 | bool is_null() const { return priority_ == kNullPriority; } |
| 83 | |
| 84 | Priority priority() const { return priority_; } |
| 85 | |
| 86 | #if !defined(NDEBUG) |
| 87 | const T& value() const { return iterator_->second; } |
| 88 | #else |
| 89 | const T& value() const { return *iterator_; } |
| 90 | #endif |
| 91 | |
| 92 | // Comparing to Pointer from a different PriorityQueue is undefined. |
| 93 | bool Equals(const Pointer& other) const { |
| 94 | return (priority_ == other.priority_) && (iterator_ == other.iterator_); |
| 95 | } |
| 96 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 97 | void Reset() { |
| 98 | *this = Pointer(); |
| 99 | } |
| 100 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 101 | private: |
| 102 | friend class PriorityQueue; |
| 103 | |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 104 | // Note that we need iterator and not const_iterator to pass to |
| 105 | // List::erase. When C++11 is turned on for Chromium, this could |
| 106 | // be changed to const_iterator and the const_casts in the rest of |
| 107 | // the file can be removed. |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 108 | typedef typename PriorityQueue::List::iterator ListIterator; |
| 109 | |
| 110 | static const Priority kNullPriority = static_cast<Priority>(-1); |
| 111 | |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 112 | // It is guaranteed that Pointer will treat |iterator| as a |
| 113 | // const_iterator. |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 114 | Pointer(Priority priority, const ListIterator& iterator) |
| 115 | : priority_(priority), iterator_(iterator) { |
| 116 | #if !defined(NDEBUG) |
| 117 | id_ = iterator_->first; |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | Priority priority_; |
| 122 | ListIterator iterator_; |
[email protected] | d826a97b | 2013-11-20 00:35:14 | [diff] [blame] | 123 | // The STL iterators when uninitialized are like uninitialized pointers |
| 124 | // which cause crashes when assigned to other iterators. We need to |
| 125 | // initialize a NULL iterator to the end of a valid list. |
| 126 | List dummy_empty_list_; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 127 | |
| 128 | #if !defined(NDEBUG) |
| 129 | // Used by the queue to check if a Pointer is valid. |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 130 | unsigned id_; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 131 | #endif |
| 132 | }; |
| 133 | |
| 134 | // Creates a new queue for |num_priorities|. |
Tsuyoshi Horo | 11e321eb | 2022-06-07 07:56:40 | [diff] [blame] | 135 | explicit PriorityQueue(Priority num_priorities) : lists_(num_priorities) { |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 136 | #if !defined(NDEBUG) |
| 137 | next_id_ = 0; |
| 138 | #endif |
| 139 | } |
| 140 | |
David Bienvenu | a03ac8c | 2020-11-06 15:55:39 | [diff] [blame] | 141 | PriorityQueue(const PriorityQueue&) = delete; |
| 142 | PriorityQueue& operator=(const PriorityQueue&) = delete; |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 143 | ~PriorityQueue() { DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); } |
| 144 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 145 | // Adds |value| with |priority| to the queue. Returns a pointer to the |
| 146 | // created element. |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 147 | Pointer Insert(T value, Priority priority) { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 148 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 149 | DCHECK_LT(priority, lists_.size()); |
| 150 | ++size_; |
| 151 | List& list = lists_[priority]; |
| 152 | #if !defined(NDEBUG) |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 153 | unsigned id = next_id_; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 154 | valid_ids_.insert(id); |
| 155 | ++next_id_; |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 156 | list.emplace_back(std::make_pair(id, std::move(value))); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 157 | #else |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 158 | list.emplace_back(std::move(value)); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 159 | #endif |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 160 | return Pointer(priority, std::prev(list.end())); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 163 | // Adds |value| with |priority| to the queue. Returns a pointer to the |
| 164 | // created element. |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 165 | Pointer InsertAtFront(T value, Priority priority) { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 166 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 167 | DCHECK_LT(priority, lists_.size()); |
| 168 | ++size_; |
| 169 | List& list = lists_[priority]; |
| 170 | #if !defined(NDEBUG) |
| 171 | unsigned id = next_id_; |
| 172 | valid_ids_.insert(id); |
| 173 | ++next_id_; |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 174 | list.emplace_front(std::make_pair(id, std::move(value))); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 175 | #else |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 176 | list.emplace_front(std::move(value)); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 177 | #endif |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 178 | return Pointer(priority, list.begin()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 181 | // Removes the value pointed by |pointer| from the queue. All pointers to this |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 182 | // value including |pointer| become invalid. Returns the erased value. |
| 183 | T Erase(const Pointer& pointer) { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 184 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 185 | DCHECK_LT(pointer.priority_, lists_.size()); |
| 186 | DCHECK_GT(size_, 0u); |
| 187 | |
| 188 | #if !defined(NDEBUG) |
| 189 | DCHECK_EQ(1u, valid_ids_.erase(pointer.id_)); |
| 190 | DCHECK_EQ(pointer.iterator_->first, pointer.id_); |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 191 | T erased = std::move(pointer.iterator_->second); |
| 192 | #else |
| 193 | T erased = std::move(*pointer.iterator_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 194 | #endif |
| 195 | |
| 196 | --size_; |
| 197 | lists_[pointer.priority_].erase(pointer.iterator_); |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 198 | return erased; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Returns a pointer to the first value of minimum priority or a null-pointer |
| 202 | // if empty. |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 203 | Pointer FirstMin() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 204 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 205 | for (size_t i = 0; i < lists_.size(); ++i) { |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 206 | List* list = const_cast<List*>(&lists_[i]); |
| 207 | if (!list->empty()) |
| 208 | return Pointer(i, list->begin()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 209 | } |
| 210 | return Pointer(); |
| 211 | } |
| 212 | |
| 213 | // Returns a pointer to the last value of minimum priority or a null-pointer |
| 214 | // if empty. |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 215 | Pointer LastMin() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 216 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 217 | for (size_t i = 0; i < lists_.size(); ++i) { |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 218 | List* list = const_cast<List*>(&lists_[i]); |
| 219 | if (!list->empty()) |
| 220 | return Pointer(i, --list->end()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 221 | } |
| 222 | return Pointer(); |
| 223 | } |
| 224 | |
| 225 | // Returns a pointer to the first value of maximum priority or a null-pointer |
| 226 | // if empty. |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 227 | Pointer FirstMax() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 228 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 229 | for (size_t i = lists_.size(); i > 0; --i) { |
| 230 | size_t index = i - 1; |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 231 | List* list = const_cast<List*>(&lists_[index]); |
| 232 | if (!list->empty()) |
| 233 | return Pointer(index, list->begin()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 234 | } |
| 235 | return Pointer(); |
| 236 | } |
| 237 | |
| 238 | // Returns a pointer to the last value of maximum priority or a null-pointer |
| 239 | // if empty. |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 240 | Pointer LastMax() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 241 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 242 | for (size_t i = lists_.size(); i > 0; --i) { |
| 243 | size_t index = i - 1; |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 244 | List* list = const_cast<List*>(&lists_[index]); |
| 245 | if (!list->empty()) |
| 246 | return Pointer(index, --list->end()); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 247 | } |
| 248 | return Pointer(); |
| 249 | } |
| 250 | |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 251 | // Given an ordering of the values in this queue by decreasing priority and |
| 252 | // then FIFO, returns a pointer to the value following the value of the given |
| 253 | // pointer (which must be non-NULL). I.e., gets the next element in decreasing |
| 254 | // priority, then FIFO order. If the given pointer is already pointing at the |
| 255 | // last value, returns a null Pointer. |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 256 | // |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 257 | // (One could also implement GetNextTowardsFirstMin() [decreasing priority, |
| 258 | // then reverse FIFO] as well as GetNextTowards{First,Last}Max() [increasing |
| 259 | // priority, then {,reverse} FIFO].) |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 260 | Pointer GetNextTowardsLastMin(const Pointer& pointer) const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 261 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 262 | DCHECK(!pointer.is_null()); |
| 263 | DCHECK_LT(pointer.priority_, lists_.size()); |
| 264 | |
| 265 | typename Pointer::ListIterator it = pointer.iterator_; |
| 266 | Priority priority = pointer.priority_; |
| 267 | DCHECK(it != lists_[priority].end()); |
| 268 | ++it; |
| 269 | while (it == lists_[priority].end()) { |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 270 | if (priority == 0u) { |
| 271 | DCHECK(pointer.Equals(LastMin())); |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 272 | return Pointer(); |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 273 | } |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 274 | --priority; |
| 275 | it = const_cast<List*>(&lists_[priority])->begin(); |
| 276 | } |
| 277 | return Pointer(priority, it); |
| 278 | } |
| 279 | |
Lily Chen | 8ce89e4 | 2018-10-23 21:13:58 | [diff] [blame] | 280 | // Given an ordering of the values in this queue by decreasing priority and |
| 281 | // then FIFO, returns a pointer to the value preceding the value of the given |
| 282 | // pointer (which must be non-NULL). I.e., gets the next element in increasing |
| 283 | // priority, then reverse FIFO order. If the given pointer is already pointing |
| 284 | // at the first value, returns a null Pointer. |
| 285 | Pointer GetPreviousTowardsFirstMax(const Pointer& pointer) const { |
| 286 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 287 | DCHECK(!pointer.is_null()); |
| 288 | DCHECK_LT(pointer.priority_, lists_.size()); |
| 289 | |
| 290 | typename Pointer::ListIterator it = pointer.iterator_; |
| 291 | Priority priority = pointer.priority_; |
| 292 | DCHECK(it != lists_[priority].end()); |
| 293 | while (it == lists_[priority].begin()) { |
| 294 | if (priority == num_priorities() - 1) { |
| 295 | DCHECK(pointer.Equals(FirstMax())); |
| 296 | return Pointer(); |
| 297 | } |
| 298 | ++priority; |
| 299 | it = const_cast<List*>(&lists_[priority])->end(); |
| 300 | } |
| 301 | return Pointer(priority, std::prev(it)); |
| 302 | } |
| 303 | |
| 304 | // Checks whether |lhs| is closer in the queue to the first max element than |
| 305 | // |rhs|. Assumes that both Pointers refer to elements in this PriorityQueue. |
| 306 | bool IsCloserToFirstMaxThan(const Pointer& lhs, const Pointer& rhs) { |
| 307 | if (lhs.Equals(rhs)) |
| 308 | return false; |
| 309 | if (lhs.priority_ == rhs.priority_) { |
| 310 | // Traverse list starting from lhs and see if we find rhs. |
| 311 | for (auto it = lhs.iterator_; it != lists_[lhs.priority_].end(); ++it) { |
| 312 | if (it == rhs.iterator_) |
| 313 | return true; |
| 314 | } |
| 315 | return false; |
| 316 | } |
| 317 | return lhs.priority_ > rhs.priority_; |
| 318 | } |
| 319 | |
| 320 | // Checks whether |lhs| is closer in the queue to the last min element than |
| 321 | // |rhs|. Assumes that both Pointers refer to elements in this PriorityQueue. |
| 322 | bool IsCloserToLastMinThan(const Pointer& lhs, const Pointer& rhs) { |
| 323 | return !lhs.Equals(rhs) && !IsCloserToFirstMaxThan(lhs, rhs); |
| 324 | } |
| 325 | |
| 326 | // Finds the first element (with respect to decreasing priority, then FIFO |
| 327 | // order) which matches the given predicate. |
| 328 | Pointer FindIf(const base::RepeatingCallback<bool(T)>& pred) { |
| 329 | for (auto pointer = FirstMax(); !pointer.is_null(); |
| 330 | pointer = GetNextTowardsLastMin(pointer)) { |
| 331 | if (pred.Run(pointer.value())) |
| 332 | return pointer; |
| 333 | } |
| 334 | return Pointer(); |
| 335 | } |
| 336 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 337 | // Empties the queue. All pointers become invalid. |
| 338 | void Clear() { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 339 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 340 | for (size_t i = 0; i < lists_.size(); ++i) { |
| 341 | lists_[i].clear(); |
| 342 | } |
| 343 | #if !defined(NDEBUG) |
| 344 | valid_ids_.clear(); |
| 345 | #endif |
| 346 | size_ = 0u; |
| 347 | } |
| 348 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 349 | // Returns the number of priorities the queue supports. |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 350 | size_t num_priorities() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 351 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 352 | return lists_.size(); |
| 353 | } |
| 354 | |
| 355 | bool empty() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 356 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | 48118c3 | 2013-10-24 00:39:16 | [diff] [blame] | 357 | return size_ == 0; |
| 358 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 359 | |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 360 | // Returns number of queued values. |
| 361 | size_t size() const { |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 362 | DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 363 | return size_; |
| 364 | } |
| 365 | |
| 366 | private: |
| 367 | typedef std::vector<List> ListVector; |
| 368 | |
| 369 | #if !defined(NDEBUG) |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 370 | unsigned next_id_; |
davidben | 1e912ea | 2016-04-20 19:17:07 | [diff] [blame] | 371 | std::unordered_set<unsigned> valid_ids_; |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 372 | #endif |
| 373 | |
| 374 | ListVector lists_; |
Tsuyoshi Horo | 11e321eb | 2022-06-07 07:56:40 | [diff] [blame] | 375 | size_t size_ = 0; |
[email protected] | 043324f4 | 2012-06-02 00:18:53 | [diff] [blame] | 376 | |
gab | 47aa7da | 2017-06-02 16:09:43 | [diff] [blame] | 377 | THREAD_CHECKER(thread_checker_); |
[email protected] | ba79eca | 2012-01-06 21:03:56 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | } // namespace net |
| 381 | |
| 382 | #endif // NET_BASE_PRIORITY_QUEUE_H_ |