forked from OpenSLAM-org/openslam_evg-thin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.hh
130 lines (94 loc) · 3.29 KB
/
heap.hh
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
/*********************************************************************
EVG-THIN v1.1: A Thinning Approximation to the Extended Voronoi Graph
Copyright (C) 2006 - Patrick Beeson ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
*********************************************************************/
/**
heap.hh was originally written by Jefferson Provost.
Defines a minheap template class for priority queues, etc.
Insertions and deletions are O(log(N)).
heap<T,Compare> on any comparible class T. Compare should be a
binary comparison functor implementing the comparison function for
ordering the heap. It defaults to greater<T> (which implements a
minheap. (yes, greater<T> => minheap. -jp)
Example usage:
heap<int> minh; -- creates a minheap of integers
heap<float, less<float> > -- creates a maxheap of floats
heap<foo> -- creates a minheap of elements of type foo, foo must
have > and = operators defined.
See the class definition below for the public operations on heaps.
**/
#ifndef _HEAP_HH_
#define _HEAP_HH_
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template <typename T, typename Compare = greater<T> >
class heap
{
public:
const T& first(void) const; // returns the first (min) item in the heap
const T& nth(int n) const; // returns the nth item in the heap
void push(const T& item); // adds an item to the heap
void pop(void); // removes the first item from the heap
int size(void) const; // returns the number of items in the heap
bool empty(void) const; // returns true if the heap is empty
void clear(void); // removes all elements
protected:
Compare _comp;
vector<T> _store;
};
template <class T,class Compare>
bool heap<T,Compare>::empty(void) const
{
return this->size() == 0;
}
template <class T,class Compare>
int heap<T,Compare>::size(void) const
{
return _store.size();
}
template <class T,class Compare>
void heap<T,Compare>::push(const T& item)
{
_store.push_back(item);
push_heap(_store.begin(), _store.end(), _comp);
}
template <class T,class Compare>
void heap<T,Compare>::pop(void)
{
if (!empty()) {
pop_heap(_store.begin(), _store.end(), _comp);
_store.pop_back();
}
}
template <class T,class Compare>
const T& heap<T,Compare>::first(void) const
{
return _store[0];
}
template <class T,class Compare>
const T& heap<T,Compare>::nth(int n) const
{
if (n >= size())
n=size()-1;
return _store[n];
}
template<class T, class Compare>
void heap<T,Compare>::clear(void)
{
_store.clear();
}
#endif // _HEAP_HH_