-
Notifications
You must be signed in to change notification settings - Fork 0
/
judydup.cpp
317 lines (246 loc) · 7.82 KB
/
judydup.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// Copyright (C) 2011 Jason E. Aten. All rights reserved.
//
#include "dv.h"
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <math.h>
#include <openssl/sha.h>
#include "autotag.h"
#include "l3obj.h"
//#include "minifs.h"
#include "objects.h"
#include "terp.h"
#include "judydup.h"
#include <dlfcn.h>
#include "ut.h"
#include "tostring.h"
#include <stdio.h>
#include <editline/readline.h>
#include "dstaq.h"
#include "Judy.h"
#include "xcep.h"
/////////////////////// end of includes
// new name : judydup.cpp
// orig name: judy1dup.c
// jea: modify to use the judyL and judyS routines instead of Judy1
/*******************************************************************
* Name: Judy1Dup
*
* Description:
* Clone (duplicate) a Judy Array.
*
* Parameters:
* PPvoid_t PPDest (OUT)
* Pointer to a new Judy array with the same
* index/value pairs as PSource.
* Any initial value pointed to by PPDest is ignored.
*
* Pvoid_t PSource (IN)
* Ptr to source Judy array being duplicated.
* If PSource is NULL, TRUE is returned since this
* is simply a valid Null Judy array.
*
* JError_t *PJError (OUT)
* Judy error structure pointer.
*
* Returns:
* JERR - error, see PJError
* !JERR - success
*/
int
Judy1Dup(PPvoid_t PPDest, Pvoid_t PSource, JError_t * PJError)
{
Pvoid_t newJArray = 0; // new Judy1 array to ppopulate
Word_t kindex; // Key/index
int Ins_rv = 0; // Insert return value
for (kindex = 0L, Ins_rv = Judy1First(PSource, &kindex, PJError);
Ins_rv == 1; Ins_rv = Judy1Next(PSource, &kindex, PJError))
{
Ins_rv = Judy1Set(&newJArray, kindex, PJError);
}
if (Ins_rv == JERR)
return Ins_rv;
*PPDest = newJArray;
return Ins_rv;
} /* Judy1Dup */
/* sample use, from examples/Judy1DupCheck.c
static Word_t knowns[] = { 0, 1, 1024, 4095, 4096, 4097, 4098, 123456 };
int i;
Pvoid_t PJArray = 0;
Pvoid_t PJArrayNew = 0;
Word_t Index;
int Judy_rv; // Judy Return value
JError_t JError;
// populate a judy array with known values
PJArray = ularray2Judy(knowns, LARRAYSIZE(knowns));
printf("Testing Judy1Dup ...");
// dup the judy array
if ((Judy1Dup(&PJArrayNew, PJArray, &JError)) == JERR)
{
printf("Judy1Dup failed: error %d\n", JU_ERRNO(&JError));
return (2);
}
*/
#if 0
int
JudyLDup(PPvoid_t PPDest, Pvoid_t PSource, JError_t * PJError)
{
Pvoid_t newJArray = 0; // new JudyL array to ppopulate
Word_t kindex; // Key/index
int Ins_rv = 0; // Insert return value
for (kindex = 0L, Ins_rv = JudyLFirst(PSource, &kindex, PJError);
Ins_rv == 1; Ins_rv = JudyLNext(PSource, &kindex, PJError))
{
Ins_rv = JudyLSet(&newJArray, kindex, PJError);
}
if (Ins_rv == JERR)
return Ins_rv;
*PPDest = newJArray;
return Ins_rv;
} /* JudyLDup */
#endif
int copy_judyL(void* src, void** dest) {
/*
JLI(PValue, PJLArray, Index) // JudyLIns()
Insert an Index and Value into the JudyL array PJLArray.
If the Index is successfully inserted, the Value is ini‐
tialized to 0. If the Index was already present, the
Value is not modified.
Return PValue pointing to Value. Your program can use
this pointer to read or modify Value until the next
JLI() (insert), JLD() (delete) or JLFA() (freearray) is
executed on PJLArray. Examples:
*PValue = 1234;
Value = *PValue;
Return PValue set to PJERR if a malloc() fail occured.
*/
*dest = 0; // default value, if we don't finish
Pvoid_t newJArray = 0; // new JudyL array to ppopulate
Word_t Index = 0; // array index
//Word_t Value = 0; // array element value
Word_t * PValue = 0; // pointer to array element value
Word_t * PValueIns = 0; // pointer to array element value
// int Rc_int = 0; // return code
volatile int complete = 0;
complete = 0;
//Word_t* DestPValue = 0;
XTRY
case XCODE:
JLF(PValue, src, Index);
if (PValue == PJERR) { l3throw(XARRAYDUP_FAILURE); }
while (PValue != NULL)
{
DV(printf("%lu -> %.6f\n", Index, *((double*)PValue)));
JLI(PValueIns, newJArray, Index);
if (PValueIns == PJERR) { l3throw(XARRAYDUP_FAILURE); }
*PValueIns = *PValue;
JLN(PValue, src, Index);
if (PValue == NULL) {
complete = 1;
break;
}
}
break;
case XFINALLY:
if (complete) {
*dest = newJArray;
}
break;
XENDX
return 0;
}
int copy_judySL(void* src, void** dest) {
*dest = 0; // default value, if we don't finish
Pvoid_t newJArray = 0; // new JudyL array to ppopulate
Word_t * PValue = 0; // pointer to array element value
Word_t * PValueIns = 0; // pointer to array element value
volatile int complete = 0;
complete = 0;
uint8_t Index[BUFSIZ]; // string to sort.
Index[0] = '\0'; // start with smallest string.
XTRY
case XCODE:
JSLF(PValue, src, Index);
if (PValue == PJERR) { l3throw(XARRAYDUP_FAILURE); }
while (PValue != NULL)
{
DV(printf("%s -> %.06f\n", Index, *((double*)PValue)));
JSLI(PValueIns, newJArray, Index);
if (PValueIns == PJERR) { l3throw(XARRAYDUP_FAILURE); }
*PValueIns = *PValue;
JSLN(PValue, src, Index);
if (PValue == NULL) {
complete = 1;
break;
}
}
break;
case XFINALLY:
if (complete) {
*dest = newJArray;
}
break;
XENDX
return 0;
}
int copy_judySL_typed(judys_llref* src, judys_llref** dest) {
// use the assignment operator
**dest = *src;
#if 0
*dest = 0; // default value, if we don't finish
Pvoid_t newJArray = 0; // new JudyL array to ppopulate
Word_t * PValue = 0; // pointer to array element value
Word_t * PValueIns = 0; // pointer to array element value
volatile int complete = 0;
complete = 0;
uint8_t Index[BUFSIZ]; // string to sort.
Index[0] = '\0'; // start with smallest string.
XTRY
case XCODE:
JSLF(PValue, src, Index);
if (PValue == PJERR) { l3throw(XARRAYDUP_FAILURE); }
while (PValue != NULL)
{
DV(printf("%s -> %.06f\n", Index, *((double*)PValue)));
JSLI(PValueIns, newJArray, Index);
if (PValueIns == PJERR) { l3throw(XARRAYDUP_FAILURE); }
*PValueIns = *PValue;
JSLN(PValue, src, Index);
if (PValue == NULL) {
complete = 1;
break;
}
}
break;
case XFINALLY:
if (complete) {
*dest = newJArray;
}
break;
XENDX
#endif
return 0;
}
#if 0 // insert() usage out of date, we've gone to add_alias now
void test_judySL() {
l3obj* o = 0;
make_new_obj("testclass","testclastobjname", defptag_get(),0,&o);
l3path name("tester");
insert(&name, 0, main_env);
llref* llr = (llref*)lookup_hashtable(main_env, name()); // can return 0.
if (llr) {
l3obj* found = llr->_obj;
print(found,"",0);
}
}
#endif // 0