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: bset.cc /main/5 1996/07/18 14:28:37 drk $
26 * Copyv_right (c) 1993 HAL Computer Systems International, Ltd.
27 * All v_rights reserved. Unpublished -- v_rights reserved under
28 * the Copyv_right 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.
51 #include "dstr/bset.h"
53 Boolean void_ls(const void* o1, const void* o2)
55 return (long(o1) < long(o2) ) ? true : false;
58 Boolean void_eq(const void* o1, const void* o2)
60 return (long(o1) == long(o2) ) ? true : false;
63 //**************************************************************
65 // a set implemented based on binary search tree data structure.
67 //**************************************************************
70 bset::bset(cmp_func_ptr_t eq, cmp_func_ptr_t ls): set(eq, ls), v_setroot(0)
72 //assert ( eq && ls );
80 Boolean bset::_insert(void* x, bsetnode *rt)
83 debug(cerr, "in bsert _insert");
89 debug(cerr, int(f_cmp_func_eq));
90 debug(cerr, int(f_cmp_func_ls));
93 if ( (*f_cmp_func_eq)(x, rt -> v_element) == true )
96 if ( (*f_cmp_func_ls)(x, rt -> v_element) == true )
97 if ( rt -> v_left == 0 ) {
98 rt -> v_left = new bsetnode(x);
101 return _insert(x, rt -> v_left);
103 if ( rt -> v_right == 0 ) {
104 rt -> v_right = new bsetnode(x);
107 return _insert(x, rt -> v_right);
110 Boolean bset::insert(void* x)
113 debug(cerr, "in bsert insert");
114 debug(cerr, int(f_cmp_func_eq));
115 debug(cerr, int(f_cmp_func_ls));
116 debug(cerr, int(this));
118 debug(cerr, int(v_setroot));
121 if ( v_setroot == 0 ) {
122 v_setroot = new bsetnode(x);
126 //debug(cerr, "to bsert _insert");
128 return _insert(x, v_setroot);
132 /////////////////////////////////////////////////////////////
133 // delete the min node from true rooted at rt and return its
135 /////////////////////////////////////////////////////////////
136 void* bset::_remove_min(void* x, bsetnode*& rt)
138 if ( rt -> v_left == 0 ) {
140 return _move(rt, rt -> v_right);
142 void* y = rt -> v_element;
148 return _remove_min(x, rt -> v_left);
151 //**********************************************************************
152 // move the content and pointers of node2 to node1 and remove node2
153 //**********************************************************************
154 void* bset::_move(bsetnode* node1, bsetnode* node2)
156 void* y = node1 -> v_element ;
158 node1 -> v_element = node2 -> v_element ;
159 node2 -> v_element = 0;
160 node1 -> v_left = node2 -> v_left ;
162 node1 -> v_right = node2 -> v_right ;
163 node2 -> v_right = 0;
169 void* bset::_remove(void* x, bsetnode*& rt)
171 if ( (*f_cmp_func_ls)(x, rt -> v_element) == true ) // v_left branch
172 return _remove(x, rt -> v_left);
174 if ( !((*f_cmp_func_eq)(x, rt -> v_element) == true ) ) // v_right branch
175 return _remove(x, rt -> v_right);
177 if ( rt -> v_left == 0 && rt -> v_right == 0 ) { // find the node.
178 void* y = rt -> v_element ;
183 if ( rt -> v_left == 0 ) {
184 return _move(rt, rt -> v_right);
186 if ( rt -> v_right == 0 ) {
187 return _move(rt, rt -> v_left);
189 void* y = rt -> v_element ;
190 rt -> v_element = _remove_min(x, rt -> v_right );
195 void* bset::remove(void* x)
197 if ( v_setroot == 0 )
200 return _remove(x, v_setroot);
203 void* bset::_member(const void* x, bsetnode *rt)
206 //debug(cerr, "member void zeor");
208 } else if ( (*f_cmp_func_eq)(x, rt -> v_element) == true )
209 return rt -> v_element;
210 else if ( (*f_cmp_func_ls)(x, rt -> v_element) == true )
211 return _member(x, rt -> v_left);
213 return _member(x, rt -> v_right);
216 void* bset::member(const void* x)
219 debug(cerr, "member");
220 debug(cerr, int(f_cmp_func_eq));
221 debug(cerr, int(f_cmp_func_ls));
222 debug(cerr, int(this));
224 return _member(x, v_setroot);
227 void bset::apply(app_func_ptr_t f)
229 _apply(v_setroot, f);
232 void bset::_apply(bsetnode* rt, app_func_ptr_t f)
238 _apply( rt -> v_left, f );
239 _apply( rt -> v_right, f );
244 void bset::del_elements(app_func_ptr_t func)
246 _del_elements(v_setroot, func);
250 void bset::_del_elements(bsetnode* rt, app_func_ptr_t func)
254 bsetnode *lt = rt -> v_left;
255 bsetnode *rg = rt -> v_right;
260 func(rt -> v_element);
263 _del_elements( lt, func );
264 _del_elements( rg, func );
268 void bset::unlink_elements()
270 _unlink_elements(v_setroot);
274 void bset::_unlink_elements(bsetnode* rt)
278 bsetnode *lt = rt -> v_left;
279 bsetnode *rg = rt -> v_right;
285 _unlink_elements( lt );
286 _unlink_elements( rg );
290 void bset::_smaller_member(const void* x, bsetnode* rt, void*& answer)
292 if ( rt == 0 ) return;
294 if ( (*f_cmp_func_eq)(x, rt -> v_element) == true ) {
295 answer = rt -> v_element;
299 if ( (*f_cmp_func_ls)(rt -> v_element, x) == true ) {
300 answer = rt -> v_element;
301 _smaller_member(x, rt -> v_right, answer);
304 _smaller_member(x, rt -> v_left, answer);
309 if ( (*f_cmp_func_ls)(x, rt -> v_element) == true ) {
311 return _smaller_member(x, rt -> v_left);
316 if ( (*f_cmp_func_eq)(x, rt -> v_element) == true ||
319 return rt -> v_element;
321 return _smaller_member(x, rt -> v_right);
325 void* bset::_larger_member(const void* x, bsetnode* rt)
327 if ( gt(x, rt -> v_element) == true ) {
329 return _larger_member(x, rt -> v_right);
334 if ( (*f_cmp_func_eq)(x, rt -> v_element) == true ||
337 return rt -> v_element;
339 return _larger_member(x, rt -> v_left);
343 void* bset::smaller_member(const void* x)
346 _smaller_member(x, v_setroot, answer);
350 void* bset::larger_member(const void* x)
352 return _larger_member(x, v_setroot);
355 Boolean bset::gt(const void* x, const void* y)
357 if ( (*f_cmp_func_eq)(x, y) == false &&
358 (*f_cmp_func_ls)(x, y) == false
365 ostream& bset::asciiOut(ostream& out)
367 _asciiOut(out, v_setroot);
371 void bset::_asciiOut(ostream& out, bsetnode* rt)
374 out << long(rt -> v_element);
376 _asciiOut( out, rt -> v_left );
377 _asciiOut( out, rt -> v_right );
381 #ifdef REGRESSION_TEST
383 #include <sys/time.h>
384 #include "utility/pm_random.h"
387 in_bset_test(bset& bst, int* ins, unsigned int& in_cts, int* outs, unsigned int& out_cts, pm_random& rand_gen)
389 if ( in_cts == 0 ) return 0;
391 int k = rand_gen.rand() % in_cts;
393 int_swap(ins[k], ins[in_cts-1]);
395 //cerr << "<-------------- removing " << ins[in_cts-1] << "\n";
396 long j = (long)bst.remove((void*)ins[in_cts-1]);
398 if ( j != ins[in_cts-1] ) {
399 cerr << "can't correctly remove " << ins[in_cts-1] << "\n";
403 if ( bst.member((void*)ins[in_cts-1]) != 0 ) {
404 cerr << "element " << ins[in_cts-1] << " still in the set\n";
408 outs[out_cts++] = ins[in_cts-1];
415 out_bset_test(bset& bst, int* ins, unsigned int& in_cts, int* outs, unsigned int& out_cts,
418 if ( out_cts == 0 ) return 0;
420 int k = rand_gen.rand() % out_cts;
422 int_swap(outs[k], outs[out_cts-1]);
424 if ( bst.member((void*)outs[out_cts-1]) != 0 ) {
425 cerr << "can still find " << outs[out_cts-1] << "\n";
429 //cerr << "--------------> inserting " << outs[out_cts-1] << "\n";
431 bst.insert((void*)outs[out_cts-1]);
433 if ( bst.member((void*)outs[out_cts-1]) == 0 ) {
434 cerr << "can't find " << outs[out_cts-1] << "\n";
438 ins[in_cts++] = outs[out_cts-1];
446 bset_test(unsigned int in_cts, unsigned int out_cts, pm_random& rand_gen, unsigned int cycles)
450 int* ins = new int[in_cts+out_cts];
451 int* outs = new int[in_cts+out_cts];
453 bset bst(void_eq, void_ls);
455 for ( int i=0; i<in_cts; i++ ) {
457 bst.insert((void*)ins[i]);
460 for ( i=0; i<out_cts; i++ ) {
461 outs[i] = i + in_cts + 1;
464 for ( i=0; i<cycles; i++ ) {
465 if ( rand_gen.rand_01() > 0.5 )
466 ok |= in_bset_test(bst, ins, in_cts, outs, out_cts, rand_gen);
468 ok |= out_bset_test(bst, ins, in_cts, outs, out_cts, rand_gen);