Chromium Code Reviews| Index: net/base/priority_queue.h |
| diff --git a/net/base/priority_queue.h b/net/base/priority_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c4f14f09bfaaf4e4126ba34d6d2adca352114c43 |
| --- /dev/null |
| +++ b/net/base/priority_queue.h |
| @@ -0,0 +1,125 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_BASE_PRIORITY_QUEUE_H_ |
| +#define NET_BASE_PRIORITY_QUEUE_H_ |
| +#pragma once |
| + |
| +#include <list> |
| + |
| +#include "base/basictypes.h" |
| +#include "net/base/net_export.h" |
| +#include "net/base/request_priority.h" |
| + |
| +namespace net { |
| + |
| +// A simple priority queue. The order of values is by RequestPriority and then |
|
cbentzel
2012/01/03 19:39:09
It would be nice if this weren't coupled with Requ
szym
2012/01/03 20:48:52
Having NUM_PRIORITIES const allows the compiler to
|
| +// FIFO. The queue supports changing the priority of its elements. |
|
cbentzel
2012/01/03 19:39:09
How do you change the priority of the elements? I
szym
2012/01/03 20:48:52
There used to be Update/Move which essentially Era
|
| +// All operations are O(1) time (assuming constant NUM_PRIORITIES). |
|
cbentzel
2012/01/03 19:39:09
You should document why and where this is needed i
|
| +template<typename T> |
| +class PriorityQueue { |
|
cbentzel
2012/01/03 19:39:09
This should probably derive from NonThreadSafe or
|
| + public: |
| + typedef std::list<T> List; |
| + // Note that we need iterator not const_iterator to pass to List::erase. |
| + // When C++0x comes, this could be changed to const_iterator and const could |
| + // be added to First, Last, and OldestLowest. |
| + typedef typename std::list<T>::iterator ListIterator; |
|
cbentzel
2012/01/03 19:39:09
ListIterator could probably be typedef'd within Po
|
| + |
| + // A pointer to a value stored in the queue. The pointer becomes invalid |
| + // when the queue is destroyed or the value is erased or moved. |
| + class Pointer { |
| + public: |
| + // Constructs a null pointer. |
| + Pointer() : priority_(NUM_PRIORITIES) {} |
| + |
| + bool is_null() const { return priority_ == NUM_PRIORITIES; } |
| + RequestPriority priority() const { return priority_; } |
| + |
| + const T& value() const { return *iterator_; } |
| + |
| + // Comparing to Pointer from a different PriorityQueue is undefined. |
| + bool equals(const Pointer& other) const { |
| + return (priority_ == other.priority_) && (iterator_ == other.iterator_); |
| + } |
| + |
| + protected: |
| + friend class PriorityQueue; |
| + |
| + Pointer(RequestPriority priority, const ListIterator& iterator) |
| + : priority_(priority), iterator_(iterator) {} |
| + |
| + RequestPriority priority_; |
| + ListIterator iterator_; |
| + }; |
| + |
| + PriorityQueue() : size_(0) {} |
| + |
| + // Adds |value| with |priority| to the queue. Returns a pointer to the |
| + // created element. |
| + Pointer Insert(const T& value, RequestPriority priority) { |
| + ++size_; |
| + List& list = lists_[priority]; |
| + return Pointer(priority, list.insert(list.end(), value)); |
| + } |
| + |
| + // Removes the value pointed by |pointer| from the queue. All pointers to this |
| + // value including |pointer| become invalid. |
| + void Erase(const Pointer& pointer) { |
|
cbentzel
2012/01/03 19:39:09
Should this be a member on Pointer?
Also, should
cbentzel
2012/01/03 19:39:09
It feels like this class could benefit with some d
szym
2012/01/03 20:48:52
Since First returns a temporary, to allow queue.Er
|
| + --size_; |
| + lists_[pointer.priority_].erase(pointer.iterator_); |
| + } |
| + |
| + // Returns a pointer to the first value in order or a null-pointer if empty. |
| + Pointer First() { |
| + for (size_t priority = 0; priority < NUM_PRIORITIES; ++priority) { |
| + List& list = lists_[priority]; |
| + if (!list.empty()) |
| + return Pointer(static_cast<RequestPriority>(priority), list.begin()); |
| + } |
| + return Pointer(); |
| + } |
| + |
| + // Returns a pointer to the last value in order or a null-pointer if empty. |
| + Pointer Last() { |
| + for (size_t i = 0; i < NUM_PRIORITIES; ++i) { |
|
cbentzel
2012/01/03 19:39:09
Why not do a i = NUM_PRIORITIES; i > 0; --i?
Need
|
| + size_t priority = NUM_PRIORITIES - i - 1; |
| + List& list = lists_[priority]; |
| + if (!list.empty()) |
| + return Pointer(static_cast<RequestPriority>(priority), --list.end()); |
| + } |
| + return Pointer(); |
| + } |
| + |
| + // Returns a pointer to the oldest, lowest value or a null-pointer if empty. |
| + Pointer OldestLowest() { |
| + for (size_t i = 0; i < NUM_PRIORITIES; ++i) { |
| + size_t priority = NUM_PRIORITIES - i - 1; |
| + List& list = lists_[priority]; |
| + if (!list.empty()) |
| + return Pointer(static_cast<RequestPriority>(priority), list.begin()); |
| + } |
| + return Pointer(); |
| + } |
| + |
| + // Empties the queue. All pointers become invalid. |
| + void Clear() { |
| + for (size_t priority = 0; priority < NUM_PRIORITIES; ++priority) |
| + lists_[priority].clear(); |
| + size_ = 0u; |
| + } |
| + |
| + // Returns number of queued values. |
| + size_t size() const { |
| + return size_; |
| + } |
| + |
| + private: |
| + List lists_[NUM_PRIORITIES]; |
| + size_t size_; |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_BASE_PRIORITY_QUEUE_H_ |
| + |