-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmap.h
194 lines (164 loc) · 6.42 KB
/
map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/***************************************************************************
map.h - description
Generic hashed map class.
-------------------
begin : Thu Jul 12 2001
copyright : (C) 2001 by John D. Robertson
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef MAP_H
#define MAP_H
#include "ptrvec.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Structure req'd for each map */
typedef struct {
PTRVEC* bucketArr;
unsigned int numBuckets;
} MAP;
/***************** Function prototypes and macros *******************/
#define MAP_is_init(s) \
((s)->bucketArr)
MAP*
MAP_constructor(MAP *self,
unsigned int numBuckets,
unsigned int slotsPerBucket);
/****************************************************************
* Construct a map with numBuckets buckets. slotsPerBucket provides
* an initial sizing of the buckets, but they can grow dynamically.
*/
#define MAP_create(self, numBuckets, slotsPerBucket)\
(MAP_constructor((self)=malloc(sizeof(MAP)), numBuckets, slotsPerBucket) ? (self) : ( self ? realloc(MAP_destructor(self),0): 0))
/***********************************************************
* Same as constructor with the addition of the object being
* malloc()'ed for you.
*/
int
MAP_sinit(MAP *self,
unsigned int numBuckets,
unsigned int slotsPerBucket);
/***********************************************************
* Initialize or clear() for a static instance.
*/
void*
MAP_destructor(MAP *self);
/************************************************
* Free resources associated with object.
*/
#define MAP_destroy(s)\
{if(MAP_destructor(s)) {free(s); (s)=NULL;}}
/***********************************************************
* Same as destructor with the addition of freeing the object.
*/
int
MAP_clearAndDestroy(MAP *self, void *(* destructor)(void *item_ptr));
/****************************************************************
* Call destructors on all item_ptr's and free() them, and
* free() all key records.
*/
#define MAP_clear(self) \
MAP_clearAndDestroy(self, NULL)
/****************************************************************
* free() all key records.
*/
int
MAP_addKey(MAP *self,
const void *key_ptr,
unsigned int keyLen,
void *item_ptr);
/*********************************************************************************
* Add a key to map, no checking for duplicates.
*/
#define MAP_addTypedKey(self, key, item_ptr) \
MAP_addKey(self, &(key), sizeof(key), item_ptr)
#define MAP_addStrKey(self, keystr, item_ptr) \
MAP_addKey(self, keystr, strlen(keystr), item_ptr)
void*
MAP_findItem(MAP *self,
const void *key_ptr,
unsigned int keyLen);
/******************************************************************************
* Find the the first matching key and return it's item_ptr.
* Returns:
* NULL Not found
* item_ptr first one found
*/
#define MAP_findTypedItem(self, key) \
MAP_findItem(self, &(key), sizeof(key))
#define MAP_findStrItem(self, keystr) \
MAP_findItem(self, keystr, strlen(keystr))
void*
MAP_removeItem(MAP *self,
const void *key_ptr,
unsigned int keyLen);
/******************************************************************************
* Find the the first matching key and remove it from the map.
* Returns:
* NULL Not found
* item_ptr first one found
*/
#define MAP_removeTypedItem(self, key) \
MAP_removeItem(self, &(key), sizeof(key))
#define MAP_removeStrItem(self, keystr) \
MAP_removeItem(self, keystr, strlen(keystr))
void*
MAP_removeSpecificItem(MAP *self,
const void *key_ptr,
unsigned int keyLen,
void *pItem);
/******************************************************************************
* Find the first matching key and pointer, and remove it from the map.
* pItem is the address of the specific item to be removed.
* Returns:
* NULL Not found
* item_ptr first one found
*/
#define MAP_removeSpecificTypedItem(self, key, pItem) \
MAP_removeSpecificItem(self, &(key), sizeof(key), pItem)
#define MAP_removeSpecificStrItem(self, keystr, pItem) \
MAP_removeSpecificItem(self, keystr, strlen(keystr), pItem)
int
MAP_findItems(MAP *self,
void* rtnArr[],
unsigned int rtnArrSize,
const void *key_ptr,
unsigned int keyLen);
/******************************************************************************
* Find all matching key(s) in the map, and put the accompanying item_ptr's
* into the rtnArr. Returns:
* -1 Insufficient space in rtnArr
* 0 .. INT_MAX Number of item_ptr's returned in rtnArr
*/
#define MAP_findTypedItems(self, rtnArr, rtnArrSize, key) \
MAP_findItems(self, rtnArr, rtnArrSize, &(key), sizeof(key))
#define MAP_findStrItems(self, rtnArr, rtnArrSize, keystr) \
MAP_findItems(self, rtnArr, rtnArrSize, keystr, strlen(keystr))
int
MAP_visitAllEntries(MAP *self, int (* func)(void *item_ptr, void *data), void *data);
/******************************************************************************
* Visit all entries in the map. if (*func) returns nonzero, then
* the process stops and MAP_visitAllEntries will return non-zero.
*/
void
MAP_fetchAllItems(MAP *self, void **rtn_arr);
/******************************************************************************
* Place the item pointers into the supplied array.
*/
unsigned
MAP_numItems(MAP *self);
/******************************************************************************
* Return a count of the items indexed in the map.
*/
#ifdef __cplusplus
}
#endif
#endif