OpenGM  2.3.x
Discrete Graphical Model Library
queues.hxx
Go to the documentation of this file.
1 
2 #ifndef OPENGM_PRIORITY_QUEUE_HXX
3 #define OPENGM_PRIORITY_QUEUE_HXX
4 
5 #include <queue>
6 #include <vector>
7 
8 namespace opengm{
9 
10 template<class T>
11 class FiFoQueue {
12 private:
13  size_t begin_;
14  size_t end_;
15  size_t maxSize_;
16  std::vector<T> data_;
17 public:
18  typedef T ValueType;
19  FiFoQueue(const size_t maxSize) : begin_(0), end_(0), maxSize_(maxSize)
20  {
21  data_.resize(maxSize);
22  }
23 
24  T front(){
25  return data_[begin_];
26  }
27 
28  void pop(){
29  ++begin_;
30  }
31 
32  void push(T t){
33  data_[end_]=t;
34  ++end_;
35  //if(end_==maxSize_){
36  // end_=0;
37  //}
38  // assert(end_ != begin_); // larger tan maxSize
39  }
40 
41  bool empty(){
42  return begin_==end_;
43  }
44 
45  size_t size(){
46  if(begin_<=end_)
47  return end_-begin_;
48  else
49  return end_+maxSize_-1-begin_;
50  }
51 
52  void clear(){
53  begin_ = 0;
54  end_ = 0;
55  }
56 
57 };
58 
59 
60 
65 template<class T,class COMPARE = std::less<T> >
67 
68 
69 public:
70 
71  typedef T priority_type;
72  typedef int ValueType;
73  typedef ValueType value_type;
74  typedef ValueType const_reference;
75 
76 
77 
79  ChangeablePriorityQueue(const size_t maxSize)
80  : maxSize_(maxSize),
81  currentSize_(0),
82  heap_(maxSize_+1),
83  indices_(maxSize_+1),
84  priorities_(maxSize_+1)
85  {
86  for(int i = 0; i <= maxSize_; i++)
87  indices_[i] = -1;
88  }
89 
91  void setPriorities(T newPriority){
92  for(typename std::vector<T>::iterator it = priorities_.begin(); it !=priorities_.end(); ++it)
93  *it = newPriority;
94  return;
95  }
96 
98  void reset(){
99  for(size_t i=0; i<=currentSize_; ++i)
100  indices_[heap_[i]] = -1;
101  currentSize_ = 0;
102  }
103 
105  bool empty() const {
106  return currentSize_ == 0;
107  }
108 
110  bool contains(const int i) const{
111  return indices_[i] != -1;
112  }
113 
115  int size()const{
116  return currentSize_;
117  }
118 
119 
126  void push(const value_type i, const priority_type p) {
127  if(!contains(i)){
128  currentSize_++;
129  indices_[i] = currentSize_;
130  heap_[currentSize_] = i;
131  priorities_[i] = p;
132  bubbleUp(currentSize_);
133  }
134  else{
135  changePriority(i,p);
136  }
137  }
138 
141  const_reference top() const {
142  return heap_[1];
143  }
144 
147  priority_type topPriority() const {
148  return priorities_[heap_[1]];
149  }
150 
153  void pop() {
154  const int min = heap_[1];
155  swapItems(1, currentSize_--);
156  bubbleDown(1);
157  indices_[min] = -1;
158  heap_[currentSize_+1] = -1;
159  }
160 
162  priority_type priority(const value_type i) const{
163  return priorities_[i];
164  }
165 
167  void deleteItem(const value_type i) {
168  int ind = indices_[i];
169  swapItems(ind, currentSize_--);
170  bubbleUp(ind);
171  bubbleDown(ind);
172  indices_[i] = -1;
173  }
178  void changePriority(const value_type i,const priority_type p) {
179  if(_gt(p,priorities_[i])){
180  priorities_[i] = p;
181  bubbleDown(indices_[i]);
182  }
183  else if(_lt(p,priorities_[i])) {
184  priorities_[i] = p;
185  bubbleUp(indices_[i]);
186  }
187  }
188 private:
189 
190 
191  void swapItems(const int i,const int j) {
192  std::swap(heap_[i],heap_[j]);
193  indices_[heap_[i]] = i;
194  indices_[heap_[j]] = j;
195  }
196 
197  void bubbleUp(int k) {
198  while(k > 1 && _gt( priorities_[heap_[k/2]],priorities_[heap_[k]])) {
199  swapItems(k, k/2);
200  k = k/2;
201  }
202  }
203 
204  void bubbleDown(int k) {
205  int j;
206  while(2*k <= currentSize_) {
207  j = 2*k;
208  if(j < currentSize_ && _gt(priorities_[heap_[j]] , priorities_[heap_[j+1]]) )
209  j++;
210  if( _leqt(priorities_[heap_[k]] , priorities_[heap_[j]]))
211  break;
212  swapItems(k, j);
213  k = j;
214  }
215  }
216 
217 
218  bool _lt(const T & a,const T & b)const{
219  return comp_(a,b);
220  }
221  bool _leqt(const T & a,const T & b)const{
222  return !comp_(b,a);
223  }
224  bool _eq(const T & a,const T & b)const{
225  return !comp_(a,b) && !comp_(b,a);
226  }
227  bool _gt(const T & a,const T & b)const{
228  return !_eq(a,b) && !comp_(a,b);
229  }
230  bool _geqt(const T & a,const T & b)const{
231  return !comp_(a,b);
232  }
233 
234 
235  size_t maxSize_;
236  size_t currentSize_;
237  std::vector<int> heap_;
238  std::vector<int> indices_;
239  std::vector<T> priorities_;
240  COMPARE comp_;
241 
242 };
243 
244 }
245 
246 #endif
void reset()
reset heap - priorities are not changed
Definition: queues.hxx:98
The OpenGM namespace.
Definition: config.hxx:43
void pop()
Remove the current top element.
Definition: queues.hxx:153
priority_type priority(const value_type i) const
returns the value associated with index i
Definition: queues.hxx:162
size_t size()
Definition: queues.hxx:45
void push(T t)
Definition: queues.hxx:32
bool contains(const int i) const
check if i is an index on the PQ
Definition: queues.hxx:110
Heap-based changable priority queue with a maximum number of elemements.
Definition: queues.hxx:66
void changePriority(const value_type i, const priority_type p)
change priority of a given index. The index must be in the queue! Call push to auto insert / change ...
Definition: queues.hxx:178
bool empty() const
check if the PQ is empty
Definition: queues.hxx:105
void push(const value_type i, const priority_type p)
Insert a index with a given priority.
Definition: queues.hxx:126
priority_type topPriority() const
get top priority
Definition: queues.hxx:147
int size() const
return the number of elements in the PQ
Definition: queues.hxx:115
ChangeablePriorityQueue(const size_t maxSize)
Create an empty ChangeablePriorityQueue which can contain atmost maxSize elements.
Definition: queues.hxx:79
void deleteItem(const value_type i)
deleqte the priority associated with index i
Definition: queues.hxx:167
void setPriorities(T newPriority)
set all priorities to the given value
Definition: queues.hxx:91
FiFoQueue(const size_t maxSize)
Definition: queues.hxx:19
const_reference top() const
get index with top priority
Definition: queues.hxx:141