implement indexOf with fromIndex param

This commit is contained in:
smart_ex 2022-03-28 18:21:32 +10:00
parent 7d439740b1
commit a29aa84450
9 changed files with 50 additions and 19 deletions

5
lib/BaseTree.d.ts vendored
View File

@ -12,11 +12,13 @@ export declare class BaseTree {
get root(): Element; get root(): Element;
/** /**
* Find an element in the tree * Find an element in the tree
* @param elements elements of tree
* @param element An element to find * @param element An element to find
* @param comparator A function that checks leaf value equality * @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 * @returns {number} Index if element is found, otherwise -1
*/ */
indexOf(element: Element, comparator?: <T>(arg0: T, arg1: T) => boolean): number; static indexOf(elements: Element[], element: Element, fromIndex?: number, comparator?: <T>(arg0: T, arg1: T) => boolean): number;
/** /**
* Insert new element into the tree * Insert new element into the tree
* @param element Element to insert * @param element Element to insert
@ -29,7 +31,6 @@ export declare class BaseTree {
* @param element Updated element value * @param element Updated element value
*/ */
update(index: number, element: Element): void; update(index: number, element: Element): void;
proof(element: Element): ProofPath;
/** /**
* Get merkle path to a leaf * Get merkle path to a leaf
* @param {number} index Leaf index to generate path for * @param {number} index Leaf index to generate path for

View File

@ -20,16 +20,18 @@ class BaseTree {
} }
/** /**
* Find an element in the tree * Find an element in the tree
* @param elements elements of tree
* @param element An element to find * @param element An element to find
* @param comparator A function that checks leaf value equality * @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 * @returns {number} Index if element is found, otherwise -1
*/ */
indexOf(element, comparator) { static indexOf(elements, element, fromIndex, comparator) {
if (comparator) { if (comparator) {
return this._layers[0].findIndex((el) => comparator(element, el)); return elements.findIndex((el) => comparator(element, el));
} }
else { else {
return this._layers[0].indexOf(element); return elements.indexOf(element, fromIndex);
} }
} }
/** /**
@ -82,10 +84,6 @@ class BaseTree {
this._layers[0][index] = element; this._layers[0][index] = element;
this._processUpdate(index); this._processUpdate(index);
} }
proof(element) {
const index = this.indexOf(element);
return this.path(index);
}
/** /**
* Get merkle path to a leaf * Get merkle path to a leaf
* @param {number} index Leaf index to generate path for * @param {number} index Leaf index to generate path for

View File

@ -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'; import { BaseTree } from './BaseTree';
export default class MerkleTree extends BaseTree { export default class MerkleTree extends BaseTree {
constructor(levels: number, elements?: Element[], { hashFunction, zeroElement, }?: MerkleTreeOptions); constructor(levels: number, elements?: Element[], { hashFunction, zeroElement, }?: MerkleTreeOptions);
@ -8,6 +8,8 @@ export default class MerkleTree extends BaseTree {
* @param {Array} elements Elements to insert * @param {Array} elements Elements to insert
*/ */
bulkInsert(elements: Element[]): void; bulkInsert(elements: Element[]): void;
indexOf(element: Element, comparator?: <T>(arg0: T, arg1: T) => boolean): number;
proof(element: Element): ProofPath;
getTreeEdge(edgeIndex: number): TreeEdge; getTreeEdge(edgeIndex: number): TreeEdge;
/** /**
* 🪓 * 🪓

View File

@ -52,6 +52,13 @@ class MerkleTree extends BaseTree_1.BaseTree {
} }
this.insert(elements[elements.length - 1]); 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) { getTreeEdge(edgeIndex) {
const edgeElement = this._layers[0][edgeIndex]; const edgeElement = this._layers[0][edgeIndex];
if (edgeElement === undefined) { if (edgeElement === undefined) {

View File

@ -21,6 +21,8 @@ export declare class PartialMerkleTree extends BaseTree {
*/ */
update(index: number, element: Element): void; update(index: number, element: Element): void;
path(index: number): ProofPath; path(index: number): ProofPath;
indexOf(element: Element, comparator?: <T>(arg0: T, arg1: T) => boolean): number;
proof(element: Element): ProofPath;
/** /**
* Shifts edge of tree to left * Shifts edge of tree to left
* @param edge new TreeEdge below current edge * @param edge new TreeEdge below current edge

View File

@ -109,6 +109,13 @@ class PartialMerkleTree extends BaseTree_1.BaseTree {
pathRoot: this.root, 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 * Shifts edge of tree to left
* @param edge new TreeEdge below current edge * @param edge new TreeEdge below current edge

View File

@ -29,15 +29,17 @@ export class BaseTree {
/** /**
* Find an element in the tree * Find an element in the tree
* @param elements elements of tree
* @param element An element to find * @param element An element to find
* @param comparator A function that checks leaf value equality * @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 * @returns {number} Index if element is found, otherwise -1
*/ */
indexOf(element: Element, comparator?: <T> (arg0: T, arg1: T) => boolean): number { static indexOf(elements: Element[], element: Element, fromIndex?: number, comparator?: <T> (arg0: T, arg1: T) => boolean): number {
if (comparator) { if (comparator) {
return this._layers[0].findIndex((el) => comparator<Element>(element, el)) return elements.findIndex((el) => comparator<Element>(element, el))
} else { } else {
return this._layers[0].indexOf(element) return elements.indexOf(element, fromIndex)
} }
} }
@ -96,11 +98,6 @@ export class BaseTree {
this._processUpdate(index) this._processUpdate(index)
} }
proof(element: Element): ProofPath {
const index = this.indexOf(element)
return this.path(index)
}
/** /**
* Get merkle path to a leaf * Get merkle path to a leaf
* @param {number} index Leaf index to generate path for * @param {number} index Leaf index to generate path for

View File

@ -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 defaultHash from './simpleHash'
import { BaseTree } from './BaseTree' import { BaseTree } from './BaseTree'
@ -61,6 +61,15 @@ export default class MerkleTree extends BaseTree {
this.insert(elements[elements.length - 1]) this.insert(elements[elements.length - 1])
} }
indexOf(element: Element, comparator?: <T> (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 { getTreeEdge(edgeIndex: number): TreeEdge {
const edgeElement = this._layers[0][edgeIndex] const edgeElement = this._layers[0][edgeIndex]
if (edgeElement === undefined) { if (edgeElement === undefined) {

View File

@ -131,6 +131,14 @@ export class PartialMerkleTree extends BaseTree {
} }
} }
indexOf(element: Element, comparator?: <T> (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 * Shifts edge of tree to left