mphf_hash_table.C 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * $TOG: mphf_hash_table.C /main/4 1998/04/17 11:50:23 mgreess $
  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 "hmphf/mphf_hash_table.h"
  50. #define FULL 1
  51. mphf_hash_table::mphf_hash_table(params& params_ptr) :
  52. v_no_slots(params_ptr.v_n), v_num_filled_slots(0)
  53. {
  54. v_rep = new char[v_no_slots];
  55. v_random_table = new int[v_no_slots];
  56. v_map_table = new int[v_no_slots];
  57. clear();
  58. }
  59. mphf_hash_table::~mphf_hash_table()
  60. {
  61. delete [] v_rep ;
  62. delete [] v_random_table ;
  63. delete [] v_map_table ;
  64. }
  65. void mphf_hash_table::clear()
  66. {
  67. int i;
  68. //, right;
  69. pm_random pm(19);
  70. for ( i=0; i<v_no_slots; i++ ) {
  71. v_rep[i] = 0;
  72. }
  73. for ( i=0; i<v_no_slots; i++ ) {
  74. v_random_table[i] = i;
  75. }
  76. for ( i = 0; i < v_no_slots; i++) {
  77. //right = pm.rand() % ( v_no_slots - i) + i;
  78. int_swap( v_random_table[pm.rand() % ( v_no_slots - i) + i],
  79. v_random_table[i]
  80. );
  81. }
  82. for ( i = 0; i < v_no_slots; i++)
  83. v_map_table[v_random_table[i]] = i;
  84. v_num_filled_slots = 0;
  85. }
  86. int mphf_hash_table::fast_fit(int_pattern& pat)
  87. {
  88. int ok;
  89. int i, j, alignment, landing_slot;
  90. int right_rdm_tbl_index, left_rdm_tbl_cnt;
  91. for ( i = v_num_filled_slots; i < v_no_slots; i++ ) {
  92. ok = 0;
  93. /**************************/
  94. /* compute the alignment */
  95. /**************************/
  96. alignment = (v_no_slots + v_random_table[i] - pat[0])
  97. % v_no_slots;
  98. /**************************/
  99. /* test fit the pattern */
  100. /**************************/
  101. for ( j=1; j<pat.no_elmts(); j++ ) {
  102. landing_slot = (pat[j] + alignment) % v_no_slots;
  103. if ( v_rep[landing_slot] == FULL ) {
  104. ok = -1;
  105. break; // try another alignment
  106. }
  107. }
  108. /**************************/
  109. /* really fit the pattern */
  110. /**************************/
  111. if ( ok == 0 ) {
  112. for ( j=0; j < pat.no_elmts(); j++ ) {
  113. landing_slot = (pat[j] + alignment) % v_no_slots;
  114. v_rep[landing_slot] = FULL ;
  115. right_rdm_tbl_index = v_map_table[landing_slot];
  116. left_rdm_tbl_cnt = v_random_table[v_num_filled_slots + j];
  117. v_random_table[right_rdm_tbl_index] = left_rdm_tbl_cnt;
  118. v_map_table[left_rdm_tbl_cnt] = right_rdm_tbl_index;
  119. v_random_table[v_num_filled_slots + j] = landing_slot;
  120. v_map_table[landing_slot] = v_num_filled_slots + j;
  121. }
  122. v_num_filled_slots += pat.no_elmts();
  123. return alignment;
  124. }
  125. }
  126. return -1;
  127. }
  128. int mphf_hash_table::fit_hash_table(int_pattern& pat)
  129. {
  130. int i, j;
  131. for ( i=0; i<pat.no_elmts(); i++ ) {
  132. if ( v_rep[int(pat[i])] != (int) 0 ) {
  133. for ( j=0; j<=i; j++ )
  134. v_rep[int(pat[j])] = (int) 0;
  135. v_num_filled_slots -= i+1;
  136. return -1;
  137. } else {
  138. v_rep[int(pat[i])] = FULL;
  139. v_num_filled_slots++;
  140. }
  141. }
  142. return 0;
  143. }