From a29aa84450d07b2e80428eb3c587a0a9436f3d9f Mon Sep 17 00:00:00 2001 From: smart_ex Date: Mon, 28 Mar 2022 18:21:32 +1000 Subject: [PATCH] implement indexOf with fromIndex param --- lib/BaseTree.d.ts | 5 +++-- lib/BaseTree.js | 12 +++++------- lib/FixedMerkleTree.d.ts | 4 +++- lib/FixedMerkleTree.js | 7 +++++++ lib/PartialMerkleTree.d.ts | 2 ++ lib/PartialMerkleTree.js | 7 +++++++ src/BaseTree.ts | 13 +++++-------- src/FixedMerkleTree.ts | 11 ++++++++++- src/PartialMerkleTree.ts | 8 ++++++++ 9 files changed, 50 insertions(+), 19 deletions(-) diff --git a/lib/BaseTree.d.ts b/lib/BaseTree.d.ts index 8a6ab46..6f5fbb4 100644 --- a/lib/BaseTree.d.ts +++ b/lib/BaseTree.d.ts @@ -12,11 +12,13 @@ export declare class BaseTree { get root(): Element; /** * Find an element in the tree + * @param elements elements of tree * @param element An element to find * @param comparator A function that checks leaf value equality + * @param fromIndex The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned * @returns {number} Index if element is found, otherwise -1 */ - indexOf(element: Element, comparator?: (arg0: T, arg1: T) => boolean): number; + static indexOf(elements: Element[], element: Element, fromIndex?: number, comparator?: (arg0: T, arg1: T) => boolean): number; /** * Insert new element into the tree * @param element Element to insert @@ -29,7 +31,6 @@ export declare class BaseTree { * @param element Updated element value */ update(index: number, element: Element): void; - proof(element: Element): ProofPath; /** * Get merkle path to a leaf * @param {number} index Leaf index to generate path for diff --git a/lib/BaseTree.js b/lib/BaseTree.js index ab6407f..5d4a33b 100644 --- a/lib/BaseTree.js +++ b/lib/BaseTree.js @@ -20,16 +20,18 @@ class BaseTree { } /** * Find an element in the tree + * @param elements elements of tree * @param element An element to find * @param comparator A function that checks leaf value equality + * @param fromIndex The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned * @returns {number} Index if element is found, otherwise -1 */ - indexOf(element, comparator) { + static indexOf(elements, element, fromIndex, comparator) { if (comparator) { - return this._layers[0].findIndex((el) => comparator(element, el)); + return elements.findIndex((el) => comparator(element, el)); } else { - return this._layers[0].indexOf(element); + return elements.indexOf(element, fromIndex); } } /** @@ -82,10 +84,6 @@ class BaseTree { this._layers[0][index] = element; this._processUpdate(index); } - proof(element) { - const index = this.indexOf(element); - return this.path(index); - } /** * Get merkle path to a leaf * @param {number} index Leaf index to generate path for diff --git a/lib/FixedMerkleTree.d.ts b/lib/FixedMerkleTree.d.ts index 2c43792..c255a18 100644 --- a/lib/FixedMerkleTree.d.ts +++ b/lib/FixedMerkleTree.d.ts @@ -1,4 +1,4 @@ -import { Element, HashFunction, MerkleTreeOptions, SerializedTreeState, TreeEdge, TreeSlice } from './'; +import { Element, HashFunction, MerkleTreeOptions, ProofPath, SerializedTreeState, TreeEdge, TreeSlice } from './'; import { BaseTree } from './BaseTree'; export default class MerkleTree extends BaseTree { constructor(levels: number, elements?: Element[], { hashFunction, zeroElement, }?: MerkleTreeOptions); @@ -8,6 +8,8 @@ export default class MerkleTree extends BaseTree { * @param {Array} elements Elements to insert */ bulkInsert(elements: Element[]): void; + indexOf(element: Element, comparator?: (arg0: T, arg1: T) => boolean): number; + proof(element: Element): ProofPath; getTreeEdge(edgeIndex: number): TreeEdge; /** * 🪓 diff --git a/lib/FixedMerkleTree.js b/lib/FixedMerkleTree.js index f734768..14c9de7 100644 --- a/lib/FixedMerkleTree.js +++ b/lib/FixedMerkleTree.js @@ -52,6 +52,13 @@ class MerkleTree extends BaseTree_1.BaseTree { } this.insert(elements[elements.length - 1]); } + indexOf(element, comparator) { + return BaseTree_1.BaseTree.indexOf(this._layers[0], element, 0, comparator); + } + proof(element) { + const index = this.indexOf(element); + return this.path(index); + } getTreeEdge(edgeIndex) { const edgeElement = this._layers[0][edgeIndex]; if (edgeElement === undefined) { diff --git a/lib/PartialMerkleTree.d.ts b/lib/PartialMerkleTree.d.ts index 1f76fa9..794300a 100644 --- a/lib/PartialMerkleTree.d.ts +++ b/lib/PartialMerkleTree.d.ts @@ -21,6 +21,8 @@ export declare class PartialMerkleTree extends BaseTree { */ update(index: number, element: Element): void; path(index: number): ProofPath; + indexOf(element: Element, comparator?: (arg0: T, arg1: T) => boolean): number; + proof(element: Element): ProofPath; /** * Shifts edge of tree to left * @param edge new TreeEdge below current edge diff --git a/lib/PartialMerkleTree.js b/lib/PartialMerkleTree.js index 4d03d6a..e012abe 100644 --- a/lib/PartialMerkleTree.js +++ b/lib/PartialMerkleTree.js @@ -109,6 +109,13 @@ class PartialMerkleTree extends BaseTree_1.BaseTree { pathRoot: this.root, }; } + indexOf(element, comparator) { + return BaseTree_1.BaseTree.indexOf(this._layers[0], element, this.edgeIndex, comparator); + } + proof(element) { + const index = this.indexOf(element); + return this.path(index); + } /** * Shifts edge of tree to left * @param edge new TreeEdge below current edge diff --git a/src/BaseTree.ts b/src/BaseTree.ts index f3a561f..27edefa 100644 --- a/src/BaseTree.ts +++ b/src/BaseTree.ts @@ -29,15 +29,17 @@ export class BaseTree { /** * Find an element in the tree + * @param elements elements of tree * @param element An element to find * @param comparator A function that checks leaf value equality + * @param fromIndex The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned * @returns {number} Index if element is found, otherwise -1 */ - indexOf(element: Element, comparator?: (arg0: T, arg1: T) => boolean): number { + static indexOf(elements: Element[], element: Element, fromIndex?: number, comparator?: (arg0: T, arg1: T) => boolean): number { if (comparator) { - return this._layers[0].findIndex((el) => comparator(element, el)) + return elements.findIndex((el) => comparator(element, el)) } else { - return this._layers[0].indexOf(element) + return elements.indexOf(element, fromIndex) } } @@ -96,11 +98,6 @@ export class BaseTree { this._processUpdate(index) } - proof(element: Element): ProofPath { - const index = this.indexOf(element) - return this.path(index) - } - /** * Get merkle path to a leaf * @param {number} index Leaf index to generate path for diff --git a/src/FixedMerkleTree.ts b/src/FixedMerkleTree.ts index 4f1cfa4..a7ec47e 100644 --- a/src/FixedMerkleTree.ts +++ b/src/FixedMerkleTree.ts @@ -1,4 +1,4 @@ -import { Element, HashFunction, MerkleTreeOptions, SerializedTreeState, TreeEdge, TreeSlice } from './' +import { Element, HashFunction, MerkleTreeOptions, ProofPath, SerializedTreeState, TreeEdge, TreeSlice } from './' import defaultHash from './simpleHash' import { BaseTree } from './BaseTree' @@ -61,6 +61,15 @@ export default class MerkleTree extends BaseTree { this.insert(elements[elements.length - 1]) } + indexOf(element: Element, comparator?: (arg0: T, arg1: T) => boolean): number { + return BaseTree.indexOf(this._layers[0], element, 0, comparator) + } + + proof(element: Element): ProofPath { + const index = this.indexOf(element) + return this.path(index) + } + getTreeEdge(edgeIndex: number): TreeEdge { const edgeElement = this._layers[0][edgeIndex] if (edgeElement === undefined) { diff --git a/src/PartialMerkleTree.ts b/src/PartialMerkleTree.ts index 3fdc339..e089dcc 100644 --- a/src/PartialMerkleTree.ts +++ b/src/PartialMerkleTree.ts @@ -131,6 +131,14 @@ export class PartialMerkleTree extends BaseTree { } } + indexOf(element: Element, comparator?: (arg0: T, arg1: T) => boolean): number { + return BaseTree.indexOf(this._layers[0], element, this.edgeIndex, comparator) + } + + proof(element: Element): ProofPath { + const index = this.indexOf(element) + return this.path(index) + } /** * Shifts edge of tree to left