File tree Expand file tree Collapse file tree 6 files changed +70
-20
lines changed
main/java/convex/core/data
test/java/convex/core/data Expand file tree Collapse file tree 6 files changed +70
-20
lines changed Original file line number Diff line number Diff line change @@ -436,8 +436,8 @@ public int getBranchCount() {
436
436
for (int i =0 ; i <rc ; i ++) {
437
437
Ref <?> r =c .getRef (i );
438
438
if (r .isEmbedded ()) {
439
- ACell child = r . getValue ();
440
- if ( child != null ) result +=child . getBranchCount ();
439
+ // need to recursively count branches in embedded Ref
440
+ result +=r . branchCount ();
441
441
} else {
442
442
// we found a branch!
443
443
result +=1 ;
@@ -460,12 +460,12 @@ public <T extends ACell> Ref<T> getBranchRef(int index) {
460
460
Ref <?> r =c .getRef (i );
461
461
if (r .isEmbedded ()) {
462
462
ACell child =r .getValue ();
463
- if (child ==null ) continue ; // no branch here
463
+ if (child ==null ) continue ; // no branch here!
464
464
int cbc =child .getBranchCount ();
465
465
if (cbc >index ) return child .getBranchRef (index );
466
466
index -=cbc ;
467
467
} else {
468
- if (index ==0 ) return (Ref <T >) r ;
468
+ if (index ==0 ) return (Ref <T >) r ; // we are pointing to exactly this branch
469
469
index -=1 ;
470
470
}
471
471
}
@@ -573,7 +573,7 @@ public void attachRef(Ref<?> ref) {
573
573
}
574
574
575
575
/**
576
- * Tests if this Cell is completely encoded, i.e. has no external Refs. This implies that the
576
+ * Tests if this Cell is completely encoded, i.e. has no external branch Refs. This implies that the
577
577
* complete Cell can be represented in a single encoding.
578
578
* @return true if completely encoded, false otherwise
579
579
*/
Original file line number Diff line number Diff line change @@ -19,8 +19,9 @@ public class Cells {
19
19
20
20
/**
21
21
* The maximum number of branches possible from a single Cell encoding
22
+ * TODO: Verify this is 68 (vector with 16 embedded children with 4 branches each + embedded tail with 4 branches)
22
23
*/
23
- public static final int MAX_BRANCH_COUNT = 72 ;
24
+ public static final int MAX_BRANCH_COUNT = 68 ;
24
25
25
26
/**
26
27
* Equality method allowing for nulls
@@ -69,19 +70,9 @@ public static int refCount(ACell a) {
69
70
* @param a Cell to check (may be null)
70
71
* @return Number of Refs in the object.
71
72
*/
72
- public static int branchCount (ACell a ) {
73
- if (a ==null ) return 0 ;
74
- int n =a .getRefCount ();
75
- int bc =0 ;
76
- for (int i =0 ; i <n ; i ++) {
77
- Ref <?> ref =a .getRef (i );
78
- if (ref .isEmbedded ()) {
79
- bc +=branchCount (ref .getValue ());
80
- } else {
81
- bc ++;
82
- }
83
- }
84
- return bc ;
73
+ public static int branchCount (ACell v ) {
74
+ if (v ==null ) return 0 ;
75
+ return v .getBranchCount ();
85
76
}
86
77
87
78
/**
@@ -93,7 +84,7 @@ public static int branchCount(ACell a) {
93
84
* @return Ref for cell
94
85
*/
95
86
public static <R extends ACell > Ref <R > getRef (ACell cell , int index ) {
96
- if (cell ==null ) throw new IndexOutOfBoundsException ("Bad ref index called on null" );
87
+ if (cell ==null ) throw new IndexOutOfBoundsException ("getRef called on null" );
97
88
return cell .getRef (index );
98
89
}
99
90
Original file line number Diff line number Diff line change @@ -236,6 +236,16 @@ public Ref<T> withMinimumStatus(int newStatus) {
236
236
* @return The value contained in this Ref
237
237
*/
238
238
public abstract T getValue ();
239
+
240
+ /**
241
+ * Get the number of child branches from this Ref
242
+ * @return Number of branches
243
+ */
244
+ public int branchCount () {
245
+ ACell v =getValue ();
246
+ if (v ==null ) return 0 ;
247
+ return v .getBranchCount ();
248
+ }
239
249
240
250
@ Override
241
251
public int hashCode () {
Original file line number Diff line number Diff line change @@ -147,6 +147,9 @@ public static long uniqueRefCount(ACell a) {
147
147
Set <Ref <?>> rs =accumulateRefSet (a );
148
148
return rs .size ();
149
149
}
150
+
151
+
152
+
150
153
151
154
/**
152
155
* Utility function to locate missing data
Original file line number Diff line number Diff line change @@ -80,10 +80,31 @@ private static void doBranchTests(ACell a) {
80
80
assertTrue (bc >=0 );
81
81
assertTrue (bc <=Cells .MAX_BRANCH_COUNT );
82
82
83
+ // out of range branches should be null
84
+ assertNull (a .getBranchRef (-1 ));
85
+ assertNull (a .getBranchRef (bc ));
86
+
87
+ // bc of zero equivalent to completely encoded
88
+ assertEquals (a .isCompletelyEncoded (),bc ==0 );
89
+
90
+ int rc =a .getRefCount ();
91
+ if (rc ==0 ) {
92
+ // if no Refs, clearly none of them can be branches!
93
+ assertEquals (0 ,bc );
94
+ } else if (rc ==1 ) {
95
+ Ref <?> cr =a .getRef (0 );
96
+ if (cr .isEmbedded ()) {
97
+ assertEquals (bc ,cr .branchCount ());
98
+ } else {
99
+ assertEquals (1 ,bc );
100
+ }
101
+ }
102
+
83
103
if (bc ==0 ) {
84
104
assertNull (a .getBranchRef (0 ));
85
105
Cells .visitBranches (a , v ->fail ("Shouldn't visit any branch!" ));
86
106
} else {
107
+ // branch refs within range should be non-null
87
108
assertNotNull (a .getBranchRef (0 ));
88
109
assertNotNull (a .getBranchRef (bc -1 ));
89
110
Original file line number Diff line number Diff line change @@ -80,6 +80,31 @@ public void testChunks() {
80
80
AVector <CVMLong > v = Samples .INT_VECTOR_300 .getChunk (0 );
81
81
assertEquals (VectorTree .class , v .getChunk (0 ).concat (v ).getClass ());
82
82
}
83
+
84
+ @ Test
85
+ public void testMaxBranches () {
86
+ // non-embedded vector with 16 elements
87
+ AVector <ACell > nonEmbed =Vectors .repeat (Samples .INT_VECTOR_10 , 16 );
88
+ assertFalse (nonEmbed .isEmbedded ());
89
+
90
+ // embedded tail with 4 branches, count 64
91
+ AVector <ACell > tail =nonEmbed .concat (nonEmbed ).concat (nonEmbed ).concat (nonEmbed );
92
+ assertEquals (4 ,tail .getBranchCount ());
93
+ assertEquals (64 ,tail .count ());
94
+ assertTrue (tail .isEmbedded ());
95
+
96
+
97
+ AVector <ACell > bigEmbed =Vectors .repeat (tail ,16 );
98
+ assertEquals (16 ,bigEmbed .getRefCount ());
99
+ assertEquals (64 ,bigEmbed .getBranchCount ()); // maximum for vector?
100
+
101
+ AVector <ACell > v =tail .concat (bigEmbed );
102
+ assertFalse (v .isCompletelyEncoded ());
103
+
104
+ doVectorTests (nonEmbed );
105
+ doVectorTests (bigEmbed );
106
+ doVectorTests (v );
107
+ }
83
108
84
109
@ Test
85
110
public void testChunkConcat () {
You can’t perform that action at this time.
0 commit comments