@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.
Files changed (224) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +216 -43
  3. package/dist/graph-format.d.ts +1 -0
  4. package/dist/graph-format.d.ts.map +1 -0
  5. package/dist/graph-format.js +15798 -0
  6. package/dist/graph-format.js.map +1 -0
  7. package/dist/src/builder/arena.d.ts +85 -0
  8. package/dist/src/builder/arena.d.ts.map +1 -0
  9. package/dist/src/builder/arena.js +154 -0
  10. package/dist/src/builder/arena.js.map +1 -0
  11. package/dist/src/builder/compact.d.ts +515 -0
  12. package/dist/src/builder/compact.d.ts.map +1 -0
  13. package/dist/src/builder/compact.js +1553 -0
  14. package/dist/src/builder/compact.js.map +1 -0
  15. package/dist/src/builder/counting-sort.d.ts +96 -0
  16. package/dist/src/builder/counting-sort.d.ts.map +1 -0
  17. package/dist/src/builder/counting-sort.js +430 -0
  18. package/dist/src/builder/counting-sort.js.map +1 -0
  19. package/dist/src/builder/freeze.d.ts +72 -0
  20. package/dist/src/builder/freeze.d.ts.map +1 -0
  21. package/dist/src/builder/freeze.js +548 -0
  22. package/dist/src/builder/freeze.js.map +1 -0
  23. package/dist/src/builder/graph-builder.d.ts +602 -0
  24. package/dist/src/builder/graph-builder.d.ts.map +1 -0
  25. package/dist/src/builder/graph-builder.js +1940 -0
  26. package/dist/src/builder/graph-builder.js.map +1 -0
  27. package/dist/src/builder/options.d.ts +16 -0
  28. package/dist/src/builder/options.d.ts.map +1 -0
  29. package/dist/src/builder/options.js +21 -0
  30. package/dist/src/builder/options.js.map +1 -0
  31. package/dist/src/columns/bitmap.d.ts +96 -0
  32. package/dist/src/columns/bitmap.d.ts.map +1 -0
  33. package/dist/src/columns/bitmap.js +184 -0
  34. package/dist/src/columns/bitmap.js.map +1 -0
  35. package/dist/src/columns/column.d.ts +220 -0
  36. package/dist/src/columns/column.d.ts.map +1 -0
  37. package/dist/src/columns/column.js +2109 -0
  38. package/dist/src/columns/column.js.map +1 -0
  39. package/dist/src/columns/dictionary.d.ts +60 -0
  40. package/dist/src/columns/dictionary.d.ts.map +1 -0
  41. package/dist/src/columns/dictionary.js +102 -0
  42. package/dist/src/columns/dictionary.js.map +1 -0
  43. package/dist/src/columns/growable.d.ts +198 -0
  44. package/dist/src/columns/growable.d.ts.map +1 -0
  45. package/dist/src/columns/growable.js +296 -0
  46. package/dist/src/columns/growable.js.map +1 -0
  47. package/dist/src/columns/infer.d.ts +121 -0
  48. package/dist/src/columns/infer.d.ts.map +1 -0
  49. package/dist/src/columns/infer.js +268 -0
  50. package/dist/src/columns/infer.js.map +1 -0
  51. package/dist/src/columns/remap.d.ts +143 -0
  52. package/dist/src/columns/remap.d.ts.map +1 -0
  53. package/dist/src/columns/remap.js +588 -0
  54. package/dist/src/columns/remap.js.map +1 -0
  55. package/dist/src/columns/table.d.ts +184 -0
  56. package/dist/src/columns/table.d.ts.map +1 -0
  57. package/dist/src/columns/table.js +485 -0
  58. package/dist/src/columns/table.js.map +1 -0
  59. package/dist/src/constants.d.ts +34 -0
  60. package/dist/src/constants.d.ts.map +1 -0
  61. package/dist/src/constants.js +79 -0
  62. package/dist/src/constants.js.map +1 -0
  63. package/dist/src/errors.d.ts +76 -0
  64. package/dist/src/errors.d.ts.map +1 -0
  65. package/dist/src/errors.js +32 -0
  66. package/dist/src/errors.js.map +1 -0
  67. package/dist/src/ids/edge-id-index.d.ts +77 -0
  68. package/dist/src/ids/edge-id-index.d.ts.map +1 -0
  69. package/dist/src/ids/edge-id-index.js +155 -0
  70. package/dist/src/ids/edge-id-index.js.map +1 -0
  71. package/dist/src/ids/node-id-map.d.ts +280 -0
  72. package/dist/src/ids/node-id-map.d.ts.map +1 -0
  73. package/dist/src/ids/node-id-map.js +1139 -0
  74. package/dist/src/ids/node-id-map.js.map +1 -0
  75. package/dist/src/ids/string-store.d.ts +219 -0
  76. package/dist/src/ids/string-store.d.ts.map +1 -0
  77. package/dist/src/ids/string-store.js +526 -0
  78. package/dist/src/ids/string-store.js.map +1 -0
  79. package/dist/src/index.d.ts +30 -0
  80. package/dist/src/index.d.ts.map +1 -0
  81. package/dist/src/index.js +29 -0
  82. package/dist/src/index.js.map +1 -0
  83. package/dist/src/populate/from-csr.d.ts +61 -0
  84. package/dist/src/populate/from-csr.d.ts.map +1 -0
  85. package/dist/src/populate/from-csr.js +706 -0
  86. package/dist/src/populate/from-csr.js.map +1 -0
  87. package/dist/src/populate/from-edge-arrays.d.ts +51 -0
  88. package/dist/src/populate/from-edge-arrays.d.ts.map +1 -0
  89. package/dist/src/populate/from-edge-arrays.js +217 -0
  90. package/dist/src/populate/from-edge-arrays.js.map +1 -0
  91. package/dist/src/populate/from-records.d.ts +51 -0
  92. package/dist/src/populate/from-records.d.ts.map +1 -0
  93. package/dist/src/populate/from-records.js +447 -0
  94. package/dist/src/populate/from-records.js.map +1 -0
  95. package/dist/src/snapshot/derived.d.ts +211 -0
  96. package/dist/src/snapshot/derived.d.ts.map +1 -0
  97. package/dist/src/snapshot/derived.js +1254 -0
  98. package/dist/src/snapshot/derived.js.map +1 -0
  99. package/dist/src/snapshot/graph-meta.d.ts +23 -0
  100. package/dist/src/snapshot/graph-meta.d.ts.map +1 -0
  101. package/dist/src/snapshot/graph-meta.js +159 -0
  102. package/dist/src/snapshot/graph-meta.js.map +1 -0
  103. package/dist/src/snapshot/graph-snapshot.d.ts +509 -0
  104. package/dist/src/snapshot/graph-snapshot.d.ts.map +1 -0
  105. package/dist/src/snapshot/graph-snapshot.js +1247 -0
  106. package/dist/src/snapshot/graph-snapshot.js.map +1 -0
  107. package/dist/src/snapshot/hash.d.ts +90 -0
  108. package/dist/src/snapshot/hash.d.ts.map +1 -0
  109. package/dist/src/snapshot/hash.js +228 -0
  110. package/dist/src/snapshot/hash.js.map +1 -0
  111. package/dist/src/snapshot/queries.d.ts +74 -0
  112. package/dist/src/snapshot/queries.d.ts.map +1 -0
  113. package/dist/src/snapshot/queries.js +131 -0
  114. package/dist/src/snapshot/queries.js.map +1 -0
  115. package/dist/src/snapshot/validate.d.ts +192 -0
  116. package/dist/src/snapshot/validate.d.ts.map +1 -0
  117. package/dist/src/snapshot/validate.js +990 -0
  118. package/dist/src/snapshot/validate.js.map +1 -0
  119. package/dist/src/snapshot/views.d.ts +274 -0
  120. package/dist/src/snapshot/views.d.ts.map +1 -0
  121. package/dist/src/snapshot/views.js +856 -0
  122. package/dist/src/snapshot/views.js.map +1 -0
  123. package/dist/src/types/builder.d.ts +456 -0
  124. package/dist/src/types/builder.d.ts.map +1 -0
  125. package/dist/src/types/builder.js +11 -0
  126. package/dist/src/types/builder.js.map +1 -0
  127. package/dist/src/types/columns.d.ts +615 -0
  128. package/dist/src/types/columns.d.ts.map +1 -0
  129. package/dist/src/types/columns.js +14 -0
  130. package/dist/src/types/columns.js.map +1 -0
  131. package/dist/src/types/index.d.ts +14 -0
  132. package/dist/src/types/index.d.ts.map +1 -0
  133. package/dist/src/types/index.js +11 -0
  134. package/dist/src/types/index.js.map +1 -0
  135. package/dist/src/types/internal.d.ts +172 -0
  136. package/dist/src/types/internal.d.ts.map +1 -0
  137. package/dist/src/types/internal.js +13 -0
  138. package/dist/src/types/internal.js.map +1 -0
  139. package/dist/src/types/snapshot.d.ts +801 -0
  140. package/dist/src/types/snapshot.d.ts.map +1 -0
  141. package/dist/src/types/snapshot.js +14 -0
  142. package/dist/src/types/snapshot.js.map +1 -0
  143. package/dist/src/types/wire.d.ts +196 -0
  144. package/dist/src/types/wire.d.ts.map +1 -0
  145. package/dist/src/types/wire.js +8 -0
  146. package/dist/src/types/wire.js.map +1 -0
  147. package/dist/src/util/mask.d.ts +55 -0
  148. package/dist/src/util/mask.d.ts.map +1 -0
  149. package/dist/src/util/mask.js +71 -0
  150. package/dist/src/util/mask.js.map +1 -0
  151. package/dist/src/util/options.d.ts +22 -0
  152. package/dist/src/util/options.d.ts.map +1 -0
  153. package/dist/src/util/options.js +49 -0
  154. package/dist/src/util/options.js.map +1 -0
  155. package/dist/src/util/shared-buffers.d.ts +42 -0
  156. package/dist/src/util/shared-buffers.d.ts.map +1 -0
  157. package/dist/src/util/shared-buffers.js +52 -0
  158. package/dist/src/util/shared-buffers.js.map +1 -0
  159. package/dist/src/util/typed-array.d.ts +205 -0
  160. package/dist/src/util/typed-array.d.ts.map +1 -0
  161. package/dist/src/util/typed-array.js +289 -0
  162. package/dist/src/util/typed-array.js.map +1 -0
  163. package/dist/src/wire/bytes.d.ts +69 -0
  164. package/dist/src/wire/bytes.d.ts.map +1 -0
  165. package/dist/src/wire/bytes.js +361 -0
  166. package/dist/src/wire/bytes.js.map +1 -0
  167. package/dist/src/wire/carried-views.d.ts +111 -0
  168. package/dist/src/wire/carried-views.d.ts.map +1 -0
  169. package/dist/src/wire/carried-views.js +286 -0
  170. package/dist/src/wire/carried-views.js.map +1 -0
  171. package/dist/src/wire/from-wire.d.ts +144 -0
  172. package/dist/src/wire/from-wire.d.ts.map +1 -0
  173. package/dist/src/wire/from-wire.js +1425 -0
  174. package/dist/src/wire/from-wire.js.map +1 -0
  175. package/dist/src/wire/to-wire.d.ts +171 -0
  176. package/dist/src/wire/to-wire.d.ts.map +1 -0
  177. package/dist/src/wire/to-wire.js +652 -0
  178. package/dist/src/wire/to-wire.js.map +1 -0
  179. package/dist/tsconfig.build.tsbuildinfo +1 -0
  180. package/package.json +72 -7
  181. package/src/builder/arena.ts +192 -0
  182. package/src/builder/compact.ts +1811 -0
  183. package/src/builder/counting-sort.ts +506 -0
  184. package/src/builder/freeze.ts +689 -0
  185. package/src/builder/graph-builder.ts +2264 -0
  186. package/src/builder/options.ts +29 -0
  187. package/src/columns/bitmap.ts +196 -0
  188. package/src/columns/column.ts +2574 -0
  189. package/src/columns/dictionary.ts +114 -0
  190. package/src/columns/growable.ts +350 -0
  191. package/src/columns/infer.ts +285 -0
  192. package/src/columns/remap.ts +663 -0
  193. package/src/columns/table.ts +568 -0
  194. package/src/constants.ts +89 -0
  195. package/src/errors.ts +115 -0
  196. package/src/ids/edge-id-index.ts +179 -0
  197. package/src/ids/node-id-map.ts +1340 -0
  198. package/src/ids/string-store.ts +589 -0
  199. package/src/index.ts +139 -0
  200. package/src/lib-resizable-array-buffer.d.ts +34 -0
  201. package/src/populate/from-csr.ts +802 -0
  202. package/src/populate/from-edge-arrays.ts +257 -0
  203. package/src/populate/from-records.ts +536 -0
  204. package/src/snapshot/derived.ts +1486 -0
  205. package/src/snapshot/graph-meta.ts +167 -0
  206. package/src/snapshot/graph-snapshot.ts +1503 -0
  207. package/src/snapshot/hash.ts +241 -0
  208. package/src/snapshot/queries.ts +136 -0
  209. package/src/snapshot/validate.ts +1108 -0
  210. package/src/snapshot/views.ts +943 -0
  211. package/src/types/builder.ts +480 -0
  212. package/src/types/columns.ts +727 -0
  213. package/src/types/index.ts +14 -0
  214. package/src/types/internal.ts +195 -0
  215. package/src/types/snapshot.ts +886 -0
  216. package/src/types/wire.ts +207 -0
  217. package/src/util/mask.ts +89 -0
  218. package/src/util/options.ts +57 -0
  219. package/src/util/shared-buffers.ts +56 -0
  220. package/src/util/typed-array.ts +371 -0
  221. package/src/wire/bytes.ts +440 -0
  222. package/src/wire/carried-views.ts +327 -0
  223. package/src/wire/from-wire.ts +1690 -0
  224. 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
- ## ⚠️ IMPORTANT NOTICE ⚠️
4
-
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
6
-
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
8
-
9
- ## Purpose
10
-
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `@graphty/graph-format`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
15
-
16
- ## What is OIDC Trusted Publishing?
17
-
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
19
-
20
- ## Setup Instructions
21
-
22
- To properly configure OIDC trusted publishing for this package:
23
-
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
28
-
29
- ## DO NOT USE THIS PACKAGE
30
-
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
36
-
37
- ## More Information
38
-
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
42
-
43
- ---
44
-
45
- **Maintained for OIDC setup purposes only**
3
+ [![CI](https://github.com/graphty-org/graphty-monorepo/actions/workflows/ci.yml/badge.svg)](https://github.com/graphty-org/graphty-monorepo/actions/workflows/ci.yml)
4
+ [![Coverage Status](https://coveralls.io/repos/github/graphty-org/graphty-monorepo/badge.svg?branch=master)](https://coveralls.io/github/graphty-org/graphty-monorepo?branch=master)
5
+ [![npm version](https://img.shields.io/npm/v/@graphty/graph-format.svg)](https://www.npmjs.com/package/@graphty/graph-format)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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"}