Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: net/base/priority_queue.h

Issue 8965025: Refactoring of job dispatch in HostResolverImpl in preparation for DnsClient. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed PriorityQueue. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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_
7 #pragma once
8
9 #include <list>
10
11 #include "base/basictypes.h"
12 #include "net/base/net_export.h"
13 #include "net/base/request_priority.h"
14
15 namespace net {
16
17 // A simple priority queue. The order of values is by RequestPriority and then
18 // FIFO. The queue supports changing the priority of its elements.
19 // All operations are O(1) time (assuming constant NUM_PRIORITIES).
20 template<typename T>
21 class PriorityQueue {
22 public:
23 typedef std::list<T> List;
24 typedef typename std::list<T>::iterator ListIterator;
25
26 // A pointer to a value stored in the queue. The pointer becomes invalid
27 // when the queue is destroyed, the value is erased, or moved.
28 class Pointer {
29 public:
30 // Constructs a null pointer.
31 Pointer() : priority_(NUM_PRIORITIES) {}
32
33 bool is_null() const { return priority_ == NUM_PRIORITIES; }
34 RequestPriority priority() const { return priority_; }
35
36 T& value() { return *iterator_; }
37 const T& value() const { return *iterator_; }
38
39 // Comparing to Pointer from a different PriorityQueue is undefined.
40 bool equals(const Pointer& other) const {
41 return (priority_ == other.priority_) && (iterator_ == other.iterator_);
42 }
43
44 protected:
45 friend class PriorityQueue;
46 Pointer(RequestPriority priority, const ListIterator& iterator)
47 : priority_(priority), iterator_(iterator) {}
48
49 RequestPriority priority_;
50 ListIterator iterator_;
51 };
52
53 PriorityQueue() : size_(0) {}
54
55 // Adds |value| with |priority| to the queue. Returns a pointer to the
56 // created element.
57 Pointer Insert(const T& value, RequestPriority priority) {
58 ++size_;
59 List& list = lists_[priority];
60 return Pointer(priority, list.insert(list.end(), value));
61 }
62
63 // Removes the value pointed by |pointer| from the queue. All pointers to this
64 // value including |pointer| become invalid.
65 void Erase(const Pointer& pointer) {
66 --size_;
67 lists_[pointer.priority_].erase(pointer.iterator_);
68 }
69
70 // Moves the value pointed by |pointer| to the end of all values with priority
71 // |priority| and returns a pointer to the moved value. All pointers to this
72 // value become invalid. No-op if priority is not changed.
73 Pointer Move(const Pointer& pointer, RequestPriority priority) {
74 if (pointer.priority_ == priority)
75 return pointer;
76 List& list = lists_[priority];
77 list.splice(list.end(), lists_[pointer.priority_], pointer.iterator_);
78 return Pointer(priority, --list.end());
79 }
80
81 // Returns a pointer to the first value in order or a null-pointer if empty.
82 Pointer First() {
83 for (size_t priority = 0; priority < NUM_PRIORITIES; ++priority) {
84 List& list = lists_[priority];
85 if (!list.empty())
86 return Pointer(static_cast<RequestPriority>(priority), list.begin());
87 }
88 return Pointer();
89 }
90
91 // Returns a pointer to the last value in order or a null-pointer if empty.
92 Pointer Last() {
93 for (size_t i = 0; i < NUM_PRIORITIES; ++i) {
94 size_t priority = NUM_PRIORITIES - i - 1;
95 List& list = lists_[priority];
96 if (!list.empty())
97 return Pointer(static_cast<RequestPriority>(priority), --list.end());
98 }
99 return Pointer();
100 }
101
102 // Returns a pointer to the oldest, lowest value or a null-pointer if empty.
103 Pointer OldestLowest() {
104 for (size_t i = 0; i < NUM_PRIORITIES; ++i) {
105 size_t priority = NUM_PRIORITIES - i - 1;
106 List& list = lists_[priority];
107 if (!list.empty())
108 return Pointer(static_cast<RequestPriority>(priority), list.begin());
109 }
110 return Pointer();
111 }
112
113 // Empties the queue. All pointers become invalid.
114 void Clear() {
115 for (size_t priority = 0; priority < NUM_PRIORITIES; ++priority)
116 lists_[priority].clear();
117 size_ = 0u;
118 }
119
120 // Returns number of queued values.
121 size_t size() const {
122 return size_;
123 }
124
125 private:
126 List lists_[NUM_PRIORITIES];
127 size_t size_;
128 };
129
130 } // namespace net
131
132 #endif // NET_BASE_PRIORITY_QUEUE_H_
133
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698