Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from ChainSafe/cayman/cleanup
Browse files Browse the repository at this point in the history
Vector clean up
  • Loading branch information
wemeetagain authored Apr 15, 2021
2 parents 2b03b9f + 69872a9 commit 7213f24
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 239 deletions.
24 changes: 14 additions & 10 deletions src/MutableVector.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {Vector} from "./Vector";
import {PersistentVector} from "./Vector";

/**
* A mutable reference to a Vector
* A mutable reference to a PersistentVector
*/
export class MutableVector<T> implements Iterable<T> {
private constructor(public vector: Vector<T>) {}
private constructor(public vector: PersistentVector<T>) {}

static empty<T>(): MutableVector<T> {
return new MutableVector(Vector.empty());
return new MutableVector(PersistentVector.empty);
}

static from<T>(values: Iterable<T>): MutableVector<T> {
return new MutableVector(Vector.from(values));
return new MutableVector(PersistentVector.from(values));
}

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

get(index: number): T | null {
get(index: number): T | undefined {
return this.vector.get(index);
}

Expand All @@ -36,7 +36,7 @@ export class MutableVector<T> implements Iterable<T> {
}

push(value: T): void {
this.vector = this.vector.append(value);
this.vector = this.vector.push(value);
}

pop(): T | undefined {
Expand All @@ -45,19 +45,23 @@ export class MutableVector<T> implements Iterable<T> {
return last ?? undefined;
}

*[Symbol.iterator](): Generator<T> {
*[Symbol.iterator](): IterableIterator<T> {
yield* this.vector[Symbol.iterator]();
}

forEach(func: (t: T, i: number) => void): void {
this.vector.readOnlyForEach(func);
this.vector.forEach(func);
}

map<T2>(func: (t: T, i: number) => T2): T2[] {
return this.vector.readOnlyMap(func);
return this.vector.map(func);
}

clone(): MutableVector<T> {
return new MutableVector(this.vector);
}

toArray(): T[] {
return this.vector.toArray();
}
}
Loading

0 comments on commit 7213f24

Please sign in to comment.