Skip to content

Commit

Permalink
Add raw methods to persistentSet
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed Sep 24, 2020
1 parent a2278e9 commit a8663cb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions sdk-core/assembly/collections/persistentSet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { collections } from ".";
import { storage } from "../storage";
import { util } from "../util";
import { PersistentMap } from "./persistentMap";
import { PersistentVector } from "./persistentVector";
import { math } from "../math";
Expand Down Expand Up @@ -58,6 +59,21 @@ export class PersistentSet<T> {
this._vector.push(item);
}

/**
* @param register_id The register to read the new item from
*/
add_raw(register_id: u64): void {
const item = util.read_register(register_id);
const hashedItem = math.keccak256(item);

if (this._map.contains(hashedItem)) {
return;
}

this._map.set(hashedItem, this._vector.length);
this._vector.push(decode<T>(item));
}

/**
* Deletes all items.
*/
Expand Down Expand Up @@ -115,6 +131,16 @@ export class PersistentSet<T> {
return this._map.contains(this._hashedItem(item));
}

/**
* Returns whether the set contains the contents of the specified register
*
* @param register_id The register containing the item to check for
* @returns Boolean indicating whether the set contains the contents of the specified register
*/
has_raw(register_id: u64): bool {
return this._map.contains(math.keccak256(util.read_register(register_id)));
}

/**
*
* @returns
Expand Down

0 comments on commit a8663cb

Please sign in to comment.