UNPKG

3.58 kBJavaScriptView Raw
1import { mixin } from "../mixin.js";
2const IGrid1DMixin = mixin({
3 order() {
4 return [0];
5 },
6 includes(x) {
7 return x >= 0 && x < this.size[0];
8 },
9 indexAt(x) {
10 return this.includes(x) ? this.indexAtUnsafe(x) : -1;
11 },
12 indexAtUnsafe(x) {
13 return this.offset + (x | 0) * this.stride[0];
14 },
15 getAt(x) {
16 return this.includes(x) ? this.data[this.indexAtUnsafe(x)] : 0;
17 },
18 getAtUnsafe(x) {
19 return this.data[this.indexAtUnsafe(x)];
20 },
21 setAt(x, val) {
22 return this.includes(x) ? (this.data[this.indexAtUnsafe(x)] = val, true) : false;
23 },
24 setAtUnsafe(x, val) {
25 this.data[this.indexAtUnsafe(x)] = val;
26 return true;
27 }
28});
29const IGrid2DMixin = mixin({
30 order() {
31 return Math.abs(this.stride[1]) > Math.abs(this.stride[0]) ? [1, 0] : [0, 1];
32 },
33 includes(x, y) {
34 const size = this.size;
35 return x >= 0 && x < size[0] && y >= 0 && y < size[1];
36 },
37 indexAt(x, y) {
38 return this.includes(x, y) ? this.indexAtUnsafe(x, y) : -1;
39 },
40 indexAtUnsafe(x, y) {
41 return this.offset + (x | 0) * this.stride[0] + (y | 0) * this.stride[1];
42 },
43 getAt(x, y) {
44 return this.includes(x, y) ? this.data[this.indexAtUnsafe(x, y)] : 0;
45 },
46 getAtUnsafe(x, y) {
47 return this.data[this.indexAtUnsafe(x, y)];
48 },
49 setAt(x, y, val) {
50 return this.includes(x, y) ? (this.data[this.indexAtUnsafe(x, y)] = val, true) : false;
51 },
52 setAtUnsafe(x, y, val) {
53 this.data[this.indexAtUnsafe(x, y)] = val;
54 return true;
55 }
56});
57const IGrid3DMixin = mixin({
58 order() {
59 return __strideOrder(this.stride);
60 },
61 includes(x, y, z) {
62 const size = this.size;
63 return x >= 0 && x < size[0] && y >= 0 && y < size[1] && z >= 0 && z < size[2];
64 },
65 indexAt(x, y, z) {
66 return this.includes(x, y, z) ? this.indexAtUnsafe(x, y, z) : -1;
67 },
68 indexAtUnsafe(x, y, z) {
69 const stride = this.stride;
70 return this.offset + (x | 0) * stride[0] + (y | 0) * stride[1] + (z | 0) * stride[2];
71 },
72 getAt(x, y, z) {
73 return this.includes(x, y, z) ? this.data[this.indexAtUnsafe(x, y, z)] : 0;
74 },
75 getAtUnsafe(x, y, z) {
76 return this.data[this.indexAtUnsafe(x, y, z)];
77 },
78 setAt(x, y, z, val) {
79 return this.includes(x, y, z) ? (this.data[this.indexAtUnsafe(x, y, z)] = val, true) : false;
80 },
81 setAtUnsafe(x, y, z, val) {
82 this.data[this.indexAtUnsafe(x, y, z)] = val;
83 return true;
84 }
85});
86const IGrid4DMixin = mixin({
87 order() {
88 return __strideOrder(this.stride);
89 },
90 includes(x, y, z, w) {
91 const size = this.size;
92 return x >= 0 && x < size[0] && y >= 0 && y < size[1] && z >= 0 && z < size[2] && w >= 0 && w < size[3];
93 },
94 indexAt(x, y, z, w) {
95 return this.includes(x, y, z, w) ? this.indexAtUnsafe(x, y, z, w) : -1;
96 },
97 indexAtUnsafe(x, y, z, w) {
98 const stride = this.stride;
99 return this.offset + (x | 0) * stride[0] + (y | 0) * stride[1] + (z | 0) * stride[2] + (w | 0) * stride[3];
100 },
101 getAt(x, y, z, w) {
102 return this.includes(x, y, z, w) ? this.data[this.indexAtUnsafe(x, y, z, w)] : 0;
103 },
104 getAtUnsafe(x, y, z, w) {
105 return this.data[this.indexAtUnsafe(x, y, z, w)];
106 },
107 setAt(x, y, z, w, val) {
108 return this.includes(x, y, z, w) ? (this.data[this.indexAtUnsafe(x, y, z, w)] = val, true) : false;
109 },
110 setAtUnsafe(x, y, z, w, val) {
111 this.data[this.indexAtUnsafe(x, y, z, w)] = val;
112 return true;
113 }
114});
115const __strideOrder = (strides) => [...strides].map((x, i) => [x, i]).sort((a, b) => Math.abs(b[0]) - Math.abs(a[0])).map((x) => x[1]);
116export {
117 IGrid1DMixin,
118 IGrid2DMixin,
119 IGrid3DMixin,
120 IGrid4DMixin
121};