heap.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*====================================================================*
  2. - Copyright (C) 2001 Leptonica. All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions
  6. - are met:
  7. - 1. Redistributions of source code must retain the above copyright
  8. - notice, this list of conditions and the following disclaimer.
  9. - 2. Redistributions in binary form must reproduce the above
  10. - copyright notice, this list of conditions and the following
  11. - disclaimer in the documentation and/or other materials
  12. - provided with the distribution.
  13. -
  14. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
  18. - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *====================================================================*/
  26. #ifndef LEPTONICA_HEAP_H
  27. #define LEPTONICA_HEAP_H
  28. /*
  29. * heap.h
  30. *
  31. * Expandable priority queue configured as a heap for arbitrary void* data
  32. *
  33. * The L_Heap is used to implement a priority queue. The elements
  34. * in the heap are ordered in either increasing or decreasing key value.
  35. * The key is a float field 'keyval' that is required to be
  36. * contained in the elements of the queue.
  37. *
  38. * The heap is a simple binary tree with the following constraints:
  39. * - the key of each node is >= the keys of the two children
  40. * - the tree is complete, meaning that each level (1, 2, 4, ...)
  41. * is filled and the last level is filled from left to right
  42. *
  43. * The tree structure is implicit in the queue array, with the
  44. * array elements numbered as a breadth-first search of the tree
  45. * from left to right. It is thus guaranteed that the largest
  46. * (or smallest) key belongs to the first element in the array.
  47. *
  48. * Heap sort is used to sort the array. Once an array has been
  49. * sorted as a heap, it is convenient to use it as a priority queue,
  50. * because the min (or max) elements are always at the root of
  51. * the tree (element 0), and once removed, the heap can be
  52. * resorted in not more than log[n] steps, where n is the number
  53. * of elements on the heap. Likewise, if an arbitrary element is
  54. * added to the end of the array A, the sorted heap can be restored
  55. * in not more than log[n] steps.
  56. *
  57. * A L_Heap differs from a L_Queue in that the elements in the former
  58. * are sorted by a key. Internally, the array is maintained
  59. * as a queue, with a pointer to the end of the array. The
  60. * head of the array always remains at array[0]. The array is
  61. * maintained (sorted) as a heap. When an item is removed from
  62. * the head, the last item takes its place (thus reducing the
  63. * array length by 1), and this is followed by array element
  64. * swaps to restore the heap property. When an item is added,
  65. * it goes at the end of the array, and is swapped up to restore
  66. * the heap. If the ptr array is full, adding another item causes
  67. * the ptr array size to double.
  68. *
  69. * For further implementation details, see heap.c.
  70. */
  71. struct L_Heap
  72. {
  73. l_int32 nalloc; /* size of allocated ptr array */
  74. l_int32 n; /* number of elements stored in the heap */
  75. void **array; /* ptr array */
  76. l_int32 direction; /* L_SORT_INCREASING or L_SORT_DECREASING */
  77. };
  78. typedef struct L_Heap L_HEAP;
  79. #endif /* LEPTONICA_HEAP_H */