Skip to content

Commit

Permalink
use an external function
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Apr 15, 2024
1 parent 13dfc0d commit 52299d1
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/jsonpak/sorted.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import private/[bitabs, jsontree, jsonnode, rawops], std/[algorithm, importutils]
import private/[bitabs, jsontree, jsonnode, rawops], std/[algorithm, sequtils, importutils]

type
SortedJsonTree* = distinct JsonTree
Expand Down Expand Up @@ -54,7 +54,7 @@ proc rawTest(tree, value: JsonTree, n: NodePos): bool =
return true

proc `==`*(a, b: SortedJsonTree): bool {.inline.} =
## The equality comparison for SortedJsonTree is faster than the one for JsonTree.
## The equality comparison for `SortedJsonTree` is faster than the one for `JsonTree`.
privateAccess(JsonTree)
if JsonTree(a).nodes.len != JsonTree(b).nodes.len:
return false
Expand All @@ -79,13 +79,10 @@ proc rawDeduplicate(tree: var JsonTree, n: NodePos, parents: var seq[PatchPos])
nextChild tree, pos
else:
dec last
let oldfull = tree.nodes.len
let diff = 1 + span(tree, pos+1)
let endpos = pos + diff
for i in countup(endpos, oldfull-1):
tree.nodes[i-diff] = tree.nodes[i]
setLen(tree.nodes, oldfull-diff)
dec totaldiff, diff
let diff = -1 - span(tree, pos+1)
let endpos = pos - diff
tree.nodes.delete(pos, endpos - 1)
inc totaldiff, diff
if totaldiff < 0:
rawUpdateParents(tree, parents, totaldiff)
parents.setLen(parents.high)
Expand All @@ -105,7 +102,7 @@ proc rawDeduplicate(tree: var JsonTree, n: NodePos, parents: var seq[PatchPos])

proc deduplicate*(tree: var SortedJsonTree) =
## Deduplicates keys in `tree` recursively. If duplicate keys are found,
## only the last occurrence of each key is kept.
## only the last occurrence of the key is kept.
##
## The deduplication is performed in-place.
var parents: seq[PatchPos] = @[]
Expand Down

0 comments on commit 52299d1

Please sign in to comment.