1 /****************************************************************************
2     Copyright (C) 1987-2015 by Jeffery P. Hansen
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 ****************************************************************************/
18 /*
19     Data structure and initilization for the hash table.  The hash table is
20     used to look up circuit elements by name.  This is used durring the download
21     phase and when setting and deleting scope probes.
22 */
23 
24 #ifndef __hash_h
25 #define __hash_h
26 
27 #include "config.h"
28 
29 #if HAVE_STDINT_H
30 #include <stdint.h>
31 #endif
32 
33 #include "misc.h"
34 
35 #define INITIAL_HASHSIZE	32
36 #define HASH_MAXLOAD		4
37 
38 __BEGIN_DECLS
39 
40 union hash_key {
41   char *s;
42   intptr_t d;
43   void *p;
44 };
45 
46 typedef struct hash_elem_str {
47   union			hash_key key;
48   uintptr_t		hashcode;
49   void			*value;
50   struct		hash_elem_str *next;
51 } HashElem;
52 
53 typedef struct hash_str {
54   void		*vtable;	/* Memory allocator function table */
55   unsigned	size;		/* Number of hash buckets (must be power of 2) */
56   uintptr_t	mask;		/* Mask for hash addressed (size-1) */
57   unsigned	num;		/* Number of elements in hash */
58   int		loop_ok;	/* OK to use Hash_next() */
59   HashElem	**elems;	/* Elements buffer */
60 } Hash;
61 
62 /* String hash */
63 typedef Hash SHash;
64 /* Number hash */
65 typedef Hash NHash;
66 /* Pointer hash */
67 typedef Hash PHash;
68 
69 typedef void HashElemDelFunc(HashElem *, Hash *);
70 
71 uintptr_t computestrhash(const char *);
72 
73 void SHashElem_uninit(HashElem *, Hash *);
74 
75 /*******************************************************************************
76  * Create new Hash
77  *
78  * param int use_ob Usage of the undoable objects flag
79  *
80  ******************************************************************************/
81 Hash *new_Hash(int);
82 
83 void Hash_init(Hash *, int);
84 
85 void delete_Hash(Hash *, HashElemDelFunc *);
86 
87 void Hash_uninit(Hash *, HashElemDelFunc *);
88 
89 HashElem *Hash_first(Hash *);
90 
91 HashElem *Hash_next(Hash *, HashElem *);
92 
93 void Hash_flush(Hash * H, HashElemDelFunc * hdel);
94 
95 void Hash_resize(Hash * H, int reqSize);
96 
97 void *SHash_find(Hash *, const char *);
98 
99 #define HashElem_obj(E)		(E)->value
100 #define SHashElem_key(E)	(E)->key.s
101 #define NHashElem_key(E)	(E)->key.d
102 #define PHashElem_key(E)	(E)->key.p
103 #define Hash_numElems(H)	(H)->num
104 
105 #define new_SHash()	((SHash*)new_Hash(1))
106 #define new_SHash_noob()	((SHash*)new_Hash(0))
107 #define delete_SHash(H)	delete_Hash(H,SHashElem_uninit)
108 #define SHash_init(H)	Hash_init(H,1)
109 #define SHash_uninit(H)	Hash_uninit(H,SHashElem_uninit)
110 int SHash_insert(Hash*,const char*,void*);
111 int SHash_replace(Hash*,const char*,void*);
112 int SHash_remove(Hash*,const char*);
113 #define SHash_flush(H)	Hash_flush(H,SHashElem_uninit)
114 #define SHash_resize(H, reqSize) Hash_resize(H,reqSize)
115 
116 #define new_NHash()	((NHash*)new_Hash(1))
117 #define new_NHash_noob()	((NHash*)new_Hash(0))
118 #define delete_NHash(H)	delete_Hash(H,0)
119 #define NHash_init(H)	Hash_init(H,1)
120 #define NHash_uninit(H)	Hash_uninit(H,0)
121 void *NHash_find(Hash*,intptr_t);
122 int NHash_insert(Hash*,intptr_t,void*);
123 int NHash_replace(Hash*,intptr_t,void*);
124 int NHash_remove(Hash*,intptr_t);
125 #define NHash_flush(H)	Hash_flush(H,0)
126 #define NHash_resize(H, reqSize) Hash_resize(H,reqSize)
127 
128 #define new_PHash()	((PHash*)new_Hash(1))
129 #define new_PHash_noob() ((PHash*)new_Hash(0))
130 #define delete_PHash(H)	delete_Hash(H,0)
131 #define PHash_init(H)	Hash_init(H,1)
132 #define PHash_init_noob(H)	Hash_init(H,0)
133 #define PHash_uninit(H)	Hash_uninit(H,0)
134 #define PHash_find(H,P) NHash_find(H,(intptr_t)(P))
135 #define PHash_insert(H,P,O) NHash_insert(H,(intptr_t)(P),(void*)O)
136 #define PHash_remove(H,P) NHash_remove(H,(intptr_t)(P))
137 #define PHash_flush(H)	Hash_flush(H,0)
138 #define PHash_resize(H, reqSize) Hash_resize(H,reqSize)
139 
140 __END_DECLS
141 
142 #endif // __hash_h
143