Skip to content

Commit f40f34e

Browse files
committed
Update docs
1 parent 4ea7b99 commit f40f34e

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

locked.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"sync"
66
)
77

8-
// LockedMap is a set implementation using a map and a read-write mutex. Instances of this type are safe to be used
9-
// concurrently. Iteration holds the read lock for the duration of the iteration.
8+
// LockedMap is a set implementation using a map and a mutex (via the sync.Cond). Instances of this type are safe to be used
9+
// concurrently. Iteration holds the lock for the duration of the iteration.
1010
type LockedMap[M comparable] struct {
1111
*sync.Cond
1212
iterating bool
@@ -98,6 +98,8 @@ func (s *LockedMap[M]) Cardinality() int {
9898
return len(s.m)
9999
}
100100

101+
// Iterator yields all elements in the set. It holds a lock for the duration of iteration, so calling other methods will block
102+
// until iteration is complete.
101103
func (s *LockedMap[M]) Iterator(yield func(M) bool) {
102104
s.Cond.L.Lock()
103105
s.iterating = true

locked_ordered.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ func (s *LockedOrdered[M]) Cardinality() int {
103103
return len(s.values)
104104
}
105105

106+
// Iterator yields all elements in the set in order. It holds a lock for the duration of iteration, so calling other methods will block
107+
// until iteration is complete.
106108
func (s *LockedOrdered[M]) Iterator(yield func(M) bool) {
107109
s.Cond.L.Lock()
108110
s.iterating = true

mapset.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (s Map[M]) Cardinality() int {
5555
return len(s)
5656
}
5757

58+
// Iterator yields all elements in the set.
5859
func (s Map[M]) Iterator(yield func(M) bool) {
5960
for k := range s {
6061
if !yield(k) {

ordered.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (s *Ordered[M]) Cardinality() int {
6868
return len(s.values)
6969
}
7070

71+
// Iterator yields all elements in the set in order.
7172
func (s *Ordered[M]) Iterator(yield func(M) bool) {
7273
for _, k := range s.values {
7374
if !yield(k) {

set.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"slices"
1010
)
1111

12-
// Set is a collection of unique elements. The elements must be comparable.
13-
// Each set implementation must implement this interface as the set functions within this package
14-
// build on top of this interface.
12+
// Set is a collection of unique elements. The elements must be comparable. Each set implementation must implement this
13+
// interface as the set functions within this package build on top of this interface.
1514
type Set[M comparable] interface {
1615
// Add an element to the set. Returns true if the element was not already in the set.
1716
Add(M) bool
@@ -28,9 +27,10 @@ type Set[M comparable] interface {
2827
// Contains returns true if the set contains the element.
2928
Contains(M) bool
3029

31-
// Iterator for the set. The yield function is called for each element in the set.
32-
// If yield function returns false, the iteration is stopped. The order of iteration
33-
// is not guaranteed. Changing the set while iterating over it is undefined behavior.
30+
// Iterator for the set elements. The yield function is called for each element in the set. If the yield function
31+
// returns false, the iteration is stopped. The order of iteration is not guaranteed unless the set is ordered.
32+
// Changing the set while iterating over it is undefined. It may or may not be safe to change the set or allowed
33+
// based on the implementation in use. Implementations must document their iteration safety.
3434
Iterator(yield func(M) bool)
3535

3636
// Remove an element from the set. Returns true if the element was in the set.

sync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func (s *SyncMap[M]) Cardinality() int {
6363
return n
6464
}
6565

66+
// Iterator yields all elements in the set. It is safe to call concurrently with other methods, but the order and
67+
// behavior is undefined, as per [sync.Map]'s `Range`.
6668
func (s *SyncMap[M]) Iterator(yield func(M) bool) {
6769
s.m.Range(func(key, _ interface{}) bool {
6870
return yield(key.(M))

0 commit comments

Comments
 (0)