From aff4499ae2aedcbff71146602d0009afd044af10 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 1 Jan 2015 16:50:09 +0800 Subject: [PATCH] common/PrioritizedQueue: do not 'using namespace std' * do not assume `using namespace std` before including PrioritizedQueue.h Signed-off-by: Kefu Chai --- src/common/PrioritizedQueue.h | 62 +++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/common/PrioritizedQueue.h b/src/common/PrioritizedQueue.h index ee1dc9cd0e17..5ae94a5fbc28 100644 --- a/src/common/PrioritizedQueue.h +++ b/src/common/PrioritizedQueue.h @@ -49,13 +49,14 @@ class PrioritizedQueue { int64_t max_tokens_per_subqueue; int64_t min_cost; + typedef std::list > ListPairs; template static unsigned filter_list_pairs( - list > *l, F f, - list *out) { + ListPairs *l, F f, + std::list *out) { unsigned ret = 0; if (out) { - for (typename list >::reverse_iterator i = l->rbegin(); + for (typename ListPairs::reverse_iterator i = l->rbegin(); i != l->rend(); ++i) { if (f(i->second)) { @@ -63,7 +64,7 @@ class PrioritizedQueue { } } } - for (typename list >::iterator i = l->begin(); + for (typename ListPairs::iterator i = l->begin(); i != l->end(); ) { if (f(i->second)) { @@ -78,10 +79,11 @@ class PrioritizedQueue { struct SubQueue { private: - map > > q; + typedef std::map Classes; + Classes q; unsigned tokens, max_tokens; int64_t size; - typename map > >::iterator cur; + typename Classes::iterator cur; public: SubQueue(const SubQueue &other) : q(other.q), @@ -114,18 +116,18 @@ class PrioritizedQueue { tokens = 0; } void enqueue(K cl, unsigned cost, T item) { - q[cl].push_back(make_pair(cost, item)); + q[cl].push_back(std::make_pair(cost, item)); if (cur == q.end()) cur = q.begin(); size++; } void enqueue_front(K cl, unsigned cost, T item) { - q[cl].push_front(make_pair(cost, item)); + q[cl].push_front(std::make_pair(cost, item)); if (cur == q.end()) cur = q.begin(); size++; } - pair front() const { + std::pair front() const { assert(!(q.empty())); assert(cur != q.end()); return cur->second.front(); @@ -150,8 +152,8 @@ class PrioritizedQueue { return q.empty(); } template - void remove_by_filter(F f, list *out) { - for (typename map > >::iterator i = q.begin(); + void remove_by_filter(F f, std::list *out) { + for (typename Classes::iterator i = q.begin(); i != q.end(); ) { size -= filter_list_pairs(&(i->second), f, out); @@ -166,15 +168,15 @@ class PrioritizedQueue { if (cur == q.end()) cur = q.begin(); } - void remove_by_class(K k, list *out) { - typename map > >::iterator i = q.find(k); + void remove_by_class(K k, std::list *out) { + typename Classes::iterator i = q.find(k); if (i == q.end()) return; size -= i->second.size(); if (i == cur) ++cur; if (out) { - for (typename list >::reverse_iterator j = + for (typename ListPairs::reverse_iterator j = i->second.rbegin(); j != i->second.rend(); ++j) { @@ -195,11 +197,13 @@ class PrioritizedQueue { f->dump_int("first_item_cost", front().first); } }; - map high_queue; - map queue; + + typedef std::map SubQueues; + SubQueues high_queue; + SubQueues queue; SubQueue *create_queue(unsigned priority) { - typename map::iterator p = queue.find(priority); + typename SubQueues::iterator p = queue.find(priority); if (p != queue.end()) return &p->second; total_priority += priority; @@ -218,7 +222,7 @@ class PrioritizedQueue { void distribute_tokens(unsigned cost) { if (total_priority == 0) return; - for (typename map::iterator i = queue.begin(); + for (typename SubQueues::iterator i = queue.begin(); i != queue.end(); ++i) { i->second.put_tokens(((i->first * cost) / total_priority) + 1); @@ -234,13 +238,13 @@ public: unsigned length() const { unsigned total = 0; - for (typename map::const_iterator i = queue.begin(); + for (typename SubQueues::const_iterator i = queue.begin(); i != queue.end(); ++i) { assert(i->second.length()); total += i->second.length(); } - for (typename map::const_iterator i = high_queue.begin(); + for (typename SubQueues::const_iterator i = high_queue.begin(); i != high_queue.end(); ++i) { assert(i->second.length()); @@ -250,8 +254,8 @@ public: } template - void remove_by_filter(F f, list *removed = 0) { - for (typename map::iterator i = queue.begin(); + void remove_by_filter(F f, std::list *removed = 0) { + for (typename SubQueues::iterator i = queue.begin(); i != queue.end(); ) { unsigned priority = i->first; @@ -264,7 +268,7 @@ public: ++i; } } - for (typename map::iterator i = high_queue.begin(); + for (typename SubQueues::iterator i = high_queue.begin(); i != high_queue.end(); ) { i->second.remove_by_filter(f, removed); @@ -276,8 +280,8 @@ public: } } - void remove_by_class(K k, list *out = 0) { - for (typename map::iterator i = queue.begin(); + void remove_by_class(K k, std::list *out = 0) { + for (typename SubQueues::iterator i = queue.begin(); i != queue.end(); ) { i->second.remove_by_class(k, out); @@ -289,7 +293,7 @@ public: ++i; } } - for (typename map::iterator i = high_queue.begin(); + for (typename SubQueues::iterator i = high_queue.begin(); i != high_queue.end(); ) { i->second.remove_by_class(k, out); @@ -345,7 +349,7 @@ public: // if there are multiple buckets/subqueues with sufficient tokens, // we behave like a strict priority queue among all subqueues that // are eligible to run. - for (typename map::iterator i = queue.begin(); + for (typename SubQueues::iterator i = queue.begin(); i != queue.end(); ++i) { assert(!(i->second.empty())); @@ -377,7 +381,7 @@ public: f->dump_int("max_tokens_per_subqueue", max_tokens_per_subqueue); f->dump_int("min_cost", min_cost); f->open_array_section("high_queues"); - for (typename map::const_iterator p = high_queue.begin(); + for (typename SubQueues::const_iterator p = high_queue.begin(); p != high_queue.end(); ++p) { f->open_object_section("subqueue"); @@ -387,7 +391,7 @@ public: } f->close_section(); f->open_array_section("queues"); - for (typename map::const_iterator p = queue.begin(); + for (typename SubQueues::const_iterator p = queue.begin(); p != queue.end(); ++p) { f->open_object_section("subqueue"); -- 2.47.3