| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2001-2006 OmniTI, Inc. All rights reserved |
|---|
| 3 |
* |
|---|
| 4 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
* modification, are permitted provided that the following conditions |
|---|
| 6 |
* are met: |
|---|
| 7 |
* 1. Redistributions of source code must retain the above copyright |
|---|
| 8 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer in the |
|---|
| 11 |
* documentation and/or other materials provided with the distribution. |
|---|
| 12 |
* 3. The name of the author may not be used to endorse or promote products |
|---|
| 13 |
* derived from this software without specific prior written permission. |
|---|
| 14 |
* |
|---|
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 17 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 18 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 19 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 20 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 21 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 22 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 23 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 24 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 25 |
*/ |
|---|
| 26 |
|
|---|
| 27 |
#ifndef _ECHASH_H |
|---|
| 28 |
#define _ECHASH_H |
|---|
| 29 |
|
|---|
| 30 |
#ifndef API_EXPORT |
|---|
| 31 |
#define API_EXPORT(a) a |
|---|
| 32 |
#endif |
|---|
| 33 |
|
|---|
| 34 |
typedef void (*ECHashFreeFunc)(void *); |
|---|
| 35 |
|
|---|
| 36 |
typedef struct _ec_hash_bucket { |
|---|
| 37 |
const char *k; |
|---|
| 38 |
int klen; |
|---|
| 39 |
void *data; |
|---|
| 40 |
struct _ec_hash_bucket *next; |
|---|
| 41 |
} ec_hash_bucket; |
|---|
| 42 |
|
|---|
| 43 |
typedef struct { |
|---|
| 44 |
ec_hash_bucket **buckets; |
|---|
| 45 |
unsigned int table_size; |
|---|
| 46 |
unsigned int initval; |
|---|
| 47 |
unsigned int num_used_buckets; |
|---|
| 48 |
unsigned int size; |
|---|
| 49 |
} ec_hash_table; |
|---|
| 50 |
|
|---|
| 51 |
#define ECHASH_EMPTY { NULL, 0, 0, 0, 0 } |
|---|
| 52 |
|
|---|
| 53 |
typedef struct { |
|---|
| 54 |
void *p2; |
|---|
| 55 |
int p1; |
|---|
| 56 |
} ec_hash_iter; |
|---|
| 57 |
|
|---|
| 58 |
#define EC_HASH_ITER_ZERO { NULL, 0 } |
|---|
| 59 |
|
|---|
| 60 |
API_EXPORT(void) echash_init(ec_hash_table *h); |
|---|
| 61 |
API_EXPORT(int) echash_store(ec_hash_table *h, const char *k, int klen, void *data); |
|---|
| 62 |
API_EXPORT(int) echash_replace(ec_hash_table *h, const char *k, |
|---|
| 63 |
int klen, void *data, |
|---|
| 64 |
ECHashFreeFunc keyfree, ECHashFreeFunc datafree); |
|---|
| 65 |
API_EXPORT(int) echash_retrieve(ec_hash_table *h, const char *k, int klen, void **data); |
|---|
| 66 |
API_EXPORT(int) echash_delete(ec_hash_table *h, const char *k, int klen, |
|---|
| 67 |
ECHashFreeFunc keyfree, ECHashFreeFunc datafree); |
|---|
| 68 |
API_EXPORT(void) echash_delete_all(ec_hash_table *h, ECHashFreeFunc keyfree, |
|---|
| 69 |
ECHashFreeFunc datafree); |
|---|
| 70 |
API_EXPORT(void) echash_destroy(ec_hash_table *h, ECHashFreeFunc keyfree, |
|---|
| 71 |
ECHashFreeFunc datafree); |
|---|
| 72 |
|
|---|
| 73 |
/* This is an iterator and requires the hash to not be written to during the |
|---|
| 74 |
iteration process. |
|---|
| 75 |
To use: |
|---|
| 76 |
ec_hash_iter iter = EC_HASH_ITER_ZERO; |
|---|
| 77 |
|
|---|
| 78 |
const char *k; |
|---|
| 79 |
int klen; |
|---|
| 80 |
void *data; |
|---|
| 81 |
|
|---|
| 82 |
while(echash_next(h, &iter, &k, &klen, &data)) { |
|---|
| 83 |
.... use k, klen and data .... |
|---|
| 84 |
} |
|---|
| 85 |
*/ |
|---|
| 86 |
API_EXPORT(int) echash_next(ec_hash_table *h, ec_hash_iter *iter, |
|---|
| 87 |
const char **k, int *klen, void **data); |
|---|
| 88 |
API_EXPORT(int) echash_firstkey(ec_hash_table *h, const char **k, int *klen); |
|---|
| 89 |
API_EXPORT(int) echash_nextkey(ec_hash_table *h, const char **k, int *klen, char *lk, int lklen); |
|---|
| 90 |
|
|---|
| 91 |
/* This function serves no real API use sans calculating expected buckets |
|---|
| 92 |
for keys (or extending the hash... which is unsupported) */ |
|---|
| 93 |
API_EXPORT(unsigned int) echash__hash(const char *k, unsigned int length, |
|---|
| 94 |
unsigned int initval); |
|---|
| 95 |
API_EXPORT(ec_hash_bucket *) echash__new_bucket(const char *k, int klen, |
|---|
| 96 |
void *data); |
|---|
| 97 |
API_EXPORT(void) echash__rebucket(ec_hash_table *h, int newsize); |
|---|
| 98 |
#endif |
|---|