Skip to content
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
24 changes: 15 additions & 9 deletions set/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var pool = sync.Pool{}

// Set is an implementation of ISet using the builtin map type. Set is threadsafe.
type Set struct {
items map[interface{}]bool
items map[interface{}]struct{}
lock sync.RWMutex
flattened []interface{}
}
Expand All @@ -41,7 +41,7 @@ func (set *Set) Add(items ...interface{}) {

set.flattened = nil
for _, item := range items {
set.items[item] = true
set.items[item] = struct{}{}
}
}

Expand All @@ -59,9 +59,11 @@ func (set *Set) Remove(items ...interface{}) {
// Exists returns a bool indicating if the given item exists in the set.
func (set *Set) Exists(item interface{}) bool {
set.lock.RLock()
defer set.lock.RUnlock()

_, ok := set.items[item]

set.lock.RUnlock()

return ok
}

Expand All @@ -84,17 +86,21 @@ func (set *Set) Flatten() []interface{} {
// Len returns the number of items in the set.
func (set *Set) Len() int64 {
set.lock.RLock()
defer set.lock.RUnlock()

return int64(len(set.items))
size := int64(len(set.items))

set.lock.RUnlock()

return size
}

// Clear will remove all items from the set.
func (set *Set) Clear() {
set.lock.Lock()
defer set.lock.Unlock()

set.items = map[interface{}]bool{}
set.items = map[interface{}]struct{}{}

set.lock.Unlock()
}

// All returns a bool indicating if all of the supplied items exist in the set.
Expand Down Expand Up @@ -134,7 +140,7 @@ func (set *Set) Dispose() {
func New(items ...interface{}) *Set {
set := pool.Get().(*Set)
for _, item := range items {
set.items[item] = true
set.items[item] = struct{}{}
}

return set
Expand All @@ -143,7 +149,7 @@ func New(items ...interface{}) *Set {
func init() {
pool.New = func() interface{} {
return &Set{
items: make(map[interface{}]bool, 10),
items: make(map[interface{}]struct{}, 10),
}
}
}
30 changes: 30 additions & 0 deletions set/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,33 @@ func BenchmarkFlatten(b *testing.B) {
set.Flatten()
}
}

func BenchmarkLen(b *testing.B) {
set := New()
for i := 0; i < 50; i++ {
item := strconv.Itoa(i)
set.Add(item)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Len()
}
}

func BenchmarkExists(b *testing.B) {
set := New()
set.Add(1)

b.ResetTimer()
for i := 0; i < b.N; i++ {
set.Exists(1)
}
}

func BenchmarkClear(b *testing.B) {
set := New()
for i := 0; i < b.N; i++ {
set.Clear()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compile error on this line:

# github.com/Workiva/go-datastructures/set
../../workspace/src/github.com/Workiva/go-datastructures/set/dict_test.go:205: undefined: set
FAIL    github.com/Workiva/go-datastructures/set [build failed]

}
}