Last active
March 10, 2024 04:01
-
-
Save Phryxia/a94200ef46ad42eb45b250a8730fe32f to your computer and use it in GitHub Desktop.
TypeScript implementation of Infinite Dimension Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RecursiveArray<T> = (T | RecursiveArray<T>)[] | |
type UtilizedRecursiveArray<T> = RecursiveArray<T> & { | |
get(...indices: number[]): T | RecursiveArray<T> | |
set(value: T, ...indices: number[]): T | |
} | |
function utilize<T>(rarr: RecursiveArray<T>): UtilizedRecursiveArray<T> { | |
const res = rarr as any | |
res.get = (...indices: number[]) => { | |
let el: T | RecursiveArray<T> = res | |
for (const index of indices) { | |
if ((el as Array<any>)?.length == null) { | |
throw new Error(`There is no array in given index (${indices.join(', ')})`) | |
} | |
el = (el as RecursiveArray<T>)[index] | |
} | |
return el | |
} | |
res.set = (value: T, ...indices: number[]) => { | |
let el: T | RecursiveArray<T> = res | |
for (let i = 0; i < indices.length; ++i) { | |
const index = indices[i] | |
if (i === indices.length - 1) { | |
(el as RecursiveArray<T>)[index] = value | |
break | |
} | |
let child = (el as RecursiveArray<T>)[index] | |
if (child === undefined) { | |
child = (el as RecursiveArray<T>)[index] = [] | |
} else if ((child as Array<any>)?.length == null) { | |
throw new Error(`There is already a non array element in index (${indices.join(', ')})`) | |
} | |
el = child as RecursiveArray<T> | |
} | |
return value | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
Caveat
Don't use type
undefined
for type parameterT
inUtilizedRecursiveArray<T>
. There is no way to distinguish intended value or actual non existence of such array, in line 26. If you want to use, you have to give up for dynamic array creation during new indices path.