2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: buckets.cc /main/3 1996/06/11 17:19:53 cde-hal $
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
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
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.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
52 #include "hmphf/buckets.h"
54 bucket::bucket(char* key, int orig_position, Boolean copy) :
59 v_orig_pos(orig_position)
68 x = new char[len + 1];
69 *((char *) memcpy(x, key, len) + len) = '\0';
75 key_ptr = new slist_void_ptr_cell(x);
80 slist_void_ptr_cell *lp = key_ptr;
81 slist_void_ptr_cell* tmp_lp = 0;
86 lp = (slist_void_ptr_cell*)(lp -> v_succ);
91 int bucket::add_key(char* key, Boolean copy)
99 x = new char[len + 1];
100 *((char *) memcpy(x, key, len) + len) = '\0';
107 slist_void_ptr_cell* z = new slist_void_ptr_cell(x);
109 z -> v_succ = key_ptr;
116 ostream& operator<<(ostream& out, bucket& bt)
118 slist_void_ptr_cell *lp = bt.key_ptr;
122 out << ((char*)(lp -> void_ptr())) << " ";
123 lp = (slist_void_ptr_cell*)(lp -> v_succ);
130 ////////////////////////////////////////////////////
133 ////////////////////////////////////////////////////
135 //buckets::buckets(char* key_file, params& pms) :
137 buckets::buckets(char** keys, params& pms) :
138 v_no_buckets(pms.v_b), v_max_bucket_sz(0),
140 b_convertor(pms.v_n, 128, rnd),
141 h_convertor(pms.v_n, 128, rnd)
143 v_bucket_array = new bucketPtr[v_no_buckets];
146 for ( i=0; i < (unsigned int) v_no_buckets; v_bucket_array[i++] = 0);
151 for ( i=0; i<pms.v_n; i++ ) {
153 //debug(cerr, keys[i]);
154 hash = bucket_num(keys[i], pms);
156 if ( v_bucket_array[hash] == 0 ) {
157 v_bucket_array[hash] = new bucket(keys[i], hash, false);
160 k = v_bucket_array[hash] -> add_key(keys[i], false);
163 v_max_bucket_sz = MAX(v_max_bucket_sz, k);
171 for ( int i=0; i<v_no_buckets; i++ )
172 delete v_bucket_array[i];
174 delete v_bucket_array;
177 void buckets::set_control_bit(int cb)
179 for ( int i=0; i<v_no_buckets; i++ ) {
181 (*this)[i] -> set_control_bit(cb);
185 int buckets::bucket_num(char* k, params& pms)
187 //MESSAGE(cerr, "bucket_num");
189 int sum = b_convertor.atoi(k, strlen(k), pms.v_r, pms.v_n);
193 if ( sum < (int) pms.v_p1 ) {
196 sum %= (pms.v_b - pms.v_p2);
205 //int buckets::hash_value(char* k, int offset, int range)
208 //MESSAGE(cerr, "hash_value");
211 ////debug(cerr, strlen(k));
216 //debug(cerr, offset+1);
217 //debug(cerr, range);
219 // int hv = h_convertor.atoi(k, strlen(k), offset+1, range);
224 //if ( strcmp(k, "mphf_funcs.h") == 0 ||
225 // strcmp(k, "mmdb_fast_mphf") == 0 ) {
227 //debug(cerr, offset);
228 //debug(cerr, range);
236 void buckets::sort_by_size()
238 //MESSAGE(cerr, "sort()");
239 int* links = new int[v_no_buckets];
241 for ( i=0; i<v_no_buckets; links[i++]=-1 );
243 int* sizes = new int[v_max_bucket_sz+1];
244 for ( i=0; i<v_max_bucket_sz+1; sizes[i++]=-1 );
246 // arrage buckets according to their size
248 for ( i = 0; i < v_no_buckets; i++ ) {
250 if ( v_bucket_array[i] == 0 )
253 x = v_bucket_array[i] -> v_no_keys;
260 bucketPtr* new_bkt_array = new bucketPtr[v_no_buckets];
264 for ( i = v_max_bucket_sz; i >= 0; i-- ) {
266 if ( sizes[i] == -1 )
272 while ( idx != -1 ) {
274 new_bkt_array[j++] = v_bucket_array[idx];
276 //debug(cerr, new_bkt_array[j-1] -> v_no_keys);
277 //debug(cerr, *new_bkt_array[j-1]);
279 v_bucket_array[idx] = 0;
285 for ( ; j<v_no_buckets; new_bkt_array[j++] = 0 );
290 delete v_bucket_array;
291 v_bucket_array = new_bkt_array;
295 /*************************************************/
296 /* return -1 if no more pattern can be generated */
297 /* return 0 if a pattern is generated */
298 /*************************************************/
300 buckets::get_pattern(int idx, int_pattern& pat, params& pms)
303 v_bucket_array[idx] -> v_control_bit++;
304 for ( ; v_bucket_array[idx] -> v_control_bit<2 ;
305 v_bucket_array[idx] -> v_control_bit ++
308 if ( use_current_params(idx, pat, pms) == 0 )
316 buckets::use_current_params(int idx, int_pattern& pat,
317 params& pms, Boolean use_g_value)
320 slist_void_ptr_cell *lp = v_bucket_array[idx] -> key_ptr;
325 //debug(cerr, (char*)(lp -> void_ptr()));
326 //cerr << (char*)(lp -> void_ptr()) << "\n";
328 if ( use_g_value == false ) {
330 hash_value( ((char*)(lp -> void_ptr())),
331 pms.v_r + v_bucket_array[idx] -> v_control_bit,
338 //cerr << (char*)(lp -> void_ptr()) << "\n";
339 //debug(cerr, pms.r + v_bucket_array[idx] -> v_control_bit);
341 debug(cerr, ((ostring*)(lp -> void_ptr())) -> get());
342 int from_tbl1 = hash_value(
343 ((char*)(lp -> void_ptr())),
344 pms.r + v_bucket_array[idx] -> v_control_bit,
348 debug(cerr, from_tbl1);
349 debug(cerr, v_bucket_array[idx] -> v_g_value);
355 ((char*)(lp -> void_ptr())),
356 pms.v_r + v_bucket_array[idx] -> v_control_bit,
360 v_bucket_array[idx] -> v_g_value
365 lp = (slist_void_ptr_cell*)(lp -> v_succ);
368 //debug(cerr, v_bucket_array[idx] -> no_keys());
369 pat.reset_elmts(v_bucket_array[idx] -> no_keys());
373 return pat.duplicate();
376 ostream& operator<<(ostream& out, buckets& bts)
378 for ( int i=0; i<bts.no_buckets(); i++ )
380 debug(cerr, bts[i] -> orig_pos());