heap.C 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * $XConsortium: heap.cc /main/4 1996/07/18 14:29:27 drk $
  25. *
  26. * Copyright (c) 1993 HAL Computer Systems International, Ltd.
  27. * All rights reserved. Unpublished -- rights reserved under
  28. * the Copyright Laws of the United States. USE OF A COPYRIGHT
  29. * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
  30. * OR DISCLOSURE.
  31. *
  32. * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
  33. * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
  34. * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
  35. * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
  36. * INTERNATIONAL, LTD.
  37. *
  38. * RESTRICTED RIGHTS LEGEND
  39. * Use, duplication, or disclosure by the Government is subject
  40. * to the restrictions as set forth in subparagraph (c)(l)(ii)
  41. * of the Rights in Technical Data and Computer Software clause
  42. * at DFARS 252.227-7013.
  43. *
  44. * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
  45. * 1315 Dell Avenue
  46. * Campbell, CA 95008
  47. *
  48. */
  49. #include "dstr/heap.h"
  50. heap::heap(cmp_func_ptr_t eq, cmp_func_ptr_t ls, int max) :
  51. buffer(MAX(1, max)*sizeof(voidPtr)), f_cmp_func_eq(eq), f_cmp_func_ls(ls)
  52. {
  53. // the 0th element is never used.
  54. char z[16];
  55. memset(z, 0, 16);
  56. buffer::put(z, sizeof(voidPtr));
  57. v_updated = false;
  58. }
  59. heap::~heap()
  60. {
  61. /*
  62. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  63. int ct = count();
  64. for ( int i=1; i<=ct; i++ )
  65. delete heap_space[i];
  66. */
  67. }
  68. /*
  69. int heap::count()
  70. {
  71. // the 0th element is excluded
  72. return buffer::content_sz() / sizeof(voidPtr) - 1;
  73. }
  74. */
  75. Boolean heap::insert(voidPtr elm)
  76. {
  77. if ( buf_sz() < (int)(content_sz() + 2*sizeof(voidPtr)) )
  78. buffer::expand_chunk(2*buf_sz()) ;
  79. long x = long(elm);
  80. buffer::put(x);
  81. v_updated = true;
  82. return true;
  83. }
  84. Boolean heap::insert_heapify(voidPtr elm)
  85. {
  86. insert(elm);
  87. int j = count(); int i = j/2;
  88. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  89. while ( i > 0 && (*f_cmp_func_ls)(heap_space[i], elm) == true ) {
  90. heap_space[j] = heap_space[i];
  91. j = i; i /= 2;
  92. }
  93. heap_space[j] = elm;
  94. return true;
  95. }
  96. void heap::adjust(int i, call_back_func_ptr_t call_back)
  97. {
  98. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  99. int j = 2*i;
  100. voidPtr item = heap_space[i];
  101. while ( j <= count() ) {
  102. if ( j < count() &&
  103. (*f_cmp_func_ls)(heap_space[j], heap_space[j+1]) == true
  104. ) {
  105. j++;
  106. }
  107. if ( (*f_cmp_func_ls)(item, heap_space[j]) == false )
  108. break;
  109. else {
  110. heap_space[j/2] = heap_space[j];
  111. if ( call_back != 0 )
  112. (*call_back)(j/2, heap_space[j/2]);
  113. }
  114. j *= 2;
  115. }
  116. heap_space[j/2] = item;
  117. if ( call_back != 0 )
  118. (*call_back)(j/2, item);
  119. }
  120. voidPtr heap::max_elm()
  121. {
  122. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  123. if ( count() > 0 )
  124. return heap_space[1];
  125. else
  126. return 0;
  127. }
  128. void heap::adjust_max_to(voidPtr new_max_item)
  129. {
  130. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  131. heap_space[1] = new_max_item;
  132. adjust(1);
  133. v_updated = true;
  134. }
  135. void heap::heapify()
  136. {
  137. //for ( int i = (int)floor(double(count())/2); i>=1; adjust(i--) );
  138. for ( int i = count()/2; i>=1; adjust(i--) );
  139. }
  140. void heap::remove()
  141. {
  142. set_content_sz(0);
  143. buffer::put((unsigned int)0);
  144. v_updated = false;
  145. }
  146. void heap::delete_max(call_back_func_ptr_t call_back)
  147. {
  148. //MESSAGE(cerr, "delete max");
  149. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  150. int ct = count();
  151. heap_space[1] = heap_space[ct];
  152. set_content_sz(content_sz() - sizeof(voidPtr));
  153. ct--;
  154. adjust(1, call_back);
  155. }
  156. void heap::_delete_max(int i, voidPtr* heap_array)
  157. {
  158. int target = ( heap_array[i+1] == 0 ) ? 2*i : 2*i+1;
  159. voidPtr tmp = heap_array[i];
  160. heap_array[i] = heap_array[target] ;
  161. heap_array[target] = tmp;
  162. if ( heap_array[target] != 0 )
  163. _delete_max(target, heap_array);
  164. return;
  165. }
  166. int heap::first()
  167. {
  168. return ( count() >= 1 ) ? 1 : 0;
  169. }
  170. void heap::next(int& ind)
  171. {
  172. ind = ( ind < count() ) ? (ind+1) : 0;
  173. }
  174. voidPtr heap::operator()(int ind)
  175. {
  176. if ( INRANGE(ind, 1, count()) ) {
  177. voidPtr *heap_space = (voidPtr*)buffer::get_base();
  178. return heap_space[ind];
  179. } else
  180. return 0;
  181. }