Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on-trie vector data structure #25

Merged
merged 8 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/collections/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LookupMap } from "./lookup-map";
import { Vector } from "./vector";

export {
LookupMap
LookupMap,
Vector
}
124 changes: 124 additions & 0 deletions src/collections/vector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import * as near from '../api'
import {u8ArrayToString} from '../utils'

const ERR_INDEX_OUT_OF_BOUNDS = "Index out of bounds"
const ERR_INCONSISTENT_STATE = "The collection is an inconsistent state. Did previous smart contract execution terminate unexpectedly?"

/// An iterable implementation of vector that stores its content on the trie.
/// Uses the following map: index -> element
export class Vector {
constructor(prefix) {
this.length = 0
this.prefix = prefix
}

len() {
return this.length
}

isEmpty() {
return this.length == 0
}

indexToKey(index) {
let data = new Uint32Array([index])
let array = new Uint8Array(data.buffer)
let key = u8ArrayToString(array)
return this.prefix + key
}

get(index) {
if (index >= this.length) {
return None
}
let storageKey = this.indexToKey(index)
return near.jsvmStorageRead(storageKey)
}

/// Removes an element from the vector and returns it in serialized form.
/// The removed element is replaced by the last element of the vector.
/// Does not preserve ordering, but is `O(1)`.
swapRemove(index) {
if (index >= this.length) {
throw new Error(ERR_INDEX_OUT_OF_BOUNDS)
} else if (index + 1 == this.length) {
return this.pop()
} else {
let key = this.indexToKey(index)
let last = this.pop()
if (near.jsvmStorageWrite(key, last)) {
return near.storageGetEvicted()
} else {
throw new Error(ERR_INCONSISTENT_STATE)
}
}
}

push(element) {
let key = this.indexToKey(this.length)
this.length += 1
near.jsvmStorageWrite(key, element)
}

pop() {
if (this.isEmpty()) {
return null
} else {
let lastIndex = this.length - 1
let lastKey = this.indexToKey(lastIndex)
this.length -= 1
if (near.jsvmStorageRemove(lastKey)) {
return near.storageGetEvicted()
} else {
throw new Error(ERR_INCONSISTENT_STATE)
}
}
}

replace(index, element) {
if (index >= this.length) {
throw new Error(ERR_INDEX_OUT_OF_BOUNDS)
} else {
let key = this.indexToKey(index)
if (near.jsvmStorageWrite(key, element)) {
return near.storageGetEvicted()
} else {
throw new Error(ERR_INCONSISTENT_STATE)
}
}
}

extend(elements) {
for (let element of elements) {
this.push(element)
}
}

[Symbol.iterator]() {
return new VectorIterator(this)
}

toArray() {
let ret = []
for (let v of this) {
ret.push(v)
}
return ret
}
}

class VectorIterator {
constructor(vector) {
this.current = 0
this.vector = vector
}

next() {
if (this.current < this.vector.len()) {
let value = this.vector.get(this.current)
this.current += 1
return {value, done: false}
}
return {value: null, done: true}
}
}
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function u8ArrayToString(array) {
let ret = ''
for (let e of array) {
ret += String.fromCharCode(e)
}
return ret
}