1 |
4 |
atypic |
/*
|
2 |
|
|
* Copyright (c) 2007 Eirik A. Nygaard <eirikald@pvv.ntnu.no>
|
3 |
|
|
*
|
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:
|
10 |
|
|
*
|
11 |
|
|
* The above copyright notice and this permission notice shall be included in
|
12 |
|
|
* all copies or substantial portions of the Software.
|
13 |
|
|
*
|
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 THE
|
17 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
19 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
20 |
|
|
* IN THE SOFTWARE.
|
21 |
|
|
*
|
22 |
|
|
*/
|
23 |
|
|
|
24 |
|
|
#ifndef _HASH_H_
|
25 |
|
|
#define _HASH_H_
|
26 |
|
|
|
27 |
|
|
typedef unsigned long hash;
|
28 |
|
|
|
29 |
|
|
typedef hash (*hash_hash)(const void *key);
|
30 |
|
|
typedef hash (*hash_cmp)(const void *k1, const void *k2);
|
31 |
|
|
|
32 |
|
|
struct hash_bucket {
|
33 |
|
|
void *key;
|
34 |
|
|
void *value;
|
35 |
|
|
hash hash;
|
36 |
|
|
} *h_bucket;
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
typedef struct {
|
40 |
|
|
struct hash_bucket *h_bucket;
|
41 |
|
|
hash_hash h_hash;
|
42 |
|
|
hash_cmp h_cmp;
|
43 |
|
|
unsigned long h_size, h_used;
|
44 |
|
|
float h_threshold;
|
45 |
|
|
} hash_t;
|
46 |
|
|
|
47 |
|
|
hash_t *_hash_create(hash_cmp cmp, hash_hash hash, unsigned long size, float threshold);
|
48 |
|
|
void hash_destroy(hash_t *h, int freevalue);
|
49 |
|
|
int _hash_insert(hash_t *h, void *key, void *value);
|
50 |
|
|
int _hash_remove(hash_t *h, void *key, int freevalue);
|
51 |
|
|
void *_hash_lookup(hash_t *h, const void *key);
|
52 |
|
|
|
53 |
|
|
#define HASH_DECLARE(name, hcmp, hhash, keytype, valuetype) \
|
54 |
|
|
hash_t * name ## _create (hash size, float threshold) { return _hash_create((hash_cmp)hcmp, (hash_hash)hhash, size, threshold); } \
|
55 |
|
|
int name ## _insert (hash_t *h, keytype * key, valuetype * value) { return _hash_insert(h, key, value); } \
|
56 |
|
|
int name ## _remove (hash_t *h, keytype * key, int freevalue) { return _hash_remove(h, key, freevalue); } \
|
57 |
|
|
valuetype * name ## _lookup (hash_t *h, const keytype * key) { return _hash_lookup(h, key); }
|
58 |
|
|
|
59 |
|
|
#define HASH_DEFINE(name, hcmp, hhash, keytype, valuetype) \
|
60 |
|
|
hash_t * name ## _create (hash size, float threshold); \
|
61 |
|
|
int name ## _insert (hash_t *h, keytype * key, valuetype * value); \
|
62 |
|
|
int name ## _remove (hash_t *h, keytype * key, int freevalue); \
|
63 |
|
|
valuetype * name ## _lookup (hash_t *h, const keytype * key);
|
64 |
|
|
|
65 |
|
|
#endif /* _HASH_H_ */
|