Skip to content

Commit

Permalink
Set copy and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Apr 1, 2022
1 parent 96fdb7e commit afb9950
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions hashset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ false

- [type Set](<#type-set>)
- [func New[K any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Set[K]](<#func-new>)
- [func (s *Set[K]) Copy() *Set[K]](<#func-setk-copy>)
- [func (s *Set[K]) Each(fn func(key K))](<#func-setk-each>)
- [func (s *Set[K]) Has(val K) bool](<#func-setk-has>)
- [func (s *Set[K]) Put(val K)](<#func-setk-put>)
Expand All @@ -70,6 +71,14 @@ func New[K any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Set[K]

New returns an empty hashset\.

### func \(\*Set\[K\]\) [Copy](<https://github.com/zyedidia/generic/blob/master/hashset/set.go#L50>)

```go
func (s *Set[K]) Copy() *Set[K]
```

Copy returns a copy of this set\.

### func \(\*Set\[K\]\) [Each](<https://github.com/zyedidia/generic/blob/master/hashset/set.go#L43>)

```go
Expand Down
9 changes: 8 additions & 1 deletion hashset/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Set[K any] struct {
// New returns an empty hashset.
func New[K any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Set[K] {
return &Set[K]{
m: hashmap.NewMap[K, struct{}](capacity, equals, hash),
m: hashmap.New[K, struct{}](capacity, equals, hash),
}
}

Expand Down Expand Up @@ -45,3 +45,10 @@ func (s *Set[K]) Each(fn func(key K)) {
fn(key)
})
}

// Copy returns a copy of this set.
func (s *Set[K]) Copy() *Set[K] {
return &Set[K]{
m: s.m.Copy(),
}
}
5 changes: 2 additions & 3 deletions mapset/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"math/rand"
"testing"

g "github.com/zyedidia/generic"
"github.com/zyedidia/generic/mapset"
)

Expand All @@ -19,7 +18,7 @@ func checkeq[K comparable](set mapset.Set[K], get func(k K) bool, t *testing.T)

func TestCrossCheck(t *testing.T) {
stdm := make(map[int]bool)
set := mapset.New[int](1, g.Equals[int], g.HashInt)
set := mapset.New[int]()

const nops = 1000
for i := 0; i < nops; i++ {
Expand Down Expand Up @@ -47,7 +46,7 @@ func TestCrossCheck(t *testing.T) {
}

func Example() {
set := mapset.New[string](3, g.Equals[string], g.HashString)
set := mapset.New[string]()
set.Put("foo")
set.Put("bar")
set.Put("baz")
Expand Down

0 comments on commit afb9950

Please sign in to comment.