Skip to content

Commit

Permalink
adding method to create set from keys of map of type comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-babu authored and deckarep committed Mar 5, 2023
1 parent b0e3450 commit 2f79c69
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
Empty file added go.sum
Empty file.
24 changes: 24 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,27 @@ func NewThreadUnsafeSet[T comparable](vals ...T) Set[T] {
}
return &s
}

// Creates and returns a new set with the given keys of the map.
// Operations on the resulting set are thread-safe.
func NewSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
s := NewSet[T]()

for k := range val {
s.Add(k)
}

return s
}

// Creates and returns a new set with the given keys of the map.
// Operations on the resulting set are not thread-safe.
func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] {
s := NewThreadUnsafeSet[T]()

for k := range val {
s.Add(k)
}

return s
}
80 changes: 79 additions & 1 deletion set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ SOFTWARE.

package mapset

import "testing"
import (
"testing"
)

func makeSetInt(ints []int) Set[int] {
s := NewSet[int]()
Expand Down Expand Up @@ -1096,6 +1098,82 @@ func Test_ToSliceUnthreadsafe(t *testing.T) {
}
}

func Test_NewSetFromMapKey_Ints(t *testing.T) {
m := map[int]int{
5: 5,
2: 3,
}

s := NewSetFromMapKeys(m)

if len(m) != s.Cardinality() {
t.Errorf("Length of Set is not the same as the map. Expected: %d. Actual: %d", len(m), s.Cardinality())
}

for k := range m {
if !s.Contains(k) {
t.Errorf("Element %d not found in map: %v", k, m)
}
}
}

func Test_NewSetFromMapKey_Strings(t *testing.T) {
m := map[int]int{
5: 5,
2: 3,
}

s := NewSetFromMapKeys(m)

if len(m) != s.Cardinality() {
t.Errorf("Length of Set is not the same as the map. Expected: %d. Actual: %d", len(m), s.Cardinality())
}

for k := range m {
if !s.Contains(k) {
t.Errorf("Element %q not found in map: %v", k, m)
}
}
}

func Test_NewThreadUnsafeSetFromMapKey_Ints(t *testing.T) {
m := map[int]int{
5: 5,
2: 3,
}

s := NewThreadUnsafeSetFromMapKeys(m)

if len(m) != s.Cardinality() {
t.Errorf("Length of Set is not the same as the map. Expected: %d. Actual: %d", len(m), s.Cardinality())
}

for k := range m {
if !s.Contains(k) {
t.Errorf("Element %d not found in map: %v", k, m)
}
}
}

func Test_NewThreadUnsafeSetFromMapKey_Strings(t *testing.T) {
m := map[int]int{
5: 5,
2: 3,
}

s := NewThreadUnsafeSetFromMapKeys(m)

if len(m) != s.Cardinality() {
t.Errorf("Length of Set is not the same as the map. Expected: %d. Actual: %d", len(m), s.Cardinality())
}

for k := range m {
if !s.Contains(k) {
t.Errorf("Element %q not found in map: %v", k, m)
}
}
}

func Test_Example(t *testing.T) {
/*
requiredClasses := NewSet()
Expand Down

0 comments on commit 2f79c69

Please sign in to comment.