]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/hashtab.c
2 * Copyright (C) 1989-94 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 AtomMake(char *name
, void *data
) /* makes an atom */
43 /* char *name; */ /* WARNING: is just pointed to */
46 xpmHashAtom object
= (xpmHashAtom
) XpmMalloc(sizeof(struct _xpmHashAtom
));
55 /************************\
57 * hash table routines *
59 \************************/
62 * Hash function definition:
63 * HASH_FUNCTION: hash function, hash = hashcode, hp = pointer on char,
64 * hash2 = temporary for hashcode.
65 * INITIAL_TABLE_SIZE in slots
66 * HASH_TABLE_GROWS how hash table grows.
69 /* Mock lisp function */
70 #define HASH_FUNCTION hash = (hash << 5) - hash + *hp++;
71 /* #define INITIAL_HASH_SIZE 2017 */
72 #define INITIAL_HASH_SIZE 256 /* should be enough for colors */
73 #define HASH_TABLE_GROWS size = size * 2;
75 /* aho-sethi-ullman's HPJ (sizes should be primes)*/
77 #define HASH_FUNCTION hash <<= 4; hash += *hp++; \
78 if(hash2 = hash & 0xf0000000) hash ^= (hash2 >> 24) ^ hash2;
79 #define INITIAL_HASH_SIZE 4095 /* should be 2^n - 1 */
80 #define HASH_TABLE_GROWS size = size << 1 + 1;
83 /* GNU emacs function */
85 #define HASH_FUNCTION hash = (hash << 3) + (hash >> 28) + *hp++;
86 #define INITIAL_HASH_SIZE 2017
87 #define HASH_TABLE_GROWS size = size * 2;
90 /* end of hash functions */
93 * The hash table is used to store atoms via their NAME:
95 * NAME --hash--> ATOM |--name--> "foo"
96 * |--data--> any value which has to be stored
101 * xpmHashSlot gives the slot (pointer to xpmHashAtom) of a name
102 * (slot points to NULL if it is not defined)
107 xpmHashSlot(xpmHashTable
*table
, char *s
)
109 xpmHashAtom
*atomTable
= table
->atomTable
;
116 while (*hp
) { /* computes hash function */
119 p
= atomTable
+ hash
% table
->size
;
122 if (ns
[0] == s
[0] && strcmp(ns
, s
) == 0)
126 p
= atomTable
+ table
->size
- 1;
132 HashTableGrows(xpmHashTable
*table
)
134 xpmHashAtom
*atomTable
= table
->atomTable
;
135 int size
= table
->size
;
143 table
->limit
= size
/ 3;
144 atomTable
= (xpmHashAtom
*) XpmMalloc(size
* sizeof(*atomTable
));
146 return (XpmNoMemory
);
147 table
->atomTable
= atomTable
;
148 for (p
= atomTable
+ size
; p
> atomTable
;)
150 for (i
= 0, p
= t
; i
< oldSize
; i
++, p
++)
152 xpmHashAtom
*ps
= xpmHashSlot(table
, (*p
)->name
);
161 * xpmHashIntern(table, name, data)
162 * an xpmHashAtom is created if name doesn't exist, with the given data.
166 xpmHashIntern(xpmHashTable
*table
, char *tag
, void *data
)
170 if (!*(slot
= xpmHashSlot(table
, tag
))) {
171 /* undefined, make a new atom with the given data */
172 if (!(*slot
= AtomMake(tag
, data
)))
173 return (XpmNoMemory
);
174 if (table
->used
>= table
->limit
) {
177 if ((ErrorStatus
= HashTableGrows(table
)) != XpmSuccess
)
178 return (ErrorStatus
);
188 * must be called before allocating any atom
192 xpmHashTableInit(xpmHashTable
*table
)
195 xpmHashAtom
*atomTable
;
197 table
->size
= INITIAL_HASH_SIZE
;
198 table
->limit
= table
->size
/ 3;
200 atomTable
= (xpmHashAtom
*) XpmMalloc(table
->size
* sizeof(*atomTable
));
202 return (XpmNoMemory
);
203 for (p
= atomTable
+ table
->size
; p
> atomTable
;)
205 table
->atomTable
= atomTable
;
210 * frees a hashtable and all the stored atoms
214 xpmHashTableFree(xpmHashTable
*table
)
217 xpmHashAtom
*atomTable
= table
->atomTable
;
219 for (p
= atomTable
+ table
->size
; p
> atomTable
;)
223 table
->atomTable
= NULL
;