-
Notifications
You must be signed in to change notification settings - Fork 0
/
JudyHS.h
218 lines (191 loc) · 5.22 KB
/
JudyHS.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef _JUDYHS_CPP_WRAPPER_H
#define _JUDYHS_CPP_WRAPPER_H
/**
* JudyHS wrapper by Tim Starling. May be reused, modified and redistributed without restriction.
*/
#include <string>
#include <list>
#include <Judy.h>
/**
* value_type must be the size of a pointer
*/
template <class value_type>
class JudyHSWord
{
public:
JudyHSWord() : handle(NULL) {}
~JudyHSWord() { FreeArray(); }
/**
* Set the given index to a given value. If it already exists, overwrite it.
*/
value_type & Set(const char *index, Word_t length, const value_type & value)
{
value_type *p = (value_type*)JudyHSIns(&handle, (void*)index, length, NULL);
*p = value;
return *p;
}
value_type & Set(const std::string & s, const value_type & value) { return Set(s.data(), s.length(), value); }
/**
* Add the given array element. If it already exists, leave it unchanged. If not,
* initialise it with the given value.
*/
value_type & Add(const char *index, Word_t length, const value_type & value)
{
value_type *p = (value_type*)JudyHSIns(&handle, (void*)index, length, NULL);
if (!*p) {
*p = value;
}
return *p;
}
value_type & Add(const char *index, Word_t length) { return Add(index, length, value_type()); }
value_type & Add(const std::string & s, const value_type & value) { return Add(s.data(), s.size(), value); }
value_type & Add(const std::string & s) { return Add(s.data(), s.size()); }
value_type & operator[](const std::string & s) { return Add(s); }
int Delete(const char *index, Word_t length)
{
return JudyHSDel(&handle, (void*)index, length, NULL);
}
int Delete(const std::string & s) { return Delete(s.data(), s.size()); }
value_type * Get(const char *index, Word_t length)
{
return (value_type*)JudyHSGet(handle, (void*)index, length);
}
value_type * Get(const std::string & s) { return (value_type*)Get(s.data(), s.size()); }
Word_t FreeArray() { return JudyHSFreeArray(&handle, NULL); }
protected:
Pvoid_t handle;
};
/**
* set class that's kind of compatible with set<string>, if you use your imagination
*/
class JudySet : public JudyHSWord<int>
{
public:
void insert(const std::string & s) {
Add(s, 1);
}
bool find(const std::string & s) {
if (Get(s)) {
return true;
} else {
return false;
}
}
bool end() {
return false;
}
};
/*
* General value storage
*/
template <class value_type>
class JudyHS
{
protected:
struct List {
List() : prev(NULL), next(NULL) {}
List(const value_type & v) : value(v), prev(NULL), next(NULL) {}
value_type value;
List * prev;
List * next;
};
public:
JudyHS() : handle(NULL), storage(NULL) {}
~JudyHS() { FreeArray(); }
/**
* Set the given index to a given value. If it already exists, overwrite it.
*/
value_type & Set(const char *index, Word_t length, const value_type & value)
{
// Add to the list
List * node = new List(value);
AddNode(node);
// Insert into the judy array
List ** pNode = (List**)JudyHSIns(&handle, (void*)index, length, NULL);
if (*pNode) {
DeleteNode(*pNode);
}
*pNode = node;
return node->value;
}
value_type & Set(const std::string & s, const value_type & value) { return Set(s.data(), s.size(), value); }
/**
* Add the given array element. If it already exists, leave it unchanged. If not,
* initialise it with the given value.
*/
value_type & Add(const char *index, Word_t length, const value_type & value)
{
List ** pNode = (List**)JudyHSIns(&handle, (void*)index, length, NULL);
if (!*pNode) {
// Add to the list
List * node = new List(value);
AddNode(node);
// Register the list in the array
*pNode = node;
}
return (*pNode)->value;
}
value_type & Add(const char *index, Word_t length) { return Add(index, length, value_type()); }
value_type & Add(const std::string & s, const value_type & value) { return Add(s.data(), s.size(), value); }
value_type & Add(const std::string & s) { return Add(s.data(), s.size()); }
value_type & operator[](const std::string & s) { return Add(s); }
value_type * Get(const char *index, Word_t length)
{
List ** pNode = (List**)JudyHSGet(handle, (void*)index, length);
if (!pNode) {
return NULL;
} else {
return &((*pNode)->value);
}
}
value_type * Get(const std::string & s)
{
return Get(s.data(), s.size());
}
int Delete(const char *index, Word_t length)
{
List ** pNode = (List**)JudyHSGet(handle, (void*)index, length);
if (pNode) {
// Unlink and delete node
DeleteNode(*pNode);
// Remove from array
return JudyHSDel(&handle, (void*)index, length, NULL);
} else {
return 0;
}
}
int Delete(const std::string & s)
{
return Delete(s.data(), s.size());
}
Word_t FreeArray() {
// Free all list storage
List * node = storage;
List * next;
while (node) {
next = node->next;
delete node;
node = next;
}
// Destroy the judy array
return JudyHSFreeArray(&handle, NULL);
}
protected:
Pvoid_t handle;
List * storage;
void AddNode(List * node) {
List * oldHead = storage;
storage = node;
storage->next = oldHead;
if (oldHead) oldHead->prev = node;
}
void DeleteNode(List * node) {
if (node->prev) node->prev->next = node->next;
if (node->next) node->next->prev = node->prev;
if (node == storage) {
storage = NULL;
}
delete node;
}
};
#endif