Debian Codegraph @

src/thirdparty/VLFeat/heap-def.h

raw
1 /** @file heap-def.h
2 ** @brief Heap preprocessor metaprogram
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 All rights reserved.
9
10 This file is part of the VLFeat library and is made available under
11 the terms of the BSD license (see the COPYING file).
12 */
13
14 /** @file heap-def.h
15
16 A heap organizes an array of objects in a priority queue. This module
17 is a template metaprogram that defines heap operations on array of
18 generic objects, or even generic object containers.
19
20 - @ref heap-def-overview "Overview"
21 - @ref heap-def-overview-general "General usage"
22 - @ref heap-def-tech "Technical details"
23
24 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
25 @section heap-def-overview Overview
26 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
27
28 To use @ref heap-def.h one must specify at least a prefix and the data
29 type for the heap elements:
30
31 @code
32 #define VL_HEAP_prefix my_heap
33 #define VL_HEAP_type float
34 #include <vl/heap-def.h>
35 @endcode
36
37 This code fragment defines a number of functions prefixed by
38 ::VL_HEAP_prefix, such as @c my_heap_push (::VL_HEAP_push) and @c
39 my_heap_pop (::VL_HEAP_pop), that implement the heap operations.
40 These functions operate on an array that has type ::VL_HEAP_array.
41 By default, this is defined to be:
42
43 @code
44 #define VL_HEAP_array VL_HEAP_type*
45 #define VL_HEAP_array_const VL_HEAP_type const*
46 @endcode
47
48 The array itself is accessed uniquely by means of two functions:
49
50 - ::VL_HEAP_cmp, that compares two array elements. The default
51 implementation assumes that ::VL_HEAP_type is numeric.
52 - ::VL_HEAP_swap, that swaps two array elements. The default
53 implementation assumes that ::VL_HEAP_type can be copied by the @c
54 = operator.
55
56 The heap state is a integer @c numElements (of type ::vl_size) counting
57 the number of elements of the array that are currently part of the heap
58 and the content of the first @c numElements elements of the array. The
59 portion of the array that constitutes the heap satisfies a certain
60 invariant property (heap property, @ref heap-def-tech). From a user
61 viewpoint, the most important consequence is that the first element
62 of the array (the one of index 0) is also the smallest (according to
63 ::VL_HEAP_cmp).
64
65 Elements are added to the heap by ::VL_HEAP_push and removed from the
66 heap by ::VL_HEAP_pop. A push operation adds to the heap the array
67 element immediately after the last element already in the heap
68 (i.e. the element of index @c numElements) and increases the number of
69 heap elements @c numElements. Elements in the heap are swapped as required in
70 order to maintain the heap consistency. Similarly, a pop operation
71 removes the first (smaller) element from the heap and decreases the
72 number of heap elements @c numElements.
73
74 The values of nodes currently in the heap can be updated by
75 ::VL_HEAP_update. Notice however that using this function requires
76 knowing the index of the element that needs to be updated up to the
77 swapping operations that the heap performs to maintain
78 consistency. Typically, this requires redefining ::VL_HEAP_swap to
79 keep track of such changes (@ref heap-def-overview-general).
80
81 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
82 @subsection heap-def-overview-general General usage
83 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
84
85 The heap container may be mapped to any type by reimplementing
86 ::VL_HEAP_cmp and ::VL_HEAP_swap explicitly. For instance
87 the following code redefines ::VL_HEAP_cmp to deal with the case
88 in which the heap is an array of structures:
89
90 @code
91 typedef struct _S { int x ; } S ;
92 int s_cmp (S const * v, vl_uindex a, vl_uindex b) {
93 return v[a].x - v[b].x ;
94 }
95 #define VL_HEAP_prefix s_heap
96 #define VL_HEAP_type S
97 #define VL_HEAP_cmp s_cmp
98 #include <vl/heap-def.h>
99 @endcode
100
101 In the following example, the heap itself is an arbitrary structure:
102
103 @code
104 typedef struct _H { int* array ; } H ;
105 int h_cmp (H const * h, vl_uindex a, vl_uindex b) {
106 return h->array[a] - h->array[b] ;
107 }
108 int h_swap (H * h, vl_uindex a, vl_uindex b) {
109 int t = h->array[a] ;
110 h->array[a] = h->array[b] ;
111 h->array[b] = t ;
112 }
113 #define VL_HEAP_prefix h_heap
114 #define VL_HEAP_swap h_swap
115 #define VL_HEAP_cmp h_cmp
116 #include <vl/heap-def.h>
117 @endcode
118
119 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
120 @section heap-def-tech Technical details
121 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
122
123 The heap is organised as a binary tree with the property (<em>heap
124 property</em>) that any node is not larger than any of its
125 children. In particular, the root is the smallest node.
126
127 @ref heap-def.h uses the standard binary tree representation as a linear
128 array. Tree nodes are mapped to array elements as follows:
129 <code>array[0]</code> corresponds to the root, <code>array[1]</code>
130 and <code>array[2]</code> to the root left and right children and so
131 on. In this way, the tree structure is fully specified by the total
132 number of nodes <code>N</code>.
133
134 Assuming that the heap has <code>N</code> nodes (from
135 <code>array[0]</code> to <code>array[N-1]</code>), adding the node
136 <code>array[N]</code> to the heap is done by a <em>push down</em>
137 operation: if the node <code>array[N]</code> is smaller than its
138 parent (violating the heap property) it is pushed down by swapping it
139 with the parent, and so on recursively.
140
141 Removing the smallest element <code>array[0]</code> with an heap of
142 <code>N</code> nodes is done by swapping <code>array[0]</code> with
143 <code>array[N-1]</code>. If then <code>array[0]</code> is larger than
144 any of its children, it is swapped with the smallest of the two and
145 so on recursively (<em>push up</em> operation).
146
147 Restoring the heap property after an element <code>array[i]</code>
148 has been modified can be done by a push up or push down operation on
149 that node.
150
151 **/
152
153 #include "host.h"
154 #include <assert.h>
155
156 #ifndef VL_HEAP_prefix
157 #error "VL_HEAP_prefix must be defined"
158 #endif
159
160 #ifndef VL_HEAP_array
161 #ifndef VL_HEAP_type
162 #error "VL_HEAP_type must be defined if VL_HEAP_array is not"
163 #endif
164 #define VL_HEAP_array VL_HEAP_type*
165 #define VL_HEAP_array_const VL_HEAP_type const*
166 #endif
167
168 #ifndef VL_HEAP_array_const
169 #define VL_HEAP_array_const VL_HEAP_array
170 #endif
171
172 #ifdef __DOXYGEN__
173 #define VL_HEAP_prefix HeapObject /**< Prefix of the heap functions */
174 #define VL_HEAP_type HeapType /**< Data type of the heap elements */
175 #define VL_HEAP_array HeapType* /**< Data type of the heap container */
176 #define VL_HEAP_array HeapType const* /**< Const data type of the heap container */
177 #endif
178
179 /* ---------------------------------------------------------------- */
180
181 #ifndef VL_HEAP_DEF_H
182 #define VL_HEAP_DEF_H
183
184 /** @internal @brief Get index of parent node
185 ** @param index a node index.
186 ** @return index of the parent node.
187 **/
188
189 VL_INLINE vl_uindex
190 vl_heap_parent (vl_uindex index)
191 {
192 if (index == 0) return 0 ;
193 return (index - 1) / 2 ;
194 }
195
196 /** @internal @brief Get index of left child
197 ** @param index a node index.
198 ** @return index of the left child.
199 **/
200
201 VL_INLINE vl_uindex
202 vl_heap_left_child (vl_uindex index)
203 {
204 return 2 * index + 1 ;
205 }
206
207 /** @internal @brief Get index of right child
208 ** @param index a node index.
209 ** @return index of the right child.
210 **/
211
212 VL_INLINE vl_uindex
213 vl_heap_right_child (vl_uindex index)
214 {
215 return vl_heap_left_child (index) + 1 ;
216 }
217
218 /* VL_HEAP_DEF_H */
219 #endif
220
221 /* ---------------------------------------------------------------- */
222
223 #if ! defined(VL_HEAP_cmp) || defined(__DOXYGEN__)
224 #define VL_HEAP_cmp VL_XCAT(VL_HEAP_prefix, _cmp)
225
226 /** @brief Compare two heap elements
227 ** @param array heap array.
228 ** @param indexA index of the first element @c A to compare.
229 ** @param indexB index of the second element @c B to comapre.
230 ** @return a negative number if @c A<B, 0 if @c A==B, and
231 ** a positive number if if @c A>B.
232 **/
233
234 VL_INLINE VL_HEAP_type
235 VL_HEAP_cmp
236 (VL_HEAP_array_const array,
237 vl_uindex indexA,
238 vl_uindex indexB)
239 {
240 return array[indexA] - array[indexB] ;
241 }
242
243 /* VL_HEAP_cmp */
244 #endif
245
246 /* ---------------------------------------------------------------- */
247
248 #if ! defined(VL_HEAP_swap) || defined(__DOXYGEN__)
249 #define VL_HEAP_swap VL_XCAT(VL_HEAP_prefix, _swap)
250
251 /** @brief Swap two heap elements
252 ** @param array array of nodes.
253 ** @param array heap array.
254 ** @param indexA index of the first node to swap.
255 ** @param indexB index of the second node to swap.
256 **
257 ** The function swaps the two heap elements @a a and @ b. The function
258 ** uses a temporary element and the copy operator, which must be
259 ** well defined for the heap elements.
260 **/
261
262 VL_INLINE void
263 VL_HEAP_swap
264 (VL_HEAP_array array,
265 vl_uindex indexA,
266 vl_uindex indexB)
267 {
268 VL_HEAP_type t = array [indexA] ;
269 array [indexA] = array [indexB] ;
270 array [indexB] = t ;
271 }
272
273 /* VL_HEAP_swap */
274 #endif
275
276 /* ---------------------------------------------------------------- */
277
278 #if ! defined(VL_HEAP_up) || defined(__DOXYGEN__)
279 #define VL_HEAP_up VL_XCAT(VL_HEAP_prefix, _up)
280
281 /** @brief Heap up operation
282 ** @param array pointer to the heap array.
283 ** @param heapSize size of the heap.
284 ** @param index index of the node to push up.
285 **/
286
287 VL_INLINE void
288 VL_HEAP_up
289 (VL_HEAP_array array, vl_size heapSize, vl_uindex index)
290 {
291 vl_uindex leftIndex = vl_heap_left_child (index) ;
292 vl_uindex rightIndex = vl_heap_right_child (index) ;
293
294 /* no childer: stop */
295 if (leftIndex >= heapSize) return ;
296
297 /* only left childer: easy */
298 if (rightIndex >= heapSize) {
299 if (VL_HEAP_cmp (array, index, leftIndex) > 0) {
300 VL_HEAP_swap (array, index, leftIndex) ;
301 }
302 return ;
303 }
304
305 /* both childern */
306 {
307 if (VL_HEAP_cmp (array, leftIndex, rightIndex) < 0) {
308 /* swap with left */
309 if (VL_HEAP_cmp (array, index, leftIndex) > 0) {
310 VL_HEAP_swap (array, index, leftIndex) ;
311 VL_HEAP_up (array, heapSize, leftIndex) ;
312 }
313 } else {
314 /* swap with right */
315 if (VL_HEAP_cmp (array, index, rightIndex) > 0) {
316 VL_HEAP_swap (array, index, rightIndex) ;
317 VL_HEAP_up (array, heapSize, rightIndex) ;
318 }
319 }
320 }
321 }
322
323 /* VL_HEAP_up */
324 #endif
325
326 /* ---------------------------------------------------------------- */
327
328 #if ! defined(VL_HEAP_down) || defined(__DOXYGEN__)
329 #define VL_HEAP_down VL_XCAT(VL_HEAP_prefix, _down)
330
331 /** @brief Heap down operation
332 ** @param array pointer to the heap node array.
333 ** @param index index of the node to push up.
334 **/
335
336 VL_INLINE void
337 VL_HEAP_down
338 (VL_HEAP_array array, vl_uindex index)
339 {
340 vl_uindex parentIndex ;
341
342 if (index == 0) return ;
343
344 parentIndex = vl_heap_parent (index) ;
345
346 if (VL_HEAP_cmp (array, index, parentIndex) < 0) {
347 VL_HEAP_swap (array, index, parentIndex) ;
348 VL_HEAP_down (array, parentIndex) ;
349 }
350 }
351
352 /* VL_HEAP_down */
353 #endif
354
355 /* ---------------------------------------------------------------- */
356
357 #if ! defined(VL_HEAP_push) || defined(__DOXYGEN__)
358 #define VL_HEAP_push VL_XCAT(VL_HEAP_prefix, _push)
359
360 /** @brief Heap push operation
361 ** @param array pointer to the heap array.
362 ** @param heapSize (in/out) size of the heap.
363 **
364 ** The function adds to the heap the element of index @c heapSize
365 ** and increments @c heapSize.
366 **/
367
368 VL_INLINE void
369 VL_HEAP_push
370 (VL_HEAP_array array, vl_size *heapSize)
371 {
372 VL_HEAP_down (array, *heapSize) ;
373 *heapSize += 1 ;
374 }
375
376 /* VL_HEAP_push */
377 #endif
378
379 /* ---------------------------------------------------------------- */
380
381 #if ! defined(VL_HEAP_pop) || defined(__DOXYGEN__)
382 #define VL_HEAP_pop VL_XCAT(VL_HEAP_prefix, _pop)
383
384 /** @brief Heap pop operation
385 ** @param array pointer to the heap array.
386 ** @param heapSize (in/out) size of the heap.
387 ** @return index of the popped element.
388 **
389 ** The function extracts from the heap the element of index 0
390 ** (the smallest element) and decreases @c heapSize.
391 **
392 ** The element extracted is moved as the first element after
393 ** the heap end (thus it has index @c heapSize). For convenience,
394 ** this index is returned by the function.
395 **
396 ** Popping from an empty heap is undefined.
397 **/
398
399 VL_INLINE vl_uindex
400 VL_HEAP_pop
401 (VL_HEAP_array array, vl_size *heapSize)
402 {
403 assert (*heapSize) ;
404
405 *heapSize -= 1 ;
406
407 VL_HEAP_swap (array, 0, *heapSize) ;
408
409 if (*heapSize > 1) {
410 VL_HEAP_up (array, *heapSize, 0) ;
411 }
412
413 return *heapSize ;
414 }
415
416 /* VL_HEAP_pop */
417 #endif
418
419 /* ---------------------------------------------------------------- */
420
421 #if ! defined(VL_HEAP_update) || defined(__DOXYGEN__)
422 #define VL_HEAP_update VL_XCAT(VL_HEAP_prefix, _update)
423
424 /** @brief Heap update operation
425 ** @param array pointer to the heap array.
426 ** @param heapSize size of the heap.
427 ** @param index index of the node to update.
428 **
429 ** The function updates the heap to account for a change to the
430 ** element of index @c index in the heap.
431 **
432 ** Notice that using this
433 ** function requires knowing the index of the heap index of
434 ** element that was changed. Since the heap swaps elements in the
435 ** array, this is in general different from the index that that
436 ** element had originally.
437 **/
438
439 VL_INLINE void
440 VL_HEAP_update
441 (VL_HEAP_array array,
442 vl_size heapSize,
443 vl_uindex index)
444 {
445 VL_HEAP_up (array, heapSize, index) ;
446 VL_HEAP_down (array, index) ;
447 }
448
449 /* VL_HEAP_update */
450 #endif
451
452 /* ---------------------------------------------------------------- */
453
454 #undef VL_HEAP_cmp
455 #undef VL_HEAP_swap
456 #undef VL_HEAP_up
457 #undef VL_HEAP_down
458 #undef VL_HEAP_push
459 #undef VL_HEAP_pop
460 #undef VL_HEAP_update
461 #undef VL_HEAP_prefix
462 #undef VL_HEAP_type
463 #undef VL_HEAP_array
464 #undef VL_HEAP_array_const