Chromium Code Reviews| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 // If the highest priority is 0, FirstMin() returns the first in order. | 27 // If the highest priority is 0, FirstMin() returns the first in order. |
| 28 // | 28 // |
| 29 // In debug-mode, the internal queues store (id, value) pairs where id is used | 29 // In debug-mode, the internal queues store (id, value) pairs where id is used |
| 30 // to validate Pointers. | 30 // to validate Pointers. |
| 31 // | 31 // |
| 32 template<typename T> | 32 template<typename T> |
| 33 class PriorityQueue : public base::NonThreadSafe { | 33 class PriorityQueue : public base::NonThreadSafe { |
| 34 private: | 34 private: |
| 35 // This section is up-front for Pointer only. | 35 // This section is up-front for Pointer only. |
| 36 #if !defined(NDEBUG) | 36 #if !defined(NDEBUG) |
| 37 typedef std::list<std::pair<size_t, T> > List; | 37 typedef std::list<std::pair<size_t, T> > List; |
|
stevez
2012/04/11 16:13:58
why size_t for the id? it's not a size.
szym
2012/05/17 19:52:01
Done.
| |
| 38 #else | 38 #else |
| 39 typedef std::list<T> List; | 39 typedef std::list<T> List; |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 public: | 42 public: |
| 43 typedef uint32 Priority; | 43 typedef uint32 Priority; |
| 44 | 44 |
| 45 // A pointer to a value stored in the queue. The pointer becomes invalid | 45 // A pointer to a value stored in the queue. The pointer becomes invalid |
| 46 // when the queue is destroyed or cleared, or the value is erased. | 46 // when the queue is destroyed or cleared, or the value is erased. |
| 47 class Pointer { | 47 class Pointer { |
| 48 public: | 48 public: |
| 49 // Constructs a null pointer. | 49 // Constructs a null pointer. |
| 50 Pointer() : priority_(kNullPriority) { | 50 Pointer() : priority_(kNullPriority) { |
|
stevez
2012/04/11 16:13:58
If you intend to make this copyable, define the co
szym
2012/05/17 19:52:01
The default copy constructor and assignment are fi
stevez
2012/05/22 14:22:40
http://www.corp.google.com/eng/doc/cppguide.xml?sh
szym
2012/05/23 01:26:49
Understood. Done.
| |
| 51 #if !defined(NDEBUG) | 51 #if !defined(NDEBUG) |
| 52 id_ = static_cast<size_t>(-1); | 52 id_ = static_cast<size_t>(-1); |
| 53 #endif | 53 #endif |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool is_null() const { return priority_ == kNullPriority; } | 56 bool is_null() const { return priority_ == kNullPriority; } |
| 57 | 57 |
| 58 Priority priority() const { return priority_; } | 58 Priority priority() const { return priority_; } |
| 59 | 59 |
| 60 #if !defined(NDEBUG) | 60 #if !defined(NDEBUG) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 Priority priority_; | 92 Priority priority_; |
| 93 ListIterator iterator_; | 93 ListIterator iterator_; |
| 94 | 94 |
| 95 #if !defined(NDEBUG) | 95 #if !defined(NDEBUG) |
| 96 // Used by the queue to check if a Pointer is valid. | 96 // Used by the queue to check if a Pointer is valid. |
| 97 size_t id_; | 97 size_t id_; |
| 98 #endif | 98 #endif |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 // Creates a new queue for |num_priorities|. | 101 // Creates a new queue for |num_priorities|. |
| 102 explicit PriorityQueue(Priority num_priorities) | 102 explicit PriorityQueue(Priority num_priorities) |
|
stevez
2012/04/11 16:13:58
Similarly, DISALLOW_COPY_OR_ASSIGN, or define copy
szym
2012/05/17 19:52:01
Done.
| |
| 103 : lists_(num_priorities), size_(0) { | 103 : lists_(num_priorities), size_(0) { |
| 104 #if !defined(NDEBUG) | 104 #if !defined(NDEBUG) |
| 105 next_id_ = 0; | 105 next_id_ = 0; |
| 106 #endif | 106 #endif |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Adds |value| with |priority| to the queue. Returns a pointer to the | 109 // Adds |value| with |priority| to the queue. Returns a pointer to the |
| 110 // created element. | 110 // created element. |
| 111 Pointer Insert(const T& value, Priority priority) { | 111 Pointer Insert(const T& value, Priority priority) { |
| 112 DCHECK(CalledOnValidThread()); | 112 DCHECK(CalledOnValidThread()); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 136 DCHECK_EQ(pointer.iterator_->first, pointer.id_); | 136 DCHECK_EQ(pointer.iterator_->first, pointer.id_); |
| 137 #endif | 137 #endif |
| 138 | 138 |
| 139 --size_; | 139 --size_; |
| 140 lists_[pointer.priority_].erase(pointer.iterator_); | 140 lists_[pointer.priority_].erase(pointer.iterator_); |
| 141 } | 141 } |
| 142 | 142 |
| 143 // Returns a pointer to the first value of minimum priority or a null-pointer | 143 // Returns a pointer to the first value of minimum priority or a null-pointer |
| 144 // if empty. | 144 // if empty. |
| 145 Pointer FirstMin() { | 145 Pointer FirstMin() { |
| 146 DCHECK(CalledOnValidThread()); | 146 DCHECK(CalledOnValidThread()); |
|
stevez
2012/04/11 16:13:58
For all these methods - is it worth checking size_
szym
2012/05/17 19:52:01
In the original review it was considered unnecessa
| |
| 147 for (size_t i = 0; i < lists_.size(); ++i) { | 147 for (size_t i = 0; i < lists_.size(); ++i) { |
| 148 if (!lists_[i].empty()) | 148 if (!lists_[i].empty()) |
| 149 return Pointer(i, lists_[i].begin()); | 149 return Pointer(i, lists_[i].begin()); |
| 150 } | 150 } |
| 151 return Pointer(); | 151 return Pointer(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Returns a pointer to the last value of minimum priority or a null-pointer | 154 // Returns a pointer to the last value of minimum priority or a null-pointer |
| 155 // if empty. | 155 // if empty. |
| 156 Pointer LastMin() { | 156 Pointer LastMin() { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 #endif | 213 #endif |
| 214 | 214 |
| 215 ListVector lists_; | 215 ListVector lists_; |
| 216 size_t size_; | 216 size_t size_; |
| 217 }; | 217 }; |
| 218 | 218 |
| 219 } // namespace net | 219 } // namespace net |
| 220 | 220 |
| 221 #endif // NET_BASE_PRIORITY_QUEUE_H_ | 221 #endif // NET_BASE_PRIORITY_QUEUE_H_ |
| 222 | 222 |
| 223 | |
| OLD | NEW |