Skip to content

Commit

Permalink
terrible code
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Apr 15, 2024
1 parent 49975dc commit 4876a14
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/jsonpak/private/rawops_sorted.nim
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/jsonpak/sorted.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import private/[jsontree, jsonnode, rawops_sorted], std/importutils
import private/[jsontree, rawops_sorted], std/importutils

type
SortedJsonTree* = distinct JsonTree

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`.
Expand Down

0 comments on commit 4876a14

Please sign in to comment.