|
1 | 1 | type Op = (agg: number, x: number) => number; |
2 | 2 | type Range = [number, number]; |
3 | 3 |
|
| 4 | +// this file is based on dust2's implementation, for more details see: |
| 5 | +// https://github.com/mrc-ide/dust2/blob/83721f78fda9b4124e1cda3ae24e3f2da82ca2d0/inst/include/dust2/array.hpp#L115 |
| 6 | +// |
| 7 | +// it calculates column major flat indicies from subranges along each dimension and applies |
| 8 | +// +, *, ... operations to those values |
| 9 | + |
4 | 10 | const reduceAll = (arr: number[], op: Op, init: number) => { |
5 | 11 | return arr.reduce(op, init); |
6 | 12 | }; |
@@ -131,6 +137,21 @@ export type DimUtils = { |
131 | 137 | mult: number[]; |
132 | 138 | }; |
133 | 139 |
|
| 140 | +/* |
| 141 | + Example usage |
| 142 | + ============= |
| 143 | +
|
| 144 | + const dim = getDimObj(); |
| 145 | +
|
| 146 | + dim.x = [2, 3, 5]; // setter triggers and will calculate dim, size and mult |
| 147 | + |
| 148 | + console.log(dim.x) |
| 149 | + -> { |
| 150 | + dim: [2, 3, 5], // same as input |
| 151 | + size: 30, // 2 * 3 * 5, total size of flat array |
| 152 | + mult: [1, 2, 6] // stride length, cumulative prod of dim starting at 1 |
| 153 | + } |
| 154 | +*/ |
134 | 155 | const getDimObj = () => { |
135 | 156 | const dim: Record<string, DimUtils> = {}; |
136 | 157 | return new Proxy(dim, { |
|
0 commit comments