2 * Copyright (C) 2006-2010 B.A.T.M.A.N. contributors:
4 * Simon Wunderlich, Marek Lindner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #ifndef _BATMAN_HASH_H
23 #define _BATMAN_HASH_H
24 #define HASHIT(name) struct hash_it_t name = { \
25 .index = -1, .bucket = NULL, \
26 .prev_bucket = NULL, \
27 .first_bucket = NULL }
30 typedef int (*hashdata_compare_cb)(void *, void *);
31 typedef int (*hashdata_choose_cb)(void *, int);
32 typedef void (*hashdata_free_cb)(void *);
35 void *data; /* pointer to the data */
36 struct element_t *next; /* overflow bucket pointer */
41 struct element_t *bucket;
42 struct element_t *prev_bucket;
43 struct element_t **first_bucket;
47 struct element_t **table; /* the hashtable itself, with the buckets */
48 int elements; /* number of elements registered */
49 int size; /* size of hashtable */
50 hashdata_compare_cb compare;/* callback to a compare function. should
51 * compare 2 element datas for their keys,
52 * return 0 if same and not 0 if not
54 hashdata_choose_cb choose; /* the hashfunction, should return an index
55 * based on the key in the data of the first
56 * argument and the size the second */
60 void hash_init(struct hashtable_t *hash);
62 /* allocates and clears the hash */
63 struct hashtable_t *hash_new(int size, hashdata_compare_cb compare,
64 hashdata_choose_cb choose);
66 /* remove bucket (this might be used in hash_iterate() if you already found the
67 * bucket you want to delete and don't need the overhead to find it again with
68 * hash_remove(). But usually, you don't want to use this function, as it
69 * fiddles with hash-internals. */
70 void *hash_remove_bucket(struct hashtable_t *hash, struct hash_it_t *hash_it_t);
72 /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
73 * called to remove the elements inside of the hash. if you don't remove the
74 * elements, memory might be leaked. */
75 void hash_delete(struct hashtable_t *hash, hashdata_free_cb free_cb);
77 /* free only the hashtable and the hash itself. */
78 void hash_destroy(struct hashtable_t *hash);
80 /* adds data to the hashtable. returns 0 on success, -1 on error */
81 int hash_add(struct hashtable_t *hash, void *data);
83 /* removes data from hash, if found. returns pointer do data on success, so you
84 * can remove the used structure yourself, or NULL on error . data could be the
85 * structure you use with just the key filled, we just need the key for
87 void *hash_remove(struct hashtable_t *hash, void *data);
89 /* finds data, based on the key in keydata. returns the found data on success,
91 void *hash_find(struct hashtable_t *hash, void *keydata);
93 /* resize the hash, returns the pointer to the new hash or NULL on
94 * error. removes the old hash on success */
95 struct hashtable_t *hash_resize(struct hashtable_t *hash, int size);
97 /* iterate though the hash. first element is selected with iter_in NULL. use
98 * the returned iterator to access the elements until hash_it_t returns NULL. */
99 struct hash_it_t *hash_iterate(struct hashtable_t *hash,
100 struct hash_it_t *iter_in);
102 /* print the hash table for debugging */
103 void hash_debug(struct hashtable_t *hash);