implement indexOf with fromIndex param
This commit is contained in:
parent
7d439740b1
commit
a29aa84450
5
lib/BaseTree.d.ts
vendored
5
lib/BaseTree.d.ts
vendored
@ -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?: <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
|
||||
* @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
|
||||
|
@ -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
|
||||
|
4
lib/FixedMerkleTree.d.ts
vendored
4
lib/FixedMerkleTree.d.ts
vendored
@ -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?: <T>(arg0: T, arg1: T) => boolean): number;
|
||||
proof(element: Element): ProofPath;
|
||||
getTreeEdge(edgeIndex: number): TreeEdge;
|
||||
/**
|
||||
* 🪓
|
||||
|
@ -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) {
|
||||
|
2
lib/PartialMerkleTree.d.ts
vendored
2
lib/PartialMerkleTree.d.ts
vendored
@ -21,6 +21,8 @@ export declare class PartialMerkleTree extends BaseTree {
|
||||
*/
|
||||
update(index: number, element: Element): void;
|
||||
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
|
||||
* @param edge new TreeEdge below current edge
|
||||
|
@ -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
|
||||
|
@ -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?: <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) {
|
||||
return this._layers[0].findIndex((el) => comparator<Element>(element, el))
|
||||
return elements.findIndex((el) => comparator<Element>(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
|
||||
|
@ -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?: <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 {
|
||||
const edgeElement = this._layers[0][edgeIndex]
|
||||
if (edgeElement === undefined) {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user