Comparing version 1.8.0 to 1.9.0
{ | ||
"name": "xdim", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "Multi-Dimensional Functions. Create, Query, and Transform Multi-Dimensional Data.", | ||
@@ -16,7 +16,9 @@ "main": "src/xdim.js", | ||
"f": "npm run format", | ||
"format": "npx prettier --arrow-parens=avoid --print-width=160 --trailing-comma=none --write */*.js */*.ts", | ||
"format": "sh -c \"npx prettier --arrow-parens=avoid --print-width=160 --trailing-comma=none --write */*.js */*.ts; exit 0\"", | ||
"perf": "node tests/perf.js", | ||
"test": "npm run test:js && npm run test:ts && npm run perf", | ||
"prepublish": "npm run format && npm run build && npm run test", | ||
"test": "npm run test:js && npm run test:ts && npm run test:types && npm run perf", | ||
"test:js": "npm run build && for f in tests/*.js; do node -r esm $f; done", | ||
"test:ts": "npm run build && for f in tests/*.ts; do npx ts-node $f; done" | ||
"test:ts": "npm run build && for f in tests/*.ts; do npx ts-node $f; done", | ||
"test:types": "npx tsc --moduleResolution node --noEmit --target es2020 ./tests/*.ts" | ||
}, | ||
@@ -52,4 +54,6 @@ "repository": { | ||
"devDependencies": { | ||
"flug": "^2.3.1" | ||
"esm": "^3.2.25", | ||
"flug": "^2.3.1", | ||
"typescript": "^4.7.4" | ||
} | ||
} |
@@ -307,3 +307,4 @@ # xdim | ||
row: 768 | ||
} | ||
}, | ||
arrayTypes: ["Array", "Array", "Int8Array"] // optional | ||
}); | ||
@@ -319,4 +320,4 @@ ``` | ||
[ | ||
[-99, -99, ... ], // band's first row of columns with length being the number of columns | ||
[-99, -99, ... ], // band's second row | ||
Int8Array[-99, -99, ... ], // band's first row of columns with length being the number of columns | ||
Int8Array[-99, -99, ... ], // band's second row | ||
. | ||
@@ -329,4 +330,4 @@ . | ||
[ | ||
[-99, -99, ... ], // band's first row of columns with length being the number of columns | ||
[-99, -99, ... ], // band's second row | ||
Int8Array[-99, -99, ... ], // band's first row of columns with length being the number of columns | ||
Int8Array[-99, -99, ... ], // band's second row | ||
] | ||
@@ -333,0 +334,0 @@ ] |
@@ -54,5 +54,15 @@ // export type data = Array<data | number | string>; | ||
export type ArrayTypeString = "Array" | "Int8Array" | "Uint8Array" | "Uint8ClampedArray" | "Int16Array" | "Uint16Array" | "Float32Array" | "Float64Array" | "BigInt64Array" | "BigUint64Array"; | ||
export type ArrayTypeStrings = ReadonlyArray<ArrayTypeString>; | ||
export function addDims<A extends Array<any>>({ arr, fill, lens, arrayTypes }: { | ||
arr: A, | ||
fill?: undefined | number | string, | ||
lens?: number[], | ||
arrayTypes?: ArrayTypeStrings | ||
}): A; | ||
export function checkValidity(layout: string): true; | ||
export function createMatrix<S extends Readonly<number[]>, F = undefined>({ fill, shape }: { fill?: F; shape: S }): MultidimensionalArray<F, S["length"]>; | ||
export function createMatrix<S extends Readonly<number[]>, F = undefined>({ fill, shape, arrayTypes }: { fill?: F; shape: S, arrayTypes?: ArrayTypeString[] | Readonly<Array<ArrayTypeString>> }): MultidimensionalArray<F, S["length"]>; | ||
@@ -107,3 +117,4 @@ export function iterClip<D>({ | ||
useLayoutCache, | ||
sizes | ||
sizes, | ||
arrayTypes | ||
}: { | ||
@@ -114,5 +125,7 @@ fill?: F | undefined; | ||
sizes: Sizes; | ||
arrayTypes?: ArrayTypeStrings; | ||
}): { | ||
shape: S, | ||
data: ReturnType<typeof createMatrix<S, F>> | ||
data: ReturnType<typeof createMatrix<S, F>>, | ||
arrayTypes | ||
} | ||
@@ -119,0 +132,0 @@ |
@@ -6,2 +6,20 @@ const layoutCache = {}; | ||
const ARRAY_TYPES = { | ||
Array, | ||
Int8Array, | ||
Uint8Array, | ||
Uint8ClampedArray, | ||
Int16Array, | ||
Uint16Array, | ||
Float32Array, | ||
Float64Array | ||
}; | ||
try { | ||
ARRAY_TYPES.BigInt64Array = BigInt64Array; | ||
ARRAY_TYPES.BigUint64Array = BigUint64Array; | ||
} catch (error) { | ||
// pass | ||
} | ||
function parseDimensions(str) { | ||
@@ -383,4 +401,4 @@ const dims = {}; | ||
// add dimension to an array until the limit reaches zero | ||
function addDims({ arr, fill = undefined, lens }) { | ||
// add dimensions to an array until the limit reaches zero | ||
function addDims({ arr, fill = undefined, lens, arrayTypes }) { | ||
// no new dimensions to add | ||
@@ -390,5 +408,6 @@ if (lens.length === 0) return arr; | ||
const len = lens[0]; | ||
if (lens.length === 0) { | ||
if (lens.length === 1) { | ||
const lastArrayType = arrayTypes ? arrayTypes[arrayTypes.length - 1] : "Array"; | ||
for (let i = 0; i < arr.length; i++) { | ||
arr[i] = new Array(len).fill(fill); | ||
arr[i] = new ARRAY_TYPES[lastArrayType](len).fill(fill); | ||
} | ||
@@ -399,3 +418,3 @@ } else { | ||
arr[i] = sub; | ||
addDims({ arr: sub, lens: lens.slice(1) }); | ||
addDims({ arr: sub, lens: lens.slice(1), arrayTypes }); | ||
} | ||
@@ -406,10 +425,15 @@ } | ||
function createMatrix({ fill = undefined, shape }) { | ||
// to-do: maybe only call fill if not undefined or default typed array value? | ||
function createMatrix({ fill = undefined, shape, arrayTypes }) { | ||
const len = shape[0]; | ||
if (shape.length === 1) { | ||
const arrayType = arrayTypes ? arrayTypes[0] : "Array"; | ||
return new ARRAY_TYPES[arrayType](len).fill(fill); | ||
} | ||
const arr = new Array(len).fill(fill); | ||
return addDims({ arr, fill, lens: shape.slice(1) }); | ||
return addDims({ arr, fill, lens: shape.slice(1), arrayTypes }); | ||
} | ||
// generates an in-memory data structure to hold the data | ||
function prepareData({ fill = undefined, layout, useLayoutCache = true, sizes }) { | ||
function prepareData({ fill = undefined, layout, useLayoutCache = true, sizes, arrayTypes }) { | ||
if (typeof layout === "string") layout = parse(layout, { useLayoutCache }); | ||
@@ -423,3 +447,2 @@ | ||
return it.parts.reduce((total, part) => { | ||
console.log("part.dim:", part.dim); | ||
if (!(part.dim in sizes)) throw new Error(`[xdim] could not find "${part.dim}" in sizes: { ${Object.keys(sizes).join(", ")} }`); | ||
@@ -431,5 +454,5 @@ return total * sizes[part.dim]; | ||
const data = createMatrix({ fill, shape }); | ||
const data = createMatrix({ fill, shape, arrayTypes }); | ||
return { data, shape }; | ||
return { data, shape, arrayTypes }; | ||
} | ||
@@ -530,2 +553,3 @@ | ||
module.exports = { | ||
addDims, | ||
checkValidity, | ||
@@ -532,0 +556,0 @@ createMatrix, |
52468
758
410
3