heap.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * \file heap.h
  30. *
  31. * <pre>
  32. * Expandable priority queue configured as a heap for arbitrary void* data
  33. *
  34. * The L_Heap is used to implement a priority queue. The elements
  35. * in the heap are ordered in either increasing or decreasing key value.
  36. * The key is a float field 'keyval' that is required to be
  37. * contained in the elements of the queue.
  38. *
  39. * The heap is a simple binary tree with the following constraints:
  40. * - the key of each node is >= the keys of the two children
  41. * - the tree is complete, meaning that each level (1, 2, 4, ...)
  42. * is filled and the last level is filled from left to right
  43. *
  44. * The tree structure is implicit in the queue array, with the
  45. * array elements numbered as a breadth-first search of the tree
  46. * from left to right. It is thus guaranteed that the largest
  47. * (or smallest) key belongs to the first element in the array.
  48. *
  49. * Heap sort is used to sort the array. Once an array has been
  50. * sorted as a heap, it is convenient to use it as a priority queue,
  51. * because the min (or max) elements are always at the root of
  52. * the tree (element 0), and once removed, the heap can be
  53. * resorted in not more than log[n] steps, where n is the number
  54. * of elements on the heap. Likewise, if an arbitrary element is
  55. * added to the end of the array A, the sorted heap can be restored
  56. * in not more than log[n] steps.
  57. *
  58. * A L_Heap differs from a L_Queue in that the elements in the former
  59. * are sorted by a key. Internally, the array is maintained
  60. * as a queue, with a pointer to the end of the array. The
  61. * head of the array always remains at array[0]. The array is
  62. * maintained (sorted) as a heap. When an item is removed from
  63. * the head, the last item takes its place (thus reducing the
  64. * array length by 1), and this is followed by array element
  65. * swaps to restore the heap property. When an item is added,
  66. * it goes at the end of the array, and is swapped up to restore
  67. * the heap. If the ptr array is full, adding another item causes
  68. * the ptr array size to double.
  69. *
  70. * For further implementation details, see heap.c.
  71. * </pre>
  72. */
  73. /*! Heap of arbitrary void* data */
  74. struct L_Heap
  75. {
  76. l_int32 nalloc; /*!< size of allocated ptr array */
  77. l_int32 n; /*!< number of elements stored in the heap */
  78. void **array; /*!< ptr array */
  79. l_int32 direction; /*!< L_SORT_INCREASING or L_SORT_DECREASING */
  80. };
  81. typedef struct L_Heap L_HEAP;
  82. #endif /* LEPTONICA_HEAP_H */