Skip to content

Commit

Permalink
Idiomatic js length for collections (#180)
Browse files Browse the repository at this point in the history
* Idiomatic js length for collections
* add private to the noop setter
  • Loading branch information
ailisp authored Aug 22, 2022
1 parent 663354e commit f30c813
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 51 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ someMethod() {
this.v.pop()

// len, isEmpty
let len = this.v.len()
let isEmpty = this.v.isEnpty()
let len = this.v.length
let isEmpty = this.v.isEmpty()

// iterate
for (let element of this.v) {
Expand Down Expand Up @@ -346,8 +346,8 @@ someMethod() {
this.m.set('ghi', 'ddd')

// len, isEmpty
let len = this.m.len()
let isEmpty = this.m.isEnpty()
let len = this.m.length
let isEmpty = this.m.isEmpty()

// iterate
for (let [k, v] of this.m) {
Expand Down Expand Up @@ -396,8 +396,8 @@ someMethod() {
this.s.remove('def')

// len, isEmpty
let len = this.s.len()
let isEmpty = this.s.isEnpty()
let len = this.s.length
let isEmpty = this.s.isEmpty()

// iterate
for (let e of this.s) {
Expand Down
4 changes: 2 additions & 2 deletions lib/collections/unordered-map.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Bytes } from "../utils";
import { Vector } from "./vector";
export declare class UnorderedMap {
readonly length: number;
readonly prefix: Bytes;
readonly keyIndexPrefix: Bytes;
readonly keys: Vector;
readonly values: Vector;
constructor(prefix: Bytes);
len(): number;
get length(): number;
private set length(value);
isEmpty(): boolean;
get(key: Bytes): unknown | null;
set(key: Bytes, value: unknown): unknown | null;
Expand Down
15 changes: 8 additions & 7 deletions lib/collections/unordered-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,23 @@ function getIndexRaw(keyIndexPrefix, key) {
}
export class UnorderedMap {
constructor(prefix) {
this.length = 0;
this.prefix = prefix;
this.keyIndexPrefix = prefix + "i";
let indexKey = prefix + "k";
let indexValue = prefix + "v";
this.keys = new Vector(indexKey);
this.values = new Vector(indexValue);
}
len() {
let keysLen = this.keys.len();
let valuesLen = this.values.len();
get length() {
let keysLen = this.keys.length;
let valuesLen = this.values.length;
if (keysLen != valuesLen) {
throw new Error(ERR_INCONSISTENT_STATE);
}
return keysLen;
}
// noop, called by deserialize
set length(_l) { }
isEmpty() {
let keysIsEmpty = this.keys.isEmpty();
let valuesIsEmpty = this.values.isEmpty();
Expand Down Expand Up @@ -65,7 +66,7 @@ export class UnorderedMap {
return this.values.replace(index, value);
}
else {
let nextIndex = this.len();
let nextIndex = this.length;
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.keys.push(key);
Expand All @@ -77,15 +78,15 @@ export class UnorderedMap {
let indexLookup = this.keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
if (indexRaw) {
if (this.len() == 1) {
if (this.length == 1) {
// If there is only one element then swap remove simply removes it without
// swapping with the last element.
near.storageRemove(indexLookup);
}
else {
// If there is more than one element then swap remove swaps it with the last
// element.
let lastKey = this.keys.get(this.len() - 1);
let lastKey = this.keys.get(this.length - 1);
if (!lastKey) {
throw new Error(ERR_INCONSISTENT_STATE);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/collections/unordered-set.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Bytes } from "../utils";
import { Vector } from "./vector";
export declare class UnorderedSet {
readonly length: number;
readonly prefix: Bytes;
readonly elementIndexPrefix: Bytes;
readonly elements: Vector;
constructor(prefix: Bytes);
len(): number;
get length(): number;
private set length(value);
isEmpty(): boolean;
contains(element: unknown): boolean;
set(element: unknown): boolean;
Expand Down
13 changes: 7 additions & 6 deletions lib/collections/unordered-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ function deserializeIndex(rawIndex) {
}
export class UnorderedSet {
constructor(prefix) {
this.length = 0;
this.prefix = prefix;
this.elementIndexPrefix = prefix + "i";
let elementsPrefix = prefix + "e";
this.elements = new Vector(elementsPrefix);
}
len() {
return this.elements.len();
get length() {
return this.elements.length;
}
// noop, called by deserialize
set length(_l) { }
isEmpty() {
return this.elements.isEmpty();
}
Expand All @@ -36,7 +37,7 @@ export class UnorderedSet {
return false;
}
else {
let nextIndex = this.len();
let nextIndex = this.length;
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.elements.push(element);
Expand All @@ -47,15 +48,15 @@ export class UnorderedSet {
let indexLookup = this.elementIndexPrefix + JSON.stringify(element);
let indexRaw = near.storageRead(indexLookup);
if (indexRaw) {
if (this.len() == 1) {
if (this.length == 1) {
// If there is only one element then swap remove simply removes it without
// swapping with the last element.
near.storageRemove(indexLookup);
}
else {
// If there is more than one element then swap remove swaps it with the last
// element.
let lastElement = this.elements.get(this.len() - 1);
let lastElement = this.elements.get(this.length - 1);
if (!lastElement) {
throw new Error(ERR_INCONSISTENT_STATE);
}
Expand Down
1 change: 0 additions & 1 deletion lib/collections/vector.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export declare class Vector {
length: number;
readonly prefix: Bytes;
constructor(prefix: Bytes);
len(): number;
isEmpty(): boolean;
get(index: number): unknown | null;
swapRemove(index: number): unknown | null;
Expand Down
5 changes: 1 addition & 4 deletions lib/collections/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export class Vector {
this.length = 0;
this.prefix = prefix;
}
len() {
return this.length;
}
isEmpty() {
return this.length == 0;
}
Expand Down Expand Up @@ -122,7 +119,7 @@ export class VectorIterator {
this.vector = vector;
}
next() {
if (this.current < this.vector.len()) {
if (this.current < this.vector.length) {
let value = this.vector.get(this.current);
this.current += 1;
return { value, done: false };
Expand Down
17 changes: 9 additions & 8 deletions src/collections/unordered-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ function getIndexRaw(keyIndexPrefix: Bytes, key: Bytes): Bytes {
}

export class UnorderedMap {
readonly length: number;
readonly prefix: Bytes;
readonly keyIndexPrefix: Bytes;
readonly keys: Vector;
readonly values: Vector;

constructor(prefix: Bytes) {
this.length = 0;
this.prefix = prefix;
this.keyIndexPrefix = prefix + "i";
let indexKey = prefix + "k";
Expand All @@ -40,15 +38,18 @@ export class UnorderedMap {
this.values = new Vector(indexValue);
}

len() {
let keysLen = this.keys.len();
let valuesLen = this.values.len();
get length() {
let keysLen = this.keys.length;
let valuesLen = this.values.length;
if (keysLen != valuesLen) {
throw new Error(ERR_INCONSISTENT_STATE);
}
return keysLen;
}

// noop, called by deserialize
private set length(_l: number) {}

isEmpty(): boolean {
let keysIsEmpty = this.keys.isEmpty();
let valuesIsEmpty = this.values.isEmpty();
Expand Down Expand Up @@ -79,7 +80,7 @@ export class UnorderedMap {
let index = deserializeIndex(indexRaw);
return this.values.replace(index, value);
} else {
let nextIndex = this.len();
let nextIndex = this.length;
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.keys.push(key);
Expand All @@ -92,14 +93,14 @@ export class UnorderedMap {
let indexLookup = this.keyIndexPrefix + JSON.stringify(key);
let indexRaw = near.storageRead(indexLookup);
if (indexRaw) {
if (this.len() == 1) {
if (this.length == 1) {
// If there is only one element then swap remove simply removes it without
// swapping with the last element.
near.storageRemove(indexLookup);
} else {
// If there is more than one element then swap remove swaps it with the last
// element.
let lastKey = this.keys.get(this.len() - 1);
let lastKey = this.keys.get(this.length - 1);
if (!lastKey) {
throw new Error(ERR_INCONSISTENT_STATE);
}
Expand Down
15 changes: 8 additions & 7 deletions src/collections/unordered-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ function deserializeIndex(rawIndex: Bytes): number {
}

export class UnorderedSet {
readonly length: number;
readonly prefix: Bytes;
readonly elementIndexPrefix: Bytes;
readonly elements: Vector;

constructor(prefix: Bytes) {
this.length = 0;
this.prefix = prefix;
this.elementIndexPrefix = prefix + "i";
let elementsPrefix = prefix + "e";
this.elements = new Vector(elementsPrefix);
}

len(): number {
return this.elements.len();
get length(): number {
return this.elements.length;
}

// noop, called by deserialize
private set length(_l: number) {}

isEmpty(): boolean {
return this.elements.isEmpty();
}
Expand All @@ -50,7 +51,7 @@ export class UnorderedSet {
if (near.storageRead(indexLookup)) {
return false;
} else {
let nextIndex = this.len();
let nextIndex = this.length;
let nextIndexRaw = serializeIndex(nextIndex);
near.storageWrite(indexLookup, nextIndexRaw);
this.elements.push(element);
Expand All @@ -62,14 +63,14 @@ export class UnorderedSet {
let indexLookup = this.elementIndexPrefix + JSON.stringify(element);
let indexRaw = near.storageRead(indexLookup);
if (indexRaw) {
if (this.len() == 1) {
if (this.length == 1) {
// If there is only one element then swap remove simply removes it without
// swapping with the last element.
near.storageRemove(indexLookup);
} else {
// If there is more than one element then swap remove swaps it with the last
// element.
let lastElement = this.elements.get(this.len() - 1);
let lastElement = this.elements.get(this.length - 1);
if (!lastElement) {
throw new Error(ERR_INCONSISTENT_STATE);
}
Expand Down
6 changes: 1 addition & 5 deletions src/collections/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export class Vector {
this.prefix = prefix;
}

len(): number {
return this.length;
}

isEmpty(): boolean {
return this.length == 0;
}
Expand Down Expand Up @@ -139,7 +135,7 @@ export class VectorIterator {
}

next(): { value: unknown | null; done: boolean } {
if (this.current < this.vector.len()) {
if (this.current < this.vector.length) {
let value = this.vector.get(this.current);
this.current += 1;
return { value, done: false };
Expand Down
2 changes: 1 addition & 1 deletion tests/src/unordered-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UnorderedMapTestContract extends NearContract {

@view
len() {
return this.unorderedMap.len();
return this.unorderedMap.length;
}

@view
Expand Down
2 changes: 1 addition & 1 deletion tests/src/unordered-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnorderedSetTestContract extends NearContract {

@view
len() {
return this.unorderedSet.len();
return this.unorderedSet.length;
}

@view
Expand Down
2 changes: 1 addition & 1 deletion tests/src/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class VectorTestContract extends NearContract {

@view
len() {
return this.vector.len();
return this.vector.length;
}

@view
Expand Down

0 comments on commit f30c813

Please sign in to comment.