yat  0.21pre
PriorityQueue.h
1 #ifndef theplu_yat_utility_priority_queue
2 #define theplu_yat_utility_priority_queue
3 
4 // $Id: PriorityQueue.h 3999 2020-10-08 23:22:32Z peter $
5 //
6 // Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020 Peter Johansson
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with yat. If not, see <http://www.gnu.org/licenses/>.
20 
21 #include "BasicQueue.h"
22 #include "utility.h"
23 #include "yat_assert.h"
24 
25 #include <mutex>
26 #include <set>
27 #include <utility>
28 
29 namespace theplu {
30 namespace yat {
31 namespace utility {
32 
54  template<typename T, class Compare = std::less<T> >
56  : public detail::BasicQueue<PriorityQueue<T, Compare>
57  , T
58  , std::multiset<T, Compare> >
59  {
60  friend class detail::BasicQueue<PriorityQueue<T, Compare>
61  ,T,std::multiset<T,Compare> >;
62  typedef detail::BasicQueue<PriorityQueue<T, Compare>
63  , T
64  , std::multiset<T,Compare> > Base;
65  public:
69  PriorityQueue(void) {}
70 
74  explicit PriorityQueue(const Compare& comp)
75  : Base(std::multiset<T, Compare>(comp)) {}
76 
81  : Base(other) {}
82 
87  {
88  this->assign(lhs);
89  return *this;
90  }
91 
92  private:
93  void pop_impl(T& value, const std::unique_lock<std::mutex>& lock)
94  {
95  // The obvious choice would be to create a temp copy of front,
96  // pop the queue and then return by-value. This is, however,
97  // dangerous because if the copy constructor throws, the queue
98  // has been popped and the element is lost. Instead we choose to
99  // return via passed reference.
100  typename std::multiset<T, Compare>::iterator it = this->q_.end();
101  YAT_ASSERT(this->q_.size());
102  --it;
103  value = std::move_if_noexcept(*it);
104  this->q_.erase(it);
105  }
106 
107  void push_impl(const T& value, std::unique_lock<std::mutex>& lock)
108  {
109  this->q_.insert(value);
110  }
111 
112 
113  void push_impl(T&& value, std::unique_lock<std::mutex>& lock)
114  {
115  this->q_.insert(std::move(value));
116  }
117  };
118 
119 }}}
120 #endif
PriorityQueue(const PriorityQueue &other)
Copy constructor.
Definition: PriorityQueue.h:80
The Department of Theoretical Physics namespace as we define it.
Some useful functions are placed here.
Definition: stl_utility.h:64
Multi-thread safe priority queue.
Definition: PriorityQueue.h:55
Definition: BasicQueue.h:36
PriorityQueue & operator=(const PriorityQueue &lhs)
assignment operator
Definition: PriorityQueue.h:86
PriorityQueue(const Compare &comp)
Create a PriorityQueue with comp as Compare functor.
Definition: PriorityQueue.h:74

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14