@@ -47,7 +47,7 @@ public final class Index<K extends ABlobLike<?>, V extends ACell> extends AIndex
47
47
48
48
/**
49
49
* Entry for this node of the radix tree. Invariant assumption that the prefix
50
- * is correct. May be null if there is no entry at this node.
50
+ * is correct. Will be null if there is no entry at this node.
51
51
*/
52
52
private final MapEntry <K , V > entry ;
53
53
@@ -518,14 +518,28 @@ public int encodeRaw(byte[] bs, int pos) {
518
518
pos = Format .writeVLCCount (bs ,pos , count );
519
519
if (count == 0 ) return pos ; // nothing more to know... this must be the empty singleton
520
520
521
- pos = MapEntry .encodeCompressed (entry ,bs ,pos ); // entry may be null
522
- if (count == 1 ) return pos ; // must be a single entry
521
+ if (count == 1 ) {
522
+ // directly encode single entry
523
+ pos =entry .getKeyRef ().encode (bs ,pos );
524
+ pos =entry .getValueRef ().encode (bs ,pos );
525
+ return pos ; // must be a single entry, exit early
526
+ } else {
527
+ if (entry ==null ) {
528
+ bs [pos ++]=Tag .NULL ; // no entry present
529
+ } else {
530
+ bs [pos ++]=Tag .VECTOR ;
531
+ pos =entry .getKeyRef ().encode (bs ,pos );
532
+ pos =entry .getValueRef ().encode (bs ,pos );
533
+ }
534
+ }
523
535
524
536
// We only have a meaningful depth if more than one entry
525
537
bs [pos ++] = (byte )depth ;
538
+
539
+ // write mask
540
+ pos = Utils .writeShort (bs ,pos ,mask );
526
541
527
542
// finally write children
528
- pos = Utils .writeShort (bs ,pos ,mask );
529
543
int n = children .length ;
530
544
for (int i = 0 ; i < n ; i ++) {
531
545
pos = encodeChild (bs ,pos ,i );
@@ -559,14 +573,22 @@ public static <K extends ABlobLike<?>, V extends ACell> Index<K, V> read(Blob b,
559
573
if (count < 0 ) throw new BadFormatException ("Negative count!" );
560
574
if (count == 0 ) return (Index <K , V >) EMPTY ;
561
575
576
+ // index for reading
562
577
int epos =pos +1 +Format .getVLCCountLength (count );
563
578
564
-
565
- byte etype =b .byteAt (epos ++);
566
579
MapEntry <K ,V > me ;
567
- if (etype ==Tag .NULL ) {
568
- me =null ;
569
- } else if (etype ==Tag .VECTOR ){
580
+ boolean hasEntry ;
581
+ if (count ==1 ) {
582
+ hasEntry =true ;
583
+ } else {
584
+ byte c =b .byteAt (epos ++); // Read byte
585
+ switch (c ) {
586
+ case Tag .NULL : hasEntry =false ; break ;
587
+ case Tag .VECTOR : hasEntry =true ; break ;
588
+ default : throw new BadFormatException ("Invalid MapEntry tag in Index: " +c );
589
+ }
590
+ }
591
+ if (hasEntry ) {
570
592
Ref <K > kr =Format .readRef (b ,epos );
571
593
epos +=kr .getEncodingLength ();
572
594
Ref <V > vr =Format .readRef (b ,epos );
@@ -581,7 +603,7 @@ public static <K extends ABlobLike<?>, V extends ACell> Index<K, V> read(Blob b,
581
603
return result ;
582
604
}
583
605
} else {
584
- throw new BadFormatException ( "Invalid MapEntry tag in Index: " + etype ) ;
606
+ me = null ;
585
607
}
586
608
587
609
Index <K ,V > result ;
0 commit comments