Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Heap.h
Go to the documentation of this file.
1 
12 #ifndef __KOME_CORE_HEAP_H__
13 #define __KOME_CORE_HEAP_H__
14 
15 
16 #include <vector>
17 #include <boost/function.hpp>
18 
19 
20 namespace kome {
21  namespace core {
22 
27  template < typename T > class Heap {
28  public:
33  Heap() {
34  m_arr = NULL;
35  m_size = 0;
36  m_arrSize = 0;
37  m_less = std::less< T >();
38  }
39 
45  Heap( boost::function< bool( T&, T& ) > lessFun ) {
46  m_arr = NULL;
47  m_size = 0;
48  m_arrSize = 0;
49  m_less = lessFun;
50  }
51 
56  virtual ~Heap() {
57  clear();
58  }
59 
60  protected:
62  T* m_arr;
63 
65  unsigned int m_size;
66 
68  unsigned int m_arrSize;
69 
71  boost::function< bool( T&, T& ) > m_less;
72 
73 
74  public:
79  void clear() {
80  // delete
81  if( m_arr != NULL ) {
82  delete[] m_arr;
83  }
84 
85  // initialize
86  m_arr = NULL;
87  m_size = 0;
88  m_arrSize = 0;
89  }
90 
96  unsigned size() {
97  return m_size;
98  }
99 
106  T at( const unsigned int index ) {
107  return m_arr[ index ];
108  }
109 
115  void add( T elm ) {
116  // check array
117  if( m_size + 1 >= m_arrSize ) {
118  // new array
119  m_arrSize *= 2;
120  if( m_arrSize < 10 ) {
121  m_arrSize = 10;
122  }
123  T* arr = new T[ m_arrSize ];
124 
125  // copy
126  memcpy( arr, m_arr, sizeof( T ) * m_size );
127 
128  // swap
129  if( m_arr != NULL ) {
130  delete[] m_arr;
131  }
132  m_arr = arr;
133  }
134 
135  // add
136  m_arr[ m_size ] = elm;
137  int idx = (int)m_size;
138  m_size++;
139 
140  // swap
141  while( idx > 0 ) {
142  int hlfIdx = ( idx - 1 ) / 2;
143  if( m_less( m_arr[ idx ], m_arr[ hlfIdx ] ) ) {
144  T tmp = m_arr[ idx ];
145  m_arr[ idx ] = m_arr[ hlfIdx ];
146  m_arr[ hlfIdx ] = tmp;
147  idx = hlfIdx;
148  }
149  else {
150  idx = -1;
151  }
152  }
153  }
154 
160  void remove( const unsigned int index ) {
161  // check the index
162  if( index >= m_size ) {
163  return;
164  }
165 
166  // array[ N ] -> array[ index ]
167  m_arr[ index ] = m_arr[ m_size - 1 ];
168  m_size--;
169 
170  // swap
171  int idx = index;
172  if( idx > 0 && m_less( m_arr[ idx ], m_arr[ idx / 2 ] ) ) { // parent
173  while( idx > 0 ) {
174  int hlfIdx = ( idx - 1 ) / 2;
175  if( m_less( m_arr[ idx ], m_arr[ hlfIdx ] ) ) {
176  T tmp = m_arr[ idx ];
177  m_arr[ idx ] = m_arr[ hlfIdx ];
178  m_arr[ hlfIdx ] = tmp;
179  idx = hlfIdx;
180  }
181  else {
182  idx = -1;
183  }
184  }
185  }
186  else { // children
187  while( idx < (int)m_size ) {
188  // index
189  int lIdx = idx * 2 + 1;
190  int rIdx = idx * 2 + 2;
191 
192  // check left
193  bool lswap = false;
194  if( lIdx < (int)m_size ) {
195  if( m_less( m_arr[ lIdx ], m_arr[ idx ] ) ) {
196  lswap = true;
197  }
198  }
199 
200  // check right
201  bool rswap = false;
202  if( rIdx < (int)m_size ) {
203  if( m_less( m_arr[ rIdx ], m_arr[ idx ] ) ) {
204  rswap = true;
205  }
206  }
207 
208  // compare
209  if( lswap && rswap ) {
210  if( m_less( m_arr[ rIdx ], m_arr[ lIdx ] ) ) {
211  lswap = false;
212  }
213  else {
214  rswap = false;
215  }
216  }
217 
218  // swap
219  int swapIdx = (int)m_size;
220  if( lswap ) {
221  swapIdx = lIdx;
222  }
223  else if( rswap ) {
224  swapIdx = rIdx;
225  }
226 
227  if( swapIdx < (int)m_size ) {
228  T tmp = m_arr[ idx ];
229  m_arr[ idx ] = m_arr[ swapIdx ];
230  m_arr[ swapIdx ] = tmp;
231  }
232  idx = swapIdx;
233  }
234  }
235  }
236  };
237  }
238 }
239 
240 #endif // __KOME_CORE_HEAP_H__
T at(const unsigned int index)
gets heap element
Definition: Heap.h:106
virtual ~Heap()
destructor
Definition: Heap.h:56
void clear()
clears heap
Definition: Heap.h:79
unsigned int m_size
Definition: Heap.h:65
Heap()
constructor
Definition: Heap.h:33
void add(T elm)
adds element
Definition: Heap.h:115
#define NULL
Definition: CoreMacros.h:18
Heap(boost::function< bool(T &, T &) > lessFun)
constructor
Definition: Heap.h:45
boost::function< bool(T &, T &) > m_less
Definition: Heap.h:71
heap data management class
Definition: Heap.h:27
unsigned size()
gets heap size
Definition: Heap.h:96
unsigned int m_arrSize
Definition: Heap.h:68