@graphty/graph-io 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 (339) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +250 -28
  3. package/dist/chunks/children-CL3Cy0ez.js +238 -0
  4. package/dist/chunks/children-CL3Cy0ez.js.map +1 -0
  5. package/dist/chunks/escape-DyI8JofU.js +938 -0
  6. package/dist/chunks/escape-DyI8JofU.js.map +1 -0
  7. package/dist/chunks/importer-CQnJuWJw.js +2987 -0
  8. package/dist/chunks/importer-CQnJuWJw.js.map +1 -0
  9. package/dist/chunks/importer-CpCpfbxr.js +2015 -0
  10. package/dist/chunks/importer-CpCpfbxr.js.map +1 -0
  11. package/dist/chunks/importer-DbnGYr3_.js +2342 -0
  12. package/dist/chunks/importer-DbnGYr3_.js.map +1 -0
  13. package/dist/chunks/importer-GozH8DkN.js +3050 -0
  14. package/dist/chunks/importer-GozH8DkN.js.map +1 -0
  15. package/dist/chunks/records-CGpxszm1.js +605 -0
  16. package/dist/chunks/records-CGpxszm1.js.map +1 -0
  17. package/dist/chunks/text-CajMdVFy.js +189 -0
  18. package/dist/chunks/text-CajMdVFy.js.map +1 -0
  19. package/dist/chunks/writer-DxSKC7TL.js +2842 -0
  20. package/dist/chunks/writer-DxSKC7TL.js.map +1 -0
  21. package/dist/csv.d.ts +1 -0
  22. package/dist/csv.js +1702 -0
  23. package/dist/csv.js.map +1 -0
  24. package/dist/dot.d.ts +1 -0
  25. package/dist/dot.js +8 -0
  26. package/dist/dot.js.map +1 -0
  27. package/dist/gexf.d.ts +1 -0
  28. package/dist/gexf.js +3466 -0
  29. package/dist/gexf.js.map +1 -0
  30. package/dist/gml.d.ts +1 -0
  31. package/dist/gml.js +2647 -0
  32. package/dist/gml.js.map +1 -0
  33. package/dist/graph-io.d.ts +1 -0
  34. package/dist/graph-io.js +790 -0
  35. package/dist/graph-io.js.map +1 -0
  36. package/dist/graphml.d.ts +1 -0
  37. package/dist/graphml.js +8 -0
  38. package/dist/graphml.js.map +1 -0
  39. package/dist/json.d.ts +1 -0
  40. package/dist/json.js +11 -0
  41. package/dist/json.js.map +1 -0
  42. package/dist/neo4j.d.ts +1 -0
  43. package/dist/neo4j.js +2046 -0
  44. package/dist/neo4j.js.map +1 -0
  45. package/dist/pajek.d.ts +1 -0
  46. package/dist/pajek.js +8 -0
  47. package/dist/pajek.js.map +1 -0
  48. package/dist/src/children.d.ts +134 -0
  49. package/dist/src/children.d.ts.map +1 -0
  50. package/dist/src/children.js +274 -0
  51. package/dist/src/children.js.map +1 -0
  52. package/dist/src/common/attributes.d.ts +229 -0
  53. package/dist/src/common/attributes.d.ts.map +1 -0
  54. package/dist/src/common/attributes.js +368 -0
  55. package/dist/src/common/attributes.js.map +1 -0
  56. package/dist/src/common/codes.d.ts +105 -0
  57. package/dist/src/common/codes.d.ts.map +1 -0
  58. package/dist/src/common/codes.js +107 -0
  59. package/dist/src/common/codes.js.map +1 -0
  60. package/dist/src/common/declared-types.d.ts +84 -0
  61. package/dist/src/common/declared-types.d.ts.map +1 -0
  62. package/dist/src/common/declared-types.js +326 -0
  63. package/dist/src/common/declared-types.js.map +1 -0
  64. package/dist/src/common/direction.d.ts +206 -0
  65. package/dist/src/common/direction.d.ts.map +1 -0
  66. package/dist/src/common/direction.js +370 -0
  67. package/dist/src/common/direction.js.map +1 -0
  68. package/dist/src/common/escape.d.ts +92 -0
  69. package/dist/src/common/escape.d.ts.map +1 -0
  70. package/dist/src/common/escape.js +212 -0
  71. package/dist/src/common/escape.js.map +1 -0
  72. package/dist/src/common/export.d.ts +249 -0
  73. package/dist/src/common/export.d.ts.map +1 -0
  74. package/dist/src/common/export.js +594 -0
  75. package/dist/src/common/export.js.map +1 -0
  76. package/dist/src/common/format.d.ts +59 -0
  77. package/dist/src/common/format.d.ts.map +1 -0
  78. package/dist/src/common/format.js +106 -0
  79. package/dist/src/common/format.js.map +1 -0
  80. package/dist/src/common/ids.d.ts +83 -0
  81. package/dist/src/common/ids.d.ts.map +1 -0
  82. package/dist/src/common/ids.js +158 -0
  83. package/dist/src/common/ids.js.map +1 -0
  84. package/dist/src/common/input.d.ts +100 -0
  85. package/dist/src/common/input.d.ts.map +1 -0
  86. package/dist/src/common/input.js +335 -0
  87. package/dist/src/common/input.js.map +1 -0
  88. package/dist/src/common/lists.d.ts +34 -0
  89. package/dist/src/common/lists.d.ts.map +1 -0
  90. package/dist/src/common/lists.js +185 -0
  91. package/dist/src/common/lists.js.map +1 -0
  92. package/dist/src/common/options.d.ts +108 -0
  93. package/dist/src/common/options.d.ts.map +1 -0
  94. package/dist/src/common/options.js +265 -0
  95. package/dist/src/common/options.js.map +1 -0
  96. package/dist/src/common/report.d.ts +187 -0
  97. package/dist/src/common/report.d.ts.map +1 -0
  98. package/dist/src/common/report.js +274 -0
  99. package/dist/src/common/report.js.map +1 -0
  100. package/dist/src/common/temporal.d.ts +71 -0
  101. package/dist/src/common/temporal.d.ts.map +1 -0
  102. package/dist/src/common/temporal.js +266 -0
  103. package/dist/src/common/temporal.js.map +1 -0
  104. package/dist/src/common/text.d.ts +104 -0
  105. package/dist/src/common/text.d.ts.map +1 -0
  106. package/dist/src/common/text.js +255 -0
  107. package/dist/src/common/text.js.map +1 -0
  108. package/dist/src/common/weights.d.ts +77 -0
  109. package/dist/src/common/weights.d.ts.map +1 -0
  110. package/dist/src/common/weights.js +156 -0
  111. package/dist/src/common/weights.js.map +1 -0
  112. package/dist/src/common/writer.d.ts +51 -0
  113. package/dist/src/common/writer.d.ts.map +1 -0
  114. package/dist/src/common/writer.js +108 -0
  115. package/dist/src/common/writer.js.map +1 -0
  116. package/dist/src/common/xml.d.ts +245 -0
  117. package/dist/src/common/xml.d.ts.map +1 -0
  118. package/dist/src/common/xml.js +942 -0
  119. package/dist/src/common/xml.js.map +1 -0
  120. package/dist/src/formats/csv/exporter.d.ts +70 -0
  121. package/dist/src/formats/csv/exporter.d.ts.map +1 -0
  122. package/dist/src/formats/csv/exporter.js +682 -0
  123. package/dist/src/formats/csv/exporter.js.map +1 -0
  124. package/dist/src/formats/csv/header.d.ts +66 -0
  125. package/dist/src/formats/csv/header.d.ts.map +1 -0
  126. package/dist/src/formats/csv/header.js +152 -0
  127. package/dist/src/formats/csv/header.js.map +1 -0
  128. package/dist/src/formats/csv/importer.d.ts +82 -0
  129. package/dist/src/formats/csv/importer.d.ts.map +1 -0
  130. package/dist/src/formats/csv/importer.js +849 -0
  131. package/dist/src/formats/csv/importer.js.map +1 -0
  132. package/dist/src/formats/csv/index.d.ts +60 -0
  133. package/dist/src/formats/csv/index.d.ts.map +1 -0
  134. package/dist/src/formats/csv/index.js +63 -0
  135. package/dist/src/formats/csv/index.js.map +1 -0
  136. package/dist/src/formats/csv/records.d.ts +188 -0
  137. package/dist/src/formats/csv/records.d.ts.map +1 -0
  138. package/dist/src/formats/csv/records.js +702 -0
  139. package/dist/src/formats/csv/records.js.map +1 -0
  140. package/dist/src/formats/csv/values.d.ts +105 -0
  141. package/dist/src/formats/csv/values.d.ts.map +1 -0
  142. package/dist/src/formats/csv/values.js +192 -0
  143. package/dist/src/formats/csv/values.js.map +1 -0
  144. package/dist/src/formats/dot/exporter.d.ts +52 -0
  145. package/dist/src/formats/dot/exporter.d.ts.map +1 -0
  146. package/dist/src/formats/dot/exporter.js +836 -0
  147. package/dist/src/formats/dot/exporter.js.map +1 -0
  148. package/dist/src/formats/dot/importer.d.ts +102 -0
  149. package/dist/src/formats/dot/importer.d.ts.map +1 -0
  150. package/dist/src/formats/dot/importer.js +1291 -0
  151. package/dist/src/formats/dot/importer.js.map +1 -0
  152. package/dist/src/formats/dot/index.d.ts +7 -0
  153. package/dist/src/formats/dot/index.d.ts.map +1 -0
  154. package/dist/src/formats/dot/index.js +7 -0
  155. package/dist/src/formats/dot/index.js.map +1 -0
  156. package/dist/src/formats/dot/names.d.ts +29 -0
  157. package/dist/src/formats/dot/names.d.ts.map +1 -0
  158. package/dist/src/formats/dot/names.js +28 -0
  159. package/dist/src/formats/dot/names.js.map +1 -0
  160. package/dist/src/formats/dot/tokenizer.d.ts +114 -0
  161. package/dist/src/formats/dot/tokenizer.d.ts.map +1 -0
  162. package/dist/src/formats/dot/tokenizer.js +341 -0
  163. package/dist/src/formats/dot/tokenizer.js.map +1 -0
  164. package/dist/src/formats/gexf/exporter.d.ts +56 -0
  165. package/dist/src/formats/gexf/exporter.d.ts.map +1 -0
  166. package/dist/src/formats/gexf/exporter.js +1395 -0
  167. package/dist/src/formats/gexf/exporter.js.map +1 -0
  168. package/dist/src/formats/gexf/importer.d.ts +73 -0
  169. package/dist/src/formats/gexf/importer.d.ts.map +1 -0
  170. package/dist/src/formats/gexf/importer.js +1880 -0
  171. package/dist/src/formats/gexf/importer.js.map +1 -0
  172. package/dist/src/formats/gexf/index.d.ts +96 -0
  173. package/dist/src/formats/gexf/index.d.ts.map +1 -0
  174. package/dist/src/formats/gexf/index.js +97 -0
  175. package/dist/src/formats/gexf/index.js.map +1 -0
  176. package/dist/src/formats/gexf/schema.d.ts +135 -0
  177. package/dist/src/formats/gexf/schema.d.ts.map +1 -0
  178. package/dist/src/formats/gexf/schema.js +323 -0
  179. package/dist/src/formats/gexf/schema.js.map +1 -0
  180. package/dist/src/formats/gml/exporter.d.ts +69 -0
  181. package/dist/src/formats/gml/exporter.d.ts.map +1 -0
  182. package/dist/src/formats/gml/exporter.js +1093 -0
  183. package/dist/src/formats/gml/exporter.js.map +1 -0
  184. package/dist/src/formats/gml/importer.d.ts +66 -0
  185. package/dist/src/formats/gml/importer.d.ts.map +1 -0
  186. package/dist/src/formats/gml/importer.js +1331 -0
  187. package/dist/src/formats/gml/importer.js.map +1 -0
  188. package/dist/src/formats/gml/index.d.ts +85 -0
  189. package/dist/src/formats/gml/index.d.ts.map +1 -0
  190. package/dist/src/formats/gml/index.js +88 -0
  191. package/dist/src/formats/gml/index.js.map +1 -0
  192. package/dist/src/formats/gml/syntax.d.ts +186 -0
  193. package/dist/src/formats/gml/syntax.d.ts.map +1 -0
  194. package/dist/src/formats/gml/syntax.js +467 -0
  195. package/dist/src/formats/gml/syntax.js.map +1 -0
  196. package/dist/src/formats/graphml/constants.d.ts +169 -0
  197. package/dist/src/formats/graphml/constants.d.ts.map +1 -0
  198. package/dist/src/formats/graphml/constants.js +165 -0
  199. package/dist/src/formats/graphml/constants.js.map +1 -0
  200. package/dist/src/formats/graphml/exporter.d.ts +34 -0
  201. package/dist/src/formats/graphml/exporter.d.ts.map +1 -0
  202. package/dist/src/formats/graphml/exporter.js +1176 -0
  203. package/dist/src/formats/graphml/exporter.js.map +1 -0
  204. package/dist/src/formats/graphml/importer.d.ts +31 -0
  205. package/dist/src/formats/graphml/importer.d.ts.map +1 -0
  206. package/dist/src/formats/graphml/importer.js +1607 -0
  207. package/dist/src/formats/graphml/importer.js.map +1 -0
  208. package/dist/src/formats/graphml/index.d.ts +8 -0
  209. package/dist/src/formats/graphml/index.d.ts.map +1 -0
  210. package/dist/src/formats/graphml/index.js +8 -0
  211. package/dist/src/formats/graphml/index.js.map +1 -0
  212. package/dist/src/formats/graphml/tree.d.ts +72 -0
  213. package/dist/src/formats/graphml/tree.d.ts.map +1 -0
  214. package/dist/src/formats/graphml/tree.js +290 -0
  215. package/dist/src/formats/graphml/tree.js.map +1 -0
  216. package/dist/src/formats/json/dialect.d.ts +125 -0
  217. package/dist/src/formats/json/dialect.d.ts.map +1 -0
  218. package/dist/src/formats/json/dialect.js +262 -0
  219. package/dist/src/formats/json/dialect.js.map +1 -0
  220. package/dist/src/formats/json/exporter.d.ts +89 -0
  221. package/dist/src/formats/json/exporter.d.ts.map +1 -0
  222. package/dist/src/formats/json/exporter.js +1358 -0
  223. package/dist/src/formats/json/exporter.js.map +1 -0
  224. package/dist/src/formats/json/importer.d.ts +108 -0
  225. package/dist/src/formats/json/importer.d.ts.map +1 -0
  226. package/dist/src/formats/json/importer.js +1838 -0
  227. package/dist/src/formats/json/importer.js.map +1 -0
  228. package/dist/src/formats/json/index.d.ts +8 -0
  229. package/dist/src/formats/json/index.d.ts.map +1 -0
  230. package/dist/src/formats/json/index.js +8 -0
  231. package/dist/src/formats/json/index.js.map +1 -0
  232. package/dist/src/formats/neo4j/exporter.d.ts +68 -0
  233. package/dist/src/formats/neo4j/exporter.d.ts.map +1 -0
  234. package/dist/src/formats/neo4j/exporter.js +1055 -0
  235. package/dist/src/formats/neo4j/exporter.js.map +1 -0
  236. package/dist/src/formats/neo4j/header.d.ts +52 -0
  237. package/dist/src/formats/neo4j/header.d.ts.map +1 -0
  238. package/dist/src/formats/neo4j/header.js +131 -0
  239. package/dist/src/formats/neo4j/header.js.map +1 -0
  240. package/dist/src/formats/neo4j/importer.d.ts +73 -0
  241. package/dist/src/formats/neo4j/importer.d.ts.map +1 -0
  242. package/dist/src/formats/neo4j/importer.js +932 -0
  243. package/dist/src/formats/neo4j/importer.js.map +1 -0
  244. package/dist/src/formats/neo4j/index.d.ts +79 -0
  245. package/dist/src/formats/neo4j/index.d.ts.map +1 -0
  246. package/dist/src/formats/neo4j/index.js +83 -0
  247. package/dist/src/formats/neo4j/index.js.map +1 -0
  248. package/dist/src/formats/pajek/exporter.d.ts +58 -0
  249. package/dist/src/formats/pajek/exporter.d.ts.map +1 -0
  250. package/dist/src/formats/pajek/exporter.js +825 -0
  251. package/dist/src/formats/pajek/exporter.js.map +1 -0
  252. package/dist/src/formats/pajek/importer.d.ts +88 -0
  253. package/dist/src/formats/pajek/importer.d.ts.map +1 -0
  254. package/dist/src/formats/pajek/importer.js +1047 -0
  255. package/dist/src/formats/pajek/importer.js.map +1 -0
  256. package/dist/src/formats/pajek/index.d.ts +7 -0
  257. package/dist/src/formats/pajek/index.d.ts.map +1 -0
  258. package/dist/src/formats/pajek/index.js +7 -0
  259. package/dist/src/formats/pajek/index.js.map +1 -0
  260. package/dist/src/formats/pajek/syntax.d.ts +112 -0
  261. package/dist/src/formats/pajek/syntax.d.ts.map +1 -0
  262. package/dist/src/formats/pajek/syntax.js +269 -0
  263. package/dist/src/formats/pajek/syntax.js.map +1 -0
  264. package/dist/src/index.d.ts +35 -0
  265. package/dist/src/index.d.ts.map +1 -0
  266. package/dist/src/index.js +39 -0
  267. package/dist/src/index.js.map +1 -0
  268. package/dist/src/registry.d.ts +207 -0
  269. package/dist/src/registry.d.ts.map +1 -0
  270. package/dist/src/registry.js +481 -0
  271. package/dist/src/registry.js.map +1 -0
  272. package/dist/src/sniff.d.ts +104 -0
  273. package/dist/src/sniff.d.ts.map +1 -0
  274. package/dist/src/sniff.js +357 -0
  275. package/dist/src/sniff.js.map +1 -0
  276. package/dist/src/types.d.ts +238 -0
  277. package/dist/src/types.d.ts.map +1 -0
  278. package/dist/src/types.js +29 -0
  279. package/dist/src/types.js.map +1 -0
  280. package/dist/tsconfig.build.tsbuildinfo +1 -0
  281. package/package.json +122 -7
  282. package/src/children.ts +335 -0
  283. package/src/common/attributes.ts +520 -0
  284. package/src/common/codes.ts +153 -0
  285. package/src/common/declared-types.ts +374 -0
  286. package/src/common/direction.ts +518 -0
  287. package/src/common/escape.ts +231 -0
  288. package/src/common/export.ts +817 -0
  289. package/src/common/format.ts +111 -0
  290. package/src/common/ids.ts +176 -0
  291. package/src/common/input.ts +378 -0
  292. package/src/common/lists.ts +196 -0
  293. package/src/common/options.ts +377 -0
  294. package/src/common/report.ts +352 -0
  295. package/src/common/temporal.ts +302 -0
  296. package/src/common/text.ts +294 -0
  297. package/src/common/weights.ts +202 -0
  298. package/src/common/writer.ts +123 -0
  299. package/src/common/xml.ts +1053 -0
  300. package/src/formats/csv/exporter.ts +894 -0
  301. package/src/formats/csv/header.ts +172 -0
  302. package/src/formats/csv/importer.ts +1104 -0
  303. package/src/formats/csv/index.ts +88 -0
  304. package/src/formats/csv/records.ts +813 -0
  305. package/src/formats/csv/values.ts +224 -0
  306. package/src/formats/dot/exporter.ts +1014 -0
  307. package/src/formats/dot/importer.ts +1549 -0
  308. package/src/formats/dot/index.ts +7 -0
  309. package/src/formats/dot/names.ts +40 -0
  310. package/src/formats/dot/tokenizer.ts +384 -0
  311. package/src/formats/gexf/exporter.ts +1696 -0
  312. package/src/formats/gexf/importer.ts +2333 -0
  313. package/src/formats/gexf/index.ts +142 -0
  314. package/src/formats/gexf/schema.ts +361 -0
  315. package/src/formats/gml/exporter.ts +1404 -0
  316. package/src/formats/gml/importer.ts +1591 -0
  317. package/src/formats/gml/index.ts +128 -0
  318. package/src/formats/gml/syntax.ts +545 -0
  319. package/src/formats/graphml/constants.ts +225 -0
  320. package/src/formats/graphml/exporter.ts +1458 -0
  321. package/src/formats/graphml/importer.ts +2027 -0
  322. package/src/formats/graphml/index.ts +8 -0
  323. package/src/formats/graphml/tree.ts +318 -0
  324. package/src/formats/json/dialect.ts +317 -0
  325. package/src/formats/json/exporter.ts +1616 -0
  326. package/src/formats/json/importer.ts +2271 -0
  327. package/src/formats/json/index.ts +8 -0
  328. package/src/formats/neo4j/exporter.ts +1287 -0
  329. package/src/formats/neo4j/header.ts +156 -0
  330. package/src/formats/neo4j/importer.ts +1220 -0
  331. package/src/formats/neo4j/index.ts +116 -0
  332. package/src/formats/pajek/exporter.ts +1000 -0
  333. package/src/formats/pajek/importer.ts +1311 -0
  334. package/src/formats/pajek/index.ts +7 -0
  335. package/src/formats/pajek/syntax.ts +307 -0
  336. package/src/index.ts +244 -0
  337. package/src/registry.ts +617 -0
  338. package/src/sniff.ts +397 -0
  339. package/src/types.ts +262 -0
@@ -0,0 +1,274 @@
1
+ /**
2
+ * The children CSR over a containment column (design sections 5.10, 7.1 and 8.2): containment
3
+ * (GEXF `pid` / `<parents>`, Cytoscape `parent`, DOT clusters, GraphML nested graphs) is a node
4
+ * column with the `parent` role (u32, refersTo node) or the `parents` role (list of u32) and never
5
+ * enters the CSR of the snapshot. Importers and exporters that need to walk containers top-down
6
+ * (nested subgraphs, cluster blocks, parent lists) build this inverse index once: for every node
7
+ * `u`, `children[rowPtr[u] .. rowPtr[u + 1])` are its children in ascending index order, `roots`
8
+ * are the nodes with no parent, and `depthFirst()` is the pre-order the nested writers use.
9
+ *
10
+ * The helper is deliberately outside the core (7.1: it computes a property the arrays do not
11
+ * already imply and only io needs it). A reference is ignored when it is unset, INVALID_INDEX, out
12
+ * of range, or the node itself; a cycle (a parent chain that never reaches a root) is reported
13
+ * through `unreachable` and its members are appended to the depth-first order in index order so
14
+ * every node is written exactly once.
15
+ */
16
+ import { INVALID_INDEX } from "@graphty/graph-format";
17
+ /**
18
+ * The children CSR of a snapshot's containment column (design sections 5.10 and 7.1): the inverse
19
+ * of the parent relation as a compressed sparse row structure over node indices.
20
+ */
21
+ export class ChildrenCsr {
22
+ /**
23
+ * Build the CSR; use childrenCsr() rather than the constructor.
24
+ * @param nodeCount - the node count
25
+ * @param rowPtr - the row pointers
26
+ * @param children - the child list
27
+ * @param parentCount - parents per node (0 for a root)
28
+ * @param dropped - references ignored
29
+ * @param column - the source column, or null
30
+ */
31
+ constructor(nodeCount, rowPtr, children, parentCount, dropped, column) {
32
+ this.nodeCount = nodeCount;
33
+ this.rowPtr = rowPtr;
34
+ this.children = children;
35
+ this.parentCount = parentCount;
36
+ this.edgeCount = children.length;
37
+ this.dropped = dropped;
38
+ this.column = column;
39
+ let rootCount = 0;
40
+ for (let u = 0; u < nodeCount; u++) {
41
+ if (parentCount[u] === 0) {
42
+ rootCount++;
43
+ }
44
+ }
45
+ const roots = new Uint32Array(rootCount);
46
+ let r = 0;
47
+ for (let u = 0; u < nodeCount; u++) {
48
+ if (parentCount[u] === 0) {
49
+ roots[r++] = u;
50
+ }
51
+ }
52
+ this.roots = roots;
53
+ this.unreachable = nodeCount - this.reachableCount();
54
+ }
55
+ /**
56
+ * The children of a node, as a view into `children` (do not modify).
57
+ * @param node - the node index
58
+ * @returns the child indices in ascending order; empty for a leaf
59
+ */
60
+ childrenOf(node) {
61
+ return this.children.subarray(this.rowPtr[node], this.rowPtr[node + 1]);
62
+ }
63
+ /**
64
+ * How many children a node has.
65
+ * @param node - the node index
66
+ * @returns the count
67
+ */
68
+ childCount(node) {
69
+ return this.rowPtr[node + 1] - this.rowPtr[node];
70
+ }
71
+ /**
72
+ * Whether a node is a container (has at least one child).
73
+ * @param node - the node index
74
+ * @returns true when it has children
75
+ */
76
+ hasChildren(node) {
77
+ return this.rowPtr[node + 1] > this.rowPtr[node];
78
+ }
79
+ /**
80
+ * How many parents a node has: 0 for a root, 1 under a `parent` column, any number under `parents`.
81
+ * @param node - the node index
82
+ * @returns the parent count
83
+ */
84
+ parentCountOf(node) {
85
+ return this.parentCount[node];
86
+ }
87
+ /**
88
+ * Whether a node has no parent.
89
+ * @param node - the node index
90
+ * @returns true for a root
91
+ */
92
+ isRoot(node) {
93
+ return this.parentCount[node] === 0;
94
+ }
95
+ /**
96
+ * Whether the snapshot has any containment at all.
97
+ * @returns true when at least one child reference was kept
98
+ */
99
+ isEmpty() {
100
+ return this.edgeCount === 0;
101
+ }
102
+ /**
103
+ * A pre-order depth-first traversal from the roots in index order (children in ascending
104
+ * order), then every node not yet visited (a cycle member, or a multi-parent node reached
105
+ * only through a cycle) in index order at depth 0. Each node appears exactly once, so nested
106
+ * writers emit every element once even for a `parents` column that shares a child.
107
+ * @returns the order, the depth of every node and whether the order differs from index order
108
+ */
109
+ depthFirst() {
110
+ const n = this.nodeCount;
111
+ const order = new Uint32Array(n);
112
+ const depth = new Uint32Array(n);
113
+ const visited = new Uint8Array(n);
114
+ const stackNode = [];
115
+ const stackCursor = [];
116
+ let position = 0;
117
+ let reordered = false;
118
+ const place = (u, d) => {
119
+ visited[u] = 1;
120
+ if (u !== position) {
121
+ reordered = true;
122
+ }
123
+ order[position++] = u;
124
+ depth[u] = d;
125
+ };
126
+ const visit = (root) => {
127
+ place(root, 0);
128
+ stackNode.push(root);
129
+ stackCursor.push(this.rowPtr[root]);
130
+ while (stackNode.length > 0) {
131
+ const top = stackNode.length - 1;
132
+ const u = stackNode[top];
133
+ const cursor = stackCursor[top];
134
+ if (cursor >= this.rowPtr[u + 1]) {
135
+ stackNode.pop();
136
+ stackCursor.pop();
137
+ continue;
138
+ }
139
+ stackCursor[top] = cursor + 1;
140
+ const v = this.children[cursor];
141
+ if (visited[v] === 1) {
142
+ continue;
143
+ }
144
+ place(v, stackNode.length);
145
+ stackNode.push(v);
146
+ stackCursor.push(this.rowPtr[v]);
147
+ }
148
+ };
149
+ for (const root of this.roots) {
150
+ visit(root);
151
+ }
152
+ for (let u = 0; u < n; u++) {
153
+ if (visited[u] === 0) {
154
+ visit(u);
155
+ }
156
+ }
157
+ return { order, depth, reordered };
158
+ }
159
+ /**
160
+ * The nodes reachable from the roots by following child references.
161
+ * @returns how many nodes a traversal from the roots reaches
162
+ */
163
+ reachableCount() {
164
+ const visited = new Uint8Array(this.nodeCount);
165
+ // every node is pushed at most once, so a stack of nodeCount entries never overflows
166
+ const stack = new Uint32Array(this.nodeCount);
167
+ let top = 0;
168
+ let count = 0;
169
+ for (const root of this.roots) {
170
+ visited[root] = 1;
171
+ count++;
172
+ stack[top++] = root;
173
+ while (top > 0) {
174
+ const u = stack[--top];
175
+ for (let k = this.rowPtr[u]; k < this.rowPtr[u + 1]; k++) {
176
+ const v = this.children[k];
177
+ if (visited[v] === 0) {
178
+ visited[v] = 1;
179
+ count++;
180
+ stack[top++] = v;
181
+ }
182
+ }
183
+ }
184
+ }
185
+ return count;
186
+ }
187
+ }
188
+ /**
189
+ * Build the children CSR of a snapshot (design section 7.1: a graph-io helper, not a core view).
190
+ * @param snapshot - the snapshot
191
+ * @param options - which column to read; the `parent` role, then `parents`, by default
192
+ * @returns the CSR; empty (every node a root) when the snapshot has no containment column
193
+ */
194
+ export function childrenCsr(snapshot, options = {}) {
195
+ const column = resolveColumn(snapshot, options.column === undefined ? "auto" : options.column);
196
+ return childrenFromColumn(snapshot.nodeCount, column);
197
+ }
198
+ /**
199
+ * Build the children CSR over one containment column without a snapshot (an importer that holds
200
+ * the column before freezing, a test).
201
+ * @param nodeCount - the node count the references index into
202
+ * @param column - a u32 `parent`-shaped column, a list-of-u32 `parents`-shaped column, or null for none
203
+ * @returns the CSR
204
+ */
205
+ export function childrenFromColumn(nodeCount, column) {
206
+ const rowPtr = new Uint32Array(nodeCount + 1);
207
+ const parentCount = new Uint32Array(nodeCount);
208
+ let dropped = 0;
209
+ const references = [];
210
+ if (column !== null) {
211
+ const rows = Math.min(column.length, nodeCount);
212
+ const keep = (child, parent) => {
213
+ if (!Number.isInteger(parent) || parent === INVALID_INDEX || parent >= nodeCount || parent === child) {
214
+ dropped++;
215
+ return;
216
+ }
217
+ references.push(child, parent);
218
+ rowPtr[parent + 1]++;
219
+ parentCount[child]++;
220
+ };
221
+ if (column.dtype === "u32") {
222
+ const { data } = column;
223
+ for (let i = 0; i < rows; i++) {
224
+ if (column.isSet(i)) {
225
+ keep(i, data[i]);
226
+ }
227
+ }
228
+ }
229
+ else if (column.dtype === "list") {
230
+ for (let i = 0; i < rows; i++) {
231
+ if (!column.isSet(i)) {
232
+ continue;
233
+ }
234
+ for (const item of column.sliceOf(i)) {
235
+ if (typeof item === "number") {
236
+ keep(i, item);
237
+ }
238
+ else {
239
+ dropped++;
240
+ }
241
+ }
242
+ }
243
+ }
244
+ else {
245
+ throw new TypeError(`a containment column must be u32 or a list of u32, found ${column.dtype} column "${column.meta.name}"`);
246
+ }
247
+ }
248
+ for (let u = 0; u < nodeCount; u++) {
249
+ rowPtr[u + 1] += rowPtr[u];
250
+ }
251
+ const children = new Uint32Array(rowPtr[nodeCount]);
252
+ const fill = rowPtr.slice(0, nodeCount);
253
+ // references were collected in ascending child order, so each parent's range fills ascending
254
+ for (let k = 0; k < references.length; k += 2) {
255
+ children[fill[references[k + 1]]++] = references[k];
256
+ }
257
+ return new ChildrenCsr(nodeCount, rowPtr, children, parentCount, dropped, column);
258
+ }
259
+ /**
260
+ * The column a ChildrenOptions selects.
261
+ * @param snapshot - the snapshot
262
+ * @param choice - the option value
263
+ * @returns the column, or null when the snapshot has none of that shape
264
+ */
265
+ function resolveColumn(snapshot, choice) {
266
+ if (typeof choice !== "string") {
267
+ return choice;
268
+ }
269
+ if (choice === "parent" || choice === "parents") {
270
+ return snapshot.nodes.byRole(choice);
271
+ }
272
+ return snapshot.nodes.byRole("parent") ?? snapshot.nodes.byRole("parents");
273
+ }
274
+ //# sourceMappingURL=children.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"children.js","sourceRoot":"","sources":["../../src/children.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAmC,aAAa,EAAE,MAAM,uBAAuB,CAAC;AA0BvF;;;GAGG;AACH,MAAM,OAAO,WAAW;IAoBpB;;;;;;;;OAQG;IACH,YACI,SAAiB,EACjB,MAAmB,EACnB,QAAqB,EACrB,WAAwB,EACxB,OAAe,EACf,MAAqB;QAErB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,SAAS,GAAG,CAAC,CAAC;QAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;YACjC,IAAI,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;gBACvB,SAAS,EAAE,CAAC;YAChB,CAAC;QACL,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;QACzC,IAAI,CAAC,GAAG,CAAC,CAAC;QACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;YACjC,IAAI,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAA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M,CAAC,CAAC;IACzC,CAAC;IACD,OAAO,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAC/E,CAAC"}
@@ -0,0 +1,229 @@
1
+ /**
2
+ * Attribute declaration shared by the typed importers (design sections 5.1, 5.5 and 5.6): turn a
3
+ * GEXF `<attribute>`, a GraphML `<key>` or a Neo4j header column into a ColumnDecl with the right
4
+ * dtype, default, options and origin metadata; parse values (scalars and lists) by the declaration;
5
+ * derive the deterministic `<name>#<origin.id>` name when a declared name collides; and the dict
6
+ * heuristic of design section 5.4.
7
+ *
8
+ * Naming rule (design section 5.6): a column's name is the GEXF `title` / GraphML `attr.name`,
9
+ * falling back to the attribute `id` when the title is absent; `origin.id` always keeps the source
10
+ * id and `origin.title` the title when it differs from the resolved name.
11
+ */
12
+ import { type ColumnDecl, type ColumnHandle, type ColumnRole, type GraphSink } from "@graphty/graph-format";
13
+ import { type IssueCategory } from "../types.js";
14
+ import { BAD_DEFAULT_CODE, BAD_OPTIONS_CODE, PRECISION_CODE, ROLE_TAKEN_CODE } from "./codes.js";
15
+ import { type DeclaredTypeSpec, type DeclaringFormat } from "./declared-types.js";
16
+ import { type ListSyntax } from "./lists.js";
17
+ import { type ImportReportBuilder, type IssueLocation } from "./report.js";
18
+ import { type TemporalValue } from "./temporal.js";
19
+ /**
20
+ * What an importer knows about a declared attribute.
21
+ * Consumed by the per-format importers and exporters under src/formats.
22
+ * @public
23
+ */
24
+ export interface AttributeDeclarationInput {
25
+ /** The declaring format. */
26
+ readonly format: DeclaringFormat;
27
+ /** The attribute id (GEXF `id`, GraphML key `id`, Neo4j header name); null when the format has none. */
28
+ readonly id: string | null;
29
+ /** The GEXF `title` / GraphML `attr.name`; null when absent (yFiles keys omit it). */
30
+ readonly title: string | null;
31
+ /** The declared type text; null for an untyped declaration (stored as string). */
32
+ readonly type: string | null;
33
+ /** The declaring namespace ("viz", "yfiles", "neo4j"), or null. */
34
+ readonly namespace?: string | null | undefined;
35
+ /** The `<default>` text, or null. */
36
+ readonly defaultText?: string | null | undefined;
37
+ /** The GEXF `<options>` text, or null. */
38
+ readonly optionsText?: string | null | undefined;
39
+ /** The list syntax of defaults, options and values; "gexf" by default. */
40
+ readonly listSyntax?: ListSyntax | undefined;
41
+ /** A role to give the column, or null. */
42
+ readonly role?: ColumnRole | null | undefined;
43
+ /** Whether the attribute is dynamic (GEXF mode="dynamic"). */
44
+ readonly dynamic?: boolean | undefined;
45
+ /** The importer's `long` option. */
46
+ readonly long: "f64" | "string";
47
+ /** Whether a column name is already taken in the target table; drives the `#id` rename. */
48
+ readonly taken?: ((name: string) => boolean) | undefined;
49
+ }
50
+ /**
51
+ * A non-fatal problem with a declaration, for the importer to record as a warning.
52
+ * Consumed by the per-format importers and exporters under src/formats.
53
+ * @public
54
+ */
55
+ export interface DeclarationIssue {
56
+ /** The issue category. */
57
+ readonly category: IssueCategory;
58
+ /** The stable code. */
59
+ readonly code: string;
60
+ /** A plain-ASCII message. */
61
+ readonly message: string;
62
+ }
63
+ /**
64
+ * A declared attribute: the ColumnDecl to push, the spec to parse values with, and what to report.
65
+ * Consumed by the per-format importers and exporters under src/formats.
66
+ * @public
67
+ */
68
+ export interface DeclaredAttribute {
69
+ /** The declaration for declareNodeColumn / declareEdgeColumn. */
70
+ readonly decl: ColumnDecl;
71
+ /** How values are parsed. */
72
+ readonly spec: DeclaredTypeSpec;
73
+ /** The list syntax values are split with. */
74
+ readonly listSyntax: ListSyntax;
75
+ /** The companion text column of a temporal attribute (design section 5.1), or null. */
76
+ readonly companion: ColumnDecl | null;
77
+ /** Whether the name received the `#id` suffix. */
78
+ readonly renamed: boolean;
79
+ /** Problems the importer records as warnings (unknown type, unparsable default or options). */
80
+ readonly issues: readonly DeclarationIssue[];
81
+ }
82
+ /** Issue code: the declared type is not one the format defines; the column is kept as string. */
83
+ export declare const UNKNOWN_TYPE_CODE = "W_UNKNOWN_ATTR_TYPE";
84
+ /** Issue code: the column was renamed `<name>#<id>` because the name was taken (design section 5.6). */
85
+ export declare const RENAMED_CODE = "W_COLUMN_RENAMED";
86
+ export { BAD_DEFAULT_CODE, BAD_OPTIONS_CODE, PRECISION_CODE, ROLE_TAKEN_CODE };
87
+ /** Rows sampled before the dict heuristic decides (design section 5.4). */
88
+ export declare const DICT_SAMPLE_ROWS = 1024;
89
+ /**
90
+ * Build the column declaration of a declared attribute.
91
+ * @param input - what the file declares
92
+ * @returns the declaration, the value spec and any warnings to record
93
+ */
94
+ export declare function declareAttribute(input: AttributeDeclarationInput): DeclaredAttribute;
95
+ /**
96
+ * Parse a value text by a declaration: a scalar, or a list split by the syntax with each item
97
+ * parsed by the item kind.
98
+ * @param text - the value text
99
+ * @param spec - the declared type
100
+ * @param listSyntax - the list syntax
101
+ * @returns the value to push (a boolean, a number, a string, a JSON value or an array)
102
+ */
103
+ export declare function parseDeclaredValue(text: string, spec: DeclaredTypeSpec, listSyntax: ListSyntax): unknown;
104
+ /**
105
+ * Parse a temporal value text by a declaration, keeping the source text when its canonical form
106
+ * differs (for the companion column).
107
+ * @param text - the value text
108
+ * @param spec - a scalar temporal spec
109
+ * @returns the value and the text to keep, or null text
110
+ */
111
+ export declare function parseDeclaredTemporal(text: string, spec: DeclaredTypeSpec): TemporalValue;
112
+ /**
113
+ * Whether a long value's text lost precision when parsed to f64 (design section 5.1: a value with
114
+ * |v| > 2^53 is stored as the nearest f64 and recorded as a `precision` issue). Decided from the
115
+ * text, since the parsed number alone cannot tell 2^53 from 2^53 + 1.
116
+ * @param spec - the declared type
117
+ * @param text - the value text (trimmed or not)
118
+ * @returns true when the importer should record a `precision` issue
119
+ */
120
+ export declare function losesPrecision(spec: DeclaredTypeSpec, text: string): boolean;
121
+ /**
122
+ * Declare a column on the sink for one domain.
123
+ * @param sink - the sink
124
+ * @param domain - node or edge
125
+ * @param decl - the declaration
126
+ * @returns the column handle
127
+ */
128
+ export declare function declareOn(sink: GraphSink, domain: "node" | "edge", decl: ColumnDecl): ColumnHandle;
129
+ /**
130
+ * The outcome of declareResolved(): the handle and the declaration as it was applied.
131
+ * Consumed by the per-format importers under src/formats.
132
+ * @public
133
+ */
134
+ export interface ResolvedDeclaration {
135
+ /** The column handle. */
136
+ readonly handle: ColumnHandle;
137
+ /** The declaration as applied (its name and role may differ from the request). */
138
+ readonly decl: ColumnDecl;
139
+ /** Whether the column was renamed `<name>#<id>` (W_COLUMN_RENAMED recorded). */
140
+ readonly renamed: boolean;
141
+ /** Whether the role was dropped because the table already holds it (W_ROLE_TAKEN recorded). */
142
+ readonly roleDropped: boolean;
143
+ }
144
+ /**
145
+ * The io rule of design section 5.6 in one place, so every importer agrees: declare a column on
146
+ * the sink and, when the sink already holds the name with another shape (an earlier import, a
147
+ * caller's column), rename it `<name>#<origin.id>` (a counter when the source has no id) and
148
+ * record W_COLUMN_RENAMED (coercion); when the table already holds the role (design section 5.5:
149
+ * at most one column per role), declare without the role and record W_ROLE_TAKEN (coercion). The
150
+ * same shape under the same name returns the existing handle, as the sink does.
151
+ * @param sink - the sink
152
+ * @param domain - node or edge
153
+ * @param decl - the wanted declaration
154
+ * @param report - the report the coercion issues are recorded in
155
+ * @param where - the line and element, when known
156
+ * @returns the handle and the declaration as applied
157
+ */
158
+ export declare function declareResolved(sink: GraphSink, domain: "node" | "edge", decl: ColumnDecl, report: ImportReportBuilder, where?: IssueLocation): ResolvedDeclaration;
159
+ /**
160
+ * Declare the companion text column of a temporal column (design section 5.1). A table holds at
161
+ * most one column per role, so when another companion already carries `timeText` this one is
162
+ * declared without the role (reported once per table); exporters find every companion through
163
+ * `extra.for`.
164
+ * @param sink - the sink
165
+ * @param domain - node or edge
166
+ * @param companion - the declaration from declareAttribute()
167
+ * @param report - the report the role loss is recorded in; when omitted the loss is silent
168
+ * @returns the column handle
169
+ */
170
+ export declare function declareCompanion(sink: GraphSink, domain: "node" | "edge", companion: ColumnDecl, report?: ImportReportBuilder): ColumnHandle;
171
+ /**
172
+ * Whether a column name is taken in a sink's table, the usual `taken` callback.
173
+ * @param sink - the sink
174
+ * @param domain - node or edge
175
+ * @returns a predicate over column names
176
+ */
177
+ export declare function takenIn(sink: GraphSink, domain: "node" | "edge"): (name: string) => boolean;
178
+ /**
179
+ * The deterministic name of a declared attribute whose preferred name is taken (design section
180
+ * 5.6): `<name>#<origin.id>`, and `<name>#<origin.id>#2`, `#3`... should that be taken as well.
181
+ * @param name - the preferred name
182
+ * @param id - the source id (the attribute id or key id); null falls back to a counter
183
+ * @param taken - whether a name is already used in the table
184
+ * @returns a free name
185
+ */
186
+ export declare function uniqueColumnName(name: string, id: string | null, taken: (name: string) => boolean): string;
187
+ /**
188
+ * The dict heuristic of design section 5.4 for an untyped string column: observe values until the
189
+ * sample is full; the column is a dict when the distinct count stays below half the rows, a string
190
+ * otherwise. The importer buffers the sampled values and declares the column on the decision.
191
+ */
192
+ export declare class DictHeuristic {
193
+ private readonly distinct;
194
+ private rowsSeen;
195
+ private readonly sampleRows;
196
+ /**
197
+ * Create a heuristic.
198
+ * @param sampleRows - rows observed before deciding; DICT_SAMPLE_ROWS by default
199
+ */
200
+ constructor(sampleRows?: number);
201
+ /**
202
+ * Rows observed so far.
203
+ * @returns the count
204
+ */
205
+ get rows(): number;
206
+ /**
207
+ * Distinct values observed so far.
208
+ * @returns the count
209
+ */
210
+ get distinctCount(): number;
211
+ /**
212
+ * Whether the sample is full.
213
+ * @returns true once sampleRows values were observed
214
+ */
215
+ get decided(): boolean;
216
+ /**
217
+ * Observe one value.
218
+ * @param value - the value text
219
+ * @returns true when this observation completed the sample
220
+ */
221
+ observe(value: string): boolean;
222
+ /**
223
+ * The dtype the sample suggests: dict when the distinct count is below rows / 2 (and at least
224
+ * one row was seen), string otherwise. Valid before the sample is full (end of input).
225
+ * @returns "dict" or "string"
226
+ */
227
+ decide(): "dict" | "string";
228
+ }
229
+ //# sourceMappingURL=attributes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"att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