@atlaspack/graph 3.5.10-typescript-5b4d3ad41.0 → 3.5.10
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/CHANGELOG.md +7 -0
- package/lib/AdjacencyList.js +3 -26
- package/lib/BitSet.js +1 -0
- package/lib/ContentGraph.js +5 -1
- package/lib/Graph.js +3 -8
- package/lib/shared-buffer.js +1 -5
- package/lib/types.js +0 -1
- package/package.json +8 -11
- package/src/{AdjacencyList.ts → AdjacencyList.js} +101 -127
- package/src/{BitSet.ts → BitSet.js} +3 -0
- package/src/{ContentGraph.ts → ContentGraph.js} +20 -21
- package/src/{Graph.ts → Graph.js} +65 -88
- package/src/{index.ts → index.js} +2 -0
- package/src/{shared-buffer.ts → shared-buffer.js} +3 -6
- package/src/{types.ts → types.js} +7 -5
- package/test/{AdjacencyList.test.ts → AdjacencyList.test.js} +29 -21
- package/test/{BitSet.test.ts → BitSet.test.js} +5 -3
- package/test/{ContentGraph.test.ts → ContentGraph.test.js} +4 -2
- package/test/{Graph.test.ts → Graph.test.js} +24 -22
- package/LICENSE +0 -201
- package/lib/AdjacencyList.d.ts +0 -607
- package/lib/BitSet.d.ts +0 -19
- package/lib/ContentGraph.d.ts +0 -23
- package/lib/Graph.d.ts +0 -91
- package/lib/index.d.ts +0 -7
- package/lib/shared-buffer.d.ts +0 -2
- package/lib/types.d.ts +0 -9
- package/tsconfig.json +0 -4
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @flow
|
|
1
2
|
import assert from 'assert';
|
|
2
3
|
import nullthrows from 'nullthrows';
|
|
3
4
|
import {SharedBuffer} from './shared-buffer';
|
|
@@ -5,49 +6,49 @@ import {fromNodeId, toNodeId} from './types';
|
|
|
5
6
|
import {
|
|
6
7
|
ALL_EDGE_TYPES,
|
|
7
8
|
NULL_EDGE_TYPE,
|
|
8
|
-
NullEdgeType,
|
|
9
|
-
AllEdgeTypes,
|
|
9
|
+
type NullEdgeType,
|
|
10
|
+
type AllEdgeTypes,
|
|
10
11
|
} from './Graph';
|
|
11
12
|
import type {NodeId} from './types';
|
|
12
13
|
|
|
13
14
|
/** The address of the node in the nodes map. */
|
|
14
|
-
type NodeAddress = number;
|
|
15
|
+
opaque type NodeAddress = number;
|
|
15
16
|
|
|
16
|
-
type EdgeHash = number;
|
|
17
|
+
opaque type EdgeHash = number;
|
|
17
18
|
|
|
18
19
|
/** The address of the edge in the edges map. */
|
|
19
|
-
type EdgeAddress = number;
|
|
20
|
+
opaque type EdgeAddress = number;
|
|
20
21
|
|
|
21
22
|
// eslint-disable-next-line no-unused-vars
|
|
22
|
-
export type SerializedAdjacencyList<TEdgeType> = {
|
|
23
|
-
nodes: Uint32Array
|
|
24
|
-
edges: Uint32Array
|
|
25
|
-
};
|
|
23
|
+
export type SerializedAdjacencyList<TEdgeType> = {|
|
|
24
|
+
nodes: Uint32Array,
|
|
25
|
+
edges: Uint32Array,
|
|
26
|
+
|};
|
|
26
27
|
|
|
27
28
|
// eslint-disable-next-line no-unused-vars
|
|
28
|
-
export type AdjacencyListOptions<TEdgeType> = {
|
|
29
|
+
export type AdjacencyListOptions<TEdgeType> = {|
|
|
29
30
|
/** The initial number of edges to accommodate. */
|
|
30
|
-
initialCapacity?: number
|
|
31
|
+
initialCapacity?: number,
|
|
31
32
|
/** The max amount by which to grow the capacity. */
|
|
32
|
-
maxGrowFactor?: number
|
|
33
|
+
maxGrowFactor?: number,
|
|
33
34
|
/** The min amount by which to grow the capacity. */
|
|
34
|
-
minGrowFactor?: number
|
|
35
|
+
minGrowFactor?: number,
|
|
35
36
|
/** The size after which to grow the capacity by the minimum factor. */
|
|
36
|
-
peakCapacity?: number
|
|
37
|
+
peakCapacity?: number,
|
|
37
38
|
/** The percentage of deleted edges above which the capacity should shrink. */
|
|
38
|
-
unloadFactor?: number
|
|
39
|
+
unloadFactor?: number,
|
|
39
40
|
/** The amount by which to shrink the capacity. */
|
|
40
|
-
shrinkFactor?: number
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
type AdjacencyListParams = {
|
|
44
|
-
initialCapacity: number
|
|
45
|
-
unloadFactor: number
|
|
46
|
-
maxGrowFactor: number
|
|
47
|
-
minGrowFactor: number
|
|
48
|
-
peakCapacity: number
|
|
49
|
-
shrinkFactor: number
|
|
50
|
-
};
|
|
41
|
+
shrinkFactor?: number,
|
|
42
|
+
|};
|
|
43
|
+
|
|
44
|
+
type AdjacencyListParams = {|
|
|
45
|
+
initialCapacity: number,
|
|
46
|
+
unloadFactor: number,
|
|
47
|
+
maxGrowFactor: number,
|
|
48
|
+
minGrowFactor: number,
|
|
49
|
+
peakCapacity: number,
|
|
50
|
+
shrinkFactor: number,
|
|
51
|
+
|};
|
|
51
52
|
|
|
52
53
|
const DEFAULT_PARAMS: AdjacencyListParams = {
|
|
53
54
|
initialCapacity: 2,
|
|
@@ -67,18 +68,18 @@ const DEFAULT_PARAMS: AdjacencyListParams = {
|
|
|
67
68
|
* `TooManyDeletes` = `3`: the edge map has too many deleted edges
|
|
68
69
|
* `NodesOverloaded` = `4`: the node map is overloaded
|
|
69
70
|
*/
|
|
70
|
-
const LinkResult: {
|
|
71
|
+
const LinkResult: {|
|
|
71
72
|
/** The edge was successfully linked */
|
|
72
|
-
EdgeAdded: 0
|
|
73
|
+
EdgeAdded: 0,
|
|
73
74
|
/** The edge already exists */
|
|
74
|
-
EdgeExists: 1
|
|
75
|
+
EdgeExists: 1,
|
|
75
76
|
/** The edge map is overloaded */
|
|
76
|
-
EdgesOverloaded: 2
|
|
77
|
+
EdgesOverloaded: 2,
|
|
77
78
|
/** The edge map has too many deleted edges */
|
|
78
|
-
TooManyDeletes: 3
|
|
79
|
+
TooManyDeletes: 3,
|
|
79
80
|
/** The node map is overloaded */
|
|
80
|
-
NodesOverloaded: 4
|
|
81
|
-
} = {
|
|
81
|
+
NodesOverloaded: 4,
|
|
82
|
+
|} = {
|
|
82
83
|
EdgeAdded: 0,
|
|
83
84
|
EdgeExists: 1,
|
|
84
85
|
EdgesOverloaded: 2,
|
|
@@ -120,7 +121,7 @@ const MAX_LINK_TRIES: 3 = 3;
|
|
|
120
121
|
* `getInboundEdgesByType` methods.
|
|
121
122
|
*
|
|
122
123
|
*/
|
|
123
|
-
export default class AdjacencyList<TEdgeType
|
|
124
|
+
export default class AdjacencyList<TEdgeType: number = NullEdgeType> {
|
|
124
125
|
#nodes: NodeTypeMap<TEdgeType | NullEdgeType>;
|
|
125
126
|
#edges: EdgeTypeMap<TEdgeType | NullEdgeType>;
|
|
126
127
|
|
|
@@ -139,9 +140,7 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
139
140
|
let nodes;
|
|
140
141
|
let edges;
|
|
141
142
|
|
|
142
|
-
// @ts-expect-error TS2339
|
|
143
143
|
if (opts?.nodes) {
|
|
144
|
-
// @ts-expect-error TS2339
|
|
145
144
|
({nodes, edges} = opts);
|
|
146
145
|
this.#nodes = new NodeTypeMap(nodes);
|
|
147
146
|
this.#edges = new EdgeTypeMap(edges);
|
|
@@ -168,7 +167,7 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
168
167
|
* Create a new `AdjacencyList` with data serialized
|
|
169
168
|
* from another `AdjacencyList`.
|
|
170
169
|
*/
|
|
171
|
-
static deserialize
|
|
170
|
+
static deserialize(
|
|
172
171
|
opts: SerializedAdjacencyList<TEdgeType>,
|
|
173
172
|
): AdjacencyList<TEdgeType> {
|
|
174
173
|
return new AdjacencyList(opts);
|
|
@@ -185,35 +184,35 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
185
184
|
}
|
|
186
185
|
|
|
187
186
|
/** Statistics about the current state of the `AdjacencyList`. */
|
|
188
|
-
get stats(): {
|
|
187
|
+
get stats(): {|
|
|
189
188
|
/** The maximum number of edges the graph can contain. */
|
|
190
|
-
capacity: number
|
|
189
|
+
capacity: number,
|
|
191
190
|
/** The number of nodes in the graph. */
|
|
192
|
-
nodes: number
|
|
191
|
+
nodes: number,
|
|
193
192
|
/** The number of edge types associated with nodes in the graph. */
|
|
194
|
-
nodeEdgeTypes: number
|
|
193
|
+
nodeEdgeTypes: number,
|
|
195
194
|
/** The size of the raw nodes buffer, in mb. */
|
|
196
|
-
nodeBufferSize: string
|
|
195
|
+
nodeBufferSize: string,
|
|
197
196
|
/** The current load on the nodes array. */
|
|
198
|
-
nodeLoad: string
|
|
197
|
+
nodeLoad: string,
|
|
199
198
|
/** The number of edges in the graph. */
|
|
200
|
-
edges: number
|
|
199
|
+
edges: number,
|
|
201
200
|
/** The number of edges deleted from the graph. */
|
|
202
|
-
deleted: number
|
|
201
|
+
deleted: number,
|
|
203
202
|
/** The number of unique edge types in the graph. */
|
|
204
|
-
edgeTypes: number
|
|
203
|
+
edgeTypes: number,
|
|
205
204
|
/** The size of the raw edges buffer, in mb. */
|
|
206
|
-
edgeBufferSize: string
|
|
205
|
+
edgeBufferSize: string,
|
|
207
206
|
/** The current load on the edges array, including deletes. */
|
|
208
|
-
edgeLoadWithDeletes: string
|
|
207
|
+
edgeLoadWithDeletes: string,
|
|
209
208
|
/** The current load on the edges array. */
|
|
210
|
-
edgeLoad: string
|
|
209
|
+
edgeLoad: string,
|
|
211
210
|
/** The total number of edge hash collisions. */
|
|
212
|
-
collisions: number
|
|
211
|
+
collisions: number,
|
|
213
212
|
/** The number of collisions for the most common hash. */
|
|
214
|
-
maxCollisions: number
|
|
213
|
+
maxCollisions: number,
|
|
215
214
|
/** The average number of collisions per hash. */
|
|
216
|
-
avgCollisions: number
|
|
215
|
+
avgCollisions: number,
|
|
217
216
|
/**
|
|
218
217
|
* The actual distribution of hashes vs. the expected (uniform) distribution.
|
|
219
218
|
*
|
|
@@ -222,11 +221,10 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
222
221
|
* > A ratio within one confidence interval (0.95 - 1.05) is indicative
|
|
223
222
|
* > that the hash function...has an expected uniform distribution.
|
|
224
223
|
*/
|
|
225
|
-
uniformity: number
|
|
226
|
-
} {
|
|
224
|
+
uniformity: number,
|
|
225
|
+
|} {
|
|
227
226
|
let edgeTypes = new Set();
|
|
228
227
|
let buckets = new Map();
|
|
229
|
-
// @ts-expect-error TS2488
|
|
230
228
|
for (let {from, to, type} of this.getAllEdges()) {
|
|
231
229
|
let hash = this.#edges.hash(from, to, type);
|
|
232
230
|
let bucket = buckets.get(hash) || new Set();
|
|
@@ -313,7 +311,6 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
313
311
|
this.#edges.from(edge),
|
|
314
312
|
this.#edges.to(edge),
|
|
315
313
|
this.#edges.typeOf(edge),
|
|
316
|
-
// @ts-expect-error TS2345
|
|
317
314
|
edges,
|
|
318
315
|
nodes,
|
|
319
316
|
this.#params.unloadFactor,
|
|
@@ -327,9 +324,7 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
327
324
|
);
|
|
328
325
|
|
|
329
326
|
// Finally, copy the new data arrays over to this graph.
|
|
330
|
-
// @ts-expect-error TS2322
|
|
331
327
|
this.#nodes = nodes;
|
|
332
|
-
// @ts-expect-error TS2322
|
|
333
328
|
this.#edges = edges;
|
|
334
329
|
}
|
|
335
330
|
|
|
@@ -423,11 +418,11 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
423
418
|
/**
|
|
424
419
|
* Iterate over all edges in insertion order.
|
|
425
420
|
*/
|
|
426
|
-
*getAllEdges(): Iterator<{
|
|
427
|
-
type: TEdgeType | NullEdgeType
|
|
428
|
-
from: NodeId
|
|
429
|
-
to: NodeId
|
|
430
|
-
}> {
|
|
421
|
+
*getAllEdges(): Iterator<{|
|
|
422
|
+
type: TEdgeType | NullEdgeType,
|
|
423
|
+
from: NodeId,
|
|
424
|
+
to: NodeId,
|
|
425
|
+
|}> {
|
|
431
426
|
for (let edge of this.#edges) {
|
|
432
427
|
yield {
|
|
433
428
|
from: this.#edges.from(edge),
|
|
@@ -528,14 +523,10 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
528
523
|
* Get a list of every node (labeled `from`) connecting _to_
|
|
529
524
|
* the given `to` node, along with the edge `type` connecting them.
|
|
530
525
|
*/
|
|
531
|
-
getInboundEdgesByType(
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
let edges: Array<{
|
|
536
|
-
from: NodeId;
|
|
537
|
-
type: TEdgeType | NullEdgeType;
|
|
538
|
-
}> = [];
|
|
526
|
+
getInboundEdgesByType(
|
|
527
|
+
to: NodeId,
|
|
528
|
+
): {|type: TEdgeType | NullEdgeType, from: NodeId|}[] {
|
|
529
|
+
let edges = [];
|
|
539
530
|
let node = this.#nodes.head(to);
|
|
540
531
|
while (node !== null) {
|
|
541
532
|
let type = this.#nodes.typeOf(node);
|
|
@@ -554,14 +545,10 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
554
545
|
* Get a list of every node (labeled `to`) connected _from_
|
|
555
546
|
* the given `from` node, along with the edge `type` connecting them.
|
|
556
547
|
*/
|
|
557
|
-
getOutboundEdgesByType(
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
let edges: Array<{
|
|
562
|
-
to: NodeId;
|
|
563
|
-
type: TEdgeType | NullEdgeType;
|
|
564
|
-
}> = [];
|
|
548
|
+
getOutboundEdgesByType(
|
|
549
|
+
from: NodeId,
|
|
550
|
+
): {|type: TEdgeType | NullEdgeType, to: NodeId|}[] {
|
|
551
|
+
let edges = [];
|
|
565
552
|
let node = this.#nodes.head(from);
|
|
566
553
|
while (node !== null) {
|
|
567
554
|
let type = this.#nodes.typeOf(node);
|
|
@@ -591,12 +578,12 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
591
578
|
| NullEdgeType
|
|
592
579
|
| Array<TEdgeType | NullEdgeType> = NULL_EDGE_TYPE,
|
|
593
580
|
): NodeId[] {
|
|
594
|
-
let matches = (node
|
|
581
|
+
let matches = (node) =>
|
|
595
582
|
type === ALL_EDGE_TYPES ||
|
|
596
583
|
(Array.isArray(type)
|
|
597
584
|
? type.includes(this.#nodes.typeOf(node))
|
|
598
585
|
: type === this.#nodes.typeOf(node));
|
|
599
|
-
let nodes
|
|
586
|
+
let nodes = [];
|
|
600
587
|
let seen = new Set<NodeId>();
|
|
601
588
|
let node = this.#nodes.head(from);
|
|
602
589
|
while (node !== null) {
|
|
@@ -621,7 +608,7 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
621
608
|
fn: (nodeId: NodeId) => boolean,
|
|
622
609
|
type: AllEdgeTypes | TEdgeType | NullEdgeType = ALL_EDGE_TYPES,
|
|
623
610
|
) {
|
|
624
|
-
const matches = (node
|
|
611
|
+
const matches = (node) =>
|
|
625
612
|
type === ALL_EDGE_TYPES || type === this.#nodes.typeOf(node);
|
|
626
613
|
|
|
627
614
|
let node = this.#nodes.head(from);
|
|
@@ -642,10 +629,10 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
642
629
|
|
|
643
630
|
forEachNodeIdConnectedTo(
|
|
644
631
|
to: NodeId,
|
|
645
|
-
fn: (nodeId: NodeId) => boolean |
|
|
632
|
+
fn: (nodeId: NodeId) => boolean | void,
|
|
646
633
|
type: AllEdgeTypes | TEdgeType | NullEdgeType = NULL_EDGE_TYPE,
|
|
647
634
|
) {
|
|
648
|
-
const matches = (node
|
|
635
|
+
const matches = (node) =>
|
|
649
636
|
type === ALL_EDGE_TYPES || type === this.#nodes.typeOf(node);
|
|
650
637
|
|
|
651
638
|
let node = this.#nodes.head(to);
|
|
@@ -679,13 +666,13 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
679
666
|
| NullEdgeType
|
|
680
667
|
| Array<TEdgeType | NullEdgeType> = NULL_EDGE_TYPE,
|
|
681
668
|
): NodeId[] {
|
|
682
|
-
let matches = (node
|
|
669
|
+
let matches = (node) =>
|
|
683
670
|
type === ALL_EDGE_TYPES ||
|
|
684
671
|
(Array.isArray(type)
|
|
685
672
|
? type.includes(this.#nodes.typeOf(node))
|
|
686
673
|
: type === this.#nodes.typeOf(node));
|
|
687
674
|
|
|
688
|
-
let nodes
|
|
675
|
+
let nodes = [];
|
|
689
676
|
let seen = new Set<NodeId>();
|
|
690
677
|
let node = this.#nodes.head(to);
|
|
691
678
|
while (node !== null) {
|
|
@@ -752,7 +739,7 @@ export default class AdjacencyList<TEdgeType extends number = NullEdgeType> {
|
|
|
752
739
|
* └────┼─────────┴─────────────────┘ │
|
|
753
740
|
* └─────────────────────────────────────────────┘
|
|
754
741
|
*/
|
|
755
|
-
export class SharedTypeMap<TItemType, THash, TAddress
|
|
742
|
+
export class SharedTypeMap<TItemType, THash, TAddress: number>
|
|
756
743
|
implements Iterable<TAddress>
|
|
757
744
|
{
|
|
758
745
|
/**
|
|
@@ -792,8 +779,7 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
792
779
|
static #TYPE: 1 = 1;
|
|
793
780
|
|
|
794
781
|
/** The largest possible capacity. */
|
|
795
|
-
|
|
796
|
-
static get MAX_CAPACITY<TItemType, THash, TAddress extends number>(): number {
|
|
782
|
+
static get MAX_CAPACITY(): number {
|
|
797
783
|
return Math.floor(
|
|
798
784
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length#what_went_wrong
|
|
799
785
|
(2 ** 31 - 1 - this.HEADER_SIZE) / this.ITEM_SIZE,
|
|
@@ -801,9 +787,7 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
801
787
|
}
|
|
802
788
|
|
|
803
789
|
/** Assert that the given `capacity` does not exceed `MAX_CAPACITY`. */
|
|
804
|
-
static assertMaxCapacity
|
|
805
|
-
capacity: number,
|
|
806
|
-
): void {
|
|
790
|
+
static assertMaxCapacity(capacity: number): void {
|
|
807
791
|
assert(capacity <= this.MAX_CAPACITY, `${this.name} capacity overflow!`);
|
|
808
792
|
}
|
|
809
793
|
|
|
@@ -831,7 +815,6 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
831
815
|
|
|
832
816
|
/** The address of the first item in the map. */
|
|
833
817
|
get addressableLimit(): number {
|
|
834
|
-
// @ts-expect-error TS2339
|
|
835
818
|
return this.constructor.HEADER_SIZE + this.capacity;
|
|
836
819
|
}
|
|
837
820
|
|
|
@@ -852,6 +835,7 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
852
835
|
if (typeof capacityOrData === 'number') {
|
|
853
836
|
let {BYTES_PER_ELEMENT} = Uint32Array;
|
|
854
837
|
let CAPACITY = SharedTypeMap.#CAPACITY;
|
|
838
|
+
// $FlowFixMe[incompatible-call]
|
|
855
839
|
this.data = new Uint32Array(
|
|
856
840
|
new SharedBuffer(this.getLength(capacityOrData) * BYTES_PER_ELEMENT),
|
|
857
841
|
);
|
|
@@ -870,7 +854,6 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
870
854
|
* and is expected to be of equal or smaller capacity to this map.
|
|
871
855
|
*/
|
|
872
856
|
set(data: Uint32Array): void {
|
|
873
|
-
// @ts-expect-error TS2339
|
|
874
857
|
let {HEADER_SIZE, ITEM_SIZE} = this.constructor;
|
|
875
858
|
let NEXT = SharedTypeMap.#NEXT;
|
|
876
859
|
let COUNT = SharedTypeMap.#COUNT;
|
|
@@ -919,34 +902,31 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
919
902
|
* get the length of the map, in bytes.
|
|
920
903
|
*/
|
|
921
904
|
getLength(capacity: number = this.capacity): number {
|
|
922
|
-
// @ts-expect-error TS2339
|
|
923
905
|
let {HEADER_SIZE, ITEM_SIZE} = this.constructor;
|
|
924
906
|
return capacity + HEADER_SIZE + ITEM_SIZE * capacity;
|
|
925
907
|
}
|
|
926
908
|
|
|
927
909
|
/** Get the next available address in the map. */
|
|
928
910
|
getNextAddress(): TAddress {
|
|
929
|
-
// @ts-expect-error TS2339
|
|
930
911
|
let {HEADER_SIZE, ITEM_SIZE} = this.constructor;
|
|
931
|
-
return (HEADER_SIZE + this.capacity + this.count * ITEM_SIZE
|
|
912
|
+
return (HEADER_SIZE + this.capacity + this.count * ITEM_SIZE: any);
|
|
932
913
|
}
|
|
933
914
|
|
|
934
915
|
/** Get the address of the first item with the given hash. */
|
|
935
916
|
head(hash: THash): TAddress | null {
|
|
936
|
-
// @ts-expect-error TS2339
|
|
937
917
|
let {HEADER_SIZE} = this.constructor;
|
|
938
|
-
return (this.data[HEADER_SIZE + (hash
|
|
918
|
+
return (this.data[HEADER_SIZE + (hash: any)]: any) || null;
|
|
939
919
|
}
|
|
940
920
|
|
|
941
921
|
/** Get the address of the next item with the same hash as the given item. */
|
|
942
922
|
next(item: TAddress): TAddress | null {
|
|
943
923
|
let NEXT = SharedTypeMap.#NEXT;
|
|
944
|
-
return (this.data[(item
|
|
924
|
+
return (this.data[(item: any) + NEXT]: any) || null;
|
|
945
925
|
}
|
|
946
926
|
|
|
947
927
|
/** Get the type of the item at the given `item` address. */
|
|
948
928
|
typeOf(item: TAddress): TItemType {
|
|
949
|
-
return this.data[item + SharedTypeMap.#TYPE]
|
|
929
|
+
return (this.data[item + SharedTypeMap.#TYPE]: any);
|
|
950
930
|
}
|
|
951
931
|
|
|
952
932
|
/**
|
|
@@ -957,10 +937,9 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
957
937
|
let COUNT = SharedTypeMap.#COUNT;
|
|
958
938
|
let NEXT = SharedTypeMap.#NEXT;
|
|
959
939
|
let TYPE = SharedTypeMap.#TYPE;
|
|
960
|
-
// @ts-expect-error TS2339
|
|
961
940
|
let {HEADER_SIZE} = this.constructor;
|
|
962
941
|
|
|
963
|
-
this.data[item + TYPE] = type
|
|
942
|
+
this.data[item + TYPE] = (type: any);
|
|
964
943
|
|
|
965
944
|
let prev = this.head(hash);
|
|
966
945
|
if (prev !== null) {
|
|
@@ -972,7 +951,7 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
972
951
|
this.data[prev + NEXT] = item;
|
|
973
952
|
} else {
|
|
974
953
|
// This is the first item in the bucket!
|
|
975
|
-
this.data[HEADER_SIZE + (hash
|
|
954
|
+
this.data[HEADER_SIZE + (hash: any)] = item;
|
|
976
955
|
}
|
|
977
956
|
this.data[COUNT]++;
|
|
978
957
|
}
|
|
@@ -984,7 +963,6 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
984
963
|
let COUNT = SharedTypeMap.#COUNT;
|
|
985
964
|
let NEXT = SharedTypeMap.#NEXT;
|
|
986
965
|
let TYPE = SharedTypeMap.#TYPE;
|
|
987
|
-
// @ts-expect-error TS2339
|
|
988
966
|
let {HEADER_SIZE} = this.constructor;
|
|
989
967
|
|
|
990
968
|
this.data[item + TYPE] = 0;
|
|
@@ -998,7 +976,6 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
998
976
|
let candidate = head;
|
|
999
977
|
while (candidate !== null && candidate !== item) {
|
|
1000
978
|
prev = candidate;
|
|
1001
|
-
// @ts-expect-error TS2322
|
|
1002
979
|
candidate = this.next(candidate);
|
|
1003
980
|
}
|
|
1004
981
|
if (prev !== null && next !== null) {
|
|
@@ -1006,9 +983,9 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
1006
983
|
} else if (prev !== null) {
|
|
1007
984
|
this.data[prev + NEXT] = 0;
|
|
1008
985
|
} else if (next !== null) {
|
|
1009
|
-
this.data[HEADER_SIZE + (hash
|
|
986
|
+
this.data[HEADER_SIZE + (hash: any)] = next;
|
|
1010
987
|
} else {
|
|
1011
|
-
this.data[HEADER_SIZE + (hash
|
|
988
|
+
this.data[HEADER_SIZE + (hash: any)] = 0;
|
|
1012
989
|
}
|
|
1013
990
|
this.data[item + NEXT] = 0;
|
|
1014
991
|
this.data[COUNT]--;
|
|
@@ -1017,7 +994,6 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
1017
994
|
forEach(cb: (item: TAddress) => void): void {
|
|
1018
995
|
let max = this.count;
|
|
1019
996
|
let len = this.length;
|
|
1020
|
-
// @ts-expect-error TS2339
|
|
1021
997
|
let {ITEM_SIZE} = this.constructor;
|
|
1022
998
|
for (
|
|
1023
999
|
let i = this.addressableLimit, count = 0;
|
|
@@ -1025,8 +1001,8 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
1025
1001
|
i += ITEM_SIZE
|
|
1026
1002
|
) {
|
|
1027
1003
|
// Skip items that don't have a type.
|
|
1028
|
-
if (this.typeOf(i
|
|
1029
|
-
cb(i
|
|
1004
|
+
if (this.typeOf((i: any))) {
|
|
1005
|
+
cb((i: any));
|
|
1030
1006
|
count++;
|
|
1031
1007
|
}
|
|
1032
1008
|
}
|
|
@@ -1036,10 +1012,10 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
1036
1012
|
// See https://github.com/facebook/flow/issues/1163#issuecomment-353523840
|
|
1037
1013
|
/*:: @@iterator(): Iterator<TAddress> { return ({}: any); } */
|
|
1038
1014
|
|
|
1015
|
+
// $FlowFixMe[unsupported-syntax]
|
|
1039
1016
|
*[Symbol.iterator](): Iterator<TAddress> {
|
|
1040
1017
|
let max = this.count;
|
|
1041
1018
|
let len = this.length;
|
|
1042
|
-
// @ts-expect-error TS2339
|
|
1043
1019
|
let {ITEM_SIZE} = this.constructor;
|
|
1044
1020
|
for (
|
|
1045
1021
|
let i = this.addressableLimit, count = 0;
|
|
@@ -1047,18 +1023,17 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
1047
1023
|
i += ITEM_SIZE
|
|
1048
1024
|
) {
|
|
1049
1025
|
if (this.data.subarray(i, i + ITEM_SIZE).some(Boolean)) {
|
|
1050
|
-
yield i
|
|
1026
|
+
yield (i: any);
|
|
1051
1027
|
count++;
|
|
1052
1028
|
}
|
|
1053
1029
|
}
|
|
1054
1030
|
}
|
|
1055
1031
|
|
|
1056
|
-
inspect(): {
|
|
1057
|
-
header: Uint32Array
|
|
1058
|
-
table: Uint32Array
|
|
1059
|
-
data: Uint32Array
|
|
1060
|
-
} {
|
|
1061
|
-
// @ts-expect-error TS2339
|
|
1032
|
+
inspect(): {|
|
|
1033
|
+
header: Uint32Array,
|
|
1034
|
+
table: Uint32Array,
|
|
1035
|
+
data: Uint32Array,
|
|
1036
|
+
|} {
|
|
1062
1037
|
const {HEADER_SIZE} = this.constructor;
|
|
1063
1038
|
let min = this.addressableLimit;
|
|
1064
1039
|
|
|
@@ -1088,7 +1063,7 @@ export class SharedTypeMap<TItemType, THash, TAddress extends number>
|
|
|
1088
1063
|
export class NodeTypeMap<TEdgeType> extends SharedTypeMap<
|
|
1089
1064
|
TEdgeType,
|
|
1090
1065
|
NodeId,
|
|
1091
|
-
NodeAddress
|
|
1066
|
+
NodeAddress,
|
|
1092
1067
|
> {
|
|
1093
1068
|
/**
|
|
1094
1069
|
* In addition to the header defined by `SharedTypeMap`, the header for
|
|
@@ -1330,7 +1305,7 @@ export class NodeTypeMap<TEdgeType> extends SharedTypeMap<
|
|
|
1330
1305
|
export class EdgeTypeMap<TEdgeType> extends SharedTypeMap<
|
|
1331
1306
|
TEdgeType,
|
|
1332
1307
|
EdgeHash,
|
|
1333
|
-
EdgeAddress
|
|
1308
|
+
EdgeAddress,
|
|
1334
1309
|
> {
|
|
1335
1310
|
/**
|
|
1336
1311
|
* In addition to the header defined by `SharedTypeMap`, the header for
|
|
@@ -1401,7 +1376,6 @@ export class EdgeTypeMap<TEdgeType> extends SharedTypeMap<
|
|
|
1401
1376
|
|
|
1402
1377
|
/** Get the next available address in the map. */
|
|
1403
1378
|
getNextAddress(): EdgeAddress {
|
|
1404
|
-
// @ts-expect-error TS2339
|
|
1405
1379
|
let {ITEM_SIZE} = this.constructor;
|
|
1406
1380
|
return this.addressableLimit + (this.count + this.deletes) * ITEM_SIZE;
|
|
1407
1381
|
}
|
|
@@ -1561,9 +1535,9 @@ export class EdgeTypeMap<TEdgeType> extends SharedTypeMap<
|
|
|
1561
1535
|
// the output widely. Then we do a series of prime multiplications and
|
|
1562
1536
|
// additions to combine the hashes into one value.
|
|
1563
1537
|
let hash = 17;
|
|
1564
|
-
hash = hash * 37 + hash32shift(from
|
|
1565
|
-
hash = hash * 37 + hash32shift(to
|
|
1566
|
-
hash = hash * 37 + hash32shift(type
|
|
1538
|
+
hash = hash * 37 + hash32shift((from: any));
|
|
1539
|
+
hash = hash * 37 + hash32shift((to: any));
|
|
1540
|
+
hash = hash * 37 + hash32shift((type: any));
|
|
1567
1541
|
// Finally, we map the hash to a value modulo the edge capacity.
|
|
1568
1542
|
hash %= this.capacity;
|
|
1569
1543
|
return hash;
|
|
@@ -1580,14 +1554,14 @@ export class EdgeTypeMap<TEdgeType> extends SharedTypeMap<
|
|
|
1580
1554
|
* - `3` TooManyDeletes: the edge map has too many deleted edges
|
|
1581
1555
|
* - `4` NodesOverloaded: the node map is overloaded
|
|
1582
1556
|
*/
|
|
1583
|
-
function link<TEdgeType
|
|
1557
|
+
function link<TEdgeType: number>(
|
|
1584
1558
|
from: NodeId,
|
|
1585
1559
|
to: NodeId,
|
|
1586
1560
|
type: TEdgeType | NullEdgeType,
|
|
1587
1561
|
edges: EdgeTypeMap<TEdgeType | NullEdgeType>,
|
|
1588
1562
|
nodes: NodeTypeMap<TEdgeType | NullEdgeType>,
|
|
1589
1563
|
unloadFactor: number = DEFAULT_PARAMS.unloadFactor,
|
|
1590
|
-
):
|
|
1564
|
+
): $Values<typeof LinkResult> {
|
|
1591
1565
|
let hash = edges.hash(from, to, type);
|
|
1592
1566
|
let edge = edges.addressOf(hash, from, to, type);
|
|
1593
1567
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
|
|
1
3
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32#implementing_count_leading_ones_and_beyond
|
|
2
4
|
function ctz32(n: number): number {
|
|
3
5
|
if (n === 0) {
|
|
@@ -118,6 +120,7 @@ export class BitSet {
|
|
|
118
120
|
let v = bits[k];
|
|
119
121
|
while (v !== 0) {
|
|
120
122
|
let t = (v & -v) >>> 0;
|
|
123
|
+
// $FlowFixMe
|
|
121
124
|
fn((k << 5) + ctz32(v));
|
|
122
125
|
v ^= t;
|
|
123
126
|
}
|
|
@@ -1,30 +1,27 @@
|
|
|
1
|
+
// @flow strict-local
|
|
1
2
|
import type {ContentKey, NodeId} from './types';
|
|
2
3
|
|
|
3
|
-
import Graph, {SerializedGraph, GraphOpts} from './Graph';
|
|
4
|
+
import Graph, {type SerializedGraph, type GraphOpts} from './Graph';
|
|
4
5
|
import nullthrows from 'nullthrows';
|
|
5
6
|
|
|
6
|
-
export type ContentGraphOpts<TNode, TEdgeType
|
|
7
|
-
TNode,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
TEdgeType extends number = 1,
|
|
16
|
-
> = SerializedGraph<TNode, TEdgeType> & {
|
|
17
|
-
_contentKeyToNodeId: Map<ContentKey, NodeId>;
|
|
18
|
-
};
|
|
7
|
+
export type ContentGraphOpts<TNode, TEdgeType: number = 1> = {|
|
|
8
|
+
...GraphOpts<TNode, TEdgeType>,
|
|
9
|
+
_contentKeyToNodeId: Map<ContentKey, NodeId>,
|
|
10
|
+
_nodeIdToContentKey: Map<NodeId, ContentKey>,
|
|
11
|
+
|};
|
|
12
|
+
export type SerializedContentGraph<TNode, TEdgeType: number = 1> = {|
|
|
13
|
+
...SerializedGraph<TNode, TEdgeType>,
|
|
14
|
+
_contentKeyToNodeId: Map<ContentKey, NodeId>,
|
|
15
|
+
|};
|
|
19
16
|
|
|
20
|
-
export default class ContentGraph<
|
|
17
|
+
export default class ContentGraph<TNode, TEdgeType: number = 1> extends Graph<
|
|
21
18
|
TNode,
|
|
22
|
-
TEdgeType
|
|
23
|
-
>
|
|
19
|
+
TEdgeType,
|
|
20
|
+
> {
|
|
24
21
|
_contentKeyToNodeId: Map<ContentKey, NodeId>;
|
|
25
22
|
_nodeIdToContentKey: Map<NodeId, ContentKey>;
|
|
26
23
|
|
|
27
|
-
constructor(opts
|
|
24
|
+
constructor(opts: ?ContentGraphOpts<TNode, TEdgeType>) {
|
|
28
25
|
if (opts) {
|
|
29
26
|
let {_contentKeyToNodeId, _nodeIdToContentKey, ...rest} = opts;
|
|
30
27
|
super(rest);
|
|
@@ -37,17 +34,19 @@ export default class ContentGraph<
|
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
|
|
37
|
+
// $FlowFixMe[prop-missing]
|
|
38
|
+
static deserialize(
|
|
41
39
|
opts: ContentGraphOpts<TNode, TEdgeType>,
|
|
42
40
|
): ContentGraph<TNode, TEdgeType> {
|
|
43
41
|
return new ContentGraph(opts);
|
|
44
42
|
}
|
|
45
43
|
|
|
44
|
+
// $FlowFixMe[prop-missing]
|
|
46
45
|
serialize(): SerializedContentGraph<TNode, TEdgeType> {
|
|
46
|
+
// $FlowFixMe[prop-missing]
|
|
47
47
|
return {
|
|
48
48
|
...super.serialize(),
|
|
49
49
|
_contentKeyToNodeId: this._contentKeyToNodeId,
|
|
50
|
-
// @ts-expect-error TS2353
|
|
51
50
|
_nodeIdToContentKey: this._nodeIdToContentKey,
|
|
52
51
|
};
|
|
53
52
|
}
|
|
@@ -69,7 +68,7 @@ export default class ContentGraph<
|
|
|
69
68
|
: this.addNodeByContentKey(contentKey, node);
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
getNodeByContentKey(contentKey: ContentKey): TNode
|
|
71
|
+
getNodeByContentKey(contentKey: ContentKey): ?TNode {
|
|
73
72
|
let nodeId = this._contentKeyToNodeId.get(contentKey);
|
|
74
73
|
if (nodeId != null) {
|
|
75
74
|
return super.getNode(nodeId);
|