Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4 - Add immutable remove operation #14

Merged
merged 2 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions trie/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@ func (trie *Trie) RemoveAtDepth(depth int, q key.Key) (reachedDepth int, removed
}
}
}

func Remove(trie *Trie, q key.Key) *Trie {
return RemoveAtDepth(0, trie, q)
}

func RemoveAtDepth(depth int, trie *Trie, q key.Key) *Trie {
switch {
case trie.IsEmptyLeaf():
return trie
case trie.IsNonEmptyLeaf() && !key.Equal(trie.Key, q):
return trie
case trie.IsNonEmptyLeaf() && key.Equal(trie.Key, q):
return &Trie{}
default:
dir := q.BitAt(depth)
afterDelete := RemoveAtDepth(depth+1, trie.Branch[dir], q)
if afterDelete == trie.Branch[dir] {
return trie
}
copy := &Trie{}
petar marked this conversation as resolved.
Show resolved Hide resolved
copy.Branch[dir] = afterDelete
copy.Branch[1-dir] = trie.Branch[1-dir]
copy.shrink()
return copy
}
}
72 changes: 72 additions & 0 deletions trie/remove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package trie

import (
"github.com/libp2p/go-libp2p-xor/key"
"math/rand"
"testing"
)

func TestImmutableRemoveIsImmutable(t *testing.T) {
for _, keySet := range testAddSamples {
trie := FromKeys(keySet.Keys)
for _, key := range keySet.Keys {
updated := Remove(trie, key)
if Equal(trie, updated) {
t.Fatalf("immuatble remove should not mutate trie, original: %v, updated: %v", trie, updated)
}
trie = updated
}
}
}

func TestMutableAndImmutableRemoveSame(t *testing.T) {
for _, keySet := range append(testAddSamples, randomTestAddSamples(100)...) {
mut := FromKeys(keySet.Keys)
immut := FromKeys(keySet.Keys)

for _, key := range keySet.Keys {
mut.Remove(key)
immut = Remove(immut, key)
if d := mut.CheckInvariant(); d != nil {
t.Fatalf("mutable trie invariant discrepancy: %v", d)
}
if d := immut.CheckInvariant(); d != nil {
t.Fatalf("immutable trie invariant discrepancy: %v", d)
}
if !Equal(mut, immut) {
t.Errorf("mutable trie %v differs from immutable trie %v", mut, immut)
}
}
}
}

func TestRemoveIsOrderIndependent(t *testing.T) {
for _, keySet := range append(testAddSamples, randomTestAddSamples(100)...) {
mut := FromKeys(keySet.Keys)
immut := FromKeys(keySet.Keys)

for j := 0; j < 100; j++ {
perm := rand.Perm(len(keySet.Keys))
for _, idx := range perm {
mut.Remove(keySet.Keys[idx])
immut = Remove(immut, keySet.Keys[idx])

if d := immut.CheckInvariant(); d != nil {
t.Fatalf("trie invariant discrepancy: %v", d)
}
if !Equal(mut, immut) {
t.Errorf("trie %v differs from trie %v", mut, immut)
}
}
}
}
}

func TestRemoveReturnsOriginalWhenNoKeyRemoved(t *testing.T) {
trie := FromKeys(testAddSamples[0].Keys)

result := Remove(trie, key.ByteKey(2))
if trie != result {
t.Fatalf("Remove should return original trie when no key was removed")
}
}