File tree Expand file tree Collapse file tree 6 files changed +16
-8
lines changed
Expand file tree Collapse file tree 6 files changed +16
-8
lines changed Original file line number Diff line number Diff line change 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.
1010type 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.
101103func (s * LockedMap [M ]) Iterator (yield func (M ) bool ) {
102104 s .Cond .L .Lock ()
103105 s .iterating = true
Original file line number Diff line number Diff 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.
106108func (s * LockedOrdered [M ]) Iterator (yield func (M ) bool ) {
107109 s .Cond .L .Lock ()
108110 s .iterating = true
Original file line number Diff line number Diff 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.
5859func (s Map [M ]) Iterator (yield func (M ) bool ) {
5960 for k := range s {
6061 if ! yield (k ) {
Original file line number Diff line number Diff 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.
7172func (s * Ordered [M ]) Iterator (yield func (M ) bool ) {
7273 for _ , k := range s .values {
7374 if ! yield (k ) {
Original file line number Diff line number Diff line change 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.
1514type 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.
Original file line number Diff line number Diff 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`.
6668func (s * SyncMap [M ]) Iterator (yield func (M ) bool ) {
6769 s .m .Range (func (key , _ interface {}) bool {
6870 return yield (key .(M ))
You can’t perform that action at this time.
0 commit comments