@maplat/tin 0.11.3 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +30 -35
- package/README.md +30 -35
- package/dist/boundary-vertices.d.ts +27 -0
- package/dist/edgebound/base.d.ts +31 -0
- package/dist/edgebound/bitset.d.ts +58 -0
- package/dist/edgebound/constrain.d.ts +68 -0
- package/dist/edgebound/index.d.ts +4 -0
- package/dist/index.cjs +7 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.html +19 -12
- package/dist/maplat_tin.cjs +5 -0
- package/dist/maplat_tin.js +3243 -0
- package/dist/maplat_tin.umd.js +5 -0
- package/dist/searchutils.d.ts +11 -2
- package/dist/strict-overlap.d.ts +5 -0
- package/dist/tin.d.ts +12 -18
- package/dist/weight-buffer.d.ts +11 -0
- package/package.json +40 -41
- package/src/boundary-vertices.ts +284 -0
- package/src/constrained-tin.ts +1 -1
- package/src/edgebound/base.ts +65 -0
- package/src/edgebound/bitset.ts +109 -0
- package/src/edgebound/constrain.license.txt +15 -0
- package/src/edgebound/constrain.ts +498 -0
- package/src/edgebound/index.ts +10 -0
- package/src/index.ts +1 -0
- package/src/kinks.ts +13 -16
- package/src/searchutils.ts +36 -2
- package/src/strict-overlap.ts +281 -0
- package/src/tin.ts +50 -382
- package/src/weight-buffer.ts +111 -0
- package/dist/tin.cjs +0 -3
- package/dist/tin.js +0 -2194
- package/dist/tin.umd.js +0 -3
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { lineString } from "@turf/helpers";
|
|
2
|
+
import lineIntersect from "@turf/line-intersect";
|
|
3
|
+
import type { Position } from "geojson";
|
|
4
|
+
import type { VertexPosition } from "./types/tin.d.ts";
|
|
5
|
+
|
|
6
|
+
interface ConvexEntry {
|
|
7
|
+
forw: Position;
|
|
8
|
+
bakw: Position;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface BoundaryVerticesParams {
|
|
12
|
+
convexBuf: Record<string, ConvexEntry>;
|
|
13
|
+
centroid: { forw: Position; bakw: Position };
|
|
14
|
+
bbox: Position[];
|
|
15
|
+
minx: number;
|
|
16
|
+
maxx: number;
|
|
17
|
+
miny: number;
|
|
18
|
+
maxy: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type EdgeRayBuckets = Array<Array<ConvexEntry>>;
|
|
22
|
+
type SamplePair = {
|
|
23
|
+
forw: [number, number];
|
|
24
|
+
bakw: [number, number];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function buildEdgeBuckets(
|
|
28
|
+
convexBuf: Record<string, ConvexEntry>,
|
|
29
|
+
centroid: { forw: Position; bakw: Position },
|
|
30
|
+
minx: number,
|
|
31
|
+
maxx: number,
|
|
32
|
+
miny: number,
|
|
33
|
+
maxy: number,
|
|
34
|
+
): EdgeRayBuckets {
|
|
35
|
+
return Object.keys(convexBuf).reduce((prev, key) => {
|
|
36
|
+
const item = convexBuf[key];
|
|
37
|
+
const forw = item.forw;
|
|
38
|
+
const bakw = item.bakw;
|
|
39
|
+
|
|
40
|
+
const vec = {
|
|
41
|
+
forw: [forw[0] - centroid.forw[0], forw[1] - centroid.forw[1]],
|
|
42
|
+
bakw: [bakw[0] - centroid.bakw[0], bakw[1] - centroid.bakw[1]],
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const xRate = vec.forw[0] === 0
|
|
46
|
+
? Infinity
|
|
47
|
+
: ((vec.forw[0] < 0 ? minx : maxx) - centroid.forw[0]) / vec.forw[0];
|
|
48
|
+
const yRate = vec.forw[1] === 0
|
|
49
|
+
? Infinity
|
|
50
|
+
: ((vec.forw[1] < 0 ? miny : maxy) - centroid.forw[1]) / vec.forw[1];
|
|
51
|
+
|
|
52
|
+
if (Math.abs(xRate) / Math.abs(yRate) < 1.1) {
|
|
53
|
+
const node = {
|
|
54
|
+
forw: [
|
|
55
|
+
vec.forw[0] * xRate + centroid.forw[0],
|
|
56
|
+
vec.forw[1] * xRate + centroid.forw[1],
|
|
57
|
+
] as Position,
|
|
58
|
+
bakw: [
|
|
59
|
+
vec.bakw[0] * xRate + centroid.bakw[0],
|
|
60
|
+
vec.bakw[1] * xRate + centroid.bakw[1],
|
|
61
|
+
] as Position,
|
|
62
|
+
};
|
|
63
|
+
if (vec.forw[0] < 0) {
|
|
64
|
+
prev[3].push(node);
|
|
65
|
+
} else {
|
|
66
|
+
prev[1].push(node);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (Math.abs(yRate) / Math.abs(xRate) < 1.1) {
|
|
71
|
+
const node = {
|
|
72
|
+
forw: [
|
|
73
|
+
vec.forw[0] * yRate + centroid.forw[0],
|
|
74
|
+
vec.forw[1] * yRate + centroid.forw[1],
|
|
75
|
+
] as Position,
|
|
76
|
+
bakw: [
|
|
77
|
+
vec.bakw[0] * yRate + centroid.bakw[0],
|
|
78
|
+
vec.bakw[1] * yRate + centroid.bakw[1],
|
|
79
|
+
] as Position,
|
|
80
|
+
};
|
|
81
|
+
if (vec.forw[1] < 0) {
|
|
82
|
+
prev[0].push(node);
|
|
83
|
+
} else {
|
|
84
|
+
prev[2].push(node);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return prev;
|
|
89
|
+
}, [[], [], [], []] as EdgeRayBuckets);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function collectSamples(
|
|
93
|
+
convexBuf: Record<string, ConvexEntry>,
|
|
94
|
+
centroid: { forw: Position; bakw: Position },
|
|
95
|
+
): { perQuad: SamplePair[][]; aggregate: SamplePair[] } {
|
|
96
|
+
const perQuad: SamplePair[][] = [[], [], [], []];
|
|
97
|
+
const aggregate: SamplePair[] = [];
|
|
98
|
+
|
|
99
|
+
Object.keys(convexBuf).forEach((key) => {
|
|
100
|
+
const item = convexBuf[key];
|
|
101
|
+
const forw = item.forw;
|
|
102
|
+
const bakw = item.bakw;
|
|
103
|
+
|
|
104
|
+
const forwDelta: [number, number] = [
|
|
105
|
+
forw[0] - centroid.forw[0],
|
|
106
|
+
forw[1] - centroid.forw[1],
|
|
107
|
+
];
|
|
108
|
+
const bakwDelta: [number, number] = [
|
|
109
|
+
bakw[0] - centroid.bakw[0],
|
|
110
|
+
centroid.bakw[1] - bakw[1],
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
const sample: SamplePair = { forw: forwDelta, bakw: bakwDelta };
|
|
114
|
+
aggregate.push(sample);
|
|
115
|
+
|
|
116
|
+
if (forwDelta[0] === 0 || forwDelta[1] === 0) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let quad = 0;
|
|
121
|
+
if (forwDelta[0] > 0) quad += 1;
|
|
122
|
+
if (forwDelta[1] > 0) quad += 2;
|
|
123
|
+
perQuad[quad].push(sample);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
return { perQuad, aggregate };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function reduceSamples(samples: SamplePair[]): [number, number] {
|
|
130
|
+
let minRatio = Infinity;
|
|
131
|
+
let sumCos = 0;
|
|
132
|
+
let sumSin = 0;
|
|
133
|
+
|
|
134
|
+
samples.forEach((sample) => {
|
|
135
|
+
const { forw, bakw } = sample;
|
|
136
|
+
const forwNorm = Math.hypot(forw[0], forw[1]);
|
|
137
|
+
const bakwNorm = Math.hypot(bakw[0], bakw[1]);
|
|
138
|
+
if (bakwNorm === 0) return;
|
|
139
|
+
|
|
140
|
+
const ratio = forwNorm / bakwNorm;
|
|
141
|
+
const theta = Math.atan2(forw[0], forw[1]) - Math.atan2(bakw[0], bakw[1]);
|
|
142
|
+
|
|
143
|
+
minRatio = Math.min(minRatio, ratio);
|
|
144
|
+
sumCos += Math.cos(theta);
|
|
145
|
+
sumSin += Math.sin(theta);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (!isFinite(minRatio)) {
|
|
149
|
+
return [1, 0];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return [minRatio, Math.atan2(sumSin, sumCos)];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function buildVertexRatio(
|
|
156
|
+
convexBuf: Record<string, ConvexEntry>,
|
|
157
|
+
centroid: { forw: Position; bakw: Position },
|
|
158
|
+
mode: "plain" | "birdeye",
|
|
159
|
+
): Array<[number, number]> {
|
|
160
|
+
const { perQuad, aggregate } = collectSamples(convexBuf, centroid);
|
|
161
|
+
const hasAllQuadrants = perQuad.every((samples) => samples.length > 0);
|
|
162
|
+
|
|
163
|
+
const groups: SamplePair[][] = mode === "birdeye"
|
|
164
|
+
? (hasAllQuadrants ? perQuad : [aggregate])
|
|
165
|
+
: [aggregate];
|
|
166
|
+
|
|
167
|
+
const vertexRatio = groups.map((samples) => reduceSamples(samples));
|
|
168
|
+
|
|
169
|
+
if (vertexRatio.length === 1) {
|
|
170
|
+
return [vertexRatio[0], vertexRatio[0], vertexRatio[0], vertexRatio[0]];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return vertexRatio;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function checkAndAdjustVertices(
|
|
177
|
+
vertices: VertexPosition[],
|
|
178
|
+
edgeNodes: EdgeRayBuckets,
|
|
179
|
+
centroid: { forw: Position; bakw: Position },
|
|
180
|
+
): void {
|
|
181
|
+
const expandRatio = [1, 1, 1, 1];
|
|
182
|
+
|
|
183
|
+
for (let i = 0; i < 4; i++) {
|
|
184
|
+
const j = (i + 1) % 4;
|
|
185
|
+
const side = lineString([vertices[i].bakw, vertices[j].bakw]);
|
|
186
|
+
|
|
187
|
+
edgeNodes[i].map((node) => {
|
|
188
|
+
const line = lineString([centroid.bakw, node.bakw]);
|
|
189
|
+
const intersect = lineIntersect(side, line);
|
|
190
|
+
|
|
191
|
+
if (intersect.features.length > 0 && intersect.features[0].geometry) {
|
|
192
|
+
const intersectPt = intersect.features[0];
|
|
193
|
+
const distance = Math.sqrt(
|
|
194
|
+
Math.pow(node.bakw[0] - centroid.bakw[0], 2) +
|
|
195
|
+
Math.pow(node.bakw[1] - centroid.bakw[1], 2),
|
|
196
|
+
);
|
|
197
|
+
const intDistance = Math.sqrt(
|
|
198
|
+
Math.pow(
|
|
199
|
+
intersectPt.geometry.coordinates[0] - centroid.bakw[0],
|
|
200
|
+
2,
|
|
201
|
+
) +
|
|
202
|
+
Math.pow(
|
|
203
|
+
intersectPt.geometry.coordinates[1] - centroid.bakw[1],
|
|
204
|
+
2,
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
const ratio = distance / intDistance;
|
|
208
|
+
|
|
209
|
+
if (ratio > expandRatio[i]) expandRatio[i] = ratio;
|
|
210
|
+
if (ratio > expandRatio[j]) expandRatio[j] = ratio;
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
vertices.forEach((vertex, i) => {
|
|
216
|
+
const ratio = expandRatio[i];
|
|
217
|
+
const bakw: Position = [
|
|
218
|
+
(vertex.bakw[0] - centroid.bakw[0]) * ratio + centroid.bakw[0],
|
|
219
|
+
(vertex.bakw[1] - centroid.bakw[1]) * ratio + centroid.bakw[1],
|
|
220
|
+
];
|
|
221
|
+
vertex.bakw = bakw;
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function finalizeVertices(
|
|
226
|
+
vertexRatio: Array<[number, number]>,
|
|
227
|
+
bbox: Position[],
|
|
228
|
+
centroid: { forw: Position; bakw: Position },
|
|
229
|
+
edgeNodes: EdgeRayBuckets,
|
|
230
|
+
): VertexPosition[] {
|
|
231
|
+
const vertices = vertexRatio.map((ratio, index) => {
|
|
232
|
+
const forVertex = bbox[index];
|
|
233
|
+
const forDelta = [
|
|
234
|
+
forVertex[0] - centroid.forw[0],
|
|
235
|
+
forVertex[1] - centroid.forw[1],
|
|
236
|
+
];
|
|
237
|
+
const forDistance = Math.sqrt(
|
|
238
|
+
Math.pow(forDelta[0], 2) + Math.pow(forDelta[1], 2),
|
|
239
|
+
);
|
|
240
|
+
const bakDistance = forDistance / ratio[0];
|
|
241
|
+
const bakTheta = Math.atan2(forDelta[0], forDelta[1]) - ratio[1];
|
|
242
|
+
const bakw: Position = [
|
|
243
|
+
centroid.bakw[0] + bakDistance * Math.sin(bakTheta),
|
|
244
|
+
centroid.bakw[1] - bakDistance * Math.cos(bakTheta),
|
|
245
|
+
];
|
|
246
|
+
|
|
247
|
+
return { forw: forVertex, bakw };
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const swap = vertices[2];
|
|
251
|
+
vertices[2] = vertices[3];
|
|
252
|
+
vertices[3] = swap;
|
|
253
|
+
|
|
254
|
+
checkAndAdjustVertices(vertices, edgeNodes, centroid);
|
|
255
|
+
|
|
256
|
+
return vertices;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Calculate the standard four boundary vertices around the centroid.
|
|
261
|
+
*/
|
|
262
|
+
export function calculatePlainVertices(
|
|
263
|
+
params: BoundaryVerticesParams,
|
|
264
|
+
): VertexPosition[] {
|
|
265
|
+
const { convexBuf, centroid, bbox, minx, maxx, miny, maxy } = params;
|
|
266
|
+
|
|
267
|
+
const edgeNodes = buildEdgeBuckets(convexBuf, centroid, minx, maxx, miny, maxy);
|
|
268
|
+
const vertexRatio = buildVertexRatio(convexBuf, centroid, "plain");
|
|
269
|
+
|
|
270
|
+
return finalizeVertices(vertexRatio, bbox, centroid, edgeNodes);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Calculate boundary vertices for bird's-eye mode using quadrant-aware ratios.
|
|
275
|
+
*/
|
|
276
|
+
export function calculateBirdeyeVertices(
|
|
277
|
+
params: BoundaryVerticesParams,
|
|
278
|
+
): VertexPosition[] {
|
|
279
|
+
const { convexBuf, centroid, bbox, minx, maxx, miny, maxy } = params;
|
|
280
|
+
const edgeNodes = buildEdgeBuckets(convexBuf, centroid, minx, maxx, miny, maxy);
|
|
281
|
+
const vertexRatio = buildVertexRatio(convexBuf, centroid, "birdeye");
|
|
282
|
+
|
|
283
|
+
return finalizeVertices(vertexRatio, bbox, centroid, edgeNodes);
|
|
284
|
+
}
|
package/src/constrained-tin.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { featureCollection, polygon } from "@turf/helpers";
|
|
2
2
|
import type { FeatureCollection, Point, Polygon } from "geojson";
|
|
3
3
|
import Delaunator from "delaunator";
|
|
4
|
-
import { Constrain as EdgeBound } from "
|
|
4
|
+
import { Constrain as EdgeBound } from "./edgebound/index.ts";
|
|
5
5
|
import type { Edge } from "@maplat/transform";
|
|
6
6
|
import type { TriangleProperties } from "./types/tin.d.ts";
|
|
7
7
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { orient2d } from "robust-predicates";
|
|
2
|
+
|
|
3
|
+
export interface DelaunatorLike {
|
|
4
|
+
coords: { readonly length: number; readonly [index: number]: number };
|
|
5
|
+
triangles: { readonly length: number; [index: number]: number };
|
|
6
|
+
halfedges: { readonly length: number; [index: number]: number };
|
|
7
|
+
hull: { readonly length: number; readonly [index: number]: number };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function nextEdge(edge: number): number {
|
|
11
|
+
return edge % 3 === 2 ? edge - 2 : edge + 1;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function prevEdge(edge: number): number {
|
|
15
|
+
return edge % 3 === 0 ? edge + 2 : edge - 1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Compute if two line segments [p1, p2] and [p3, p4] intersect.
|
|
20
|
+
*/
|
|
21
|
+
export function intersectSegments(
|
|
22
|
+
p1x: number,
|
|
23
|
+
p1y: number,
|
|
24
|
+
p2x: number,
|
|
25
|
+
p2y: number,
|
|
26
|
+
p3x: number,
|
|
27
|
+
p3y: number,
|
|
28
|
+
p4x: number,
|
|
29
|
+
p4y: number,
|
|
30
|
+
): boolean {
|
|
31
|
+
const x0 = orient2d(p1x, p1y, p3x, p3y, p4x, p4y);
|
|
32
|
+
const y0 = orient2d(p2x, p2y, p3x, p3y, p4x, p4y);
|
|
33
|
+
if ((x0 > 0 && y0 > 0) || (x0 < 0 && y0 < 0)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const x1 = orient2d(p3x, p3y, p1x, p1y, p2x, p2y);
|
|
38
|
+
const y1 = orient2d(p4x, p4y, p1x, p1y, p2x, p2y);
|
|
39
|
+
if ((x1 > 0 && y1 > 0) || (x1 < 0 && y1 < 0)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Handle the degenerate collinear case
|
|
44
|
+
if (x0 === 0 && y0 === 0 && x1 === 0 && y1 === 0) {
|
|
45
|
+
return !(
|
|
46
|
+
Math.max(p3x, p4x) < Math.min(p1x, p2x) ||
|
|
47
|
+
Math.max(p1x, p2x) < Math.min(p3x, p4x) ||
|
|
48
|
+
Math.max(p3y, p4y) < Math.min(p1y, p2y) ||
|
|
49
|
+
Math.max(p1y, p2y) < Math.min(p3y, p4y)
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class Base {
|
|
57
|
+
/**
|
|
58
|
+
* The triangulation object from Delaunator.
|
|
59
|
+
*/
|
|
60
|
+
public del: DelaunatorLike;
|
|
61
|
+
|
|
62
|
+
constructor(del: DelaunatorLike) {
|
|
63
|
+
this.del = del;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A set of numbers, stored as bits in a typed array.
|
|
3
|
+
* The amount of numbers that can be stored is limited by the length,
|
|
4
|
+
* which is fixed at construction time.
|
|
5
|
+
*/
|
|
6
|
+
abstract class BitSet {
|
|
7
|
+
protected readonly bs: Uint8Array | Uint16Array | Uint32Array;
|
|
8
|
+
protected readonly width: 8 | 16 | 32;
|
|
9
|
+
|
|
10
|
+
protected constructor(width: BitSet["width"], bs: BitSet["bs"]) {
|
|
11
|
+
this.width = width;
|
|
12
|
+
this.bs = bs;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Add a number to the set.
|
|
17
|
+
*
|
|
18
|
+
* @param idx The number to add. Must be 0 <= idx < len.
|
|
19
|
+
*/
|
|
20
|
+
add(idx: number): this {
|
|
21
|
+
const byte = Math.floor(idx / this.width);
|
|
22
|
+
const bit = idx % this.width;
|
|
23
|
+
this.bs[byte] |= 1 << bit;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Delete a number from the set.
|
|
29
|
+
*
|
|
30
|
+
* @param idx The number to delete. Must be 0 <= idx < len.
|
|
31
|
+
*/
|
|
32
|
+
delete(idx: number): this {
|
|
33
|
+
const byte = Math.floor(idx / this.width);
|
|
34
|
+
const bit = idx % this.width;
|
|
35
|
+
this.bs[byte] &= ~(1 << bit);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Add or delete a number in the set, depending on the second argument.
|
|
41
|
+
*
|
|
42
|
+
* @param idx The number to add or delete. Must be 0 <= idx < len.
|
|
43
|
+
* @param val If true, add the number, otherwise delete.
|
|
44
|
+
*/
|
|
45
|
+
set(idx: number, val: boolean): boolean {
|
|
46
|
+
const byte = Math.floor(idx / this.width);
|
|
47
|
+
const bit = idx % this.width;
|
|
48
|
+
const mask = 1 << bit;
|
|
49
|
+
this.bs[byte] ^= (-Number(val) ^ this.bs[byte]) & mask;
|
|
50
|
+
return val;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether the number is in the set.
|
|
55
|
+
*
|
|
56
|
+
* @param idx The number to test. Must be 0 <= idx < len.
|
|
57
|
+
*/
|
|
58
|
+
has(idx: number): boolean {
|
|
59
|
+
const byte = Math.floor(idx / this.width);
|
|
60
|
+
const bit = idx % this.width;
|
|
61
|
+
return (this.bs[byte] & (1 << bit)) !== 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Iterate over the numbers that are in the set.
|
|
66
|
+
*/
|
|
67
|
+
forEach(fn: (idx: number) => void): this {
|
|
68
|
+
const len = this.bs.length;
|
|
69
|
+
for (let byte = 0; byte < len; byte++) {
|
|
70
|
+
let bit = 0;
|
|
71
|
+
while (this.bs[byte] && bit < this.width) {
|
|
72
|
+
if (this.bs[byte] & (1 << bit)) {
|
|
73
|
+
fn(byte * this.width + bit);
|
|
74
|
+
}
|
|
75
|
+
bit++;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type { BitSet };
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A bit set using 8 bits per cell.
|
|
86
|
+
*/
|
|
87
|
+
export class BitSet8 extends BitSet {
|
|
88
|
+
constructor(len: number) {
|
|
89
|
+
super(8, new Uint8Array(Math.ceil(len / 8)).fill(0));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A bit set using 16 bits per cell.
|
|
95
|
+
*/
|
|
96
|
+
export class BitSet16 extends BitSet {
|
|
97
|
+
constructor(len: number) {
|
|
98
|
+
super(16, new Uint16Array(Math.ceil(len / 16)).fill(0));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A bit set using 32 bits per cell.
|
|
104
|
+
*/
|
|
105
|
+
export class BitSet32 extends BitSet {
|
|
106
|
+
constructor(len: number) {
|
|
107
|
+
super(32, new Uint32Array(Math.ceil(len / 32)).fill(0));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright 2020, Marco Gunnink
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
6
|
+
with or without fee is hereby granted, provided that the above copyright notice
|
|
7
|
+
and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
11
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
13
|
+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
14
|
+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
15
|
+
THIS SOFTWARE.
|