c_heap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  2. * This file is part of clib library
  3. * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  23. #include "c_lib.h"
  24. static int
  25. pvt_clib_heap_isLeaf( int pos , struct clib_heap *pHeap) {
  26. return ( pos >= (pHeap->pHeapPtr->no_of_elements/2)) && ( pos < pHeap->pHeapPtr->no_of_elements );
  27. }
  28. static int
  29. pvt_clib_heap_leftchild( int pos ) {
  30. return 2 * pos + 1;
  31. }
  32. static int
  33. pvt_clib_heap_compare(struct clib_array *pArray, int lIndex , int rIndex) {
  34. void *left = (void*)0;
  35. void *right = (void*)0;
  36. int compare_result = 0;
  37. clib_error rc = 0;
  38. rc = element_at_clib_array ( pArray, lIndex , &left);
  39. rc = element_at_clib_array ( pArray, rIndex , &right);
  40. compare_result = pArray->compare_fn ( left, right );
  41. if ( left ) free ( left );
  42. if ( right ) free ( right );
  43. return compare_result;
  44. }
  45. static void
  46. pvt_clib_heap_siftdown_max( struct clib_heap *pHeap, int pos ) {
  47. struct clib_array *pArray = pHeap->pHeapPtr;
  48. int n = pArray->no_of_elements;
  49. while ( !pvt_clib_heap_isLeaf(pos, pHeap) ) {
  50. int j = pvt_clib_heap_leftchild( pos );
  51. if ( ( j < ( n - 1) ) &&
  52. (pvt_clib_heap_compare( pArray, j, j+1) == -1)) {
  53. j++;
  54. }
  55. if ( pvt_clib_heap_compare( pArray, pos, j ) == 1 ||
  56. pvt_clib_heap_compare( pArray, pos, j ) == 0) return;
  57. swap_element_clib_array(pArray, pos, j);
  58. pos = j;
  59. }
  60. }
  61. static void
  62. pvt_clib_heap_siftdown_min( struct clib_heap *pHeap, int pos ) {
  63. struct clib_array *pArray = pHeap->pHeapPtr;
  64. int n = pArray->no_of_elements;
  65. while ( !pvt_clib_heap_isLeaf(pos, pHeap) ) {
  66. int j = pvt_clib_heap_leftchild( pos );
  67. if ( ( j < ( n - 1) ) &&
  68. (pvt_clib_heap_compare( pArray, j, j+1) == 1)) {
  69. j++;
  70. }
  71. if ( pvt_clib_heap_compare( pArray, pos, j ) == -1 ||
  72. pvt_clib_heap_compare( pArray, pos, j ) == 0) return;
  73. swap_element_clib_array(pArray, pos, j);
  74. pos = j;
  75. }
  76. }
  77. struct clib_heap *
  78. new_clib_heap( int default_size, clib_compare fn_c, clib_destroy fn_d ) {
  79. struct clib_heap *pHeap = ( struct clib_heap *) malloc ( sizeof ( struct clib_heap ));
  80. pHeap->pHeapPtr = new_clib_array ( default_size, fn_c, fn_d);
  81. pHeap->heap_left = 0;
  82. pHeap->heap_parent = 0;
  83. pHeap->heap_right = 0;
  84. return pHeap;
  85. }
  86. void
  87. delete_clib_heap( struct clib_heap *pHeap) {
  88. delete_clib_array ( pHeap->pHeapPtr );
  89. free ( pHeap );
  90. }
  91. void
  92. insert_clib_heap ( struct clib_heap *pHeap, void *elem, size_t elem_size) {
  93. push_back_clib_array ( pHeap->pHeapPtr, elem, elem_size);
  94. }
  95. void
  96. build_max_clib_heap ( struct clib_heap *pHeap ) {
  97. int i = 0;
  98. for ( i = (pHeap->pHeapPtr->no_of_elements / 2 ) - 1; i >= 0; i--) {
  99. pvt_clib_heap_siftdown_max(pHeap, i);
  100. }
  101. }
  102. void *
  103. extract_max_clib_heap( struct clib_heap *pHeap) {
  104. void *elem;
  105. swap_element_clib_array(pHeap->pHeapPtr,
  106. 0,
  107. pHeap->pHeapPtr->no_of_elements - 1);
  108. back_clib_array( pHeap->pHeapPtr, &elem);
  109. remove_clib_array ( pHeap->pHeapPtr, pHeap->pHeapPtr->no_of_elements - 1 );
  110. if (pHeap->pHeapPtr->no_of_elements != 0) {
  111. pvt_clib_heap_siftdown_max(pHeap, 0);
  112. }
  113. return elem;
  114. }
  115. void
  116. build_min_clib_heap ( struct clib_heap *pHeap ) {
  117. int i = 0;
  118. for ( i = (pHeap->pHeapPtr->no_of_elements / 2 ) - 1; i >= 0; i--) {
  119. pvt_clib_heap_siftdown_min(pHeap, i);
  120. }
  121. }
  122. void *
  123. extract_min_clib_heap( struct clib_heap *pHeap) {
  124. void *elem;
  125. swap_element_clib_array(pHeap->pHeapPtr,
  126. 0,
  127. pHeap->pHeapPtr->no_of_elements - 1);
  128. back_clib_array( pHeap->pHeapPtr, &elem);
  129. remove_clib_array ( pHeap->pHeapPtr, pHeap->pHeapPtr->no_of_elements - 1 );
  130. if (pHeap->pHeapPtr->no_of_elements != 0) {
  131. pvt_clib_heap_siftdown_min(pHeap, 0);
  132. }
  133. return elem;
  134. }
  135. clib_bool
  136. empty_clib_heap ( struct clib_heap *pHeap) {
  137. if ( pHeap == ( struct clib_heap*)0 )
  138. return clib_true;
  139. return pHeap->pHeapPtr->no_of_elements == 0 ? clib_true : clib_false;
  140. }
  141. void for_each_clib_heap ( struct clib_heap *pHeap, void (*fn)(void*)) {
  142. int size = size_clib_array ( pHeap->pHeapPtr );
  143. int i = 0;
  144. for ( i = 0; i < size; i++ ) {
  145. void *elem;
  146. element_at_clib_array ( pHeap->pHeapPtr, i , &elem);
  147. (fn)(elem);
  148. free ( elem );
  149. }
  150. }