From 4876a14e22d85cd1da1a4f6bdc4e8bcac9970ba1 Mon Sep 17 00:00:00 2001 From: Antonis Geralis Date: Mon, 15 Apr 2024 17:21:54 +0300 Subject: [PATCH] terrible code --- src/jsonpak/private/rawops_sorted.nim | 13 ++++++++----- src/jsonpak/sorted.nim | 6 ++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/jsonpak/private/rawops_sorted.nim b/src/jsonpak/private/rawops_sorted.nim index 7350596..e42bfd5 100644 --- a/src/jsonpak/private/rawops_sorted.nim +++ b/src/jsonpak/private/rawops_sorted.nim @@ -1,13 +1,15 @@ import bitabs, jsonnode, jsontree, rawops, std/[importutils, algorithm, sequtils] -proc rawSorted*(result: var JsonTree; tree: JsonTree, n: NodePos) = +proc rawSorted*(tree: JsonTree, n: NodePos): JsonTree = privateAccess(JsonTree) + var nodes = newSeqOfCap[Node](tree.nodes.len) + var atoms = BiTable[string]() var stack = @[n.PatchPos] while stack.len > 0: let curr = stack.pop().NodePos case curr.kind of opcodeObject: - result.nodes.add tree.nodes[curr.int] + nodes.add tree.nodes[curr.int] var pairs: seq[(string, PatchPos)] = @[] for n in keys(tree, curr): pairs.add (n.str, n.PatchPos) @@ -17,16 +19,17 @@ proc rawSorted*(result: var JsonTree; tree: JsonTree, n: NodePos) = stack.add PatchPos(n.firstSon) stack.add n.PatchPos of opcodeArray: - result.nodes.add tree.nodes[curr.int] + nodes.add tree.nodes[curr.int] var items: seq[PatchPos] = @[] for n in sonsReadonly(tree, curr): items.add n.PatchPos for i in countdown(items.high, 0): stack.add items[i] of opcodeInt, opcodeFloat, opcodeString: - result.nodes.add toAtomNode(result, curr.kind, curr.str) + nodes.add toNode(curr.kind, uint32 getOrIncl(atoms, curr.str)) else: - result.nodes.add tree.nodes[curr.int] + nodes.add tree.nodes[curr.int] + result = JsonTree(nodes: nodes, atoms: atoms) proc rawTest*(tree, value: JsonTree, n: NodePos): bool = privateAccess(JsonTree) diff --git a/src/jsonpak/sorted.nim b/src/jsonpak/sorted.nim index 2e9dc94..738a42c 100644 --- a/src/jsonpak/sorted.nim +++ b/src/jsonpak/sorted.nim @@ -1,4 +1,4 @@ -import private/[jsontree, jsonnode, rawops_sorted], std/importutils +import private/[jsontree, rawops_sorted], std/importutils type SortedJsonTree* = distinct JsonTree @@ -6,9 +6,7 @@ type proc sorted*(tree: JsonTree): SortedJsonTree {.inline.} = ## Sorts all the keys of `tree` recursively, ensuring that ## the resulting tree has its keys in lexicographic order. - privateAccess(JsonTree) - result = JsonTree(nodes: newSeqOfCap[Node](tree.nodes.len)).SortedJsonTree - rawSorted(JsonTree(result), tree, rootNodeId) + result = rawSorted(tree, rootNodeId).SortedJsonTree proc `==`*(a, b: SortedJsonTree): bool {.inline.} = ## The equality comparison for `SortedJsonTree` is faster than the one for `JsonTree`.