Skip to content

Commit

Permalink
More efficient (under some cases) dedup algo
Browse files Browse the repository at this point in the history
For reference what it does:

```nim
  var curr = 0
  var last = x.len-1
  while curr <= last:
    var i = curr
    while i < last and x[i] == x[i+1]:
      dec last
      inc i
    if i > curr:
      x.delete(curr, i-1)
    else:
      inc curr
```
  • Loading branch information
planetis-m committed Apr 15, 2024
1 parent 52299d1 commit 8dda761
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
35 changes: 21 additions & 14 deletions src/jsonpak/sorted.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,47 @@ proc `==`*(a, b: SortedJsonTree): bool {.inline.} =
rawTest(JsonTree(a), JsonTree(b), rootNodeId)

proc rawDeduplicate(tree: var JsonTree, n: NodePos, parents: var seq[PatchPos]) =
# Todo: Turn the implementation to recursive bfs.
privateAccess(JsonTree)
case n.kind
of opcodeObject:
parents.add n.PatchPos
var totaldiff = 0
var count = 0
var curr = 0
var last = len(tree, n)-1
var pos = n.firstSon.int
while count <= last:
if count == last or
(var next = pos+1; nextChild tree, next; NodePos(pos).str != NodePos(next).str):
while curr <= last:
var i = curr
var tmp = pos
var diff = 0
while i < last and
(var next = tmp+1; nextChild tree, next; NodePos(tmp).str == NodePos(next).str):
inc diff, 1 + span(tree, tmp+1)
tmp = next
dec last
inc i
if i > curr:
let endpos = pos + diff
tree.nodes.delete(pos, endpos - 1)
dec totaldiff, diff
else:
inc curr
if not isAtom(tree, pos+1):
rawDeduplicate(tree, NodePos(pos+1), parents)
inc count
inc pos
nextChild tree, pos
else:
dec last
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)
of opcodeArray:
parents.add n.PatchPos
var pos = n.int+1
let last = len(tree, n)-1
var count = 0
while count <= last:
var curr = 0
while curr <= last:
if not isAtom(tree, pos):
rawDeduplicate(tree, NodePos(pos), parents)
inc count
inc curr
nextChild tree, pos
parents.setLen(parents.high)
else:
Expand Down
11 changes: 6 additions & 5 deletions tests/tsorted.nim
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,17 @@ proc main =
"b": 1,
"c": 2,
"b": 3,
"d": 4,
"c": 5
"b": 4,
"d": 5,
"c": 6
}
})
deduplicate(tree)
assert tree == SortedJsonTree(%*{
"a": {
"b": 3,
"c": 5,
"d": 4
"b": 4,
"c": 6,
"d": 5
}
})

Expand Down

0 comments on commit 8dda761

Please sign in to comment.