2
0

buckets.C 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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: buckets.cc /main/3 1996/06/11 17:19:53 cde-hal $
  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/buckets.h"
  50. bucket::bucket(char* key, int orig_position, Boolean copy) :
  51. v_no_keys(1),
  52. v_count(0),
  53. v_control_bit(-1),
  54. v_g_value(0),
  55. v_orig_pos(orig_position)
  56. {
  57. char* x = 0;
  58. int len;
  59. switch (copy) {
  60. case true:
  61. len = strlen(key);
  62. x = new char[len + 1];
  63. *((char *) memcpy(x, key, len) + len) = '\0';
  64. break;
  65. case false:
  66. x = key;
  67. break;
  68. }
  69. key_ptr = new slist_void_ptr_cell(x);
  70. }
  71. bucket::~bucket()
  72. {
  73. slist_void_ptr_cell *lp = key_ptr;
  74. slist_void_ptr_cell* tmp_lp = 0;
  75. while ( lp ) {
  76. tmp_lp = lp;
  77. lp = (slist_void_ptr_cell*)(lp -> v_succ);
  78. delete tmp_lp;
  79. }
  80. }
  81. int bucket::add_key(char* key, Boolean copy)
  82. {
  83. char *x = 0;
  84. int len;
  85. switch (copy) {
  86. case true:
  87. len = strlen(key);
  88. x = new char[len + 1];
  89. *((char *) memcpy(x, key, len) + len) = '\0';
  90. break;
  91. case false:
  92. x = key;
  93. break;
  94. }
  95. slist_void_ptr_cell* z = new slist_void_ptr_cell(x);
  96. z -> v_succ = key_ptr;
  97. key_ptr = z;
  98. v_no_keys++;
  99. return v_no_keys;
  100. }
  101. ostream& operator<<(ostream& out, bucket& bt)
  102. {
  103. slist_void_ptr_cell *lp = bt.key_ptr;
  104. while ( lp ) {
  105. out << ((char*)(lp -> void_ptr())) << " ";
  106. lp = (slist_void_ptr_cell*)(lp -> v_succ);
  107. }
  108. out << "\n";
  109. return out;
  110. }
  111. ////////////////////////////////////////////////////
  112. //
  113. //
  114. ////////////////////////////////////////////////////
  115. //buckets::buckets(char* key_file, params& pms) :
  116. buckets::buckets(char** keys, params& pms) :
  117. v_no_buckets(pms.v_b), v_max_bucket_sz(0),
  118. rnd(pms.v_seed),
  119. b_convertor(pms.v_n, 128, rnd),
  120. h_convertor(pms.v_n, 128, rnd)
  121. {
  122. v_bucket_array = new bucketPtr[v_no_buckets];
  123. unsigned int i;
  124. for ( i=0; i < (unsigned int) v_no_buckets; v_bucket_array[i++] = 0);
  125. //debug(cerr, pms);
  126. int hash, k;
  127. for ( i=0; i<pms.v_n; i++ ) {
  128. //debug(cerr, keys[i]);
  129. hash = bucket_num(keys[i], pms);
  130. if ( v_bucket_array[hash] == 0 ) {
  131. v_bucket_array[hash] = new bucket(keys[i], hash, false);
  132. k = 1;
  133. } else {
  134. k = v_bucket_array[hash] -> add_key(keys[i], false);
  135. }
  136. v_max_bucket_sz = MAX(v_max_bucket_sz, k);
  137. }
  138. sort_by_size();
  139. }
  140. buckets::~buckets()
  141. {
  142. for ( int i=0; i<v_no_buckets; i++ )
  143. delete v_bucket_array[i];
  144. delete [] v_bucket_array;
  145. }
  146. void buckets::set_control_bit(int cb)
  147. {
  148. for ( int i=0; i<v_no_buckets; i++ ) {
  149. if ( (*this)[i] )
  150. (*this)[i] -> set_control_bit(cb);
  151. }
  152. }
  153. int buckets::bucket_num(char* k, params& pms)
  154. {
  155. //MESSAGE(cerr, "bucket_num");
  156. //debug(cerr, k);
  157. int sum = b_convertor.atoi(k, strlen(k), pms.v_r, pms.v_n);
  158. //debug(cerr, sum);
  159. if ( sum < (int) pms.v_p1 ) {
  160. sum %= pms.v_p2;
  161. } else {
  162. sum %= (pms.v_b - pms.v_p2);
  163. sum += pms.v_p2;
  164. }
  165. //debug(cerr, sum);
  166. return sum;
  167. }
  168. //int buckets::hash_value(char* k, int offset, int range)
  169. //{
  170. ///*
  171. //MESSAGE(cerr, "hash_value");
  172. //debug(cerr, k);
  173. //*/
  174. ////debug(cerr, strlen(k));
  175. //
  176. //
  177. //
  178. ///*
  179. //debug(cerr, offset+1);
  180. //debug(cerr, range);
  181. //*/
  182. // int hv = h_convertor.atoi(k, strlen(k), offset+1, range);
  183. //
  184. ////debug(cerr, hv);
  185. //
  186. ///*
  187. //if ( strcmp(k, "mphf_funcs.h") == 0 ||
  188. // strcmp(k, "mmdb_fast_mphf") == 0 ) {
  189. //debug(cerr, k);
  190. //debug(cerr, offset);
  191. //debug(cerr, range);
  192. //debug(cerr, hv);
  193. //}
  194. //*/
  195. //
  196. // return hv;
  197. //}
  198. void buckets::sort_by_size()
  199. {
  200. //MESSAGE(cerr, "sort()");
  201. int* links = new int[v_no_buckets];
  202. int i;
  203. for ( i=0; i<v_no_buckets; links[i++]=-1 );
  204. int* sizes = new int[v_max_bucket_sz+1];
  205. for ( i=0; i<v_max_bucket_sz+1; sizes[i++]=-1 );
  206. // arrage buckets according to their size
  207. int x;
  208. for ( i = 0; i < v_no_buckets; i++ ) {
  209. if ( v_bucket_array[i] == 0 )
  210. continue;
  211. x = v_bucket_array[i] -> v_no_keys;
  212. links[i] = sizes[x];
  213. sizes[x] = i;
  214. }
  215. bucketPtr* new_bkt_array = new bucketPtr[v_no_buckets];
  216. int j=0;
  217. int idx;
  218. for ( i = v_max_bucket_sz; i >= 0; i-- ) {
  219. if ( sizes[i] == -1 )
  220. continue;
  221. idx = sizes[i];
  222. //debug(cerr, i);
  223. while ( idx != -1 ) {
  224. new_bkt_array[j++] = v_bucket_array[idx];
  225. //debug(cerr, new_bkt_array[j-1] -> v_no_keys);
  226. //debug(cerr, *new_bkt_array[j-1]);
  227. v_bucket_array[idx] = 0;
  228. idx = links[idx];
  229. }
  230. }
  231. for ( ; j<v_no_buckets; new_bkt_array[j++] = 0 );
  232. delete [] sizes;
  233. delete [] links;
  234. delete [] v_bucket_array;
  235. v_bucket_array = new_bkt_array;
  236. }
  237. /*************************************************/
  238. /* return -1 if no more pattern can be generated */
  239. /* return 0 if a pattern is generated */
  240. /*************************************************/
  241. int
  242. buckets::get_pattern(int idx, int_pattern& pat, params& pms)
  243. {
  244. v_bucket_array[idx] -> v_control_bit++;
  245. for ( ; v_bucket_array[idx] -> v_control_bit<2 ;
  246. v_bucket_array[idx] -> v_control_bit ++
  247. ) {
  248. if ( use_current_params(idx, pat, pms) == 0 )
  249. return 0;
  250. }
  251. return -1;
  252. }
  253. int
  254. buckets::use_current_params(int idx, int_pattern& pat,
  255. params& pms, Boolean use_g_value)
  256. {
  257. int i = 0;
  258. slist_void_ptr_cell *lp = v_bucket_array[idx] -> key_ptr;
  259. while ( lp ) {
  260. //debug(cerr, (char*)(lp -> void_ptr()));
  261. //cerr << (char*)(lp -> void_ptr()) << "\n";
  262. if ( use_g_value == false ) {
  263. pat.insert(
  264. hash_value( ((char*)(lp -> void_ptr())),
  265. pms.v_r + v_bucket_array[idx] -> v_control_bit,
  266. pms.v_n
  267. ),
  268. i++
  269. );
  270. } else {
  271. //cerr << (char*)(lp -> void_ptr()) << "\n";
  272. //debug(cerr, pms.r + v_bucket_array[idx] -> v_control_bit);
  273. /*
  274. debug(cerr, ((ostring*)(lp -> void_ptr())) -> get());
  275. int from_tbl1 = hash_value(
  276. ((char*)(lp -> void_ptr())),
  277. pms.r + v_bucket_array[idx] -> v_control_bit,
  278. pms.n
  279. );
  280. debug(cerr, from_tbl1);
  281. debug(cerr, v_bucket_array[idx] -> v_g_value);
  282. */
  283. pat.insert(
  284. (
  285. hash_value(
  286. ((char*)(lp -> void_ptr())),
  287. pms.v_r + v_bucket_array[idx] -> v_control_bit,
  288. pms.v_n
  289. )
  290. +
  291. v_bucket_array[idx] -> v_g_value
  292. ) % pms.v_n,
  293. i++
  294. );
  295. }
  296. lp = (slist_void_ptr_cell*)(lp -> v_succ);
  297. }
  298. //debug(cerr, v_bucket_array[idx] -> no_keys());
  299. pat.reset_elmts(v_bucket_array[idx] -> no_keys());
  300. //debug(cerr, pat);
  301. return pat.duplicate();
  302. }
  303. ostream& operator<<(ostream& out, buckets& bts)
  304. {
  305. for ( int i=0; i<bts.no_buckets(); i++ )
  306. if ( bts[i] ) {
  307. debug(cerr, bts[i] -> orig_pos());
  308. debug(out, *bts[i]);
  309. }
  310. return out;
  311. }