Skip to content

Commit e30c49b

Browse files
committed
Improve Index encoding
1 parent d634d6e commit e30c49b

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

convex-core/src/main/java/convex/core/data/Index.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,27 @@ public final class Index<K extends ABlobLike<?>, V extends ACell> extends AIndex
4545
*/
4646
public static final Index<?, ?> EMPTY = Cells.intern(new Index<ABlob, ACell>(0, null, EMPTY_CHILDREN,(short) 0, 0L));
4747

48-
/**
49-
* Child entries, i.e. nodes with keys where this node is a common prefix. Only contains children where mask is set.
50-
* Child entries must have at least one entry.
51-
*/
52-
private final Ref<Index<K, V>>[] children;
53-
5448
/**
5549
* Entry for this node of the radix tree. Invariant assumption that the prefix
5650
* is correct. May be null if there is no entry at this node.
5751
*/
5852
private final MapEntry<K, V> entry;
5953

54+
/**
55+
* Depth of radix tree entry in number of hex digits.
56+
*/
57+
private final long depth;
58+
6059
/**
6160
* Mask of child entries, 16 bits for each hex digit that may be present.
6261
*/
6362
private final short mask;
64-
63+
6564
/**
66-
* Depth of radix tree entry in number of hex digits.
65+
* Child entries, i.e. nodes with keys where this node is a common prefix. Only contains children where mask is set.
66+
* Child entries must have at least one entry.
6767
*/
68-
private final long depth;
69-
68+
private final Ref<Index<K, V>>[] children;
7069

7170
@SuppressWarnings({ "rawtypes", "unchecked" })
7271
protected Index(long depth, MapEntry<K, V> entry, Ref<Index>[] entries,
@@ -523,7 +522,7 @@ public int encodeRaw(byte[] bs, int pos) {
523522
if (count == 1) return pos; // must be a single entry
524523

525524
// We only have a meaningful depth if more than one entry
526-
pos = Format.writeVLCCount(bs,pos, depth);
525+
bs[pos++] = (byte)depth;
527526

528527
// finally write children
529528
pos = Utils.writeShort(bs,pos,mask);
@@ -586,13 +585,12 @@ public static <K extends ABlobLike<?>, V extends ACell> Index<K, V> read(Blob b,
586585
}
587586

588587
Index<K,V> result;
589-
long depth = Format.readVLCCount(b,epos);
590-
if (depth < 0) throw new BadFormatException("Negative depth!");
588+
int depth = 0xFF & b.byteAt(epos);
591589
if (depth >=MAX_DEPTH) {
592590
if (depth==MAX_DEPTH) throw new BadFormatException("More than one entry and MAX_DEPTH");
593591
throw new BadFormatException("Excessive depth!");
594592
}
595-
epos+=Format.getVLCCountLength(depth);
593+
epos+=1;
596594

597595
// Need to include children
598596
short mask = b.shortAt(epos);

convex-core/src/test/java/convex/core/data/SetsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public void testIncludeExclude() {
4141
assertTrue(s.isEmpty());
4242
assertSame(s, Sets.empty());
4343
}
44+
45+
@Test
46+
public void testSetEncoding() {
47+
// Set should be encoded as a map with different tag and extra value Ref(s)
48+
ASet<?> s=Sets.of(123);
49+
AMap<?,?> m=Maps.of(123,null);
50+
assertEquals(m.getEncoding().slice(1),s.getEncoding().append(Blob.SINGLE_ZERO).slice(1));
51+
}
4452

4553
@Test
4654
public void testPrimitiveEquality() {

0 commit comments

Comments
 (0)