#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H
-#include "common/Mutex.h"
#include "common/Formatter.h"
#include <map>
-#include <utility>
#include <list>
-#include <algorithm>
/**
* Manages queue for normal and strict priority items
}
void put_tokens(unsigned t) {
tokens += t;
- if (tokens > max_tokens)
+ if (tokens > max_tokens) {
tokens = max_tokens;
+ }
}
void take_tokens(unsigned t) {
- if (tokens > t)
+ if (tokens > t) {
tokens -= t;
- else
+ } else {
tokens = 0;
+ }
}
void enqueue(K cl, unsigned cost, T item) {
q[cl].push_back(std::make_pair(cost, item));
assert(!(q.empty()));
assert(cur != q.end());
cur->second.pop_front();
- if (cur->second.empty())
+ if (cur->second.empty()) {
q.erase(cur++);
- else
+ } else {
++cur;
- if (cur == q.end())
+ }
+ if (cur == q.end()) {
cur = q.begin();
+ }
size--;
}
unsigned length() const {
) {
size -= filter_list_pairs(&(i->second), f, out);
if (i->second.empty()) {
- if (cur == i)
+ if (cur == i) {
++cur;
+ }
q.erase(i++);
} else {
++i;
}
void remove_by_class(K k, std::list<T> *out) {
typename Classes::iterator i = q.find(k);
- if (i == q.end())
+ if (i == q.end()) {
return;
+ }
size -= i->second.size();
- if (i == cur)
+ if (i == cur) {
++cur;
+ }
if (out) {
for (typename ListPairs::reverse_iterator j =
i->second.rbegin();
}
}
q.erase(i);
- if (cur == q.end())
+ if (cur == q.end()) {
cur = q.begin();
+ }
}
void dump(Formatter *f) const {
f->dump_int("max_tokens", max_tokens);
f->dump_int("size", size);
f->dump_int("num_keys", q.size());
- if (!empty())
+ if (!empty()) {
f->dump_int("first_item_cost", front().first);
+ }
}
};
SubQueue *create_queue(unsigned priority) {
typename SubQueues::iterator p = queue.find(priority);
- if (p != queue.end())
+ if (p != queue.end()) {
return &p->second;
+ }
total_priority += priority;
SubQueue *sq = &queue[priority];
sq->set_max_tokens(max_tokens_per_subqueue);
}
void distribute_tokens(unsigned cost) {
- if (total_priority == 0)
+ if (total_priority == 0) {
return;
+ }
for (typename SubQueues::iterator i = queue.begin();
i != queue.end();
++i) {
if (!(high_queue.empty())) {
T ret = high_queue.rbegin()->second.front().second;
high_queue.rbegin()->second.pop_front();
- if (high_queue.rbegin()->second.empty())
+ if (high_queue.rbegin()->second.empty()) {
high_queue.erase(high_queue.rbegin()->first);
+ }
return ret;
}
unsigned cost = i->second.front().first;
i->second.take_tokens(cost);
i->second.pop_front();
- if (i->second.empty())
+ if (i->second.empty()) {
remove_queue(i->first);
+ }
distribute_tokens(cost);
return ret;
}
T ret = queue.rbegin()->second.front().second;
unsigned cost = queue.rbegin()->second.front().first;
queue.rbegin()->second.pop_front();
- if (queue.rbegin()->second.empty())
+ if (queue.rbegin()->second.empty()) {
remove_queue(queue.rbegin()->first);
+ }
distribute_tokens(cost);
return ret;
}