Skip to content

Commit

Permalink
refactor(vector-pools): address TS strictNullChecks flag
Browse files Browse the repository at this point in the history
BREAKING CHANGE: update return types of various class methods

- some AList, ArrayList, LinkedList, VecPool methods now return
  `undefined` if operation failed
  • Loading branch information
postspectacular committed Jun 7, 2019
1 parent e4781e3 commit 981b5ce
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 37 deletions.
8 changes: 4 additions & 4 deletions packages/vector-pools/src/alist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export abstract class AVecList<T extends StridedVec> {

abstract get length(): number;

abstract add(): T;
abstract add(): T | undefined;

abstract insert(i: number): T;
abstract insert(i: number): T | undefined;

abstract remove(v: T): boolean;

abstract has(v: T): boolean;

abstract nth(n: number): T;
abstract nth(n: number): T | undefined;

indices(res: Vec = [], i = 0, local = true) {
const start = this.start;
Expand All @@ -76,7 +76,7 @@ export abstract class AVecList<T extends StridedVec> {
protected alloc() {
let idx: number;
if (this.freeIDs.length > 0) {
idx = this.freeIDs.pop();
idx = this.freeIDs.pop()!;
} else if (this.length >= this.capacity) {
return;
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/vector-pools/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ export interface AttribPoolOpts {
}

export interface IVecPool extends IRelease {
malloc(size: number, type?: GLType | Type): TypedArray;
malloc(size: number, type?: GLType | Type): TypedArray | undefined;

mallocWrapped(
size: number,
stride?: number,
type?: GLType | Type
): StridedVec;
): StridedVec | undefined;

mallocArray(
num: number,
size: number,
cstride?: number,
estride?: number,
type?: GLType | Type
): StridedVec[];
): StridedVec[] | undefined;

free(vec: StridedVec | TypedArray): boolean;

Expand Down
5 changes: 3 additions & 2 deletions packages/vector-pools/src/array-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class VecArrayList<T extends StridedVec> extends AVecList<T> {
}

add() {
const v: T = this.alloc();
const v: T | undefined = this.alloc();
if (v) {
this.items.push(v);
}
Expand All @@ -45,7 +45,7 @@ export class VecArrayList<T extends StridedVec> extends AVecList<T> {

insert(i: number) {
if (!this.length && i !== 0) return;
const v: T = this.alloc();
const v: T | undefined = this.alloc();
if (v) {
this.items.splice(i, 0, v);
}
Expand All @@ -59,6 +59,7 @@ export class VecArrayList<T extends StridedVec> extends AVecList<T> {
this.items.splice(idx, 1);
return true;
}
return false;
}

has(v: T) {
Expand Down
22 changes: 11 additions & 11 deletions packages/vector-pools/src/attrib-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AttribPool implements IRelease {
order: string[];
specs: IObjectOf<AttribSpec>;
pool: MemPool;
addr: number;
addr!: number;
capacity: number;

byteStride: number;
Expand All @@ -42,7 +42,7 @@ export class AttribPool implements IRelease {
? new MemPool(opts.mem)
: opts.mem;
this.capacity = opts.num;
this.resizable = opts.resizable;
this.resizable = !!opts.resizable;
this.specs = {};
this.attribs = {};
this.order = [];
Expand Down Expand Up @@ -85,11 +85,11 @@ export class AttribPool implements IRelease {
this.initDefaults(specs);
}

attribValue(id: string, i: number): number | Vec {
attribValue(id: string, i: number): number | Vec | undefined {
const spec = this.specs[id];
assert(!!spec, `invalid attrib: ${id}`);
if (i >= this.capacity) return;
i *= spec.stride;
i *= spec.stride!;
return spec.size > 1
? this.attribs[id].subarray(i, i + spec.size)
: this.attribs[id][i];
Expand All @@ -99,7 +99,7 @@ export class AttribPool implements IRelease {
const buf = this.attribs[id];
assert(!!buf, `invalid attrib: ${id}`);
const spec = this.specs[id];
const stride = spec.stride;
const stride = spec.stride!;
const size = spec.size;
if (size > 1) {
for (let i = 0, j = 0, n = this.capacity; i < n; i++, j += stride) {
Expand All @@ -117,7 +117,7 @@ export class AttribPool implements IRelease {
assert(!!spec, `invalid attrib: ${id}`);
this.ensure(index + 1);
const buf = this.attribs[id];
index *= spec.stride;
index *= spec.stride!;
const isNum = typeof v === "number";
assert(
() => (!isNum && spec.size > 1) || (isNum && spec.size === 1),
Expand All @@ -142,7 +142,7 @@ export class AttribPool implements IRelease {
assert(!!spec, `invalid attrib: ${id}`);
const n = vals.length;
const v = vals[0];
const stride = spec.stride;
const stride = spec.stride!;
this.ensure(n);
const buf = this.attribs[id];
const isNum = typeof v === "number";
Expand Down Expand Up @@ -191,7 +191,7 @@ export class AttribPool implements IRelease {
asNativeType(a.type),
this.pool.buf,
newAddr + (a.byteOffset || 0),
(newCapacity - 1) * a.stride + a.size
(newCapacity - 1) * a.stride! + a.size
);
buf.set(this.attribs[id]);
this.attribs[id] = buf;
Expand Down Expand Up @@ -261,7 +261,7 @@ export class AttribPool implements IRelease {
asNativeType(a.type),
this.pool.buf,
this.addr + (a.byteOffset || 0),
(this.capacity - 1) * a.stride + a.size
(this.capacity - 1) * a.stride! + a.size
);
}
this.setDefaults(specs, start, end);
Expand All @@ -276,7 +276,7 @@ export class AttribPool implements IRelease {
const a = specs[id];
if (a.default == null) continue;
const buf = this.attribs[id];
const s = a.stride;
const s = a.stride!;
const v = a.default;
if (typeof v === "number") {
for (let i = start; i < end; i++) {
Expand Down Expand Up @@ -334,7 +334,7 @@ export class AttribPool implements IRelease {
// ...in offset order to avoid successor attrib vals
for (let id of order) {
const a = specs[id];
const sStride = a.stride;
const sStride = a.stride!;
const src = attribs[id];
const [dest, dStride] = newAttribs[id];
if (typeof a.default === "number") {
Expand Down
21 changes: 11 additions & 10 deletions packages/vector-pools/src/linked-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { AVecList } from "./alist";
import { VecFactory } from "./api";

interface Cell<T extends StridedVec> {
prev: CellVec<T>;
next: CellVec<T>;
prev: CellVec<T> | null;
next: CellVec<T> | null;
}

type CellVec<T extends StridedVec> = T & Cell<T>;

export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
head: CellVec<T>;
tail: CellVec<T>;
head: CellVec<T> | null;
tail: CellVec<T> | null;
readonly closed: boolean;

protected _length: number;
Expand Down Expand Up @@ -49,8 +49,8 @@ export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
let v = this.head;
const first = v;
do {
yield v;
v = v.next;
yield v!;
v = v!.next;
} while (v && v !== first);
}
}
Expand All @@ -68,7 +68,7 @@ export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
this.tail = v;
if (this.closed) {
v.next = this.head;
this.head.prev = v;
this.head!.prev = v;
}
this._length++;
} else {
Expand All @@ -80,7 +80,7 @@ export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
return v;
}

insert(i: number): T {
insert(i: number): T | undefined {
if (!this._length) {
return i === 0 ? this.add() : undefined;
}
Expand Down Expand Up @@ -123,6 +123,7 @@ export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
delete v.next;
return true;
}
return false;
}

has(value: T) {
Expand All @@ -132,7 +133,7 @@ export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
if (v === value) {
return true;
}
v = v.next;
v = v!.next;
} while (v && v !== first);
return false;
}
Expand All @@ -157,6 +158,6 @@ export class VecLinkedList<T extends StridedVec> extends AVecList<T> {
v = v.prev;
}
}
return v;
return v!;
}
}
17 changes: 10 additions & 7 deletions packages/vector-pools/src/vec-pool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, TypedArray } from "@thi.ng/api";
import { isTypedArray } from "@thi.ng/checks";
import { MemPool, MemPoolOpts, MemPoolStats } from "@thi.ng/malloc";
import { IVector } from "@thi.ng/vectors";
import { StridedVec } from "@thi.ng/vectors";
import { GLType, IVecPool } from "./api";
import { asNativeType } from "./convert";
import { wrap } from "./wrap";
Expand All @@ -19,17 +19,20 @@ export class VecPool implements IVecPool {
return this.pool.stats();
}

malloc(size: number, type: GLType | Type = Type.F32): TypedArray {
malloc(
size: number,
type: GLType | Type = Type.F32
): TypedArray | undefined {
return this.pool.callocAs(asNativeType(type), size);
}

mallocWrapped(
size: number,
stride = 1,
type: GLType | Type = Type.F32
): IVector<any> {
): StridedVec | undefined {
const buf = this.pool.callocAs(asNativeType(type), size * stride);
return wrap(buf, size, 0, stride);
return buf ? wrap(buf, size, 0, stride) : undefined;
}

/**
Expand Down Expand Up @@ -62,20 +65,20 @@ export class VecPool implements IVecPool {
cstride = 1,
estride = size,
type: GLType | Type = Type.F32
): IVector<any>[] {
): StridedVec[] | undefined {
const buf = this.malloc(
Math.max(cstride, estride, size) * num,
asNativeType(type)
);
if (!buf) return;
const res: IVector<any>[] = [];
const res: StridedVec[] = [];
for (let i = 0; i < num; i += estride) {
res.push(wrap(buf, size, i, cstride));
}
return res;
}

free(vec: IVector<any> | TypedArray) {
free(vec: StridedVec | TypedArray) {
const buf = (<any>vec).buf;
return buf
? isTypedArray(buf)
Expand Down

0 comments on commit 981b5ce

Please sign in to comment.