You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

464 lines
13 KiB

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