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

Speed up removal from a set #127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions lists/doublylinkedlist/doublylinkedlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ func (list *List) Remove(index int) {
list.size--
}

// Filter only keeps element for which hook returns true.
func (list *List) Filter(hook func(value interface{}) bool) {

for element := list.first; element != nil; element = element.next {
if !hook(element.value) {
if element == list.first {
list.first = element.next
}
if element == list.last {
list.last = element.prev
}
if element.prev != nil {
element.prev.next = element.next
}
if element.next != nil {
element.next.prev = element.prev
}
list.size--
}
}

}

// Contains check if values (one or more) are present in the set.
// All values have to be present in the set for the method to return true.
// Performance time complexity of n^2.
Expand Down
16 changes: 16 additions & 0 deletions lists/doublylinkedlist/doublylinkedlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ func TestListRemove(t *testing.T) {
}
}

func TestListFilter(t *testing.T) {
list := New()
list.Add("a")
list.Add("b", "c", "d", "e", "f")
list.Filter(func(v interface{}) bool { return v == "b" || v == "d" })
if actualValue, ok := list.Get(0); actualValue != "b" || !ok {
t.Errorf("Got %v expected %v", actualValue, "b")
}
if actualValue, ok := list.Get(1); actualValue != "d" || !ok {
t.Errorf("Got %v expected %v", actualValue, "d")
}
if actualValue := list.Size(); actualValue != 2 {
t.Errorf("Got %v expected %v", actualValue, 2)
}
}

func TestListGet(t *testing.T) {
list := New()
list.Add("a")
Expand Down
12 changes: 6 additions & 6 deletions sets/linkedhashset/linkedhashset.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func (set *Set) Add(items ...interface{}) {
}

// Remove removes the items (one or more) from the set.
// Slow operation, worst-case O(n^2).
func (set *Set) Remove(items ...interface{}) {
for _, item := range items {
if _, contains := set.table[item]; contains {
delete(set.table, item)
index := set.ordering.IndexOf(item)
set.ordering.Remove(index)
}
delete(set.table, item)
}
// Only keep non-deleted items
set.ordering.Filter(func(item interface{}) bool {
_, contains := set.table[item]
return contains
})
}

// Contains check if items (one or more) are present in the set.
Expand Down