1 | import { mixin } from "../mixin.js";
|
2 |
|
3 |
|
4 |
|
5 | export const IGrid1DMixin = mixin({
|
6 | order() {
|
7 | return [0];
|
8 | },
|
9 | includes(x) {
|
10 | return x >= 0 && x < this.size[0];
|
11 | },
|
12 | indexAt(x) {
|
13 | return this.includes(x) ? this.indexAtUnsafe(x) : -1;
|
14 | },
|
15 | indexAtUnsafe(x) {
|
16 | return this.offset + (x | 0) * this.stride[0];
|
17 | },
|
18 | getAt(x) {
|
19 | return this.includes(x) ? this.data[this.indexAtUnsafe(x)] : 0;
|
20 | },
|
21 | getAtUnsafe(x) {
|
22 | return this.data[this.indexAtUnsafe(x)];
|
23 | },
|
24 | setAt(x, val) {
|
25 | return this.includes(x)
|
26 | ? ((this.data[this.indexAtUnsafe(x)] = val), true)
|
27 | : false;
|
28 | },
|
29 | setAtUnsafe(x, val) {
|
30 | this.data[this.indexAtUnsafe(x)] = val;
|
31 | return true;
|
32 | },
|
33 | });
|
34 |
|
35 |
|
36 |
|
37 | export const IGrid2DMixin = mixin({
|
38 | order() {
|
39 | return Math.abs(this.stride[1]) > Math.abs(this.stride[0])
|
40 | ? [1, 0]
|
41 | : [0, 1];
|
42 | },
|
43 | includes(x, y) {
|
44 | const size = this.size;
|
45 | return x >= 0 && x < size[0] && y >= 0 && y < size[1];
|
46 | },
|
47 | indexAt(x, y) {
|
48 | return this.includes(x, y) ? this.indexAtUnsafe(x, y) : -1;
|
49 | },
|
50 | indexAtUnsafe(x, y) {
|
51 | return (this.offset + (x | 0) * this.stride[0] + (y | 0) * this.stride[1]);
|
52 | },
|
53 | getAt(x, y) {
|
54 | return this.includes(x, y) ? this.data[this.indexAtUnsafe(x, y)] : 0;
|
55 | },
|
56 | getAtUnsafe(x, y) {
|
57 | return this.data[this.indexAtUnsafe(x, y)];
|
58 | },
|
59 | setAt(x, y, val) {
|
60 | return this.includes(x, y)
|
61 | ? ((this.data[this.indexAtUnsafe(x, y)] = val), true)
|
62 | : false;
|
63 | },
|
64 | setAtUnsafe(x, y, val) {
|
65 | this.data[this.indexAtUnsafe(x, y)] = val;
|
66 | return true;
|
67 | },
|
68 | });
|
69 |
|
70 |
|
71 |
|
72 | export const IGrid3DMixin = mixin({
|
73 | order() {
|
74 | return strideOrder(this.stride);
|
75 | },
|
76 | includes(x, y, z) {
|
77 | const size = this.size;
|
78 | return (x >= 0 &&
|
79 | x < size[0] &&
|
80 | y >= 0 &&
|
81 | y < size[1] &&
|
82 | z >= 0 &&
|
83 | z < size[2]);
|
84 | },
|
85 | indexAt(x, y, z) {
|
86 | return this.includes(x, y, z) ? this.indexAtUnsafe(x, y, z) : -1;
|
87 | },
|
88 | indexAtUnsafe(x, y, z) {
|
89 | const stride = this.stride;
|
90 | return (this.offset +
|
91 | (x | 0) * stride[0] +
|
92 | (y | 0) * stride[1] +
|
93 | (z | 0) * stride[2]);
|
94 | },
|
95 | getAt(x, y, z) {
|
96 | return this.includes(x, y, z)
|
97 | ? this.data[this.indexAtUnsafe(x, y, z)]
|
98 | : 0;
|
99 | },
|
100 | getAtUnsafe(x, y, z) {
|
101 | return this.data[this.indexAtUnsafe(x, y, z)];
|
102 | },
|
103 | setAt(x, y, z, val) {
|
104 | return this.includes(x, y, z)
|
105 | ? ((this.data[this.indexAtUnsafe(x, y, z)] = val), true)
|
106 | : false;
|
107 | },
|
108 | setAtUnsafe(x, y, z, val) {
|
109 | this.data[this.indexAtUnsafe(x, y, z)] = val;
|
110 | return true;
|
111 | },
|
112 | });
|
113 |
|
114 |
|
115 |
|
116 | export const IGrid4DMixin = mixin({
|
117 | order() {
|
118 | return strideOrder(this.stride);
|
119 | },
|
120 | includes(x, y, z, w) {
|
121 | const size = this.size;
|
122 | return (x >= 0 &&
|
123 | x < size[0] &&
|
124 | y >= 0 &&
|
125 | y < size[1] &&
|
126 | z >= 0 &&
|
127 | z < size[2] &&
|
128 | w >= 0 &&
|
129 | w < size[3]);
|
130 | },
|
131 | indexAt(x, y, z, w) {
|
132 | return this.includes(x, y, z, w) ? this.indexAtUnsafe(x, y, z, w) : -1;
|
133 | },
|
134 | indexAtUnsafe(x, y, z, w) {
|
135 | const stride = this.stride;
|
136 | return (this.offset +
|
137 | (x | 0) * stride[0] +
|
138 | (y | 0) * stride[1] +
|
139 | (z | 0) * stride[2] +
|
140 | (w | 0) * stride[3]);
|
141 | },
|
142 | getAt(x, y, z, w) {
|
143 | return this.includes(x, y, z, w)
|
144 | ? this.data[this.indexAtUnsafe(x, y, z, w)]
|
145 | : 0;
|
146 | },
|
147 | getAtUnsafe(x, y, z, w) {
|
148 | return this.data[this.indexAtUnsafe(x, y, z, w)];
|
149 | },
|
150 | setAt(x, y, z, w, val) {
|
151 | return this.includes(x, y, z, w)
|
152 | ? ((this.data[this.indexAtUnsafe(x, y, z, w)] = val), true)
|
153 | : false;
|
154 | },
|
155 | setAtUnsafe(x, y, z, w, val) {
|
156 | this.data[this.indexAtUnsafe(x, y, z, w)] = val;
|
157 | return true;
|
158 | },
|
159 | });
|
160 | const strideOrder = (strides) => [...strides]
|
161 | .map((x, i) => [x, i])
|
162 | .sort((a, b) => Math.abs(b[0]) - Math.abs(a[0]))
|
163 | .map((x) => x[1]);
|