master
c 1,942 lines 55.3 KB
Raw
1 // Copyright (C) 2000 - 2002 Hewlett-Packard Company
2 //
3 // This program is free software; you can redistribute it and/or modify it
4 // under the term of the GNU Lesser General Public License as published by the
5 // Free Software Foundation; either version 2 of the License, or (at your
6 // option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITHOUT
9 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 // for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program; if not, write to the Free Software Foundation,
15 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // _________________
17
18 // @(#) $Revision: 4.38 $ $Source: /judy/src/JudyCommon/JudyCascade.c $
19
20 #ifdef JUDY1
21 #include "Judy1.h"
22 #else
23 #include "JudyL.h"
24 #endif
25
26 #include "JudyPrivate1L.h"
27
28 extern int j__udyCreateBranchL(Pjp_t, Pjp_t, uint8_t *, Word_t, Pvoid_t);
29 extern int j__udyCreateBranchB(Pjp_t, Pjp_t, uint8_t *, Word_t, Pvoid_t);
30
31 DBGCODE(extern void JudyCheckSorted(Pjll_t Pjll, Word_t Pop1, long IndexSize);)
32
33 static const jbb_t StageJBBZero; // zeroed versions of namesake struct.
34
35 // TBD: There are multiple copies of (some of) these CopyWto3, Copy3toW,
36 // CopyWto7 and Copy7toW functions in Judy1Cascade.c, JudyLCascade.c, and
37 // JudyDecascade.c. These static functions should probably be moved to a
38 // common place, made macros, or something to avoid having four copies.
39
40
41 // ****************************************************************************
42 // __ J U D Y C O P Y X T O W
43
44
45 FUNCTION static void j__udyCopy3toW(
46 PWord_t PDest,
47 uint8_t * PSrc,
48 Word_t LeafIndexes)
49 {
50 do
51 {
52 JU_COPY3_PINDEX_TO_LONG(*PDest, PSrc);
53 PSrc += 3;
54 PDest += 1;
55
56 } while(--LeafIndexes);
57
58 } //j__udyCopy3toW()
59
60
61 #ifdef JU_64BIT
62
63 FUNCTION static void j__udyCopy4toW(
64 PWord_t PDest,
65 uint32_t * PSrc,
66 Word_t LeafIndexes)
67 {
68 do { *PDest++ = *PSrc++;
69 } while(--LeafIndexes);
70
71 } // j__udyCopy4toW()
72
73
74 FUNCTION static void j__udyCopy5toW(
75 PWord_t PDest,
76 uint8_t * PSrc,
77 Word_t LeafIndexes)
78 {
79 do
80 {
81 JU_COPY5_PINDEX_TO_LONG(*PDest, PSrc);
82 PSrc += 5;
83 PDest += 1;
84
85 } while(--LeafIndexes);
86
87 } // j__udyCopy5toW()
88
89
90 FUNCTION static void j__udyCopy6toW(
91 PWord_t PDest,
92 uint8_t * PSrc,
93 Word_t LeafIndexes)
94 {
95 do
96 {
97 JU_COPY6_PINDEX_TO_LONG(*PDest, PSrc);
98 PSrc += 6;
99 PDest += 1;
100
101 } while(--LeafIndexes);
102
103 } // j__udyCopy6toW()
104
105
106 FUNCTION static void j__udyCopy7toW(
107 PWord_t PDest,
108 uint8_t * PSrc,
109 Word_t LeafIndexes)
110 {
111 do
112 {
113 JU_COPY7_PINDEX_TO_LONG(*PDest, PSrc);
114 PSrc += 7;
115 PDest += 1;
116
117 } while(--LeafIndexes);
118
119 } // j__udyCopy7toW()
120
121 #endif // JU_64BIT
122
123
124 // ****************************************************************************
125 // __ J U D Y C O P Y W T O X
126
127
128 FUNCTION static void j__udyCopyWto3(
129 uint8_t * PDest,
130 PWord_t PSrc,
131 Word_t LeafIndexes)
132 {
133 do
134 {
135 JU_COPY3_LONG_TO_PINDEX(PDest, *PSrc);
136 PSrc += 1;
137 PDest += 3;
138
139 } while(--LeafIndexes);
140
141 } // j__udyCopyWto3()
142
143
144 #ifdef JU_64BIT
145
146 FUNCTION static void j__udyCopyWto4(
147 uint8_t * PDest,
148 PWord_t PSrc,
149 Word_t LeafIndexes)
150 {
151 uint32_t *PDest32 = (uint32_t *)PDest;
152
153 do
154 {
155 *PDest32 = *PSrc;
156 PSrc += 1;
157 PDest32 += 1;
158 } while(--LeafIndexes);
159
160 } // j__udyCopyWto4()
161
162
163 FUNCTION static void j__udyCopyWto5(
164 uint8_t * PDest,
165 PWord_t PSrc,
166 Word_t LeafIndexes)
167 {
168 do
169 {
170 JU_COPY5_LONG_TO_PINDEX(PDest, *PSrc);
171 PSrc += 1;
172 PDest += 5;
173
174 } while(--LeafIndexes);
175
176 } // j__udyCopyWto5()
177
178
179 FUNCTION static void j__udyCopyWto6(
180 uint8_t * PDest,
181 PWord_t PSrc,
182 Word_t LeafIndexes)
183 {
184 do
185 {
186 JU_COPY6_LONG_TO_PINDEX(PDest, *PSrc);
187 PSrc += 1;
188 PDest += 6;
189
190 } while(--LeafIndexes);
191
192 } // j__udyCopyWto6()
193
194
195 FUNCTION static void j__udyCopyWto7(
196 uint8_t * PDest,
197 PWord_t PSrc,
198 Word_t LeafIndexes)
199 {
200 do
201 {
202 JU_COPY7_LONG_TO_PINDEX(PDest, *PSrc);
203 PSrc += 1;
204 PDest += 7;
205
206 } while(--LeafIndexes);
207
208 } // j__udyCopyWto7()
209
210 #endif // JU_64BIT
211
212
213 // ****************************************************************************
214 // COMMON CODE (MACROS):
215 //
216 // Free objects in an array of valid JPs, StageJP[ExpCnt] == last one may
217 // include Immeds, which are ignored.
218
219 #define FREEALLEXIT(ExpCnt,StageJP,Pjpm) \
220 { \
221 Word_t _expct = (ExpCnt); \
222 while (_expct--) j__udyFreeSM(&((StageJP)[_expct]), Pjpm); \
223 return(-1); \
224 }
225
226 // Clear the array that keeps track of the number of JPs in a subexpanse:
227
228 #define ZEROJP(SubJPCount) \
229 { \
230 int ii; \
231 for (ii = 0; ii < cJU_NUMSUBEXPB; ii++) (SubJPCount[ii]) = 0; \
232 }
233
234 // ****************************************************************************
235 // __ J U D Y S T A G E J B B T O J B B
236 //
237 // Create a mallocd BranchB (jbb_t) from a staged BranchB while "splaying" a
238 // single old leaf. Return -1 if out of memory, otherwise 1.
239
240 static int j__udyStageJBBtoJBB(
241 Pjp_t PjpLeaf, // JP of leaf being splayed.
242 Pjbb_t PStageJBB, // temp jbb_t on stack.
243 Pjp_t PjpArray, // array of JPs to splayed new leaves.
244 uint8_t * PSubCount, // count of JPs for each subexpanse.
245 Pjpm_t Pjpm) // the jpm_t for JudyAlloc*().
246 {
247 Pjbb_t PjbbRaw; // pointer to new bitmap branch.
248 Pjbb_t Pjbb;
249 Word_t subexp;
250
251 // Get memory for new BranchB:
252
253 if ((PjbbRaw = j__udyAllocJBB(Pjpm)) == (Pjbb_t) NULL) return(-1);
254 Pjbb = P_JBB(PjbbRaw);
255
256 // Copy staged BranchB into just-allocated BranchB:
257
258 *Pjbb = *PStageJBB;
259
260 // Allocate the JP subarrays (BJP) for the new BranchB:
261
262 for (subexp = 0; subexp < cJU_NUMSUBEXPB; subexp++)
263 {
264 Pjp_t PjpRaw;
265 Pjp_t Pjp;
266 Word_t NumJP; // number of JPs in each subexpanse.
267
268 if ((NumJP = PSubCount[subexp]) == 0) continue; // empty.
269
270 // Out of memory, back out previous allocations:
271
272 if ((PjpRaw = j__udyAllocJBBJP(NumJP, Pjpm)) == (Pjp_t) NULL)
273 {
274 while(subexp--)
275 {
276 if ((NumJP = PSubCount[subexp]) == 0) continue;
277
278 PjpRaw = JU_JBB_PJP(Pjbb, subexp);
279 j__udyFreeJBBJP(PjpRaw, NumJP, Pjpm);
280 }
281 j__udyFreeJBB(PjbbRaw, Pjpm);
282 return(-1); // out of memory.
283 }
284 Pjp = P_JP(PjpRaw);
285
286 // Place the JP subarray pointer in the new BranchB, copy subarray JPs, and
287 // advance to the next subexpanse:
288
289 JU_JBB_PJP(Pjbb, subexp) = PjpRaw;
290 JU_COPYMEM(Pjp, PjpArray, NumJP);
291 PjpArray += NumJP;
292
293 } // for each subexpanse.
294
295 // Change the PjpLeaf from Leaf to BranchB:
296
297 PjpLeaf->jp_Addr = (Word_t) PjbbRaw;
298 PjpLeaf->jp_Type += cJU_JPBRANCH_B2 - cJU_JPLEAF2; // Leaf to BranchB.
299
300 return(1);
301
302 } // j__udyStageJBBtoJBB()
303
304
305 // ****************************************************************************
306 // __ J U D Y J L L 2 T O J L B 1
307 //
308 // Create a LeafB1 (jlb_t = JLB1) from a Leaf2 (2-byte Indexes and for JudyL,
309 // Word_t Values). Return NULL if out of memory, else a pointer to the new
310 // LeafB1.
311 //
312 // NOTE: Caller must release the Leaf2 that was passed in.
313
314 FUNCTION static Pjlb_t j__udyJLL2toJLB1(
315 uint16_t * Pjll, // array of 16-bit indexes.
316 #ifdef JUDYL
317 Pjv_t Pjv, // array of associated values.
318 #endif
319 Word_t LeafPop1, // number of indexes/values.
320 Pvoid_t Pjpm) // jpm_t for JudyAlloc*()/JudyFree*().
321 {
322 Pjlb_t PjlbRaw;
323 Pjlb_t Pjlb;
324 int offset;
325 JUDYLCODE(int subexp;)
326
327 // Allocate the LeafB1:
328
329 if ((PjlbRaw = j__udyAllocJLB1(Pjpm)) == (Pjlb_t) NULL)
330 return((Pjlb_t) NULL);
331 Pjlb = P_JLB(PjlbRaw);
332
333 // Copy Leaf2 indexes to LeafB1:
334
335 for (offset = 0; offset < LeafPop1; ++offset)
336 JU_BITMAPSETL(Pjlb, Pjll[offset]);
337
338 #ifdef JUDYL
339
340 // Build LeafVs from bitmap:
341
342 for (subexp = 0; subexp < cJU_NUMSUBEXPL; ++subexp)
343 {
344 struct _POINTER_VALUES
345 {
346 Word_t pv_Pop1; // size of value area.
347 Pjv_t pv_Pjv; // raw pointer to value area.
348 } pv[cJU_NUMSUBEXPL];
349
350 // Get the population of the subexpanse, and if any, allocate a LeafV:
351
352 pv[subexp].pv_Pop1 = j__udyCountBitsL(JU_JLB_BITMAP(Pjlb, subexp));
353
354 if (pv[subexp].pv_Pop1)
355 {
356 Pjv_t Pjvnew;
357
358 // TBD: There is an opportunity to put pop == 1 value in pointer:
359
360 pv[subexp].pv_Pjv = j__udyLAllocJV(pv[subexp].pv_Pop1, Pjpm);
361
362 // Upon out of memory, free all previously allocated:
363
364 if (pv[subexp].pv_Pjv == (Pjv_t) NULL)
365 {
366 while(subexp--)
367 {
368 if (pv[subexp].pv_Pop1)
369 {
370 j__udyLFreeJV(pv[subexp].pv_Pjv, pv[subexp].pv_Pop1,
371 Pjpm);
372 }
373 }
374 j__udyFreeJLB1(PjlbRaw, Pjpm);
375 return((Pjlb_t) NULL);
376 }
377
378 Pjvnew = P_JV(pv[subexp].pv_Pjv);
379 JU_COPYMEM(Pjvnew, Pjv, pv[subexp].pv_Pop1);
380 Pjv += pv[subexp].pv_Pop1; // advance value pointer.
381
382 // Place raw pointer to value array in bitmap subexpanse:
383
384 JL_JLB_PVALUE(Pjlb, subexp) = pv[subexp].pv_Pjv;
385
386 } // populated subexpanse.
387 } // each subexpanse.
388
389 #endif // JUDYL
390
391 return(PjlbRaw); // pointer to LeafB1.
392
393 } // j__udyJLL2toJLB1()
394
395
396 // ****************************************************************************
397 // __ J U D Y C A S C A D E 1
398 //
399 // Create bitmap leaf from 1-byte Indexes and Word_t Values.
400 //
401 // TBD: There must be a better way.
402 //
403 // Only for JudyL 32 bit: (note, unifdef disallows comment on next line)
404
405 #if (defined(JUDYL) || (! defined(JU_64BIT)))
406
407 FUNCTION int j__udyCascade1(
408 Pjp_t Pjp,
409 Pvoid_t Pjpm)
410 {
411 Word_t DcdP0;
412 uint8_t * PLeaf;
413 Pjlb_t PjlbRaw;
414 Pjlb_t Pjlb;
415 Word_t Pop1;
416 Word_t ii; // temp for loop counter
417 JUDYLCODE(Pjv_t Pjv;)
418
419 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF1);
420 assert((JU_JPDCDPOP0(Pjp) & 0xFF) == (cJU_LEAF1_MAXPOP1-1));
421
422 PjlbRaw = j__udyAllocJLB1(Pjpm);
423 if (PjlbRaw == (Pjlb_t) NULL) return(-1);
424
425 Pjlb = P_JLB(PjlbRaw);
426 PLeaf = (uint8_t *) P_JLL(Pjp->jp_Addr);
427 Pop1 = JU_JPLEAF_POP0(Pjp) + 1;
428
429 JUDYLCODE(Pjv = JL_LEAF1VALUEAREA(PLeaf, Pop1);)
430
431 // Copy 1 byte index Leaf to bitmap Leaf
432 for (ii = 0; ii < Pop1; ii++) JU_BITMAPSETL(Pjlb, PLeaf[ii]);
433
434 #ifdef JUDYL
435 // Build 8 subexpanse Value leaves from bitmap
436 for (ii = 0; ii < cJU_NUMSUBEXPL; ii++)
437 {
438 // Get number of Indexes in subexpanse
439 if ((Pop1 = j__udyCountBitsL(JU_JLB_BITMAP(Pjlb, ii))))
440 {
441 Pjv_t PjvnewRaw; // value area of new leaf.
442 Pjv_t Pjvnew;
443
444 PjvnewRaw = j__udyLAllocJV(Pop1, Pjpm);
445 if (PjvnewRaw == (Pjv_t) NULL) // out of memory.
446 {
447 // Free prevously allocated LeafVs:
448 while(ii--)
449 {
450 if ((Pop1 = j__udyCountBitsL(JU_JLB_BITMAP(Pjlb, ii))))
451 {
452 PjvnewRaw = JL_JLB_PVALUE(Pjlb, ii);
453 j__udyLFreeJV(PjvnewRaw, Pop1, Pjpm);
454 }
455 }
456 // Free the bitmap leaf
457 j__udyLFreeJLB1(PjlbRaw,Pjpm);
458 return(-1);
459 }
460 Pjvnew = P_JV(PjvnewRaw);
461 JU_COPYMEM(Pjvnew, Pjv, Pop1);
462
463 Pjv += Pop1;
464 JL_JLB_PVALUE(Pjlb, ii) = PjvnewRaw;
465 }
466 }
467 #endif // JUDYL
468
469 DcdP0 = JU_JPDCDPOP0(Pjp) | (PLeaf[0] & cJU_DCDMASK(1));
470 JU_JPSETADT(Pjp, (Word_t)PjlbRaw, DcdP0, cJU_JPLEAF_B1);
471
472 return(1); // return success
473
474 } // j__udyCascade1()
475
476 #endif // (!(JUDY1 && JU_64BIT))
477
478
479 // ****************************************************************************
480 // __ J U D Y C A S C A D E 2
481 //
482 // Entry PLeaf of size LeafPop1 is either compressed or splayed with pointer
483 // returned in Pjp. Entry Levels sizeof(Word_t) down to level 2.
484 //
485 // Splay or compress the 2-byte Index Leaf that Pjp point to. Return *Pjp as a
486 // (compressed) cJU_LEAFB1 or a cJU_BRANCH_*2
487
488 FUNCTION int j__udyCascade2(
489 Pjp_t Pjp,
490 Pvoid_t Pjpm)
491 {
492 uint16_t * PLeaf; // pointer to leaf, explicit type.
493 Word_t End, Start; // temporaries.
494 Word_t ExpCnt; // count of expanses of splay.
495 Word_t CIndex; // current Index word.
496 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
497
498 // Temp staging for parts(Leaves) of newly splayed leaf
499 jp_t StageJP [cJU_LEAF2_MAXPOP1]; // JPs of new leaves
500 uint8_t StageExp [cJU_LEAF2_MAXPOP1]; // Expanses of new leaves
501 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
502 jbb_t StageJBB; // staged bitmap branch
503
504 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF2);
505 assert((JU_JPDCDPOP0(Pjp) & 0xFFFF) == (cJU_LEAF2_MAXPOP1-1));
506
507 // Get the address of the Leaf
508 PLeaf = (uint16_t *) P_JLL(Pjp->jp_Addr);
509
510 // And its Value area
511 JUDYLCODE(Pjv = JL_LEAF2VALUEAREA(PLeaf, cJU_LEAF2_MAXPOP1);)
512
513 // If Leaf is in 1 expanse -- just compress it to a Bitmap Leaf
514
515 CIndex = PLeaf[0];
516 if (!JU_DIGITATSTATE(CIndex ^ PLeaf[cJU_LEAF2_MAXPOP1-1], 2))
517 {
518 // cJU_JPLEAF_B1
519 Word_t DcdP0;
520 Pjlb_t PjlbRaw;
521 PjlbRaw = j__udyJLL2toJLB1(PLeaf,
522 #ifdef JUDYL
523 Pjv,
524 #endif
525 cJU_LEAF2_MAXPOP1, Pjpm);
526 if (PjlbRaw == (Pjlb_t)NULL) return(-1); // out of memory
527
528 // Merge in another Dcd byte because compressing
529 DcdP0 = (CIndex & cJU_DCDMASK(1)) | JU_JPDCDPOP0(Pjp);
530 JU_JPSETADT(Pjp, (Word_t)PjlbRaw, DcdP0, cJU_JPLEAF_B1);
531
532 return(1);
533 }
534
535 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
536
537 StageJBB = StageJBBZero; // zero staged bitmap branch
538 ZEROJP(SubJPCount);
539
540 // Splay the 2 byte index Leaf to 1 byte Index Leaves
541 for (ExpCnt = Start = 0, End = 1; ; End++)
542 {
543 // Check if new expanse or last one
544 if ( (End == cJU_LEAF2_MAXPOP1)
545 ||
546 (JU_DIGITATSTATE(CIndex ^ PLeaf[End], 2))
547 )
548 {
549 // Build a leaf below the previous expanse
550 //
551 Pjp_t PjpJP = StageJP + ExpCnt;
552 Word_t Pop1 = End - Start;
553 Word_t expanse = JU_DIGITATSTATE(CIndex, 2);
554 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
555 //
556 // set the bit that is the current expanse
557 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
558 #ifdef SUBEXPCOUNTS
559 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
560 #endif
561 // count number of expanses in each subexpanse
562 SubJPCount[subexp]++;
563
564 // Save byte expanse of leaf
565 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex, 2);
566
567 if (Pop1 == 1) // cJU_JPIMMED_1_01
568 {
569 Word_t DcdP0;
570 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(1)) |
571 CIndex;
572 #ifdef JUDY1
573 JU_JPSETADT(PjpJP, 0, DcdP0, cJ1_JPIMMED_1_01);
574 #else // JUDYL
575 JU_JPSETADT(PjpJP, Pjv[Start], DcdP0,
576 cJL_JPIMMED_1_01);
577 #endif // JUDYL
578 }
579 else if (Pop1 <= cJU_IMMED1_MAXPOP1) // bigger
580 {
581 // cJL_JPIMMED_1_02..3: JudyL 32
582 // cJ1_JPIMMED_1_02..7: Judy1 32
583 // cJL_JPIMMED_1_02..7: JudyL 64
584 // cJ1_JPIMMED_1_02..15: Judy1 64
585 #ifdef JUDYL
586 Pjv_t PjvnewRaw; // value area of leaf.
587 Pjv_t Pjvnew;
588
589 // Allocate Value area for Immediate Leaf
590 PjvnewRaw = j__udyLAllocJV(Pop1, Pjpm);
591 if (PjvnewRaw == (Pjv_t) NULL)
592 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
593
594 Pjvnew = P_JV(PjvnewRaw);
595
596 // Copy to Values to Value Leaf
597 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
598 PjpJP->jp_Addr = (Word_t) PjvnewRaw;
599
600 // Copy to JP as an immediate Leaf
601 JU_COPYMEM(PjpJP->jp_LIndex, PLeaf + Start,
602 Pop1);
603 #else
604 JU_COPYMEM(PjpJP->jp_1Index, PLeaf + Start,
605 Pop1);
606 #endif
607 // Set Type, Population and Index size
608 PjpJP->jp_Type = cJU_JPIMMED_1_02 + Pop1 - 2;
609 }
610
611 // 64Bit Judy1 does not have Leaf1: (note, unifdef disallows comment on next
612 // line)
613
614 #if (! (defined(JUDY1) && defined(JU_64BIT)))
615 else if (Pop1 <= cJU_LEAF1_MAXPOP1) // still bigger
616 {
617 // cJU_JPLEAF1
618 Word_t DcdP0;
619 Pjll_t PjllRaw; // pointer to new leaf.
620 Pjll_t Pjll;
621 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
622
623 // Get a new Leaf
624 PjllRaw = j__udyAllocJLL1(Pop1, Pjpm);
625 if (PjllRaw == (Pjll_t)NULL)
626 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
627
628 Pjll = P_JLL(PjllRaw);
629 #ifdef JUDYL
630 // Copy to Values to new Leaf
631 Pjvnew = JL_LEAF1VALUEAREA(Pjll, Pop1);
632 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
633 #endif
634 // Copy Indexes to new Leaf
635 JU_COPYMEM((uint8_t *)Pjll, PLeaf+Start, Pop1);
636
637 DBGCODE(JudyCheckSorted(Pjll, Pop1, 1);)
638
639 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(2))
640 |
641 (CIndex & cJU_DCDMASK(2-1))
642 |
643 (Pop1 - 1);
644
645 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, DcdP0,
646 cJU_JPLEAF1);
647 }
648 #endif // (!(JUDY1 && JU_64BIT)) // Not 64Bit Judy1
649
650 else // biggest
651 {
652 // cJU_JPLEAF_B1
653 Word_t DcdP0;
654 Pjlb_t PjlbRaw;
655 PjlbRaw = j__udyJLL2toJLB1(
656 PLeaf + Start,
657 #ifdef JUDYL
658 Pjv + Start,
659 #endif
660 Pop1, Pjpm);
661 if (PjlbRaw == (Pjlb_t)NULL)
662 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
663
664 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(2))
665 |
666 (CIndex & cJU_DCDMASK(2-1))
667 |
668 (Pop1 - 1);
669
670 JU_JPSETADT(PjpJP, (Word_t)PjlbRaw, DcdP0,
671 cJU_JPLEAF_B1);
672 }
673 ExpCnt++;
674 // Done?
675 if (End == cJU_LEAF2_MAXPOP1) break;
676
677 // New Expanse, Start and Count
678 CIndex = PLeaf[End];
679 Start = End;
680 }
681 }
682
683 // Now put all the Leaves below a BranchL or BranchB:
684 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
685 {
686 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
687 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
688
689 Pjp->jp_Type = cJU_JPBRANCH_L2;
690 }
691 else
692 {
693 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
694 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
695 }
696 return(1);
697
698 } // j__udyCascade2()
699
700
701 // ****************************************************************************
702 // __ J U D Y C A S C A D E 3
703 //
704 // Return *Pjp as a (compressed) cJU_LEAF2, cJU_BRANCH_L3, cJU_BRANCH_B3.
705
706 FUNCTION int j__udyCascade3(
707 Pjp_t Pjp,
708 Pvoid_t Pjpm)
709 {
710 uint8_t * PLeaf; // pointer to leaf, explicit type.
711 Word_t End, Start; // temporaries.
712 Word_t ExpCnt; // count of expanses of splay.
713 Word_t CIndex; // current Index word.
714 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
715
716 // Temp staging for parts(Leaves) of newly splayed leaf
717 jp_t StageJP [cJU_LEAF3_MAXPOP1]; // JPs of new leaves
718 Word_t StageA [cJU_LEAF3_MAXPOP1];
719 uint8_t StageExp [cJU_LEAF3_MAXPOP1]; // Expanses of new leaves
720 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
721 jbb_t StageJBB; // staged bitmap branch
722
723 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF3);
724 assert((JU_JPDCDPOP0(Pjp) & 0xFFFFFF) == (cJU_LEAF3_MAXPOP1-1));
725
726 // Get the address of the Leaf
727 PLeaf = (uint8_t *) P_JLL(Pjp->jp_Addr);
728
729 // Extract leaf to Word_t and insert-sort Index into it
730 j__udyCopy3toW(StageA, PLeaf, cJU_LEAF3_MAXPOP1);
731
732 // Get the address of the Leaf and Value area
733 JUDYLCODE(Pjv = JL_LEAF3VALUEAREA(PLeaf, cJU_LEAF3_MAXPOP1);)
734
735 // If Leaf is in 1 expanse -- just compress it (compare 1st, last & Index)
736
737 CIndex = StageA[0];
738 if (!JU_DIGITATSTATE(CIndex ^ StageA[cJU_LEAF3_MAXPOP1-1], 3))
739 {
740 Word_t DcdP0;
741 Pjll_t PjllRaw; // pointer to new leaf.
742 Pjll_t Pjll;
743 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
744
745 // Alloc a 2 byte Index Leaf
746 PjllRaw = j__udyAllocJLL2(cJU_LEAF3_MAXPOP1, Pjpm);
747 if (PjllRaw == (Pjlb_t)NULL) return(-1); // out of memory
748
749 Pjll = P_JLL(PjllRaw);
750
751 // Copy just 2 bytes Indexes to new Leaf
752 // j__udyCopyWto2((uint16_t *) Pjll, StageA, cJU_LEAF3_MAXPOP1);
753 JU_COPYMEM ((uint16_t *) Pjll, StageA, cJU_LEAF3_MAXPOP1);
754 #ifdef JUDYL
755 // Copy Value area into new Leaf
756 Pjvnew = JL_LEAF2VALUEAREA(Pjll, cJU_LEAF3_MAXPOP1);
757 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAF3_MAXPOP1);
758 #endif
759 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAF3_MAXPOP1, 2);)
760
761 // Form new JP, Pop0 field is unchanged
762 // Add in another Dcd byte because compressing
763 DcdP0 = (CIndex & cJU_DCDMASK(2)) | JU_JPDCDPOP0(Pjp);
764
765 JU_JPSETADT(Pjp, (Word_t) PjllRaw, DcdP0, cJU_JPLEAF2);
766
767 return(1); // Success
768 }
769
770 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
771
772 StageJBB = StageJBBZero; // zero staged bitmap branch
773 ZEROJP(SubJPCount);
774
775 // Splay the 3 byte index Leaf to 2 byte Index Leaves
776 for (ExpCnt = Start = 0, End = 1; ; End++)
777 {
778 // Check if new expanse or last one
779 if ( (End == cJU_LEAF3_MAXPOP1)
780 ||
781 (JU_DIGITATSTATE(CIndex ^ StageA[End], 3))
782 )
783 {
784 // Build a leaf below the previous expanse
785
786 Pjp_t PjpJP = StageJP + ExpCnt;
787 Word_t Pop1 = End - Start;
788 Word_t expanse = JU_DIGITATSTATE(CIndex, 3);
789 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
790 //
791 // set the bit that is the current expanse
792 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
793 #ifdef SUBEXPCOUNTS
794 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
795 #endif
796 // count number of expanses in each subexpanse
797 SubJPCount[subexp]++;
798
799 // Save byte expanse of leaf
800 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex, 3);
801
802 if (Pop1 == 1) // cJU_JPIMMED_2_01
803 {
804 Word_t DcdP0;
805 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(2)) |
806 CIndex;
807 #ifdef JUDY1
808 JU_JPSETADT(PjpJP, 0, DcdP0, cJ1_JPIMMED_2_01);
809 #else // JUDYL
810 JU_JPSETADT(PjpJP, Pjv[Start], DcdP0,
811 cJL_JPIMMED_2_01);
812 #endif // JUDYL
813 }
814 #if (defined(JUDY1) || defined(JU_64BIT))
815 else if (Pop1 <= cJU_IMMED2_MAXPOP1)
816 {
817 // cJ1_JPIMMED_2_02..3: Judy1 32
818 // cJL_JPIMMED_2_02..3: JudyL 64
819 // cJ1_JPIMMED_2_02..7: Judy1 64
820 #ifdef JUDYL
821 // Alloc is 1st in case of malloc fail
822 Pjv_t PjvnewRaw; // value area of new leaf.
823 Pjv_t Pjvnew;
824
825 // Allocate Value area for Immediate Leaf
826 PjvnewRaw = j__udyLAllocJV(Pop1, Pjpm);
827 if (PjvnewRaw == (Pjv_t) NULL)
828 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
829
830 Pjvnew = P_JV(PjvnewRaw);
831
832 // Copy to Values to Value Leaf
833 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
834
835 PjpJP->jp_Addr = (Word_t) PjvnewRaw;
836
837 // Copy to Index to JP as an immediate Leaf
838 JU_COPYMEM((uint16_t *) (PjpJP->jp_LIndex),
839 StageA + Start, Pop1);
840 #else // JUDY1
841 JU_COPYMEM((uint16_t *) (PjpJP->jp_1Index),
842 StageA + Start, Pop1);
843 #endif // JUDY1
844 // Set Type, Population and Index size
845 PjpJP->jp_Type = cJU_JPIMMED_2_02 + Pop1 - 2;
846 }
847 #endif // (JUDY1 || JU_64BIT)
848
849 else // Make a linear leaf2
850 {
851 // cJU_JPLEAF2
852 Word_t DcdP0;
853 Pjll_t PjllRaw; // pointer to new leaf.
854 Pjll_t Pjll;
855 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
856
857 PjllRaw = j__udyAllocJLL2(Pop1, Pjpm);
858 if (PjllRaw == (Pjll_t) NULL)
859 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
860
861 Pjll = P_JLL(PjllRaw);
862 #ifdef JUDYL
863 // Copy to Values to new Leaf
864 Pjvnew = JL_LEAF2VALUEAREA(Pjll, Pop1);
865 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
866 #endif
867 // Copy least 2 bytes per Index of Leaf to new Leaf
868 JU_COPYMEM((uint16_t *) Pjll, StageA+Start,
869 Pop1);
870
871 DBGCODE(JudyCheckSorted(Pjll, Pop1, 2);)
872
873 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(3))
874 |
875 (CIndex & cJU_DCDMASK(3-1))
876 |
877 (Pop1 - 1);
878
879 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, DcdP0,
880 cJU_JPLEAF2);
881 }
882 ExpCnt++;
883 // Done?
884 if (End == cJU_LEAF3_MAXPOP1) break;
885
886 // New Expanse, Start and Count
887 CIndex = StageA[End];
888 Start = End;
889 }
890 }
891
892 // Now put all the Leaves below a BranchL or BranchB:
893 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
894 {
895 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
896 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
897
898 Pjp->jp_Type = cJU_JPBRANCH_L3;
899 }
900 else
901 {
902 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
903 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
904 }
905 return(1);
906
907 } // j__udyCascade3()
908
909
910 #ifdef JU_64BIT // JudyCascade[4567]
911
912 // ****************************************************************************
913 // __ J U D Y C A S C A D E 4
914 //
915 // Cascade from a cJU_JPLEAF4 to one of the following:
916 // 1. if leaf is in 1 expanse:
917 // compress it into a JPLEAF3
918 // 2. if leaf contains multiple expanses:
919 // create linear or bitmap branch containing
920 // each new expanse is either a:
921 // JPIMMED_3_01 branch
922 // JPIMMED_3_02 branch
923 // JPLEAF3
924
925 FUNCTION int j__udyCascade4(
926 Pjp_t Pjp,
927 Pvoid_t Pjpm)
928 {
929 uint32_t * PLeaf; // pointer to leaf, explicit type.
930 Word_t End, Start; // temporaries.
931 Word_t ExpCnt; // count of expanses of splay.
932 Word_t CIndex; // current Index word.
933 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
934
935 // Temp staging for parts(Leaves) of newly splayed leaf
936 jp_t StageJP [cJU_LEAF4_MAXPOP1]; // JPs of new leaves
937 Word_t StageA [cJU_LEAF4_MAXPOP1];
938 uint8_t StageExp [cJU_LEAF4_MAXPOP1]; // Expanses of new leaves
939 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
940 jbb_t StageJBB; // staged bitmap branch
941
942 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF4);
943 assert((JU_JPDCDPOP0(Pjp) & 0xFFFFFFFF) == (cJU_LEAF4_MAXPOP1-1));
944
945 // Get the address of the Leaf
946 PLeaf = (uint32_t *) P_JLL(Pjp->jp_Addr);
947
948 // Extract 4 byte index Leaf to Word_t
949 j__udyCopy4toW(StageA, PLeaf, cJU_LEAF4_MAXPOP1);
950
951 // Get the address of the Leaf and Value area
952 JUDYLCODE(Pjv = JL_LEAF4VALUEAREA(PLeaf, cJU_LEAF4_MAXPOP1);)
953
954 // If Leaf is in 1 expanse -- just compress it (compare 1st, last & Index)
955
956 CIndex = StageA[0];
957 if (!JU_DIGITATSTATE(CIndex ^ StageA[cJU_LEAF4_MAXPOP1-1], 4))
958 {
959 Word_t DcdP0;
960 Pjll_t PjllRaw; // pointer to new leaf.
961 Pjll_t Pjll;
962 JUDYLCODE(Pjv_t Pjvnew;) // value area of new Leaf.
963
964 // Alloc a 3 byte Index Leaf
965 PjllRaw = j__udyAllocJLL3(cJU_LEAF4_MAXPOP1, Pjpm);
966 if (PjllRaw == (Pjlb_t)NULL) return(-1); // out of memory
967
968 Pjll = P_JLL(PjllRaw);
969
970 // Copy Index area into new Leaf
971 j__udyCopyWto3((uint8_t *) Pjll, StageA, cJU_LEAF4_MAXPOP1);
972 #ifdef JUDYL
973 // Copy Value area into new Leaf
974 Pjvnew = JL_LEAF3VALUEAREA(Pjll, cJU_LEAF4_MAXPOP1);
975 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAF4_MAXPOP1);
976 #endif
977 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAF4_MAXPOP1, 3);)
978
979 DcdP0 = JU_JPDCDPOP0(Pjp) | (CIndex & cJU_DCDMASK(3));
980 JU_JPSETADT(Pjp, (Word_t)PjllRaw, DcdP0, cJU_JPLEAF3);
981
982 return(1);
983 }
984
985 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
986
987 StageJBB = StageJBBZero; // zero staged bitmap branch
988 ZEROJP(SubJPCount);
989
990 // Splay the 4 byte index Leaf to 3 byte Index Leaves
991 for (ExpCnt = Start = 0, End = 1; ; End++)
992 {
993 // Check if new expanse or last one
994 if ( (End == cJU_LEAF4_MAXPOP1)
995 ||
996 (JU_DIGITATSTATE(CIndex ^ StageA[End], 4))
997 )
998 {
999 // Build a leaf below the previous expanse
1000
1001 Pjp_t PjpJP = StageJP + ExpCnt;
1002 Word_t Pop1 = End - Start;
1003 Word_t expanse = JU_DIGITATSTATE(CIndex, 4);
1004 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
1005 //
1006 // set the bit that is the current expanse
1007 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
1008 #ifdef SUBEXPCOUNTS
1009 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
1010 #endif
1011 // count number of expanses in each subexpanse
1012 SubJPCount[subexp]++;
1013
1014 // Save byte expanse of leaf
1015 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex, 4);
1016
1017 if (Pop1 == 1) // cJU_JPIMMED_3_01
1018 {
1019 Word_t DcdP0;
1020 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(3)) |
1021 CIndex;
1022 #ifdef JUDY1
1023 JU_JPSETADT(PjpJP, 0, DcdP0, cJ1_JPIMMED_3_01);
1024 #else // JUDYL
1025 JU_JPSETADT(PjpJP, Pjv[Start], DcdP0,
1026 cJL_JPIMMED_3_01);
1027 #endif // JUDYL
1028 }
1029 else if (Pop1 <= cJU_IMMED3_MAXPOP1)
1030 {
1031 // cJ1_JPIMMED_3_02 : Judy1 32
1032 // cJL_JPIMMED_3_02 : JudyL 64
1033 // cJ1_JPIMMED_3_02..5: Judy1 64
1034
1035 #ifdef JUDYL
1036 // Alloc is 1st in case of malloc fail
1037 Pjv_t PjvnewRaw; // value area of new leaf.
1038 Pjv_t Pjvnew;
1039
1040 // Allocate Value area for Immediate Leaf
1041 PjvnewRaw = j__udyLAllocJV(Pop1, Pjpm);
1042 if (PjvnewRaw == (Pjv_t) NULL)
1043 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1044
1045 Pjvnew = P_JV(PjvnewRaw);
1046
1047 // Copy to Values to Value Leaf
1048 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1049 PjpJP->jp_Addr = (Word_t) PjvnewRaw;
1050
1051 // Copy to Index to JP as an immediate Leaf
1052 j__udyCopyWto3(PjpJP->jp_LIndex,
1053 StageA + Start, Pop1);
1054 #else
1055 j__udyCopyWto3(PjpJP->jp_1Index,
1056 StageA + Start, Pop1);
1057 #endif
1058 // Set type, population and Index size
1059 PjpJP->jp_Type = cJU_JPIMMED_3_02 + Pop1 - 2;
1060 }
1061 else
1062 {
1063 // cJU_JPLEAF3
1064 Word_t DcdP0;
1065 Pjll_t PjllRaw; // pointer to new leaf.
1066 Pjll_t Pjll;
1067 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1068
1069 PjllRaw = j__udyAllocJLL3(Pop1, Pjpm);
1070 if (PjllRaw == (Pjll_t)NULL)
1071 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1072
1073 Pjll = P_JLL(PjllRaw);
1074
1075 // Copy Indexes to new Leaf
1076 j__udyCopyWto3((uint8_t *) Pjll, StageA + Start,
1077 Pop1);
1078 #ifdef JUDYL
1079 // Copy to Values to new Leaf
1080 Pjvnew = JL_LEAF3VALUEAREA(Pjll, Pop1);
1081 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1082 #endif
1083 DBGCODE(JudyCheckSorted(Pjll, Pop1, 3);)
1084
1085 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(4))
1086 |
1087 (CIndex & cJU_DCDMASK(4-1))
1088 |
1089 (Pop1 - 1);
1090
1091 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, DcdP0,
1092 cJU_JPLEAF3);
1093 }
1094 ExpCnt++;
1095 // Done?
1096 if (End == cJU_LEAF4_MAXPOP1) break;
1097
1098 // New Expanse, Start and Count
1099 CIndex = StageA[End];
1100 Start = End;
1101 }
1102 }
1103
1104 // Now put all the Leaves below a BranchL or BranchB:
1105 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
1106 {
1107 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
1108 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1109
1110 Pjp->jp_Type = cJU_JPBRANCH_L4;
1111 }
1112 else
1113 {
1114 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
1115 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1116 }
1117 return(1);
1118
1119 } // j__udyCascade4()
1120
1121
1122 // ****************************************************************************
1123 // __ J U D Y C A S C A D E 5
1124 //
1125 // Cascade from a cJU_JPLEAF5 to one of the following:
1126 // 1. if leaf is in 1 expanse:
1127 // compress it into a JPLEAF4
1128 // 2. if leaf contains multiple expanses:
1129 // create linear or bitmap branch containing
1130 // each new expanse is either a:
1131 // JPIMMED_4_01 branch
1132 // JPLEAF4
1133
1134 FUNCTION int j__udyCascade5(
1135 Pjp_t Pjp,
1136 Pvoid_t Pjpm)
1137 {
1138 uint8_t * PLeaf; // pointer to leaf, explicit type.
1139 Word_t End, Start; // temporaries.
1140 Word_t ExpCnt; // count of expanses of splay.
1141 Word_t CIndex; // current Index word.
1142 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
1143
1144 // Temp staging for parts(Leaves) of newly splayed leaf
1145 jp_t StageJP [cJU_LEAF5_MAXPOP1]; // JPs of new leaves
1146 Word_t StageA [cJU_LEAF5_MAXPOP1];
1147 uint8_t StageExp [cJU_LEAF5_MAXPOP1]; // Expanses of new leaves
1148 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
1149 jbb_t StageJBB; // staged bitmap branch
1150
1151 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF5);
1152 assert((JU_JPDCDPOP0(Pjp) & 0xFFFFFFFFFF) == (cJU_LEAF5_MAXPOP1-1));
1153
1154 // Get the address of the Leaf
1155 PLeaf = (uint8_t *) P_JLL(Pjp->jp_Addr);
1156
1157 // Extract 5 byte index Leaf to Word_t
1158 j__udyCopy5toW(StageA, PLeaf, cJU_LEAF5_MAXPOP1);
1159
1160 // Get the address of the Leaf and Value area
1161 JUDYLCODE(Pjv = JL_LEAF5VALUEAREA(PLeaf, cJU_LEAF5_MAXPOP1);)
1162
1163 // If Leaf is in 1 expanse -- just compress it (compare 1st, last & Index)
1164
1165 CIndex = StageA[0];
1166 if (!JU_DIGITATSTATE(CIndex ^ StageA[cJU_LEAF5_MAXPOP1-1], 5))
1167 {
1168 Word_t DcdP0;
1169 Pjll_t PjllRaw; // pointer to new leaf.
1170 Pjll_t Pjll;
1171 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1172
1173 // Alloc a 4 byte Index Leaf
1174 PjllRaw = j__udyAllocJLL4(cJU_LEAF5_MAXPOP1, Pjpm);
1175 if (PjllRaw == (Pjlb_t)NULL) return(-1); // out of memory
1176
1177 Pjll = P_JLL(PjllRaw);
1178
1179 // Copy Index area into new Leaf
1180 j__udyCopyWto4((uint8_t *) Pjll, StageA, cJU_LEAF5_MAXPOP1);
1181 #ifdef JUDYL
1182 // Copy Value area into new Leaf
1183 Pjvnew = JL_LEAF4VALUEAREA(Pjll, cJU_LEAF5_MAXPOP1);
1184 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAF5_MAXPOP1);
1185 #endif
1186 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAF5_MAXPOP1, 4);)
1187
1188 DcdP0 = JU_JPDCDPOP0(Pjp) | (CIndex & cJU_DCDMASK(4));
1189 JU_JPSETADT(Pjp, (Word_t)PjllRaw, DcdP0, cJU_JPLEAF4);
1190
1191 return(1);
1192 }
1193
1194 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
1195
1196 StageJBB = StageJBBZero; // zero staged bitmap branch
1197 ZEROJP(SubJPCount);
1198
1199 // Splay the 5 byte index Leaf to 4 byte Index Leaves
1200 for (ExpCnt = Start = 0, End = 1; ; End++)
1201 {
1202 // Check if new expanse or last one
1203 if ( (End == cJU_LEAF5_MAXPOP1)
1204 ||
1205 (JU_DIGITATSTATE(CIndex ^ StageA[End], 5))
1206 )
1207 {
1208 // Build a leaf below the previous expanse
1209
1210 Pjp_t PjpJP = StageJP + ExpCnt;
1211 Word_t Pop1 = End - Start;
1212 Word_t expanse = JU_DIGITATSTATE(CIndex, 5);
1213 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
1214 //
1215 // set the bit that is the current expanse
1216 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
1217 #ifdef SUBEXPCOUNTS
1218 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
1219 #endif
1220 // count number of expanses in each subexpanse
1221 SubJPCount[subexp]++;
1222
1223 // Save byte expanse of leaf
1224 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex, 5);
1225
1226 if (Pop1 == 1) // cJU_JPIMMED_4_01
1227 {
1228 Word_t DcdP0;
1229 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(4)) |
1230 CIndex;
1231 #ifdef JUDY1
1232 JU_JPSETADT(PjpJP, 0, DcdP0, cJ1_JPIMMED_4_01);
1233 #else // JUDYL
1234 JU_JPSETADT(PjpJP, Pjv[Start], DcdP0,
1235 cJL_JPIMMED_4_01);
1236 #endif // JUDYL
1237 }
1238 #ifdef JUDY1
1239 else if (Pop1 <= cJ1_IMMED4_MAXPOP1)
1240 {
1241 // cJ1_JPIMMED_4_02..3: Judy1 64
1242
1243 // Copy to Index to JP as an immediate Leaf
1244 j__udyCopyWto4(PjpJP->jp_1Index,
1245 StageA + Start, Pop1);
1246
1247 // Set pointer, type, population and Index size
1248 PjpJP->jp_Type = cJ1_JPIMMED_4_02 + Pop1 - 2;
1249 }
1250 #endif
1251 else
1252 {
1253 // cJU_JPLEAF4
1254 Word_t DcdP0;
1255 Pjll_t PjllRaw; // pointer to new leaf.
1256 Pjll_t Pjll;
1257 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1258
1259 // Get a new Leaf
1260 PjllRaw = j__udyAllocJLL4(Pop1, Pjpm);
1261 if (PjllRaw == (Pjll_t)NULL)
1262 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1263
1264 Pjll = P_JLL(PjllRaw);
1265
1266 // Copy Indexes to new Leaf
1267 j__udyCopyWto4((uint8_t *) Pjll, StageA + Start,
1268 Pop1);
1269 #ifdef JUDYL
1270 // Copy to Values to new Leaf
1271 Pjvnew = JL_LEAF4VALUEAREA(Pjll, Pop1);
1272 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1273 #endif
1274 DBGCODE(JudyCheckSorted(Pjll, Pop1, 4);)
1275
1276 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(5))
1277 |
1278 (CIndex & cJU_DCDMASK(5-1))
1279 |
1280 (Pop1 - 1);
1281
1282 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, DcdP0,
1283 cJU_JPLEAF4);
1284 }
1285 ExpCnt++;
1286 // Done?
1287 if (End == cJU_LEAF5_MAXPOP1) break;
1288
1289 // New Expanse, Start and Count
1290 CIndex = StageA[End];
1291 Start = End;
1292 }
1293 }
1294
1295 // Now put all the Leaves below a BranchL or BranchB:
1296 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
1297 {
1298 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
1299 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1300
1301 Pjp->jp_Type = cJU_JPBRANCH_L5;
1302 }
1303 else
1304 {
1305 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
1306 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1307 }
1308 return(1);
1309
1310 } // j__udyCascade5()
1311
1312
1313 // ****************************************************************************
1314 // __ J U D Y C A S C A D E 6
1315 //
1316 // Cascade from a cJU_JPLEAF6 to one of the following:
1317 // 1. if leaf is in 1 expanse:
1318 // compress it into a JPLEAF5
1319 // 2. if leaf contains multiple expanses:
1320 // create linear or bitmap branch containing
1321 // each new expanse is either a:
1322 // JPIMMED_5_01 ... JPIMMED_5_03 branch
1323 // JPIMMED_5_01 branch
1324 // JPLEAF5
1325
1326 FUNCTION int j__udyCascade6(
1327 Pjp_t Pjp,
1328 Pvoid_t Pjpm)
1329 {
1330 uint8_t * PLeaf; // pointer to leaf, explicit type.
1331 Word_t End, Start; // temporaries.
1332 Word_t ExpCnt; // count of expanses of splay.
1333 Word_t CIndex; // current Index word.
1334 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
1335
1336 // Temp staging for parts(Leaves) of newly splayed leaf
1337 jp_t StageJP [cJU_LEAF6_MAXPOP1]; // JPs of new leaves
1338 Word_t StageA [cJU_LEAF6_MAXPOP1];
1339 uint8_t StageExp [cJU_LEAF6_MAXPOP1]; // Expanses of new leaves
1340 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
1341 jbb_t StageJBB; // staged bitmap branch
1342
1343 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF6);
1344 assert((JU_JPDCDPOP0(Pjp) & 0xFFFFFFFFFFFF) == (cJU_LEAF6_MAXPOP1-1));
1345
1346 // Get the address of the Leaf
1347 PLeaf = (uint8_t *) P_JLL(Pjp->jp_Addr);
1348
1349 // Extract 6 byte index Leaf to Word_t
1350 j__udyCopy6toW(StageA, PLeaf, cJU_LEAF6_MAXPOP1);
1351
1352 // Get the address of the Leaf and Value area
1353 JUDYLCODE(Pjv = JL_LEAF6VALUEAREA(PLeaf, cJU_LEAF6_MAXPOP1);)
1354
1355 // If Leaf is in 1 expanse -- just compress it (compare 1st, last & Index)
1356
1357 CIndex = StageA[0];
1358 if (!JU_DIGITATSTATE(CIndex ^ StageA[cJU_LEAF6_MAXPOP1-1], 6))
1359 {
1360 Word_t DcdP0;
1361 Pjll_t PjllRaw; // pointer to new leaf.
1362 Pjll_t Pjll;
1363 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1364
1365 // Alloc a 5 byte Index Leaf
1366 PjllRaw = j__udyAllocJLL5(cJU_LEAF6_MAXPOP1, Pjpm);
1367 if (PjllRaw == (Pjlb_t)NULL) return(-1); // out of memory
1368
1369 Pjll = P_JLL(PjllRaw);
1370
1371 // Copy Index area into new Leaf
1372 j__udyCopyWto5((uint8_t *) Pjll, StageA, cJU_LEAF6_MAXPOP1);
1373 #ifdef JUDYL
1374 // Copy Value area into new Leaf
1375 Pjvnew = JL_LEAF5VALUEAREA(Pjll, cJU_LEAF6_MAXPOP1);
1376 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAF6_MAXPOP1);
1377 #endif
1378 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAF6_MAXPOP1, 5);)
1379
1380 DcdP0 = JU_JPDCDPOP0(Pjp) | (CIndex & cJU_DCDMASK(5));
1381 JU_JPSETADT(Pjp, (Word_t)PjllRaw, DcdP0, cJU_JPLEAF5);
1382
1383 return(1);
1384 }
1385
1386 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
1387
1388 StageJBB = StageJBBZero; // zero staged bitmap branch
1389 ZEROJP(SubJPCount);
1390
1391 // Splay the 6 byte index Leaf to 5 byte Index Leaves
1392 for (ExpCnt = Start = 0, End = 1; ; End++)
1393 {
1394 // Check if new expanse or last one
1395 if ( (End == cJU_LEAF6_MAXPOP1)
1396 ||
1397 (JU_DIGITATSTATE(CIndex ^ StageA[End], 6))
1398 )
1399 {
1400 // Build a leaf below the previous expanse
1401
1402 Pjp_t PjpJP = StageJP + ExpCnt;
1403 Word_t Pop1 = End - Start;
1404 Word_t expanse = JU_DIGITATSTATE(CIndex, 6);
1405 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
1406 //
1407 // set the bit that is the current expanse
1408 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
1409 #ifdef SUBEXPCOUNTS
1410 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
1411 #endif
1412 // count number of expanses in each subexpanse
1413 SubJPCount[subexp]++;
1414
1415 // Save byte expanse of leaf
1416 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex, 6);
1417
1418 if (Pop1 == 1) // cJU_JPIMMED_5_01
1419 {
1420 Word_t DcdP0;
1421 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(5)) |
1422 CIndex;
1423 #ifdef JUDY1
1424 JU_JPSETADT(PjpJP, 0, DcdP0, cJ1_JPIMMED_5_01);
1425 #else // JUDYL
1426 JU_JPSETADT(PjpJP, Pjv[Start], DcdP0,
1427 cJL_JPIMMED_5_01);
1428 #endif // JUDYL
1429 }
1430 #ifdef JUDY1
1431 else if (Pop1 <= cJ1_IMMED5_MAXPOP1)
1432 {
1433 // cJ1_JPIMMED_5_02..3: Judy1 64
1434
1435 // Copy to Index to JP as an immediate Leaf
1436 j__udyCopyWto5(PjpJP->jp_1Index,
1437 StageA + Start, Pop1);
1438
1439 // Set pointer, type, population and Index size
1440 PjpJP->jp_Type = cJ1_JPIMMED_5_02 + Pop1 - 2;
1441 }
1442 #endif
1443 else
1444 {
1445 // cJU_JPLEAF5
1446 Word_t DcdP0;
1447 Pjll_t PjllRaw; // pointer to new leaf.
1448 Pjll_t Pjll;
1449 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1450
1451 // Get a new Leaf
1452 PjllRaw = j__udyAllocJLL5(Pop1, Pjpm);
1453 if (PjllRaw == (Pjll_t)NULL)
1454 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1455
1456 Pjll = P_JLL(PjllRaw);
1457
1458 // Copy Indexes to new Leaf
1459 j__udyCopyWto5((uint8_t *) Pjll, StageA + Start,
1460 Pop1);
1461
1462 // Copy to Values to new Leaf
1463 #ifdef JUDYL
1464 Pjvnew = JL_LEAF5VALUEAREA(Pjll, Pop1);
1465 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1466 #endif
1467 DBGCODE(JudyCheckSorted(Pjll, Pop1, 5);)
1468
1469 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(6))
1470 |
1471 (CIndex & cJU_DCDMASK(6-1))
1472 |
1473 (Pop1 - 1);
1474
1475 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, DcdP0,
1476 cJU_JPLEAF5);
1477 }
1478 ExpCnt++;
1479 // Done?
1480 if (End == cJU_LEAF6_MAXPOP1) break;
1481
1482 // New Expanse, Start and Count
1483 CIndex = StageA[End];
1484 Start = End;
1485 }
1486 }
1487
1488 // Now put all the Leaves below a BranchL or BranchB:
1489 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
1490 {
1491 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
1492 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1493
1494 Pjp->jp_Type = cJU_JPBRANCH_L6;
1495 }
1496 else
1497 {
1498 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
1499 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1500 }
1501 return(1);
1502
1503 } // j__udyCascade6()
1504
1505
1506 // ****************************************************************************
1507 // __ J U D Y C A S C A D E 7
1508 //
1509 // Cascade from a cJU_JPLEAF7 to one of the following:
1510 // 1. if leaf is in 1 expanse:
1511 // compress it into a JPLEAF6
1512 // 2. if leaf contains multiple expanses:
1513 // create linear or bitmap branch containing
1514 // each new expanse is either a:
1515 // JPIMMED_6_01 ... JPIMMED_6_02 branch
1516 // JPIMMED_6_01 branch
1517 // JPLEAF6
1518
1519 FUNCTION int j__udyCascade7(
1520 Pjp_t Pjp,
1521 Pvoid_t Pjpm)
1522 {
1523 uint8_t * PLeaf; // pointer to leaf, explicit type.
1524 Word_t End, Start; // temporaries.
1525 Word_t ExpCnt; // count of expanses of splay.
1526 Word_t CIndex; // current Index word.
1527 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
1528
1529 // Temp staging for parts(Leaves) of newly splayed leaf
1530 jp_t StageJP [cJU_LEAF7_MAXPOP1]; // JPs of new leaves
1531 Word_t StageA [cJU_LEAF7_MAXPOP1];
1532 uint8_t StageExp [cJU_LEAF7_MAXPOP1]; // Expanses of new leaves
1533 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
1534 jbb_t StageJBB; // staged bitmap branch
1535
1536 assert(JU_JPTYPE(Pjp) == cJU_JPLEAF7);
1537 assert(JU_JPDCDPOP0(Pjp) == (cJU_LEAF7_MAXPOP1-1));
1538
1539 // Get the address of the Leaf
1540 PLeaf = (uint8_t *) P_JLL(Pjp->jp_Addr);
1541
1542 // Extract 7 byte index Leaf to Word_t
1543 j__udyCopy7toW(StageA, PLeaf, cJU_LEAF7_MAXPOP1);
1544
1545 // Get the address of the Leaf and Value area
1546 JUDYLCODE(Pjv = JL_LEAF7VALUEAREA(PLeaf, cJU_LEAF7_MAXPOP1);)
1547
1548 // If Leaf is in 1 expanse -- just compress it (compare 1st, last & Index)
1549
1550 CIndex = StageA[0];
1551 if (!JU_DIGITATSTATE(CIndex ^ StageA[cJU_LEAF7_MAXPOP1-1], 7))
1552 {
1553 Word_t DcdP0;
1554 Pjll_t PjllRaw; // pointer to new leaf.
1555 Pjll_t Pjll;
1556 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1557
1558 // Alloc a 6 byte Index Leaf
1559 PjllRaw = j__udyAllocJLL6(cJU_LEAF7_MAXPOP1, Pjpm);
1560 if (PjllRaw == (Pjlb_t)NULL) return(-1); // out of memory
1561
1562 Pjll = P_JLL(PjllRaw);
1563
1564 // Copy Index area into new Leaf
1565 j__udyCopyWto6((uint8_t *) Pjll, StageA, cJU_LEAF7_MAXPOP1);
1566 #ifdef JUDYL
1567 // Copy Value area into new Leaf
1568 Pjvnew = JL_LEAF6VALUEAREA(Pjll, cJU_LEAF7_MAXPOP1);
1569 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAF7_MAXPOP1);
1570 #endif
1571 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAF7_MAXPOP1, 6);)
1572
1573 DcdP0 = JU_JPDCDPOP0(Pjp) | (CIndex & cJU_DCDMASK(6));
1574 JU_JPSETADT(Pjp, (Word_t)PjllRaw, DcdP0, cJU_JPLEAF6);
1575
1576 return(1);
1577 }
1578
1579 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
1580
1581 StageJBB = StageJBBZero; // zero staged bitmap branch
1582 ZEROJP(SubJPCount);
1583
1584 // Splay the 7 byte index Leaf to 6 byte Index Leaves
1585 for (ExpCnt = Start = 0, End = 1; ; End++)
1586 {
1587 // Check if new expanse or last one
1588 if ( (End == cJU_LEAF7_MAXPOP1)
1589 ||
1590 (JU_DIGITATSTATE(CIndex ^ StageA[End], 7))
1591 )
1592 {
1593 // Build a leaf below the previous expanse
1594
1595 Pjp_t PjpJP = StageJP + ExpCnt;
1596 Word_t Pop1 = End - Start;
1597 Word_t expanse = JU_DIGITATSTATE(CIndex, 7);
1598 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
1599 //
1600 // set the bit that is the current expanse
1601 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
1602 #ifdef SUBEXPCOUNTS
1603 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
1604 #endif
1605 // count number of expanses in each subexpanse
1606 SubJPCount[subexp]++;
1607
1608 // Save byte expanse of leaf
1609 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex, 7);
1610
1611 if (Pop1 == 1) // cJU_JPIMMED_6_01
1612 {
1613 Word_t DcdP0;
1614 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(6)) |
1615 CIndex;
1616 #ifdef JUDY1
1617 JU_JPSETADT(PjpJP, 0, DcdP0, cJ1_JPIMMED_6_01);
1618 #else // JUDYL
1619 JU_JPSETADT(PjpJP, Pjv[Start], DcdP0,
1620 cJL_JPIMMED_6_01);
1621 #endif // JUDYL
1622 }
1623 #ifdef JUDY1
1624 else if (Pop1 == cJ1_IMMED6_MAXPOP1)
1625 {
1626 // cJ1_JPIMMED_6_02: Judy1 64
1627
1628 // Copy to Index to JP as an immediate Leaf
1629 j__udyCopyWto6(PjpJP->jp_1Index,
1630 StageA + Start, 2);
1631
1632 // Set pointer, type, population and Index size
1633 PjpJP->jp_Type = cJ1_JPIMMED_6_02;
1634 }
1635 #endif
1636 else
1637 {
1638 // cJU_JPLEAF6
1639 Word_t DcdP0;
1640 Pjll_t PjllRaw; // pointer to new leaf.
1641 Pjll_t Pjll;
1642 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1643
1644 // Get a new Leaf
1645 PjllRaw = j__udyAllocJLL6(Pop1, Pjpm);
1646 if (PjllRaw == (Pjll_t)NULL)
1647 FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1648 Pjll = P_JLL(PjllRaw);
1649
1650 // Copy Indexes to new Leaf
1651 j__udyCopyWto6((uint8_t *) Pjll, StageA + Start,
1652 Pop1);
1653 #ifdef JUDYL
1654 // Copy to Values to new Leaf
1655 Pjvnew = JL_LEAF6VALUEAREA(Pjll, Pop1);
1656 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1657 #endif
1658 DBGCODE(JudyCheckSorted(Pjll, Pop1, 6);)
1659
1660 DcdP0 = (JU_JPDCDPOP0(Pjp) & cJU_DCDMASK(7))
1661 |
1662 (CIndex & cJU_DCDMASK(7-1))
1663 |
1664 (Pop1 - 1);
1665
1666 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, DcdP0,
1667 cJU_JPLEAF6);
1668 }
1669 ExpCnt++;
1670 // Done?
1671 if (End == cJU_LEAF7_MAXPOP1) break;
1672
1673 // New Expanse, Start and Count
1674 CIndex = StageA[End];
1675 Start = End;
1676 }
1677 }
1678
1679 // Now put all the Leaves below a BranchL or BranchB:
1680 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
1681 {
1682 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
1683 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1684
1685 Pjp->jp_Type = cJU_JPBRANCH_L7;
1686 }
1687 else
1688 {
1689 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
1690 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1691 }
1692 return(1);
1693
1694 } // j__udyCascade7()
1695
1696 #endif // JU_64BIT
1697
1698
1699 // ****************************************************************************
1700 // __ J U D Y C A S C A D E L
1701 //
1702 // (Compressed) cJU_LEAF3[7], cJ1_JPBRANCH_L.
1703 //
1704 // Cascade from a LEAFW (under Pjp) to one of the following:
1705 // 1. if LEAFW is in 1 expanse:
1706 // create linear branch with a JPLEAF3[7] under it
1707 // 2. LEAFW contains multiple expanses:
1708 // create linear or bitmap branch containing new expanses
1709 // each new expanse is either a: 32 64
1710 // JPIMMED_3_01 branch Y N
1711 // JPIMMED_7_01 branch N Y
1712 // JPLEAF3 Y N
1713 // JPLEAF7 N Y
1714
1715 FUNCTION int j__udyCascadeL(
1716 Pjp_t Pjp,
1717 Pvoid_t Pjpm)
1718 {
1719 Pjlw_t Pjlw; // leaf to work on.
1720 Word_t End, Start; // temporaries.
1721 Word_t ExpCnt; // count of expanses of splay.
1722 Word_t CIndex; // current Index word.
1723 JUDYLCODE(Pjv_t Pjv;) // value area of leaf.
1724
1725 // Temp staging for parts(Leaves) of newly splayed leaf
1726 jp_t StageJP [cJU_LEAFW_MAXPOP1];
1727 uint8_t StageExp[cJU_LEAFW_MAXPOP1];
1728 uint8_t SubJPCount[cJU_NUMSUBEXPB]; // JPs in each subexpanse
1729 jbb_t StageJBB; // staged bitmap branch
1730
1731 // Get the address of the Leaf
1732 Pjlw = P_JLW(Pjp->jp_Addr);
1733
1734 assert(Pjlw[0] == (cJU_LEAFW_MAXPOP1 - 1));
1735
1736 // Get pointer to Value area of old Leaf
1737 JUDYLCODE(Pjv = JL_LEAFWVALUEAREA(Pjlw, cJU_LEAFW_MAXPOP1);)
1738
1739 Pjlw++; // Now point to Index area
1740
1741 // If Leaf is in 1 expanse -- first compress it (compare 1st, last & Index):
1742
1743 CIndex = Pjlw[0]; // also used far below
1744 if (!JU_DIGITATSTATE(CIndex ^ Pjlw[cJU_LEAFW_MAXPOP1 - 1],
1745 cJU_ROOTSTATE))
1746 {
1747 Pjll_t PjllRaw; // pointer to new leaf.
1748 Pjll_t Pjll;
1749 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1750
1751 // Get the common expanse to all elements in Leaf
1752 StageExp[0] = JU_DIGITATSTATE(CIndex, cJU_ROOTSTATE);
1753
1754 // Alloc a 3[7] byte Index Leaf
1755 #ifdef JU_64BIT
1756 PjllRaw = j__udyAllocJLL7(cJU_LEAFW_MAXPOP1, Pjpm);
1757 if (PjllRaw == (Pjlb_t)NULL) return(-1); // out of memory
1758
1759 Pjll = P_JLL(PjllRaw);
1760
1761 // Copy LEAFW to a cJU_JPLEAF7
1762 j__udyCopyWto7((uint8_t *) Pjll, Pjlw, cJU_LEAFW_MAXPOP1);
1763 #ifdef JUDYL
1764 // Get the Value area of new Leaf
1765 Pjvnew = JL_LEAF7VALUEAREA(Pjll, cJU_LEAFW_MAXPOP1);
1766 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAFW_MAXPOP1);
1767 #endif
1768 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAFW_MAXPOP1, 7);)
1769 #else // 32 Bit
1770 PjllRaw = j__udyAllocJLL3(cJU_LEAFW_MAXPOP1, Pjpm);
1771 if (PjllRaw == (Pjll_t) NULL) return(-1);
1772
1773 Pjll = P_JLL(PjllRaw);
1774
1775 // Copy LEAFW to a cJU_JPLEAF3
1776 j__udyCopyWto3((uint8_t *) Pjll, Pjlw, cJU_LEAFW_MAXPOP1);
1777 #ifdef JUDYL
1778 // Get the Value area of new Leaf
1779 Pjvnew = JL_LEAF3VALUEAREA(Pjll, cJU_LEAFW_MAXPOP1);
1780 JU_COPYMEM(Pjvnew, Pjv, cJU_LEAFW_MAXPOP1);
1781 #endif
1782 DBGCODE(JudyCheckSorted(Pjll, cJU_LEAFW_MAXPOP1, 3);)
1783 #endif // 32 Bit
1784
1785 // Following not needed because cJU_DCDMASK(3[7]) is == 0
1786 ////// StageJP[0].jp_DcdPopO |= (CIndex & cJU_DCDMASK(3[7]));
1787 #ifdef JU_64BIT
1788 JU_JPSETADT(&(StageJP[0]), (Word_t)PjllRaw, cJU_LEAFW_MAXPOP1-1,
1789 cJU_JPLEAF7);
1790 #else // 32BIT
1791 JU_JPSETADT(&(StageJP[0]), (Word_t)PjllRaw, cJU_LEAFW_MAXPOP1-1,
1792 cJU_JPLEAF3);
1793 #endif // 32BIT
1794 // Create a 1 element Linear branch
1795 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, 1, Pjpm) == -1)
1796 return(-1);
1797
1798 // Change the type of callers JP
1799 Pjp->jp_Type = cJU_JPBRANCH_L;
1800
1801 return(1);
1802 }
1803
1804 // Else in 2+ expanses, splay Leaf into smaller leaves at higher compression
1805
1806 StageJBB = StageJBBZero; // zero staged bitmap branch
1807 ZEROJP(SubJPCount);
1808
1809 // Splay the 4[8] byte Index Leaf to 3[7] byte Index Leaves
1810 for (ExpCnt = Start = 0, End = 1; ; End++)
1811 {
1812 // Check if new expanse or last one
1813 if ( (End == cJU_LEAFW_MAXPOP1)
1814 ||
1815 (JU_DIGITATSTATE(CIndex ^ Pjlw[End], cJU_ROOTSTATE))
1816 )
1817 {
1818 // Build a leaf below the previous expanse
1819
1820 Pjp_t PjpJP = StageJP + ExpCnt;
1821 Word_t Pop1 = End - Start;
1822 Word_t expanse = JU_DIGITATSTATE(CIndex, cJU_ROOTSTATE);
1823 Word_t subexp = expanse / cJU_BITSPERSUBEXPB;
1824 //
1825 // set the bit that is the current expanse
1826 JU_JBB_BITMAP(&StageJBB, subexp) |= JU_BITPOSMASKB(expanse);
1827 #ifdef SUBEXPCOUNTS
1828 StageJBB.jbb_subPop1[subexp] += Pop1; // pop of subexpanse
1829 #endif
1830 // count number of expanses in each subexpanse
1831 SubJPCount[subexp]++;
1832
1833 // Save byte expanse of leaf
1834 StageExp[ExpCnt] = JU_DIGITATSTATE(CIndex,
1835 cJU_ROOTSTATE);
1836
1837 if (Pop1 == 1) // cJU_JPIMMED_3[7]_01
1838 {
1839 #ifdef JU_64BIT
1840 #ifdef JUDY1
1841 JU_JPSETADT(PjpJP, 0, CIndex, cJ1_JPIMMED_7_01);
1842 #else // JUDYL
1843 JU_JPSETADT(PjpJP, Pjv[Start], CIndex,
1844 cJL_JPIMMED_7_01);
1845 #endif // JUDYL
1846
1847 #else // JU_32BIT
1848 #ifdef JUDY1
1849 JU_JPSETADT(PjpJP, 0, CIndex, cJ1_JPIMMED_3_01);
1850 #else // JUDYL
1851 JU_JPSETADT(PjpJP, Pjv[Start], CIndex,
1852 cJL_JPIMMED_3_01);
1853 #endif // JUDYL
1854 #endif // JU_32BIT
1855 }
1856 #ifdef JUDY1
1857 #ifdef JU_64BIT
1858 else if (Pop1 <= cJ1_IMMED7_MAXPOP1)
1859 #else
1860 else if (Pop1 <= cJ1_IMMED3_MAXPOP1)
1861 #endif
1862 {
1863 // cJ1_JPIMMED_3_02 : Judy1 32
1864 // cJ1_JPIMMED_7_02 : Judy1 64
1865 // Copy to JP as an immediate Leaf
1866 #ifdef JU_64BIT
1867 j__udyCopyWto7(PjpJP->jp_1Index, Pjlw+Start, 2);
1868 PjpJP->jp_Type = cJ1_JPIMMED_7_02;
1869 #else
1870 j__udyCopyWto3(PjpJP->jp_1Index, Pjlw+Start, 2);
1871 PjpJP->jp_Type = cJ1_JPIMMED_3_02;
1872 #endif // 32 Bit
1873 }
1874 #endif // JUDY1
1875 else // Linear Leaf JPLEAF3[7]
1876 {
1877 // cJU_JPLEAF3[7]
1878 Pjll_t PjllRaw; // pointer to new leaf.
1879 Pjll_t Pjll;
1880 JUDYLCODE(Pjv_t Pjvnew;) // value area of new leaf.
1881 #ifdef JU_64BIT
1882 PjllRaw = j__udyAllocJLL7(Pop1, Pjpm);
1883 if (PjllRaw == (Pjll_t) NULL) return(-1);
1884 Pjll = P_JLL(PjllRaw);
1885
1886 j__udyCopyWto7((uint8_t *) Pjll, Pjlw + Start,
1887 Pop1);
1888 #ifdef JUDYL
1889 Pjvnew = JL_LEAF7VALUEAREA(Pjll, Pop1);
1890 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1891 #endif // JUDYL
1892 DBGCODE(JudyCheckSorted(Pjll, Pop1, 7);)
1893 #else // JU_64BIT - 32 Bit
1894 PjllRaw = j__udyAllocJLL3(Pop1, Pjpm);
1895 if (PjllRaw == (Pjll_t) NULL) return(-1);
1896 Pjll = P_JLL(PjllRaw);
1897
1898 j__udyCopyWto3((uint8_t *) Pjll, Pjlw + Start,
1899 Pop1);
1900 #ifdef JUDYL
1901 Pjvnew = JL_LEAF3VALUEAREA(Pjll, Pop1);
1902 JU_COPYMEM(Pjvnew, Pjv + Start, Pop1);
1903 #endif // JUDYL
1904 DBGCODE(JudyCheckSorted(Pjll, Pop1, 3);)
1905 #endif // 32 Bit
1906
1907 #ifdef JU_64BIT
1908 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, Pop1 - 1,
1909 cJU_JPLEAF7);
1910 #else // JU_64BIT - 32 Bit
1911 JU_JPSETADT(PjpJP, (Word_t)PjllRaw, Pop1 - 1,
1912 cJU_JPLEAF3);
1913 #endif // 32 Bit
1914 }
1915 ExpCnt++;
1916 // Done?
1917 if (End == cJU_LEAFW_MAXPOP1) break;
1918
1919 // New Expanse, Start and Count
1920 CIndex = Pjlw[End];
1921 Start = End;
1922 }
1923 }
1924
1925 // Now put all the Leaves below a BranchL or BranchB:
1926 if (ExpCnt <= cJU_BRANCHLMAXJPS) // put the Leaves below a BranchL
1927 {
1928 if (j__udyCreateBranchL(Pjp, StageJP, StageExp, ExpCnt,
1929 Pjpm) == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1930
1931 Pjp->jp_Type = cJU_JPBRANCH_L;
1932 }
1933 else
1934 {
1935 if (j__udyStageJBBtoJBB(Pjp, &StageJBB, StageJP, SubJPCount, Pjpm)
1936 == -1) FREEALLEXIT(ExpCnt, StageJP, Pjpm);
1937
1938 Pjp->jp_Type = cJU_JPBRANCH_B; // cJU_LEAFW is out of sequence
1939 }
1940 return(1);
1941
1942 } // j__udyCascadeL()