]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/hashtab.c
2 * Copyright (C) 1989-95 GROUPE BULL
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
26 /*****************************************************************************\
31 * Developed by Arnaud Le Hors *
32 * this originaly comes from Colas Nahaboo as a part of Wool *
34 \*****************************************************************************/
38 LFUNC(AtomMake
, xpmHashAtom
, (char *name
, void *data
));
39 LFUNC(HashTableGrows
, int, (xpmHashTable
* table
));
42 /* Visual Age cannot deal with old, non-ansi, code */
44 AtomMake(char* name
, void* data
) /* makes an atom */
47 AtomMake(name
, data
) /* makes an atom */
48 char *name
; /* WARNING: is just pointed to */
52 xpmHashAtom object
= (xpmHashAtom
) XpmMalloc(sizeof(struct _xpmHashAtom
));
61 /************************\
63 * hash table routines *
65 \************************/
68 * Hash function definition:
69 * HASH_FUNCTION: hash function, hash = hashcode, hp = pointer on char,
70 * hash2 = temporary for hashcode.
71 * INITIAL_TABLE_SIZE in slots
72 * HASH_TABLE_GROWS how hash table grows.
75 /* Mock lisp function */
76 #define HASH_FUNCTION hash = (hash << 5) - hash + *hp++;
77 /* #define INITIAL_HASH_SIZE 2017 */
78 #define INITIAL_HASH_SIZE 256 /* should be enough for colors */
79 #define HASH_TABLE_GROWS size = size * 2;
81 /* aho-sethi-ullman's HPJ (sizes should be primes)*/
83 #define HASH_FUNCTION hash <<= 4; hash += *hp++; \
84 if(hash2 = hash & 0xf0000000) hash ^= (hash2 >> 24) ^ hash2;
85 #define INITIAL_HASH_SIZE 4095 /* should be 2^n - 1 */
86 #define HASH_TABLE_GROWS size = size << 1 + 1;
89 /* GNU emacs function */
91 #define HASH_FUNCTION hash = (hash << 3) + (hash >> 28) + *hp++;
92 #define INITIAL_HASH_SIZE 2017
93 #define HASH_TABLE_GROWS size = size * 2;
96 /* end of hash functions */
99 * The hash table is used to store atoms via their NAME:
101 * NAME --hash--> ATOM |--name--> "foo"
102 * |--data--> any value which has to be stored
107 * xpmHashSlot gives the slot (pointer to xpmHashAtom) of a name
108 * (slot points to NULL if it is not defined)
113 /* Visual Age cannot deal with old, non-ansi, code */
114 xpmHashAtom
* xpmHashSlot(xpmHashTable
* table
, char* s
)
117 xpmHashSlot(table
, s
)
122 xpmHashAtom
*atomTable
= table
->atomTable
;
129 while (*hp
) { /* computes hash function */
132 p
= atomTable
+ hash
% table
->size
;
135 if (ns
[0] == s
[0] && strcmp(ns
, s
) == 0)
139 p
= atomTable
+ table
->size
- 1;
145 /* Visual Age cannot deal with old, non-ansi, code */
146 static int HashTableGrows(xpmHashTable
* table
)
149 HashTableGrows(table
)
153 xpmHashAtom
*atomTable
= table
->atomTable
;
154 int size
= table
->size
;
162 table
->limit
= size
/ 3;
163 atomTable
= (xpmHashAtom
*) XpmMalloc(size
* sizeof(*atomTable
));
165 return (XpmNoMemory
);
166 table
->atomTable
= atomTable
;
167 for (p
= atomTable
+ size
; p
> atomTable
;)
169 for (i
= 0, p
= t
; i
< oldSize
; i
++, p
++)
171 xpmHashAtom
*ps
= xpmHashSlot(table
, (*p
)->name
);
180 * xpmHashIntern(table, name, data)
181 * an xpmHashAtom is created if name doesn't exist, with the given data.
185 /* Visual Age cannot deal with old, non-ansi, code */
186 int xpmHashIntern(xpmHashTable
* table
, char* tag
, void* data
)
189 xpmHashIntern(table
, tag
, data
)
197 if (!*(slot
= xpmHashSlot(table
, tag
))) {
198 /* undefined, make a new atom with the given data */
199 if (!(*slot
= AtomMake(tag
, data
)))
200 return (XpmNoMemory
);
201 if (table
->used
>= table
->limit
) {
204 if ((ErrorStatus
= HashTableGrows(table
)) != XpmSuccess
)
205 return (ErrorStatus
);
215 * must be called before allocating any atom
219 /* Visual Age cannot deal with old, non-ansi, code */
220 int xpmHashTableInit(xpmHashTable
* table
)
223 xpmHashTableInit(table
)
228 xpmHashAtom
*atomTable
;
230 table
->size
= INITIAL_HASH_SIZE
;
231 table
->limit
= table
->size
/ 3;
233 atomTable
= (xpmHashAtom
*) XpmMalloc(table
->size
* sizeof(*atomTable
));
235 return (XpmNoMemory
);
236 for (p
= atomTable
+ table
->size
; p
> atomTable
;)
238 table
->atomTable
= atomTable
;
243 * frees a hashtable and all the stored atoms
247 /* Visual Age cannot deal with old, non-ansi, code */
249 xpmHashTableFree(xpmHashTable
* table
)
252 xpmHashTableFree(table
)
257 xpmHashAtom
*atomTable
= table
->atomTable
;
261 for (p
= atomTable
+ table
->size
; p
> atomTable
;)
265 table
->atomTable
= NULL
;