mmlib/mmat: replace some variables by literal constants
[mmondor.git] / mmsoftware / mmlib / mmhash.h
1 /* $Id: mmhash.h,v 1.10 2007/12/05 23:47:56 mmondor Exp $ */
2
3 /*
4 * Copyright (C) 2001, 2004, 2007, Matthew Mondor
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Matthew Mondor.
18 * 4. The name of Matthew Mondor may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 * 5. Redistribution of source code may not be released under the terms of
22 * any GNU Public License derivate.
23 *
24 * THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL MATTHEW MONDOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36
37
38 #ifndef MMHASH_H
39 #define MMHASH_H
40
41
42
43 #include <stdbool.h>
44 #include <stdint.h>
45
46 #include <mmtypes.h>
47 #include <mmlist.h>
48 #include <mmpool.h>
49
50
51
52 typedef struct hashnode hashnode_t;
53 typedef struct hashtable hashtable_t;
54
55
56
57 struct hashnode {
58 pnode_t node;
59 uint32_t magic;
60 list_t *list;
61 uint32_t hash;
62 const void *key;
63 size_t keysize;
64 /*
65 * Custom user data will follow, uncluding the key element to which
66 * the previous key pointer is expected to point.
67 */
68 };
69
70 struct hashtable {
71 pnode_t node; /* Allows a pool_t of hashtable_t */
72 uint32_t magic;
73 const char *label;
74 unsigned int initial, capacity,
75 threshold, nodes;
76 float loadfactor;
77 list_t *array;
78 void *(*malloc)(size_t);
79 void (*free)(void *);
80 int (*keycomp)(const void *, const void *,
81 size_t);
82 uint32_t (*keyhash)(const void *, size_t);
83 unsigned int avgtotal, avgcnt;
84 bool dynamic, iterating;
85 };
86
87
88
89 #define MAGIC_HASHTABLE 0x4854424c /* HTBL */
90 #define MAGIC_HASHNODE 0x484e4f44 /* HNOD */
91 #define HT_DEFAULT_CAPACITY 128
92 #define HT_DEFAULT_FACTOR 0.75f
93
94 #define HASHTABLE_NODES(t) ((t)->nodes)
95 #define HASHTABLE_VALID(t) ((void *)(t) != NULL && (t)->magic == MAGIC_HASHTABLE)
96
97
98
99 extern bool hashtable_init(hashtable_t *, const char *,
100 unsigned int, float, void *(*)(size_t),
101 void (*)(void *),
102 int (*)(const void *, const void *, size_t),
103 uint32_t (*)(const void *, size_t), bool);
104 extern void hashtable_init2(hashtable_t *, const char *,
105 list_t *, unsigned int,
106 int (*)(const void *, const void *, size_t),
107 uint32_t (*)(const void *, size_t));
108 extern void hashtable_destroy(hashtable_t *, bool);
109 extern hashnode_t *hashtable_lookup(hashtable_t *, const void *, size_t);
110 extern bool hashtable_link(hashtable_t *, hashnode_t *,
111 const void *, size_t, bool);
112 extern void hashtable_unlink(hashtable_t *, hashnode_t *);
113 extern void hashtable_empty(hashtable_t *, bool);
114 extern void hashtable_iterate(hashtable_t *,
115 bool (*)(hashnode_t *, void *), void *);
116 extern uint32_t hashtable_hash(const void *, size_t);
117
118
119
120 #endif