forked from Unidata/netcdf-c
/
nchashmap.c
207 lines (186 loc) · 4.67 KB
/
nchashmap.c
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
195
196
197
198
199
200
201
202
203
204
205
206
/*********************************************************************
* Copyright 2010, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header$
*********************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nchashmap.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define DEFAULTALLOC 31
NChashmap* nchashnew(void) {return nchashnew0(DEFAULTALLOC);}
NChashmap* nchashnew0(size_t alloc)
{
NChashmap* hm;
if(sizeof(nchashid) != sizeof(void*)){
fprintf(stderr,"nchashmap: sizeof(nchashid) != sizeof(void*)");
abort();
}
hm = (NChashmap*)malloc(sizeof(NChashmap));
if(!hm) return NULL;
hm->alloc = alloc;
hm->table = (NClist**)malloc(hm->alloc*sizeof(NClist*));
if(!hm->table) {free(hm); return NULL;}
memset((void*)hm->table,0,hm->alloc*sizeof(NClist*));
return hm;
}
int
nchashfree(NChashmap* hm)
{
if(hm) {
int i;
for(i=0;i<hm->alloc;i++) {
if(hm->table[i] != NULL) nclistfree(hm->table[i]);
}
free(hm->table);
free(hm);
}
return TRUE;
}
/* Insert a <nchashid,void*> pair into the table*/
/* Fail if already there*/
int
nchashinsert(NChashmap* hm, nchashid hash, void* value)
{
int i;
size_t offset,len;
NClist* seq;
void** list;
offset = (hash % hm->alloc);
seq = hm->table[offset];
if(seq == NULL) {seq = nclistnew(); hm->table[offset] = seq;}
len = nclistlength(seq);
list = nclistcontents(seq);
for(i=0;i<len;i+=2,list+=2) {
if(hash==(nchashid)(*list)) return FALSE;
}
nclistpush(seq,(void*)hash);
nclistpush(seq,value);
hm->size++;
return TRUE;
}
/* Insert a <nchashid,void*> pair into the table*/
/* Overwrite if already there*/
int
nchashreplace(NChashmap* hm, nchashid hash, void* value)
{
int i;
size_t offset,len;
NClist* seq;
void** list;
offset = (hash % hm->alloc);
seq = hm->table[offset];
if(seq == NULL) {seq = nclistnew(); hm->table[offset] = seq;}
len = nclistlength(seq);
list = nclistcontents(seq);
for(i=0;i<len;i+=2,list+=2) {
if(hash==(nchashid)(*list)) {list[1] = value; return TRUE;}
}
nclistpush(seq,(void*)hash);
nclistpush(seq,value);
hm->size++;
return TRUE;
}
/* remove a nchashid*/
/* return TRUE if found, false otherwise*/
int
nchashremove(NChashmap* hm, nchashid hash)
{
size_t i;
size_t offset,len;
NClist* seq;
void** list;
offset = (hash % hm->alloc);
seq = hm->table[offset];
if(seq == NULL) return TRUE;
len = nclistlength(seq);
list = nclistcontents(seq);
for(i=0;i<len;i+=2,list+=2) {
if(hash==(nchashid)(*list)) {
nclistremove(seq,(i+1));
nclistremove(seq,i);
hm->size--;
if(nclistlength(seq) == 0) {nclistfree(seq); hm->table[offset] = NULL;}
return TRUE;
}
}
return FALSE;
}
/* lookup a nchashid; return DATANULL if not found*/
/* (use hashlookup if the possible values include 0)*/
void*
nchashget(NChashmap* hm, nchashid hash)
{
void* value;
if(!nchashlookup(hm,hash,&value)) return NULL;
return value;
}
int
nchashlookup(NChashmap* hm, nchashid hash, void** valuep)
{
int i;
size_t offset,len;
NClist* seq;
void** list;
offset = (hash % hm->alloc);
seq = hm->table[offset];
if(seq == NULL) return TRUE;
len = nclistlength(seq);
list = nclistcontents(seq);
for(i=0;i<len;i+=2,list+=2) {
if(hash==(nchashid)(*list)) {if(valuep) {*valuep = list[1]; return TRUE;}}
}
return FALSE;
}
/* Return the ith pair; order is completely arbitrary*/
/* Can be expensive*/
int
nchashith(NChashmap* hm, int index, nchashid* hashp, void** elemp)
{
int i;
if(hm == NULL) return FALSE;
for(i=0;i<hm->alloc;i++) {
NClist* seq = hm->table[i];
int len = nclistlength(seq) / 2;
if(len == 0) continue;
if((index - len) < 0) {
if(hashp) *hashp = (nchashid)nclistget(seq,index*2);
if(elemp) *elemp = nclistget(seq,(index*2)+1);
return TRUE;
}
index -= len;
}
return FALSE;
}
/* Return all the keys; order is completely arbitrary*/
/* Can be expensive*/
int
nchashkeys(NChashmap* hm, nchashid** keylist)
{
int i,j,index;
nchashid* keys;
if(hm == NULL) return FALSE;
if(hm->size == 0) {
keys = NULL;
} else {
keys = (nchashid*)malloc(sizeof(nchashid)*hm->size);
for(index=0,i=0;i<hm->alloc;i++) {
NClist* seq = hm->table[i];
for(j=0;j<nclistlength(seq);j+=2) {
keys[index++] = (nchashid)nclistget(seq,j);
}
}
}
if(keylist) {*keylist = keys;}
else {free(keys);}
return TRUE;
}