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
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ type myMap = Map[string,int] // the equal sign here is critical!
type myStdMap = StdMap[string, int]

func main() {
m := new(Map[string, int])

m.Copy(myStdMap{"b":2, "c":3})
m.Set("a",1)

sum := 0
for v := range m.All() {
sum += v
}
fmt.Print(sum)
m := new(Map[string, int])
m.Copy(myStdMap{"b":2, "c":3})
m.Set("a",1)
sum := 0
for v := range m.All() {
sum += v
}
fmt.Print(sum)
}

```
Expand Down
4 changes: 1 addition & 3 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"iter"
"slices"
)

// Set is a collection that keeps track of membership.
Expand Down Expand Up @@ -168,9 +167,8 @@ func (m *Set[K]) UnmarshalJSON(in []byte) (err error) {

// String returns the set as a string.
func (m *Set[K]) String() string {
vals := slices.Clone(m.Values())
ret := "{"
for i, v := range vals {
for i, v := range m.Values() {
ret += fmt.Sprintf("%#v", v)
if i < m.Len()-1 {
ret += ","
Expand Down
15 changes: 15 additions & 0 deletions set_ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ func (m *OrderedSet[K]) All() iter.Seq[K] {
v := m.Values()
return slices.Values(v)
}

// Clone returns a copy of the Set. This is a shallow clone:
// the new keys and values are set using ordinary assignment.
func (m *OrderedSet[K]) Clone() *OrderedSet[K] {
m1 := NewOrderedSet[K]()
m1.items = m.items.Clone()
return m1
}

// Add adds the value to the set.
// If the value already exists, nothing changes.
func (m *OrderedSet[K]) Add(k ...K) SetI[K] {
m.Set.Add(k...)
return m
}
9 changes: 9 additions & 0 deletions set_ordered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,12 @@ func TestOrderedSet_Range(t *testing.T) {
})
}
}

func TestOrderedSet_Clone(t *testing.T) {
m1 := NewOrderedSet("a", "b", "c")
m2 := m1.Clone()
assert.True(t, m1.Equal(m2))

m2.Add("d")
assert.False(t, m1.Equal(m2))
}
1 change: 0 additions & 1 deletion seti.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ type SetI[K comparable] interface {
Delete(k K)
All() iter.Seq[K]
Insert(seq iter.Seq[K])
Clone() *Set[K]
DeleteFunc(del func(K) bool)
}
Loading