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
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $XConsortium: tt_object_table.C /main/3 1995/10/23 10:42:52 rswiston $
29 * @(#)tt_object_table.C 1.11 @(#)
33 * Generic hash table implementation
35 * Copyright (c) 1990 by Sun Microsystems, Inc.
38 #include "tt_options.h"
39 #if defined(OPT_BUG_SUNOS_4) && defined(__GNUG__)
40 // The GNU headers for 4.x do not fix malloc.h, so abort its inclusion
44 #include <util/tt_object_table.h>
51 _Tt_object_table(int n)
54 buckets = (_Tt_object_list_ptr *)calloc(n,
55 sizeof(_Tt_object_list_ptr));
62 for (int i = 0; i < num_buckets; i++) {
63 buckets[i] = (_Tt_object_list *)0;
65 (void)free((MALLOCTYPE *)buckets);
68 _Tt_object_ptr & _Tt_object_table::
69 lookup(const _Tt_string &k) const
71 _Tt_object_list_ptr bl = buckets[k.hash(num_buckets)];
73 if (!bl.is_null() && bl->count()!=0) {
74 _Tt_object_list_cursor b(bl);
76 if ((*_getkey)(*b) == k) {
81 return _Tt_object::null_ptr();
84 int _Tt_object_table::
85 lookup(const _Tt_string &k, _Tt_object_ptr &obj) const
87 _Tt_object_list_ptr bl = buckets[k.hash(num_buckets)];
89 if (bl.is_null() || bl->count()==0) {
90 obj = (_Tt_object *)0;
93 _Tt_object_list_cursor b(bl);
95 if ((*_getkey)(*b) == k) {
100 obj = (_Tt_object *)0;
104 void _Tt_object_table::
105 insert(_Tt_object_ptr &n)
107 _Tt_string key((*_getkey)(n));
108 int bucket_no = key.hash(num_buckets);
110 if (buckets[bucket_no].is_null()) {
111 buckets[bucket_no] = new _Tt_object_list;
113 buckets[bucket_no]->push(n);
118 void _Tt_object_table::
119 remove(const _Tt_string &k)
121 _Tt_object_list_cursor b(buckets[k.hash(num_buckets)]);
123 if ((*_getkey)(*b) == k) {
132 void _Tt_object_table::
136 for (i=0;i<num_buckets;++i) {
137 if (!buckets[i].is_null()) {
138 buckets[i] = (_Tt_object_list *)0;
144 void _Tt_object_table::
145 print(_Tt_object_printfn print_elt, const _Tt_ostream &os) const
147 _Tt_object_table_ptr t = (_Tt_object_table *)this;
148 _Tt_object_table_cursor c(t);
150 (*print_elt)(os, (*c).c_pointer());
156 * XDR encoding and decoding function for tables
158 bool_t _Tt_object_table::
159 xdr(XDR *xdrs, _Tt_new_xdrfn xdrfn, _Tt_object *(*make_new)())
161 if (! xdr_int(xdrs, &_count)) {
164 if (xdrs->x_op == XDR_ENCODE) {
165 _Tt_object_table_ptr t = this;
166 _Tt_object_table_cursor cursor(t);
167 while (cursor.next()) {
168 if ((*cursor).is_null()) {
171 if (! (*xdrfn)(xdrs, (*cursor).c_pointer())) {
182 for (i=len; i > 0; --i) {
184 if (! (*xdrfn)(xdrs, ptr.c_pointer())) {
188 int found = lookup((*_getkey)(ptr),ptr2);
190 remove((*_getkey)(ptr));
205 _Tt_object_table_cursor::
206 _Tt_object_table_cursor()
208 table = (_Tt_object_table *)0;
213 _Tt_object_table_cursor::
214 _Tt_object_table_cursor(const _Tt_object_table_cursor &c)
217 listcursor = c.listcursor;
218 current_bucket = c.current_bucket;
221 _Tt_object_table_cursor::
222 _Tt_object_table_cursor(const _Tt_object_table_ptr &l)
228 _Tt_object_table_cursor::
229 ~_Tt_object_table_cursor()
233 _Tt_object_table_cursor & _Tt_object_table_cursor::
240 _Tt_object_table_cursor & _Tt_object_table_cursor::
241 reset(_Tt_object_table_ptr &l)
248 _Tt_object_ptr & _Tt_object_table_cursor::
251 if (current_bucket == -1) {
252 return _Tt_object::null_ptr();
258 _Tt_object_ptr & _Tt_object_table_cursor::
264 int _Tt_object_table_cursor::
267 if (current_bucket == -1) {
269 listcursor.reset(table->buckets[current_bucket]);
271 while (!listcursor.next()) {
273 if (current_bucket >= table->num_buckets) {
277 listcursor.reset(table->buckets[current_bucket]);
283 int _Tt_object_table_cursor::
286 if (current_bucket == -1) {
287 current_bucket = table->num_buckets-1;
288 listcursor.reset(table->buckets[current_bucket]);
290 while (!listcursor.prev()) {
292 if (current_bucket<0) {
296 listcursor.reset(table->buckets[current_bucket]);
302 int _Tt_object_table_cursor::
305 if (current_bucket==-1) return 0;
306 return listcursor.is_valid();
308 implement_ptr_to(_Tt_object_table)