@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
@@ -0,0 +1,296 @@
1
+ /**
2
+ * Growable typed staging for the builder (design section 6.2): structure-of-arrays typed arrays that
3
+ * grow by doubling (capacity rounded up to 16 elements, backing buffers a multiple of 64 bytes) into
4
+ * a resizable ArrayBuffer when the engine has one and by allocate-and-copy otherwise. The edge arrays
5
+ * (src, dst, weight), the incidence lists, the per-declaration column data and the alive / weightSet /
6
+ * validity bitmaps of the builder are all instances of the two classes here.
7
+ *
8
+ * Nothing a snapshot holds may alias staging (invariant I18): view() hands out a zero-copy window for
9
+ * the freeze passes to read, and trim() produces the exact-length copy over a fresh fixed-length
10
+ * buffer that a snapshot may keep.
11
+ */
12
+ import { allocateStagingBuffer, growTypedArray, HAS_RESIZABLE_ARRAY_BUFFER, roundUp, STAGING_ELEMENT_GRANULE, stagingByteLength, } from "../util/typed-array.js";
13
+ import { bitmapClearTrailing, bitmapCount, bitmapGet, bitmapSet, bitmapWordCount, bitmapWrite } from "./bitmap.js";
14
+ /** Every bit set: the fill word of a range of whole words. */
15
+ const ALL_ONES = 0xffffffff;
16
+ /**
17
+ * A typed array that grows by doubling (design section 6.2). `length` is the number of elements in
18
+ * use; `capacity` the allocated element count. Elements between length and capacity are unspecified
19
+ * until written. Growth may replace the backing view object, so hot loops that cache `array` re-read
20
+ * it after any call that can grow (push, pushAll, resize, ensureCapacity).
21
+ */
22
+ export class GrowableTypedArray {
23
+ /**
24
+ * Create an empty growable array.
25
+ * @param ctor - the typed-array class of the elements (Uint32Array, Float32Array, ...)
26
+ * @param options - initial capacity and buffer kind
27
+ */
28
+ constructor(ctor, options) {
29
+ this.used = 0;
30
+ this.ctor = ctor;
31
+ this.resizable = options?.resizable ?? HAS_RESIZABLE_ARRAY_BUFFER;
32
+ this.backing = this.allocate(options?.capacity ?? 0);
33
+ }
34
+ /**
35
+ * The number of elements in use.
36
+ * @returns the length
37
+ */
38
+ get length() {
39
+ return this.used;
40
+ }
41
+ /**
42
+ * The allocated element count; growth happens when length would exceed it.
43
+ * @returns the capacity
44
+ */
45
+ get capacity() {
46
+ return this.backing.length;
47
+ }
48
+ /**
49
+ * The backing view over the whole capacity, for hot loops that index it directly. Its identity
50
+ * changes whenever the array grows.
51
+ * @returns the backing typed array
52
+ */
53
+ get array() {
54
+ return this.backing;
55
+ }
56
+ /**
57
+ * Append one element.
58
+ * @param value - the element
59
+ * @returns the index it was written at (the old length)
60
+ */
61
+ push(value) {
62
+ const index = this.used;
63
+ if (index >= this.backing.length) {
64
+ this.backing = growTypedArray(this.backing, this.ctor, index + 1, this.resizable);
65
+ }
66
+ this.backing[index] = value;
67
+ this.used = index + 1;
68
+ return index;
69
+ }
70
+ /**
71
+ * Append many elements at once (bulk addEdges input).
72
+ * @param values - the elements, in order
73
+ * @returns the index the first one was written at (the old length)
74
+ */
75
+ pushAll(values) {
76
+ const index = this.used;
77
+ const end = index + values.length;
78
+ this.ensureCapacity(end);
79
+ this.backing.set(values, index);
80
+ this.used = end;
81
+ return index;
82
+ }
83
+ /**
84
+ * Read element i.
85
+ * @param i - the index, below length
86
+ * @returns the element
87
+ */
88
+ get(i) {
89
+ return this.backing[i];
90
+ }
91
+ /**
92
+ * Write element i.
93
+ * @param i - the index, below length
94
+ * @param value - the element
95
+ */
96
+ set(i, value) {
97
+ this.backing[i] = value;
98
+ }
99
+ /**
100
+ * Make sure at least `n` elements fit without a further reallocation.
101
+ * @param n - the capacity to guarantee
102
+ */
103
+ ensureCapacity(n) {
104
+ if (n > this.backing.length) {
105
+ this.backing = growTypedArray(this.backing, this.ctor, n, this.resizable);
106
+ }
107
+ }
108
+ /**
109
+ * Set the length: growing fills the new elements with `fill` (the incidence lists grow with
110
+ * INVALID_INDEX), shrinking drops the tail without releasing capacity.
111
+ * @param length - the new length
112
+ * @param fill - the value written into elements [old length, length); defaults to 0
113
+ */
114
+ resize(length, fill = 0) {
115
+ if (length > this.used) {
116
+ this.ensureCapacity(length);
117
+ this.backing.fill(fill, this.used, length);
118
+ }
119
+ this.used = length;
120
+ }
121
+ /**
122
+ * A zero-copy window over the elements in use. It aliases staging, so it is for the freeze passes
123
+ * to read from and never for a snapshot to keep (invariant I18); it is invalidated by growth.
124
+ * @returns a view of exactly `length` elements
125
+ */
126
+ view() {
127
+ return new this.ctor(this.backing.buffer, 0, this.used);
128
+ }
129
+ /**
130
+ * An exact-length copy over a fresh fixed-length ArrayBuffer: what a snapshot may keep, and what a
131
+ * compaction writes back through.
132
+ * @returns a new typed array of `length` elements
133
+ */
134
+ trim() {
135
+ const out = new this.ctor(new ArrayBuffer(this.used * this.ctor.BYTES_PER_ELEMENT), 0, this.used);
136
+ out.set(this.view());
137
+ return out;
138
+ }
139
+ /**
140
+ * Drop every element but keep the capacity (a builder reused after a merging freeze).
141
+ */
142
+ clear() {
143
+ this.used = 0;
144
+ }
145
+ /**
146
+ * Drop the elements and the backing storage (freeze({ release: true }) and dispose(), design
147
+ * section 6.1). The array is usable again afterwards; the next push allocates.
148
+ */
149
+ release() {
150
+ this.used = 0;
151
+ this.backing = this.allocate(0);
152
+ }
153
+ /**
154
+ * Allocate a backing view of at least `capacity` elements over a fresh staging buffer.
155
+ * @param capacity - the element count wanted; 0 allocates an empty view over an empty buffer
156
+ * @returns the view
157
+ */
158
+ allocate(capacity) {
159
+ const bytesPerElement = this.ctor.BYTES_PER_ELEMENT;
160
+ const byteLength = capacity === 0 ? 0 : stagingByteLength(roundUp(capacity, STAGING_ELEMENT_GRANULE), bytesPerElement);
161
+ return new this.ctor(allocateStagingBuffer(byteLength, this.resizable), 0, byteLength / bytesPerElement);
162
+ }
163
+ }
164
+ /**
165
+ * A growable packed bitmap (design section 6.2: nodeAlive, edgeAlive, weightSet and the validity of
166
+ * every staging column), in the one LSB-first u32 layout of design section 5.3. `length` is in bits;
167
+ * every bit at or above it is kept clear so view() and trim() are exact bitmaps over `length` bits.
168
+ */
169
+ export class GrowableBitmap {
170
+ /**
171
+ * Create an empty bitmap.
172
+ * @param options - initial capacity in bits and buffer kind
173
+ */
174
+ constructor(options) {
175
+ this.bits = 0;
176
+ this.words = new GrowableTypedArray(Uint32Array, {
177
+ capacity: bitmapWordCount(options?.capacity ?? 0),
178
+ resizable: options?.resizable,
179
+ });
180
+ }
181
+ /**
182
+ * The number of bits in use.
183
+ * @returns the length in bits
184
+ */
185
+ get length() {
186
+ return this.bits;
187
+ }
188
+ /**
189
+ * Append one bit.
190
+ * @param value - the bit
191
+ * @returns the index it was written at (the old length)
192
+ */
193
+ push(value) {
194
+ const index = this.bits;
195
+ if (index % 32 === 0) {
196
+ this.words.push(0);
197
+ }
198
+ if (value) {
199
+ bitmapWrite(this.words.array, index, true);
200
+ }
201
+ this.bits = index + 1;
202
+ return index;
203
+ }
204
+ /**
205
+ * Read bit i.
206
+ * @param i - the bit index, below length
207
+ * @returns true when set
208
+ */
209
+ get(i) {
210
+ return bitmapGet(this.words.array, i);
211
+ }
212
+ /**
213
+ * Write bit i.
214
+ * @param i - the bit index, below length
215
+ * @param value - the bit
216
+ */
217
+ set(i, value) {
218
+ bitmapWrite(this.words.array, i, value);
219
+ }
220
+ /**
221
+ * The number of set bits below length.
222
+ * @returns the count
223
+ */
224
+ count() {
225
+ return bitmapCount(this.words.array, this.bits);
226
+ }
227
+ /**
228
+ * Set the length in bits: growing writes `fill` into the new bits, shrinking clears the dropped
229
+ * bits so the trailing-bits-clear convention holds.
230
+ * @param length - the new length in bits
231
+ * @param fill - the value of bits [old length, length); defaults to false
232
+ */
233
+ resize(length, fill = false) {
234
+ const oldBits = this.bits;
235
+ this.words.resize(bitmapWordCount(length), 0);
236
+ if (length < oldBits) {
237
+ bitmapClearTrailing(this.words.array, length);
238
+ }
239
+ else if (fill && length > oldBits) {
240
+ this.setRange(oldBits, length);
241
+ }
242
+ this.bits = length;
243
+ }
244
+ /**
245
+ * Set every bit in [from, to): the ragged head and tail bit by bit, the whole words in between
246
+ * with one fill.
247
+ * @param from - the first bit to set
248
+ * @param to - one past the last bit to set
249
+ */
250
+ setRange(from, to) {
251
+ const { array } = this.words;
252
+ let i = from;
253
+ while (i < to && i % 32 !== 0) {
254
+ bitmapSet(array, i++);
255
+ }
256
+ const fullEnd = to - (to % 32);
257
+ if (i < fullEnd) {
258
+ array.fill(ALL_ONES, i / 32, fullEnd / 32);
259
+ i = fullEnd;
260
+ }
261
+ while (i < to) {
262
+ bitmapSet(array, i++);
263
+ }
264
+ }
265
+ /**
266
+ * A zero-copy window over the ceil(length / 32) words in use; aliases staging (invariant I18) and
267
+ * is invalidated by growth.
268
+ * @returns the words
269
+ */
270
+ view() {
271
+ return this.words.view();
272
+ }
273
+ /**
274
+ * An exact copy of the words in use over a fresh fixed-length ArrayBuffer: a validity bitmap or a
275
+ * bool column's data a snapshot may keep.
276
+ * @returns a new U32 of ceil(length / 32) words
277
+ */
278
+ trim() {
279
+ return this.words.trim();
280
+ }
281
+ /**
282
+ * Drop every bit but keep the capacity.
283
+ */
284
+ clear() {
285
+ this.bits = 0;
286
+ this.words.clear();
287
+ }
288
+ /**
289
+ * Drop the bits and the backing storage.
290
+ */
291
+ release() {
292
+ this.bits = 0;
293
+ this.words.release();
294
+ }
295
+ }
296
+ //# sourceMappingURL=growable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"growable.js","sourceRoot":"","sources":["../../../src/columns/growable.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EACH,qBAAqB,EACrB,cAAc,EACd,0BAA0B,EAC1B,OAAO,EACP,uBAAuB,EACvB,iBAAiB,GAEpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEnH,8DAA8D;AAC9D,MAAM,QAAQ,GAAG,UAAU,CAAC;AAa5B;;;;;GAKG;AACH,MAAM,OAAO,kBAAkB;IAM3B;;;;OAIG;IACH,YAAY,IAAuB,EAAE,OAAyB;QAPtD,SAAI,GAAG,CAAC,CAAC;QAQb,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,0BAA0B,CAAC;QAClE,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,IAAI,CAAC,CAAC,CAAC;IACzD,CAAC;IAED;;;OAGG;IACH,IAAI,MAAM;QACN,OAAO,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,IAAI,QAAQ;QACR,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACH,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,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AAC,GAAG,EAAE,EAAE,OAAO,GAAG,EAAE,CAAC,CAAC;YAC3C,CAAC,GAAG,OAAO,CAAC;QAChB,CAAC;QACD,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;YACZ,SAAS,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QAC1B,CAAC;IACL,CAAC;IAED;;;;OAIG;IACH,IAAI;QACA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,IAAI;QACA,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,KAAK;QACD,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACH,OAAO;QACH,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC;CACJ"}
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Type inference for untyped sources (design section 5.1, decision C4): CSV cells, GML values, DOT
3
+ * strings, `fromRecords` and `addNodeRecord`. Inference runs per COLUMN, never per cell, widening
4
+ * monotonically in the order `(unset) -> bool -> i32 -> f64 -> string -> json`; it never yields
5
+ * `f32`. The lexical grammar for text is fixed so two implementations agree (invariant I15):
6
+ *
7
+ * - `bool` is exactly `true` / `false` (case-sensitive);
8
+ * - `i32` is `/^-?(0|[1-9][0-9]*)$/` within `[-2^31, 2^31)`;
9
+ * - `f64` is a decimal or exponent literal accepted by `Number()` that is not empty, not whitespace,
10
+ * not `Infinity` / `NaN` and not a hex / octal / binary form (and whose value is finite);
11
+ * - everything else is `string`. `0` / `1` text is `i32`, never `bool`.
12
+ *
13
+ * JS values map by `typeof`: `boolean -> bool`, integral `number` in i32 range -> `i32`, other
14
+ * `number -> f64`, `string -> string`, array or plain object -> `json`; `null` and `undefined` are
15
+ * unset and do not widen.
16
+ */
17
+ /** The dtypes inference can produce, in widening order. */
18
+ export type InferredDtype = "bool" | "i32" | "f64" | "string" | "json";
19
+ /** The widening order of design section 5.1; a column only ever moves to the right. */
20
+ export declare const WIDENING_ORDER: readonly InferredDtype[];
21
+ /**
22
+ * The rank of a dtype in the widening order.
23
+ * @param dtype - an inferred dtype
24
+ * @returns 0 for bool up to 4 for json
25
+ */
26
+ export declare function wideningRank(dtype: InferredDtype): number;
27
+ /**
28
+ * Widen a column's dtype by one observation: the wider of the two, where null (unset, nothing seen
29
+ * yet) is narrower than everything.
30
+ * @param current - the column's dtype so far, or null when no value has been seen
31
+ * @param observed - the dtype of the newly observed value, or null for an unset cell
32
+ * @returns the widened dtype (null only when both are null)
33
+ */
34
+ export declare function widenDtype(current: InferredDtype | null, observed: InferredDtype | null): InferredDtype | null;
35
+ /**
36
+ * Classify one text cell by the fixed lexical grammar. Exported as the reference implementation of
37
+ * the text grammar of design section 5.1, which the text importers of graph-io must reproduce
38
+ * exactly (invariant I15); the core itself only infers from JS values, so nothing in src/ calls it.
39
+ * @public
40
+ * @param text - the cell text, exactly as read (no trimming)
41
+ * @returns bool, i32, f64 or string
42
+ */
43
+ export declare function inferTextDtype(text: string): "bool" | "i32" | "f64" | "string";
44
+ /**
45
+ * Classify one JS value by `typeof`.
46
+ * @param value - the cell value
47
+ * @returns the dtype, or null for an unset cell (`undefined` or `null`); E_COLUMN_TYPE for a bigint,
48
+ * symbol or function, which no dtype can hold
49
+ */
50
+ export declare function inferValueDtype(value: unknown): InferredDtype | null;
51
+ /**
52
+ * Parse a text cell into the storage value of a dtype the column has widened to. A text column
53
+ * never widens to json, but a column that was inferred from JS values may later receive text; in
54
+ * that case the text is stored as a string value. Part of the reference text grammar of design
55
+ * section 5.1 (see inferTextDtype).
56
+ * @public
57
+ * @param text - the cell text
58
+ * @param dtype - the column's current dtype
59
+ * @returns a boolean for bool, a number for i32 / f64, the text itself for string / json
60
+ */
61
+ export declare function parseText(text: string, dtype: InferredDtype): boolean | number | string;
62
+ /**
63
+ * Convert a JS value to the storage value of a (possibly wider) inferred dtype, applying the
64
+ * widening conversions of design section 5.1: a bool widened to a number becomes 1 / 0, a number or
65
+ * boolean widened to string becomes its canonical JS text, anything widened to json is kept as is.
66
+ * @param value - the cell value (never undefined or null; unset cells are handled by the caller)
67
+ * @param dtype - the column's dtype
68
+ * @returns the storage value; E_COLUMN_TYPE when the value cannot be represented in the dtype
69
+ */
70
+ export declare function coerceValue(value: unknown, dtype: InferredDtype): unknown;
71
+ /**
72
+ * Infer the dtype of a whole column of JS values in one pass.
73
+ * @param values - the cell values; undefined and null are unset
74
+ * @returns the widened dtype, or null when every cell is unset
75
+ */
76
+ export declare function inferValuesDtype(values: Iterable<unknown>): InferredDtype | null;
77
+ /**
78
+ * Infer the dtype of a whole column of text cells in one pass. Part of the reference text grammar
79
+ * of design section 5.1 (see inferTextDtype).
80
+ * @public
81
+ * @param cells - the cell texts; an empty string is a string value, not an unset cell (the caller
82
+ * decides how blanks map to unset before calling)
83
+ * @returns the widened dtype, or null when the column is empty
84
+ */
85
+ export declare function inferTextsDtype(cells: Iterable<string>): InferredDtype | null;
86
+ /**
87
+ * A per-column inference accumulator for row-at-a-time sources (`addNodeRecord`, CSV streaming):
88
+ * observe every cell and read `dtype`; each observe call reports whether the column widened so the
89
+ * caller can reallocate storage and record the widening in FreezeReport.widened.
90
+ */
91
+ export declare class DtypeInferrer {
92
+ private current;
93
+ /**
94
+ * The column's dtype so far.
95
+ * @returns the widened dtype, or null until a set cell has been observed
96
+ */
97
+ get dtype(): InferredDtype | null;
98
+ /**
99
+ * Observe a JS value.
100
+ * @param value - the cell value; undefined and null are unset and never widen
101
+ * @returns true when the column's dtype changed
102
+ */
103
+ observeValue(value: unknown): boolean;
104
+ /**
105
+ * Observe a text cell. Part of the reference text grammar of design section 5.1 (see
106
+ * inferTextDtype).
107
+ * @public
108
+ * @param text - the cell text
109
+ * @returns true when the column's dtype changed
110
+ */
111
+ observeText(text: string): boolean;
112
+ /**
113
+ * Force the dtype to at least the given one (a declared lower bound, or the dtype of a column
114
+ * being merged in).
115
+ * @param dtype - the dtype to widen to
116
+ * @returns true when the column's dtype changed
117
+ */
118
+ widenTo(dtype: InferredDtype): boolean;
119
+ private apply;
120
+ }
121
+ //# sourceMappingURL=infer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infer.d.ts","sourceRoot":"","sources":["../../../src/columns/infer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,2DAA2D;AAC3D,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEvE,uFAAuF;AACvF,eAAO,MAAM,cAAc,EAAE,SAAS,aAAa,EAA6C,CAAC;AAQjG;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,CAEzD;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI,EAAE,QAAQ,EAAE,aAAa,GAAG,IAAI,GAAG,aAAa,GAAG,IAAI,CAQ9G;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,QAAQ,CAe9E;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,GAAG,IAAI,CAmBpE;AAED;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CA6BvF;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,GAAG,OAAO,CAmCzE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MA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