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

Vector clean up #1

Merged
merged 1 commit into from
Apr 15, 2021
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
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