@@ -110,7 +110,7 @@ int count() {
110
110
}
111
111
112
112
// the hash function
113
- public final UnaryOperator <T > hashFn ;
113
+ private final UnaryOperator <T > hashFn ;
114
114
// the concatenation function
115
115
private final BinaryOperator <T > concatFn ;
116
116
// supplier for the value of filler
@@ -266,6 +266,16 @@ public TreeRoot<T> build(Collection<TreeLeaf<T, ?>> leaves) {
266
266
return root ;
267
267
}
268
268
269
+ /**
270
+ * Calculates the hash of the input value using the hash operator of this builder.
271
+ *
272
+ * @param value the value to be hashed.
273
+ * @return the hash value of the input value.
274
+ */
275
+ public T hash (T value ) {
276
+ return this .hashFn .apply (value );
277
+ }
278
+
269
279
/**
270
280
* Checks whether a hash chain is valid using the operators of this builder instance.
271
281
*
@@ -291,9 +301,9 @@ public boolean isValid(List<T> chain, int index, BiPredicate<T, T> eq) {
291
301
final T next = chain .get (k );
292
302
293
303
if ((index & 1 ) == 0 ) {
294
- hash = this . hashFn . apply (this .concatFn .apply (hash , next ));
304
+ hash = hash (this .concatFn .apply (hash , next ));
295
305
} else {
296
- hash = this . hashFn . apply (this .concatFn .apply (next , hash ));
306
+ hash = hash (this .concatFn .apply (next , hash ));
297
307
}
298
308
299
309
index >>>= 1 ;
@@ -302,21 +312,17 @@ public boolean isValid(List<T> chain, int index, BiPredicate<T, T> eq) {
302
312
return eq .test (hash , chain .get (chain .size () - 1 ));
303
313
}
304
314
305
- private Root <T > doBuild (TreeNode <T >[] leaves ) {
315
+ private Root <T > doBuild (TreeNode <T >[] nodes ) {
306
316
// expecting a power of two
307
- assert bitCount (leaves .length ) == 1 ;
317
+ assert bitCount (nodes .length ) == 1 ;
308
318
309
- if (leaves .length == 2 ) {
310
- return newNode (leaves [0 ], leaves [1 ], true );
311
- }
312
-
313
- final TreeNode <T >[] parents = new TreeNode [leaves .length / 2 ];
314
-
315
- for (int i = 0 ; i < leaves .length ; i += 2 ) {
316
- parents [i / 2 ] = newNode (leaves [i ], leaves [i + 1 ], false );
319
+ for (int count = nodes .length ; count > 2 ; count /= 2 ) {
320
+ for (int i = 0 ; i < count ; i += 2 ) {
321
+ nodes [i / 2 ] = newNode (nodes [i ], nodes [i + 1 ], false );
322
+ }
317
323
}
318
324
319
- return doBuild ( parents );
325
+ return newNode ( nodes [ 0 ], nodes [ 1 ], true );
320
326
}
321
327
322
328
private <N extends TreeNode <T >> N newNode (TreeNode <T > left , TreeNode <T > right , boolean root ) {
0 commit comments