-
Notifications
You must be signed in to change notification settings - Fork 790
/
heap.h
467 lines (396 loc) · 9.97 KB
/
heap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/***********************************************************************
* Software License Agreement (BSD License)
*
* Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
* Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
*
* THE BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*************************************************************************/
#ifndef RTABMAP_FLANN_HEAP_H_
#define RTABMAP_FLANN_HEAP_H_
#include <algorithm>
#include <vector>
#include <rtabmap/utilite/ULogger.h>
namespace rtflann
{
/**
* Priority Queue Implementation
*
* The priority queue is implemented with a heap. A heap is a complete
* (full) binary tree in which each parent is less than both of its
* children, but the order of the children is unspecified.
*/
template <typename T>
class Heap
{
/**
* Storage array for the heap.
* Type T must be comparable.
*/
std::vector<T> heap;
int length;
/**
* Number of element in the heap
*/
int count;
public:
/**
* Constructor.
*
* Params:
* size = heap size
*/
Heap(int size)
{
length = size;
heap.reserve(length);
count = 0;
}
/**
*
* Returns: heap size
*/
int size()
{
return count;
}
/**
*
* Returns: heap size
*/
int capacity()
{
return length;
}
/**
* Tests if the heap is empty
*
* Returns: true is heap empty, false otherwise
*/
bool empty()
{
return size()==0;
}
/**
* Clears the heap.
*/
void clear()
{
heap.clear();
count = 0;
}
struct CompareT : public std::binary_function<T,T,bool>
{
bool operator()(const T& t_1, const T& t_2) const
{
return t_2 < t_1;
}
};
/**
* Insert a new element in the heap.
*
* We select the next empty leaf node, and then keep moving any larger
* parents down until the right location is found to store this element.
*
* Params:
* value = the new element to be inserted in the heap
*/
void insert(const T& value)
{
/* If heap is full, then return without adding this element. */
if (count == length) {
return;
}
UASSERT(heap.size() < heap.capacity());
heap.push_back(value);
static CompareT compareT;
std::push_heap(heap.begin(), heap.end(), compareT);
++count;
}
/**
* Returns the node of minimum value from the heap (top of the heap).
*
* Params:
* value = out parameter used to return the min element
* Returns: false if heap empty
*/
bool popMin(T& value)
{
if (count == 0) {
return false;
}
value = heap[0];
static CompareT compareT;
std::pop_heap(heap.begin(), heap.end(), compareT);
heap.pop_back();
--count;
return true; /* Return old last node. */
}
};
template <typename T>
class IntervalHeap
{
struct Interval
{
T left;
T right;
};
/**
* Storage array for the heap.
* Type T must be comparable.
*/
std::vector<Interval> heap;
size_t capacity_;
size_t size_;
public:
/**
* Constructor.
*
* Params:
* size = heap size
*/
IntervalHeap(int capacity) : capacity_(capacity), size_(0)
{
heap.resize(capacity/2 + capacity%2 + 1); // 1-based indexing
}
/**
* @return Heap size
*/
size_t size()
{
return size_;
}
/**
* Tests if the heap is empty
* @return true is heap empty, false otherwise
*/
bool empty()
{
return size_==0;
}
/**
* Clears the heap.
*/
void clear()
{
size_ = 0;
}
void insert(const T& value)
{
/* If heap is full, then return without adding this element. */
if (size_ == capacity_) {
return;
}
// insert into the root
if (size_<2) {
if (size_==0) {
heap[1].left = value;
heap[1].right = value;
}
else {
if (value<heap[1].left) {
heap[1].left = value;
}
else {
heap[1].right = value;
}
}
++size_;
return;
}
size_t last_pos = size_/2 + size_%2;
bool min_heap;
if (size_%2) { // odd number of elements
min_heap = (value<heap[last_pos].left)? true : false;
}
else {
++last_pos;
min_heap = (value<heap[last_pos/2].left)? true : false;
}
if (min_heap) {
size_t pos = last_pos;
size_t par = pos/2;
while (pos>1 && value < heap[par].left) {
heap[pos].left = heap[par].left;
pos = par;
par = pos/2;
}
heap[pos].left = value;
++size_;
if (size_%2) { // duplicate element in last position if size is odd
heap[last_pos].right = heap[last_pos].left;
}
}
else {
size_t pos = last_pos;
size_t par = pos/2;
while (pos>1 && heap[par].right < value) {
heap[pos].right = heap[par].right;
pos = par;
par = pos/2;
}
heap[pos].right = value;
++size_;
if (size_%2) { // duplicate element in last position if size is odd
heap[last_pos].left = heap[last_pos].right;
}
}
}
/**
* Returns the node of minimum value from the heap
* @param value out parameter used to return the min element
* @return false if heap empty
*/
bool popMin(T& value)
{
if (size_ == 0) {
return false;
}
value = heap[1].left;
size_t last_pos = size_/2 + size_%2;
T elem = heap[last_pos].left;
if (size_ % 2) { // odd number of elements
--last_pos;
}
else {
heap[last_pos].left = heap[last_pos].right;
}
--size_;
if (size_<2) return true;
size_t crt=1; // root node
size_t child = crt*2;
while (child <= last_pos) {
if (child < last_pos && heap[child+1].left < heap[child].left) ++child; // pick the child with min
if (!(heap[child].left<elem)) break;
heap[crt].left = heap[child].left;
if (heap[child].right<elem) {
std::swap(elem, heap[child].right);
}
crt = child;
child *= 2;
}
heap[crt].left = elem;
return true;
}
/**
* Returns the element of maximum value from the heap
* @param value
* @return false if heap empty
*/
bool popMax(T& value)
{
if (size_ == 0) {
return false;
}
value = heap[1].right;
size_t last_pos = size_/2 + size_%2;
T elem = heap[last_pos].right;
if (size_%2) { // odd number of elements
--last_pos;
}
else {
heap[last_pos].right = heap[last_pos].left;
}
--size_;
if (size_<2) return true;
size_t crt=1; // root node
size_t child = crt*2;
while (child <= last_pos) {
if (child < last_pos && heap[child].right < heap[child+1].right) ++child; // pick the child with max
if (!(elem < heap[child].right)) break;
heap[crt].right = heap[child].right;
if (elem<heap[child].left) {
std::swap(elem, heap[child].left);
}
crt = child;
child *= 2;
}
heap[crt].right = elem;
return true;
}
bool getMin(T& value)
{
if (size_==0) {
return false;
}
value = heap[1].left;
return true;
}
bool getMax(T& value)
{
if (size_==0) {
return false;
}
value = heap[1].right;
return true;
}
};
template <typename T>
class BoundedHeap
{
IntervalHeap<T> interval_heap_;
size_t capacity_;
public:
BoundedHeap(size_t capacity) : interval_heap_(capacity), capacity_(capacity)
{
}
/**
* Returns: heap size
*/
int size()
{
return interval_heap_.size();
}
/**
* Tests if the heap is empty
* Returns: true is heap empty, false otherwise
*/
bool empty()
{
return interval_heap_.empty();
}
/**
* Clears the heap.
*/
void clear()
{
interval_heap_.clear();
}
void insert(const T& value)
{
if (interval_heap_.size()==capacity_) {
T max;
interval_heap_.getMax(max);
if (max<value) return;
interval_heap_.popMax(max);
}
interval_heap_.insert(value);
}
bool popMin(T& value)
{
return interval_heap_.popMin(value);
}
};
}
#endif //FLANN_HEAP_H_