@graphty/graph-format 0.0.0 → 0.2.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/LICENSE +21 -0
- package/README.md +216 -43
- package/dist/graph-format.d.ts +1 -0
- package/dist/graph-format.d.ts.map +1 -0
- package/dist/graph-format.js +15798 -0
- package/dist/graph-format.js.map +1 -0
- package/dist/src/builder/arena.d.ts +85 -0
- package/dist/src/builder/arena.d.ts.map +1 -0
- package/dist/src/builder/arena.js +154 -0
- package/dist/src/builder/arena.js.map +1 -0
- package/dist/src/builder/compact.d.ts +515 -0
- package/dist/src/builder/compact.d.ts.map +1 -0
- package/dist/src/builder/compact.js +1553 -0
- package/dist/src/builder/compact.js.map +1 -0
- package/dist/src/builder/counting-sort.d.ts +96 -0
- package/dist/src/builder/counting-sort.d.ts.map +1 -0
- package/dist/src/builder/counting-sort.js +430 -0
- package/dist/src/builder/counting-sort.js.map +1 -0
- package/dist/src/builder/freeze.d.ts +72 -0
- package/dist/src/builder/freeze.d.ts.map +1 -0
- package/dist/src/builder/freeze.js +548 -0
- package/dist/src/builder/freeze.js.map +1 -0
- package/dist/src/builder/graph-builder.d.ts +602 -0
- package/dist/src/builder/graph-builder.d.ts.map +1 -0
- package/dist/src/builder/graph-builder.js +1940 -0
- package/dist/src/builder/graph-builder.js.map +1 -0
- package/dist/src/builder/options.d.ts +16 -0
- package/dist/src/builder/options.d.ts.map +1 -0
- package/dist/src/builder/options.js +21 -0
- package/dist/src/builder/options.js.map +1 -0
- package/dist/src/columns/bitmap.d.ts +96 -0
- package/dist/src/columns/bitmap.d.ts.map +1 -0
- package/dist/src/columns/bitmap.js +184 -0
- package/dist/src/columns/bitmap.js.map +1 -0
- package/dist/src/columns/column.d.ts +220 -0
- package/dist/src/columns/column.d.ts.map +1 -0
- package/dist/src/columns/column.js +2109 -0
- package/dist/src/columns/column.js.map +1 -0
- package/dist/src/columns/dictionary.d.ts +60 -0
- package/dist/src/columns/dictionary.d.ts.map +1 -0
- package/dist/src/columns/dictionary.js +102 -0
- package/dist/src/columns/dictionary.js.map +1 -0
- package/dist/src/columns/growable.d.ts +198 -0
- package/dist/src/columns/growable.d.ts.map +1 -0
- package/dist/src/columns/growable.js +296 -0
- package/dist/src/columns/growable.js.map +1 -0
- package/dist/src/columns/infer.d.ts +121 -0
- package/dist/src/columns/infer.d.ts.map +1 -0
- package/dist/src/columns/infer.js +268 -0
- package/dist/src/columns/infer.js.map +1 -0
- package/dist/src/columns/remap.d.ts +143 -0
- package/dist/src/columns/remap.d.ts.map +1 -0
- package/dist/src/columns/remap.js +588 -0
- package/dist/src/columns/remap.js.map +1 -0
- package/dist/src/columns/table.d.ts +184 -0
- package/dist/src/columns/table.d.ts.map +1 -0
- package/dist/src/columns/table.js +485 -0
- package/dist/src/columns/table.js.map +1 -0
- package/dist/src/constants.d.ts +34 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +79 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/errors.d.ts +76 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +32 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/ids/edge-id-index.d.ts +77 -0
- package/dist/src/ids/edge-id-index.d.ts.map +1 -0
- package/dist/src/ids/edge-id-index.js +155 -0
- package/dist/src/ids/edge-id-index.js.map +1 -0
- package/dist/src/ids/node-id-map.d.ts +280 -0
- package/dist/src/ids/node-id-map.d.ts.map +1 -0
- package/dist/src/ids/node-id-map.js +1139 -0
- package/dist/src/ids/node-id-map.js.map +1 -0
- package/dist/src/ids/string-store.d.ts +219 -0
- package/dist/src/ids/string-store.d.ts.map +1 -0
- package/dist/src/ids/string-store.js +526 -0
- package/dist/src/ids/string-store.js.map +1 -0
- package/dist/src/index.d.ts +30 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +29 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/populate/from-csr.d.ts +61 -0
- package/dist/src/populate/from-csr.d.ts.map +1 -0
- package/dist/src/populate/from-csr.js +706 -0
- package/dist/src/populate/from-csr.js.map +1 -0
- package/dist/src/populate/from-edge-arrays.d.ts +51 -0
- package/dist/src/populate/from-edge-arrays.d.ts.map +1 -0
- package/dist/src/populate/from-edge-arrays.js +217 -0
- package/dist/src/populate/from-edge-arrays.js.map +1 -0
- package/dist/src/populate/from-records.d.ts +51 -0
- package/dist/src/populate/from-records.d.ts.map +1 -0
- package/dist/src/populate/from-records.js +447 -0
- package/dist/src/populate/from-records.js.map +1 -0
- package/dist/src/snapshot/derived.d.ts +211 -0
- package/dist/src/snapshot/derived.d.ts.map +1 -0
- package/dist/src/snapshot/derived.js +1254 -0
- package/dist/src/snapshot/derived.js.map +1 -0
- package/dist/src/snapshot/graph-meta.d.ts +23 -0
- package/dist/src/snapshot/graph-meta.d.ts.map +1 -0
- package/dist/src/snapshot/graph-meta.js +159 -0
- package/dist/src/snapshot/graph-meta.js.map +1 -0
- package/dist/src/snapshot/graph-snapshot.d.ts +509 -0
- package/dist/src/snapshot/graph-snapshot.d.ts.map +1 -0
- package/dist/src/snapshot/graph-snapshot.js +1247 -0
- package/dist/src/snapshot/graph-snapshot.js.map +1 -0
- package/dist/src/snapshot/hash.d.ts +90 -0
- package/dist/src/snapshot/hash.d.ts.map +1 -0
- package/dist/src/snapshot/hash.js +228 -0
- package/dist/src/snapshot/hash.js.map +1 -0
- package/dist/src/snapshot/queries.d.ts +74 -0
- package/dist/src/snapshot/queries.d.ts.map +1 -0
- package/dist/src/snapshot/queries.js +131 -0
- package/dist/src/snapshot/queries.js.map +1 -0
- package/dist/src/snapshot/validate.d.ts +192 -0
- package/dist/src/snapshot/validate.d.ts.map +1 -0
- package/dist/src/snapshot/validate.js +990 -0
- package/dist/src/snapshot/validate.js.map +1 -0
- package/dist/src/snapshot/views.d.ts +274 -0
- package/dist/src/snapshot/views.d.ts.map +1 -0
- package/dist/src/snapshot/views.js +856 -0
- package/dist/src/snapshot/views.js.map +1 -0
- package/dist/src/types/builder.d.ts +456 -0
- package/dist/src/types/builder.d.ts.map +1 -0
- package/dist/src/types/builder.js +11 -0
- package/dist/src/types/builder.js.map +1 -0
- package/dist/src/types/columns.d.ts +615 -0
- package/dist/src/types/columns.d.ts.map +1 -0
- package/dist/src/types/columns.js +14 -0
- package/dist/src/types/columns.js.map +1 -0
- package/dist/src/types/index.d.ts +14 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +11 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/internal.d.ts +172 -0
- package/dist/src/types/internal.d.ts.map +1 -0
- package/dist/src/types/internal.js +13 -0
- package/dist/src/types/internal.js.map +1 -0
- package/dist/src/types/snapshot.d.ts +801 -0
- package/dist/src/types/snapshot.d.ts.map +1 -0
- package/dist/src/types/snapshot.js +14 -0
- package/dist/src/types/snapshot.js.map +1 -0
- package/dist/src/types/wire.d.ts +196 -0
- package/dist/src/types/wire.d.ts.map +1 -0
- package/dist/src/types/wire.js +8 -0
- package/dist/src/types/wire.js.map +1 -0
- package/dist/src/util/mask.d.ts +55 -0
- package/dist/src/util/mask.d.ts.map +1 -0
- package/dist/src/util/mask.js +71 -0
- package/dist/src/util/mask.js.map +1 -0
- package/dist/src/util/options.d.ts +22 -0
- package/dist/src/util/options.d.ts.map +1 -0
- package/dist/src/util/options.js +49 -0
- package/dist/src/util/options.js.map +1 -0
- package/dist/src/util/shared-buffers.d.ts +42 -0
- package/dist/src/util/shared-buffers.d.ts.map +1 -0
- package/dist/src/util/shared-buffers.js +52 -0
- package/dist/src/util/shared-buffers.js.map +1 -0
- package/dist/src/util/typed-array.d.ts +205 -0
- package/dist/src/util/typed-array.d.ts.map +1 -0
- package/dist/src/util/typed-array.js +289 -0
- package/dist/src/util/typed-array.js.map +1 -0
- package/dist/src/wire/bytes.d.ts +69 -0
- package/dist/src/wire/bytes.d.ts.map +1 -0
- package/dist/src/wire/bytes.js +361 -0
- package/dist/src/wire/bytes.js.map +1 -0
- package/dist/src/wire/carried-views.d.ts +111 -0
- package/dist/src/wire/carried-views.d.ts.map +1 -0
- package/dist/src/wire/carried-views.js +286 -0
- package/dist/src/wire/carried-views.js.map +1 -0
- package/dist/src/wire/from-wire.d.ts +144 -0
- package/dist/src/wire/from-wire.d.ts.map +1 -0
- package/dist/src/wire/from-wire.js +1425 -0
- package/dist/src/wire/from-wire.js.map +1 -0
- package/dist/src/wire/to-wire.d.ts +171 -0
- package/dist/src/wire/to-wire.d.ts.map +1 -0
- package/dist/src/wire/to-wire.js +652 -0
- package/dist/src/wire/to-wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +72 -7
- package/src/builder/arena.ts +192 -0
- package/src/builder/compact.ts +1811 -0
- package/src/builder/counting-sort.ts +506 -0
- package/src/builder/freeze.ts +689 -0
- package/src/builder/graph-builder.ts +2264 -0
- package/src/builder/options.ts +29 -0
- package/src/columns/bitmap.ts +196 -0
- package/src/columns/column.ts +2574 -0
- package/src/columns/dictionary.ts +114 -0
- package/src/columns/growable.ts +350 -0
- package/src/columns/infer.ts +285 -0
- package/src/columns/remap.ts +663 -0
- package/src/columns/table.ts +568 -0
- package/src/constants.ts +89 -0
- package/src/errors.ts +115 -0
- package/src/ids/edge-id-index.ts +179 -0
- package/src/ids/node-id-map.ts +1340 -0
- package/src/ids/string-store.ts +589 -0
- package/src/index.ts +139 -0
- package/src/lib-resizable-array-buffer.d.ts +34 -0
- package/src/populate/from-csr.ts +802 -0
- package/src/populate/from-edge-arrays.ts +257 -0
- package/src/populate/from-records.ts +536 -0
- package/src/snapshot/derived.ts +1486 -0
- package/src/snapshot/graph-meta.ts +167 -0
- package/src/snapshot/graph-snapshot.ts +1503 -0
- package/src/snapshot/hash.ts +241 -0
- package/src/snapshot/queries.ts +136 -0
- package/src/snapshot/validate.ts +1108 -0
- package/src/snapshot/views.ts +943 -0
- package/src/types/builder.ts +480 -0
- package/src/types/columns.ts +727 -0
- package/src/types/index.ts +14 -0
- package/src/types/internal.ts +195 -0
- package/src/types/snapshot.ts +886 -0
- package/src/types/wire.ts +207 -0
- package/src/util/mask.ts +89 -0
- package/src/util/options.ts +57 -0
- package/src/util/shared-buffers.ts +56 -0
- package/src/util/typed-array.ts +371 -0
- package/src/wire/bytes.ts +440 -0
- package/src/wire/carried-views.ts +327 -0
- package/src/wire/from-wire.ts +1690 -0
- package/src/wire/to-wire.ts +755 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Adam Powers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,218 @@
|
|
|
1
1
|
# @graphty/graph-format
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
3
|
+
[](https://github.com/graphty-org/graphty-monorepo/actions/workflows/ci.yml)
|
|
4
|
+
[](https://coveralls.io/github/graphty-org/graphty-monorepo?branch=master)
|
|
5
|
+
[](https://www.npmjs.com/package/@graphty/graph-format)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
Frozen CSR graph data format over typed arrays, shared by CPU and WebGPU graph algorithms.
|
|
9
|
+
|
|
10
|
+
@graphty/graph-format is a zero-runtime-dependency, framework-agnostic graph DATA FORMAT: a frozen
|
|
11
|
+
compressed-sparse-row (CSR) representation over typed arrays, a mutable builder that produces frozen
|
|
12
|
+
snapshots, columnar typed node/edge attribute storage, and an id-to-index map kept outside the CSR.
|
|
13
|
+
It contains no graph algorithms, no rendering and no parsers (see @graphty/graph-io).
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **Frozen CSR snapshot** (`GraphSnapshot`): `rowPtr` / `colIdx` / `weights` / `arcToEdge` /
|
|
18
|
+
`edgeToArc` as 4-byte-aligned typed arrays in one 256-byte-aligned arena, so the whole core uploads
|
|
19
|
+
to a GPU with one `writeBuffer`. Topology, counts, flags and id map never change after `freeze()`.
|
|
20
|
+
- **Builder** (`GraphBuilder`): long-lived, mutable, repeatedly freezable; ids or indices; directed
|
|
21
|
+
or undirected (with expansion); duplicate-edge and self-loop policies; f32 or f64 weights; two
|
|
22
|
+
stable counting sorts (never a comparator sort) so every freeze is deterministic and prefix-stable.
|
|
23
|
+
Column names resolve through an index (declaring and looking up 100k columns is linear), and
|
|
24
|
+
an inferred column can be widened by name through `widenNodeColumn()` / `widenEdgeColumn()`
|
|
25
|
+
(also on the `GraphSink` contract, optional) so a text importer can give a `2.0`-style column its
|
|
26
|
+
f64 dtype whatever its values imply.
|
|
27
|
+
- **Typed attribute columns** (`AttributeTable`, `Column`): `f32 f64 i32 u32 u8 bool dict string
|
|
28
|
+
list json`, multi-component strides, Arrow-style validity bitmaps, roles, provenance, mutable
|
|
29
|
+
columns for layouts, extension tables for temporal attributes, and `gpuView()` for what the GPU
|
|
30
|
+
binds.
|
|
31
|
+
- **Id map** (`NodeIdMap`): five storage kinds (`identity`, `dense`, `numeric`, `string`, `mixed`)
|
|
32
|
+
chosen at freeze; zero bytes for the common `0..n-1` case; lazy reverse map.
|
|
33
|
+
- **Views** (lazy, cached, shared): `reverse()`, `coo()`, `edgeList()`, degrees, weighted degrees,
|
|
34
|
+
self-loops, `mate()`, `degreeOrder()`, `isSymmetric()`.
|
|
35
|
+
- **Derived graphs** (new snapshots with index maps back): `toUndirected()`, `transpose()`,
|
|
36
|
+
`simplified()`, `withoutSelfLoops()`, `filterEdges()`, `inducedSubgraph()`, `contract()`,
|
|
37
|
+
`relabel()`, `withColumns()`.
|
|
38
|
+
- **Wire form and GSNP container**: `toWire()` / `fromWire()` for `postMessage` and IndexedDB
|
|
39
|
+
(transfer-aware: shared buffers are copied, exclusive ones transferred), `toBytes()` / `fromBytes()`
|
|
40
|
+
/ `fromByteChunks()` for files and the network, with `none` / `structure` / `full` validation of
|
|
41
|
+
untrusted input.
|
|
42
|
+
- **Factories**: `fromEdgeArrays()`, `fromCsr()` (adopts caller arrays with validation),
|
|
43
|
+
`fromRecords()` (node-link JSON shapes).
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm install @graphty/graph-format
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
ESM only. Node >= 18.19.0 or any browser with ES2020 support. No runtime dependencies.
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { fromBytes, GraphBuilder } from "@graphty/graph-format";
|
|
57
|
+
|
|
58
|
+
// 1. Build. Ids are strings or numbers; indices are assigned in first-appearance order.
|
|
59
|
+
const builder = new GraphBuilder({ directed: false, weightDtype: "f32" });
|
|
60
|
+
builder.declareNodeColumn({ name: "label", dtype: "string", role: "label" });
|
|
61
|
+
builder.addNodeRecord("alice", { label: "Alice" });
|
|
62
|
+
builder.addNodeRecord("bob", { label: "Bob" });
|
|
63
|
+
builder.addEdge("alice", "bob", 2.5);
|
|
64
|
+
builder.addEdge("bob", "carol", 1); // "carol" is added on first mention
|
|
65
|
+
|
|
66
|
+
// 2. Freeze: a frozen CSR snapshot; the builder stays usable and can be frozen again.
|
|
67
|
+
const snapshot = builder.freeze();
|
|
68
|
+
console.log(snapshot.nodeCount, snapshot.edgeCount, snapshot.arcCount); // 3 2 4 (undirected: two arcs per edge)
|
|
69
|
+
|
|
70
|
+
// 3. Walk rows. Every out-arc of node u lives in [rowPtr[u], rowPtr[u + 1]).
|
|
71
|
+
const { rowPtr, colIdx, weights } = snapshot;
|
|
72
|
+
const strength = new Float64Array(snapshot.nodeCount);
|
|
73
|
+
for (let u = 0; u < snapshot.nodeCount; u++) {
|
|
74
|
+
for (let a = rowPtr[u]; a < rowPtr[u + 1]; a++) {
|
|
75
|
+
const v = colIdx[a];
|
|
76
|
+
strength[u] += weights === null ? 1 : weights[a];
|
|
77
|
+
void v; // the neighbour index
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 4. Map results back to ids only at the boundary.
|
|
82
|
+
const byId = snapshot.ids.toMap(strength); // Map<NodeId, number>: "alice" -> 2.5, "bob" -> 3.5, "carol" -> 1
|
|
83
|
+
const labels = snapshot.nodes.requireTyped("label", "string");
|
|
84
|
+
console.log(labels.valueAt(snapshot.ids.requireIndex("bob"))); // "Bob"
|
|
85
|
+
|
|
86
|
+
// 5. Views are lazy, cached and shared (call .slice() for scratch); derived graphs are new snapshots.
|
|
87
|
+
const degree = snapshot.degree(); // Uint32Array(nodeCount)
|
|
88
|
+
const { snapshot: simple, edgeRemap } = snapshot.simplified();
|
|
89
|
+
void degree;
|
|
90
|
+
void simple;
|
|
91
|
+
void edgeRemap;
|
|
92
|
+
|
|
93
|
+
// 6. Serialise: one contiguous GSNP container (or toWire() for postMessage / IndexedDB).
|
|
94
|
+
const bytes = snapshot.toBytes();
|
|
95
|
+
const back = fromBytes(bytes); // validates fully by default
|
|
96
|
+
console.log(back.ids.idOf(0), byId.get("bob")); // "alice" 3.5
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Invariants
|
|
100
|
+
|
|
101
|
+
Every snapshot, whether frozen by the builder, adopted by `fromCsr()` or decoded by `fromBytes()`,
|
|
102
|
+
satisfies all of the following (design section 3.2). Kernels and algorithms may assume them without
|
|
103
|
+
checking; `validate()` re-checks I1-I13. Changing any of them is a data-model major bump.
|
|
104
|
+
|
|
105
|
+
- **I1** `rowPtr.length === nodeCount + 1`, `rowPtr[0] === 0`, `rowPtr` non-decreasing,
|
|
106
|
+
`rowPtr[nodeCount] === arcCount === colIdx.length`.
|
|
107
|
+
- **I2** `colIdx[a] < nodeCount` for every arc; `INVALID_INDEX` never appears in a core or view array.
|
|
108
|
+
- **I3** `nodeCount`, `edgeCount`, `arcCount <= MAX_COUNT (0xFFFFFFFE)`; `edgeCount <= arcCount`.
|
|
109
|
+
Arc indices may exceed 2^31: never apply JS bitwise operators to them.
|
|
110
|
+
- **I4** Sorted rows: within a row `colIdx` is non-decreasing and equal targets are ordered by
|
|
111
|
+
ascending `arcToEdge`. There is no unsorted mode.
|
|
112
|
+
- **I5** Permutations: `arcToEdge` (arcCount entries, values `< edgeCount`) and `edgeToArc`
|
|
113
|
+
(edgeCount entries, values `< arcCount`) with `arcToEdge[edgeToArc[e]] === e`; the arc at
|
|
114
|
+
`edgeToArc[e]` stores the declared `source -> target` orientation of edge `e`.
|
|
115
|
+
- **I6** Directed: `arcCount === edgeCount` and `arcToEdge` is a permutation of `0..edgeCount-1`.
|
|
116
|
+
- **I7** Undirected: doubled storage; every non-loop arc has exactly one mate in the opposite row
|
|
117
|
+
with the same edge and weight; a self-loop has one arc; `arcCount === 2 * edgeCount - selfLoopCount`.
|
|
118
|
+
- **I8** `weights === null` or `weights.length === arcCount`; no `NaN` (infinities are legal and
|
|
119
|
+
reported by `flags.finiteWeights`).
|
|
120
|
+
- **I9** Flags are truthful: every `SnapshotFlags` member is a predicate over the arrays, computed at
|
|
121
|
+
freeze or by validation, never guessed.
|
|
122
|
+
- **I10** Alignment: every core array, 4-byte view array and GPU-bindable column array is a view over
|
|
123
|
+
a plain `ArrayBuffer` with `byteOffset % 4 === 0` and `byteLength % 4 === 0`; arena segments start
|
|
124
|
+
at multiples of 256 bytes.
|
|
125
|
+
- **I11** `ids` is a bijection between `[0, nodeCount)` and the id set (SameValueZero, no `NaN`).
|
|
126
|
+
- **I12** `nodes.rowCount === nodeCount`, `edges.rowCount === edgeCount`, `graph.rowCount === 1`;
|
|
127
|
+
every column obeys its length rules; dictionary codes and `refersTo` indices are in range.
|
|
128
|
+
- **I13** Edge attribute columns are indexed by logical edge, never by arc.
|
|
129
|
+
- **I14** Node index = order of first appearance; edge index = order of `addEdge` minus removals and
|
|
130
|
+
merges, relative order preserved. Nothing is ever sorted by id or degree at freeze.
|
|
131
|
+
- **I15** Determinism: the same builder operations with the same options give byte-identical core
|
|
132
|
+
arrays, flags, id map and columns on every run and engine.
|
|
133
|
+
- **I16** Prefix stability: without removals or merging policies between two freezes, the earlier
|
|
134
|
+
snapshot's indices are a prefix of the later one's; `freezeWithReport` says exactly when a remap
|
|
135
|
+
happened.
|
|
136
|
+
- **I17** After `freeze()` no core array, count, flag, id-map entry or immutable column changes; views
|
|
137
|
+
are memoised once and shared; only the column SET and the CONTENTS of `mutable` columns may change.
|
|
138
|
+
- **I18** No core array, view array or column aliases memory a builder can still write.
|
|
139
|
+
|
|
140
|
+
## Counting Vocabulary
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
nodeCount n
|
|
144
|
+
edgeCount number of logical edges (NetworkX number_of_edges);
|
|
145
|
+
every EDGE column has edgeCount rows
|
|
146
|
+
arcCount colIdx.length === rowPtr[nodeCount];
|
|
147
|
+
every ARC-aligned array (weights, arcToEdge, per-arc results) has arcCount entries
|
|
148
|
+
selfLoopCount logical edges with source === target
|
|
149
|
+
directed: arcCount === edgeCount
|
|
150
|
+
undirected: arcCount === 2 * edgeCount - selfLoopCount
|
|
151
|
+
INVALID_INDEX 0xFFFFFFFF, the one "no index" sentinel for node, edge and arc indices,
|
|
152
|
+
in JS return values and inside Uint32Array vectors alike
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
A **node index** is a dense integer `0 <= i < nodeCount`; a **node id** (`string | number`) lives only
|
|
156
|
+
in the id map and at API boundaries and is never coerced. A **logical edge** is one edge as declared;
|
|
157
|
+
an **arc** is one entry of `colIdx` (a directed edge is one arc, an undirected edge two, an undirected
|
|
158
|
+
self-loop one). A **row** is the arc range `[rowPtr[u], rowPtr[u + 1])` of node `u`.
|
|
159
|
+
|
|
160
|
+
## Views and Derived Graphs
|
|
161
|
+
|
|
162
|
+
Views are pure functions of the core, computed on first call, cached for the life of the snapshot
|
|
163
|
+
and SHARED (writing into one is a contract violation; `.slice()` first). `prepare(names)` computes
|
|
164
|
+
a set eagerly, `dropCaches()` releases them, `cachedViews()` lists what is resident.
|
|
165
|
+
|
|
166
|
+
| Method | Returns | Cost | Notes |
|
|
167
|
+
| ----------------------------------------------------------------- | ------------------- | ---------------------- | ----------------------------------------------------------- |
|
|
168
|
+
| `reverse()` | `ReverseView` | O(n + m) | in-adjacency; undirected: the forward arrays themselves |
|
|
169
|
+
| `coo()` | `CooView` | O(m) | `src` is the only new array |
|
|
170
|
+
| `edgeList()` | `EdgeListView` | O(m) | each logical edge once, declared orientation |
|
|
171
|
+
| `outDegree()` / `inDegree()` / `degree()` | `U32(n)` | O(n) / O(m) / O(n + m) | graph-theoretic degree counts an undirected self-loop twice |
|
|
172
|
+
| `weightedOutDegree()` / `weightedInDegree()` / `weightedDegree()` | `F64(n)` | O(m) | f64 for modularity / PageRank consumers |
|
|
173
|
+
| `selfLoopWeight()` / `totalWeight()` | `F64(n)` / `number` | O(n log d) / O(m) | |
|
|
174
|
+
| `selfLoopArcs()` / `selfLoopsPerNode()` / `selfLoopsAt(u)` | `U32` | O(n log d) | |
|
|
175
|
+
| `mate()` | `U32(arcCount)` | O(m) | undirected only (`E_DIRECTED`) |
|
|
176
|
+
| `degreeOrder({ of })` | `DegreeOrderView` | O(n + maxDegree) | `perm` plus segment offsets at the 1024 / 32 / 1 thresholds |
|
|
177
|
+
| `isSymmetric()` | `boolean` | O(m) | |
|
|
178
|
+
|
|
179
|
+
Derived graphs are new snapshots with maps back to the source (`nodeOrigin`, `edgeOrigin`,
|
|
180
|
+
`nodeRemap`, `edgeRemap`, each `null` when that index space is unchanged); the format never caches
|
|
181
|
+
them.
|
|
182
|
+
|
|
183
|
+
| Method | Node space | Edge space | Semantics |
|
|
184
|
+
| -------------------------------------- | -------------- | ---------- | -------------------------------------------------------------------------------- |
|
|
185
|
+
| `toUndirected(opts?)` | same | new | reciprocal pairs collapse keep-first; `reciprocal: true` keeps mutual pairs only |
|
|
186
|
+
| `transpose()` | same | same | every orientation swapped, zero copy of `reverse()` |
|
|
187
|
+
| `simplified(opts?)` | same | new | one edge per `(u, v)` group |
|
|
188
|
+
| `withoutSelfLoops()` | same | new | |
|
|
189
|
+
| `filterEdges(mask)` | same | new | packed bitmap over logical edges |
|
|
190
|
+
| `inducedSubgraph(indices \| { mask })` | new | new | edges with both endpoints kept |
|
|
191
|
+
| `contract(partition, opts?)` | new (k blocks) | new | Leiden / Louvain aggregation and condensation |
|
|
192
|
+
| `relabel(perm)` | permuted | same | `perm[newIndex] = oldIndex` |
|
|
193
|
+
| `withColumns(nodes?, edges?)` | same | same | shares the core, clones the column set |
|
|
194
|
+
|
|
195
|
+
## GPU Contract
|
|
196
|
+
|
|
197
|
+
`snapshot.arena` describes one `ArrayBuffer` holding the core arrays at 256-byte-aligned offsets
|
|
198
|
+
(`hotByteLength` covers `rowPtr`, `colIdx` and `weights`); every core array, every 4-byte view and
|
|
199
|
+
`table.gpuView(name)` are plain-`ArrayBuffer` views accepted by `GPUQueue.writeBuffer` without a
|
|
200
|
+
cast. `flags` (`weighted`, `multigraph`, `hasSelfLoops`, `arcToEdgeIsIdentity`, ...) let kernels
|
|
201
|
+
branch once at dispatch.
|
|
202
|
+
|
|
203
|
+
## Errors
|
|
204
|
+
|
|
205
|
+
Every failure is a `GraphFormatError` with a stable `code` (`E_INVALID_ID`, `E_DUPLICATE_EDGE`,
|
|
206
|
+
`E_COLUMN_TYPE`, `E_INVALID_SNAPSHOT`, `E_BAD_SERIALIZATION`, ...) and a `details` record naming
|
|
207
|
+
the offending index, column or invariant. A throwing call leaves the builder or snapshot unchanged.
|
|
208
|
+
|
|
209
|
+
## Versioning
|
|
210
|
+
|
|
211
|
+
Three version numbers apply: the npm package version (conventional commits), `FORMAT_VERSION` (the
|
|
212
|
+
data-model major, compared by `isGraphSnapshot()` and the wire readers) and the wire `[major, minor]`
|
|
213
|
+
of the byte container. The invariants, `INVALID_INDEX`, the counting vocabulary and the view tables
|
|
214
|
+
are public API: changing any of them is a breaking change even when no TypeScript signature changes.
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-format.d.ts","sourceRoot":"","sources":["../graph-format.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|