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

refactor: generic hash set #8

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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ dependency:
test:
echo "" > coverage.txt
for d in $(shell go list ./... | grep -v vendor); do \
go test -mod=vendor -race -v -coverprofile=profile.out -covermode=atomic $$d || exit 1; \
go test -mod=mod -race -v -coverprofile=profile.out -covermode=atomic $$d || exit 1; \
[ -f profile.out ] && cat profile.out >> coverage.txt && rm profile.out; \
done
13 changes: 11 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module github.com/StudioSol/set

go 1.12
go 1.18

require github.com/stretchr/testify v1.6.1
require (
github.com/stretchr/testify v1.6.1
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 h1:Xt4/LzbTwfocTk9ZLEu4onjeFucl88iW+v4j4PWbQuE=
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
84 changes: 84 additions & 0 deletions linkedhashset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package set

import "golang.org/x/exp/constraints"

// LinkedHashSet linked hash set implementation using linkedHashMap as its
// underlying data structure.
//
// - Does not allow storing duplicated values
// - Does not allow storing nil values
// - Maintains insertion order over iteration
type LinkedHashSet[T constraints.Ordered] struct {
linkedHashMap *linkedHashMap
}

// Add adds elements to the linked hash set
func (l *LinkedHashSet[T]) Add(elements []T) {
for _, element := range elements {
l.linkedHashMap.Put(element, nil)
}
}

// Remove removes elements from the linked hash set
func (l *LinkedHashSet[T]) Remove(elements []T) {
for _, element := range elements {
l.linkedHashMap.Remove(element)
}
}

// Iter iterates over each element of the linked hash set
func (l *LinkedHashSet[T]) Iter() <-chan T {
ch := make(chan T, l.Length())
go func() {
for element := range l.linkedHashMap.Iter() {
ch <- element.key.(T)
}
close(ch)
}()
return ch
}

// Length returns the length of the linked hash set
func (l *LinkedHashSet[T]) Length() int {
return l.linkedHashMap.Length()
}

// AsSlice returns a slice of all values of the linked hash set
func (l *LinkedHashSet[T]) AsSlice() []T {
values := make([]T, 0, l.Length())
for value := range l.Iter() {
values = append(values, value)
}
return values
}

// AsInterface returns a slice of all values of the linked hash set
// as interface{}
func (l *LinkedHashSet[T]) AsInterface() []interface{} {
values := make([]interface{}, 0, l.Length())
for value := range l.Iter() {
values = append(values, value)
}
return values
}

// InArray returns whether the given item is in array or not
func (l *LinkedHashSet[T]) InArray(search T) bool {
for item := range l.Iter() {
if item == search {
return true
}
}
return false
}

// NewLinkedHashSet returns a new LinkedHashSet with the provided items
func NewLinkedHashSet[T constraints.Ordered](values []T) *LinkedHashSet[T] {
lhm := &LinkedHashSet[T]{
linkedHashMap: newLinkedHashMap(),
}
if len(values) > 0 {
lhm.Add(values)
}
return lhm
}