Deleted Added
full compact
hash.c (178529) hash.c (178546)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *

--- 36 unchanged lines hidden (view full) ---

45 list_t **h_buckets;
46
47 int (*h_hashfn)(int, void *);
48 int (*h_cmp)(void *, void *);
49};
50
51struct hash_data {
52 hash_t *hd_hash;
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *

--- 36 unchanged lines hidden (view full) ---

45 list_t **h_buckets;
46
47 int (*h_hashfn)(int, void *);
48 int (*h_cmp)(void *, void *);
49};
50
51struct hash_data {
52 hash_t *hd_hash;
53 int (*hd_fun)();
53 int (*hd_fun)(void *, void *);
54 void *hd_key;
55 void *hd_private;
56
57 void *hd_ret;
58};
59
60static int
54 void *hd_key;
55 void *hd_private;
56
57 void *hd_ret;
58};
59
60static int
61hash_def_hash(int nbuckets, uintptr_t data)
61hash_def_hash(int nbuckets, void *arg)
62{
62{
63 uintptr_t data = (uintptr_t) arg;
63 return (data % nbuckets);
64}
65
66static int
64 return (data % nbuckets);
65}
66
67static int
67hash_def_cmp(uintptr_t d1, uintptr_t d2)
68hash_def_cmp(void *d1, void *d2)
68{
69 return (d1 != d2);
70}
71
72
73int
74hash_name(int nbuckets, const char *name)
75{

--- 15 unchanged lines hidden (view full) ---

91hash_t *
92hash_new(int nbuckets, int (*hashfn)(int, void *), int (*cmp)(void *, void *))
93{
94 hash_t *hash;
95
96 hash = xmalloc(sizeof (hash_t));
97 hash->h_buckets = xcalloc(sizeof (list_t *) * nbuckets);
98 hash->h_nbuckets = nbuckets;
69{
70 return (d1 != d2);
71}
72
73
74int
75hash_name(int nbuckets, const char *name)
76{

--- 15 unchanged lines hidden (view full) ---

92hash_t *
93hash_new(int nbuckets, int (*hashfn)(int, void *), int (*cmp)(void *, void *))
94{
95 hash_t *hash;
96
97 hash = xmalloc(sizeof (hash_t));
98 hash->h_buckets = xcalloc(sizeof (list_t *) * nbuckets);
99 hash->h_nbuckets = nbuckets;
99 hash->h_hashfn = hashfn ? hashfn : (int (*)())hash_def_hash;
100 hash->h_cmp = cmp ? cmp : (int (*)())hash_def_cmp;
100 hash->h_hashfn = hashfn ? hashfn : hash_def_hash;
101 hash->h_cmp = cmp ? cmp : hash_def_cmp;
101
102 return (hash);
103}
104
105void
106hash_add(hash_t *hash, void *key)
107{
108 int bucket = hash->h_hashfn(hash->h_nbuckets, key);

--- 10 unchanged lines hidden (view full) ---

119
120void
121hash_merge(hash_t *to, hash_t *from)
122{
123 (void) hash_iter(from, hash_add_cb, to);
124}
125
126static int
102
103 return (hash);
104}
105
106void
107hash_add(hash_t *hash, void *key)
108{
109 int bucket = hash->h_hashfn(hash->h_nbuckets, key);

--- 10 unchanged lines hidden (view full) ---

120
121void
122hash_merge(hash_t *to, hash_t *from)
123{
124 (void) hash_iter(from, hash_add_cb, to);
125}
126
127static int
127hash_remove_cb(void *key1, void *key2, hash_t *hash)
128hash_remove_cb(void *key1, void *key2, void *arg)
128{
129{
130 hash_t *hash = arg;
129 return (hash->h_cmp(key1, key2));
130}
131
132void
133hash_remove(hash_t *hash, void *key)
134{
135 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
136
137 (void) list_remove(&hash->h_buckets[bucket], key,
131 return (hash->h_cmp(key1, key2));
132}
133
134void
135hash_remove(hash_t *hash, void *key)
136{
137 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
138
139 (void) list_remove(&hash->h_buckets[bucket], key,
138 (int (*)())hash_remove_cb, hash);
140 hash_remove_cb, hash);
139}
140
141int
142hash_match(hash_t *hash, void *key, int (*fun)(void *, void *),
143 void *private)
144{
145 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
146
147 return (list_iter(hash->h_buckets[bucket], fun, private) < 0);
148}
149
150static int
141}
142
143int
144hash_match(hash_t *hash, void *key, int (*fun)(void *, void *),
145 void *private)
146{
147 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
148
149 return (list_iter(hash->h_buckets[bucket], fun, private) < 0);
150}
151
152static int
151hash_find_list_cb(void *node, struct hash_data *hd)
153hash_find_list_cb(void *node, void *arg)
152{
154{
155 struct hash_data *hd = arg;
153 int cbrc;
154 int rc = 0;
155
156 if (hd->hd_hash->h_cmp(hd->hd_key, node) == 0) {
157 if ((cbrc = hd->hd_fun(node, hd->hd_private)) < 0)
158 return (cbrc);
159 rc += cbrc;
160 }

--- 8 unchanged lines hidden (view full) ---

169 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
170 struct hash_data hd;
171
172 hd.hd_hash = hash;
173 hd.hd_fun = fun;
174 hd.hd_key = key;
175 hd.hd_private = private;
176
156 int cbrc;
157 int rc = 0;
158
159 if (hd->hd_hash->h_cmp(hd->hd_key, node) == 0) {
160 if ((cbrc = hd->hd_fun(node, hd->hd_private)) < 0)
161 return (cbrc);
162 rc += cbrc;
163 }

--- 8 unchanged lines hidden (view full) ---

172 int bucket = hash->h_hashfn(hash->h_nbuckets, key);
173 struct hash_data hd;
174
175 hd.hd_hash = hash;
176 hd.hd_fun = fun;
177 hd.hd_key = key;
178 hd.hd_private = private;
179
177 return (list_iter(hash->h_buckets[bucket], (int (*)())hash_find_list_cb,
180 return (list_iter(hash->h_buckets[bucket], hash_find_list_cb,
178 &hd));
179}
180
181/* stop on first match */
182static int
181 &hd));
182}
183
184/* stop on first match */
185static int
183hash_find_first_cb(void *node, struct hash_data *hd)
186hash_find_first_cb(void *node, void *arg)
184{
187{
188 struct hash_data *hd = arg;
185 if (hd->hd_hash->h_cmp(hd->hd_key, node) == 0) {
186 hd->hd_ret = node;
187 return (-1);
188 }
189
190 return (0);
191}
192
193int
194hash_find(hash_t *hash, void *key, void **value)
195{
196 int ret;
197 struct hash_data hd;
198
199 hd.hd_hash = hash;
200 hd.hd_fun = hash_find_first_cb;
201 hd.hd_key = key;
202
189 if (hd->hd_hash->h_cmp(hd->hd_key, node) == 0) {
190 hd->hd_ret = node;
191 return (-1);
192 }
193
194 return (0);
195}
196
197int
198hash_find(hash_t *hash, void *key, void **value)
199{
200 int ret;
201 struct hash_data hd;
202
203 hd.hd_hash = hash;
204 hd.hd_fun = hash_find_first_cb;
205 hd.hd_key = key;
206
203 ret = hash_match(hash, key, (int (*)())hash_find_first_cb, &hd);
207 ret = hash_match(hash, key, hash_find_first_cb, &hd);
204 if (ret && value)
205 *value = hd.hd_ret;
206
207 return (ret);
208}
209
210int
211hash_iter(hash_t *hash, int (*fun)(void *, void *), void *private)

--- 76 unchanged lines hidden ---
208 if (ret && value)
209 *value = hd.hd_ret;
210
211 return (ret);
212}
213
214int
215hash_iter(hash_t *hash, int (*fun)(void *, void *), void *private)

--- 76 unchanged lines hidden ---