Skip to content

Commit

Permalink
feat(vector-pools): add AttribPool, refactor lists
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 30, 2018
1 parent 25b9789 commit 019c0af
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 35 deletions.
46 changes: 30 additions & 16 deletions packages/vector-pools/src/alist.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IVector, Vec } from "@thi.ng/vectors3/api";
import { wrap } from "./wrap";
import { StridedVec, Vec } from "@thi.ng/vectors3/api";
import { VecFactory } from "./api";
import { wrap } from "./wrap";

export abstract class AVecList<T extends IVector<any>> {
export abstract class AVecList<T extends StridedVec> {

buffer: Vec;
factory: VecFactory;
Expand All @@ -14,25 +14,35 @@ export abstract class AVecList<T extends IVector<any>> {
estride: number;
cstride: number;

_free: T[];
freeIDs: number[];

/**
*
* @param buffer
* @param capacity
* @param size
* @param start
* @param cstride
* @param estride
* @param factory
*/
constructor(
buffer: Vec,
capacity: number,
size: number,
factory: VecFactory = wrap,
start = 0,
cstride = 1,
estride = size,
start = 0) {

factory: VecFactory = wrap,
) {
this.buffer = buffer || new Float32Array(size * capacity);
this.size = size;
this.factory = factory;
this.cstride = cstride;
this.estride = estride;
this.start = this.curr = start;
this.capacity = capacity;
this._free = [];
this.freeIDs = [];
}

abstract [Symbol.iterator](): IterableIterator<T>;
Expand All @@ -45,6 +55,10 @@ export abstract class AVecList<T extends IVector<any>> {

abstract remove(v: T): boolean;

abstract has(v: T): boolean;

abstract nth(n: number): T;

indices(res: Vec = [], i = 0, local = true) {
const start = this.start;
const estride = this.estride;
Expand All @@ -61,15 +75,15 @@ export abstract class AVecList<T extends IVector<any>> {
}

protected alloc() {
let v: T;
if (this._free.length > 0) {
v = this._free.pop();
let idx: number;
if (this.freeIDs.length > 0) {
idx = this.freeIDs.pop();
} else if (this.length >= this.capacity) {
return;
} else {
if (this.length < this.capacity) {
v = <T>this.factory(this.buffer, this.size, this.curr, this.cstride);
this.curr += this.estride;
}
idx = this.curr;
this.curr += this.estride;
}
return v;
return <T>this.factory(this.buffer, this.size, idx, this.cstride);
}
}
18 changes: 13 additions & 5 deletions packages/vector-pools/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { IRelease, TypedArray } from "@thi.ng/api/api";
import { Type } from "@thi.ng/malloc/api";
import { IVector, Vec } from "@thi.ng/vectors3/api";
import { StridedVec, Vec, ReadonlyVec } from "@thi.ng/vectors3/api";

export interface AttribSpec {
type: Type;
size: number;
default: number | ReadonlyVec;
byteOffset: number;
stride?: number;
}

export interface IVecPool extends IRelease {

malloc(size: number, type?: Type): TypedArray;

mallocWrapped(size: number, stride?: number, type?: Type): IVector<any>;
mallocWrapped(size: number, stride?: number, type?: Type): StridedVec;

mallocArray(num: number, size: number, cstride?: number, estride?: number, type?: Type): IVector<any>[];
mallocArray(num: number, size: number, cstride?: number, estride?: number, type?: Type): StridedVec[];

free(vec: IVector<any> | TypedArray): boolean;
free(vec: StridedVec | TypedArray): boolean;

freeAll();
}

export type VecFactory =
(buf: Vec, size: number, index: number, stride: number) => IVector<any>;
(buf: Vec, size: number, index: number, stride: number) => StridedVec;

export { Type };
22 changes: 15 additions & 7 deletions packages/vector-pools/src/array-list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IVector, Vec } from "@thi.ng/vectors3/api";
import { StridedVec, Vec } from "@thi.ng/vectors3/api";
import { AVecList } from "./alist";
import { VecFactory } from "./api";

export class VecArrayList<T extends IVector<any>> extends AVecList<T> {
export class VecArrayList<T extends StridedVec> extends AVecList<T> {

items: T[];

Expand All @@ -19,12 +19,12 @@ export class VecArrayList<T extends IVector<any>> extends AVecList<T> {
buffer: Vec,
capacity: number,
size: number,
factory?: VecFactory,
start = 0,
cstride = 1,
estride = size,
start = 0) {

super(buffer, capacity, size, factory, cstride, estride, start);
factory?: VecFactory,
) {
super(buffer, capacity, size, cstride, estride, start, factory);
this.items = [];
}

Expand Down Expand Up @@ -56,9 +56,17 @@ export class VecArrayList<T extends IVector<any>> extends AVecList<T> {
remove(v: T) {
const idx = this.items.indexOf(v);
if (idx >= 0) {
this._free.push(v);
this.freeIDs.push(v.i);
this.items.splice(idx, 1);
return true;
}
}

has(v: T) {
return this.items.indexOf(v) >= 0;
}

nth(n: number) {
return this.items[n];
}
}
Loading

0 comments on commit 019c0af

Please sign in to comment.