ug4
maxheap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__UG__LIB_ALGEBRA__AMG_SOLVER__MAXHEAP_H__
34 #define __H__UG__LIB_ALGEBRA__AMG_SOLVER__MAXHEAP_H__
35 
36 namespace ug{
37 // maxheap
38 //--------------
48 template<typename T>
49 class maxheap
50 {
51 private:
52  maxheap(const maxheap<T> *other);
53 
54 public:
56  {
57  m_arr = NULL;
58  m_height = 0;
59  m_size = 0;
60  }
61 
62 
67  maxheap(size_t n, const T *arr_)
68  {
69  m_size = 0;
70  m_heap = NULL;
71  m_posinheap = NULL;
72  create(n, arr_);
73  }
74 
75  maxheap(const std::vector<T> &v)
76  {
77  m_size = 0;
78  m_heap = NULL;
79  m_posinheap = NULL;
80  create(v.size(), &v[0]);
81  }
84  {
85  }
86 
87 
91  void create(size_t n, const T *arr_)
92  {
93  if(m_size != n)
94  {
95  m_heap.resize(n);
96  m_posinheap.resize(n);
97  }
98 
99  m_arr = arr_;
100  m_height = 0;
101  for(size_t i=0; i<n; i++) m_posinheap[i] = -1;
102  for(size_t i=0; i<n; i++) m_heap[i] = -1;
103  m_size = n;
104 
105  }
106  void create(const std::vector<T> &v)
107  {
108  create(v.size(), &v[0]);
109  }
112  void reset()
113  {
114  m_height = 0;
115  }
116 
120  void insert_item(int i)
121  {
122  if(!is_in(i))
123  {
124  UG_ASSERT(m_height < m_size, "more elements added than there are in the external array. double adds?");
125  m_posinheap[i] = m_height;
126  m_heap[m_height] = i;
127  m_height++;
128  }
129  upheap(i);
130  }
131 
132  // remove
134  void remove(int i)
135  {
136  if(!is_in(i)) return;
137  int j = m_heap[m_height-1];
138  myswap(i, j);
139  m_heap[m_height-1] = -1;
140  m_height--;
141  m_posinheap[i] = -1;
142 
143  downheap(j);
144  }
145 
146  // remove_max
149  {
150  UG_ASSERT(m_height > 0, "m_heap already empty");
151  int m = m_heap[0];
152  remove(m);
153  return m;
154  }
155 
156  // get_max
158  int get_max() const
159  {
160  return m_heap[0];
161  }
162 
169  void update(int i)
170  {
171  if(!is_in(i)) return;
172  if(m_arr[i] > m_arr[parent(i)])
173  upheap(i);
174  else
175  downheap(i);
176  }
177 
178  bool is_in(size_t i)
179  {
180  return m_posinheap[i] != -1;
181  }
182 
183 
186  void print() const
187  {
188  std::cout << "maxheap, size = " << m_size << ", height = " << m_height << std::endl;
189  for(size_t i=0; i<m_height; i++)
190  {
191  std::cout << i << ": pos: " << m_heap[i] << " parent: " << parent(m_heap[i]) << " element: " << m_arr[m_heap[i]] <<
192  (m_arr[m_heap[i]] > m_arr[parent(m_heap[i])] ? " ERR " : "") << std::endl;
193 
194  }
195  }
196 
198  size_t arr_size() const
199  {
200  return m_size;
201  }
202 
204  size_t height() const
205  {
206  return m_height;
207  }
208 
209 private:
213  void upheap(int i)
214  {
215  if(m_posinheap[i] == -1) return;
216  while(m_arr[i] > m_arr[parent(i)])
217  myswap(i, parent(i));
218  }
219 
222  void downheap(int i)
223  {
224  if(m_posinheap[i] == -1) return;
225  while(1)
226  {
227  const T &l = m_arr[leftchild(i)];
228  const T &r = m_arr[rightchild(i)];
229  const T &t = m_arr[i];
230  if(l > t || r > t)
231  {
232  if(l > r)
233  myswap(leftchild(i), i);
234  else
235  myswap(rightchild(i), i);
236  }
237  else
238  break;
239  }
240  }
241 
242  // parent
247  int parent(int index) const
248  {
249  int p = m_posinheap[index];
250  int parentpos = (p == 0 ? 0 : (p+1)/2 -1);
251  return m_heap[parentpos];
252  }
253 
254 
255  // leftchild
260  int leftchild(int index) const
261  {
262  int p = m_posinheap[index];
263  p = (p+1)*2 -1;
264  if(p < (int)m_height)
265  return m_heap[p];
266  else return index;
267  }
268 
269  // rightchild
274  int rightchild(int index) const
275  {
276  int p = m_posinheap[index];
277  p = (p+1)*2 -1 +1;
278  if(p < (int)m_height)
279  return m_heap[p];
280  else return index;
281  }
282 
283  // myswap
286  void myswap(int i, int j)
287  {
288  int posinheapi = m_posinheap[i];
289  int posinheapj = m_posinheap[j];
290 
291  m_heap[posinheapi] = j;
292  m_heap[posinheapj] = i;
293 
294  m_posinheap[i] = posinheapj;
295  m_posinheap[j] = posinheapi;
296  }
297 
298 private:
299  const T *m_arr; //< pointer to array with elements of type T
300  stdvector<int> m_heap; //< m_heap of the elements m_heap[0] is the index of the largest element of m_arr[i] forall i=0..m_size-1 and m_posinheap[i] != -1
301  stdvector<int> m_posinheap; //< m_posinheap[i] is the position of element m_arr[i] in the m_heap. -1 if removed, otherwise m_posinheap[m_heap[i]] = i
302  size_t m_height; //< m_height of the m_heap, elements m_heap[0]..m_heap[m_height-1] are valid.
303  size_t m_size; //< maximal size of the m_heap = size of array m_arr
304 };
305 
306 } // namespace ug
307 
308 #endif // __H__UG__LIB_ALGEBRA__AMG_SOLVER__MAXHEAP_H__
parameterString p
updateable priority queue class. unlike most PQ implementations, we need a method to inform the PQ of...
Definition: maxheap.h:50
size_t height() const
returns nr of elements in the heap.
Definition: maxheap.h:204
void upheap(int i)
Definition: maxheap.h:213
bool is_in(size_t i)
Definition: maxheap.h:178
void downheap(int i)
Definition: maxheap.h:222
void insert_item(int i)
Definition: maxheap.h:120
int get_max() const
returns the index in m_arr of maximal element of the heap
Definition: maxheap.h:158
void update(int i)
update the heap position of array element i (m_arr[i]) use this method if you have changed the value ...
Definition: maxheap.h:169
maxheap(const std::vector< T > &v)
Definition: maxheap.h:75
void print() const
Definition: maxheap.h:186
int leftchild(int index) const
Definition: maxheap.h:260
void reset()
Definition: maxheap.h:112
size_t m_size
Definition: maxheap.h:303
const T * m_arr
Definition: maxheap.h:299
stdvector< int > m_heap
Definition: maxheap.h:300
maxheap()
Definition: maxheap.h:55
size_t m_height
Definition: maxheap.h:302
void create(size_t n, const T *arr_)
creates the heap on a external array arr_[0]..arr_[n-1]
Definition: maxheap.h:91
maxheap(size_t n, const T *arr_)
Definition: maxheap.h:67
void remove(int i)
removes index i in m_arr
Definition: maxheap.h:134
~maxheap()
deconstructor
Definition: maxheap.h:83
void myswap(int i, int j)
Definition: maxheap.h:286
maxheap(const maxheap< T > *other)
void create(const std::vector< T > &v)
Definition: maxheap.h:106
stdvector< int > m_posinheap
Definition: maxheap.h:301
int rightchild(int index) const
Definition: maxheap.h:274
size_t arr_size() const
returns size of external array
Definition: maxheap.h:198
int parent(int index) const
Definition: maxheap.h:247
int remove_max()
returns the index in m_arr of maximal element of the m_heap and removes it from the m_heap
Definition: maxheap.h:148
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
the ug namespace