@@ -18,6 +18,7 @@ package fieldpath
1818
1919import (
2020 "fmt"
21+ "iter"
2122 "sort"
2223 "strings"
2324
@@ -47,6 +48,15 @@ func NewSet(paths ...Path) *Set {
4748 return s
4849}
4950
51+ // Copy returns a copy of the Set.
52+ // This is not a full deep copy as any contained value.Value is not copied.
53+ func (s * Set ) Copy () * Set {
54+ return & Set {
55+ Members : s .Members .Copy (),
56+ Children : s .Children .Copy (),
57+ }
58+ }
59+
5060// Insert adds the field identified by `p` to the set. Important: parent fields
5161// are NOT added to the set; if that is desired, they must be added separately.
5262func (s * Set ) Insert (p Path ) {
@@ -386,6 +396,15 @@ func (s *Set) Iterate(f func(Path)) {
386396 s .iteratePrefix (Path {}, f )
387397}
388398
399+ // All iterates over each Path in the set (preorder DFS).
400+ func (s * Set ) All () iter.Seq [Path ] {
401+ return func (yield func (Path ) bool ) {
402+ s .Iterate (func (p Path ) {
403+ yield (p )
404+ })
405+ }
406+ }
407+
389408func (s * Set ) iteratePrefix (prefix Path , f func (Path )) {
390409 s .Members .Iterate (func (pe PathElement ) { f (append (prefix , pe )) })
391410 s .Children .iteratePrefix (prefix , f )
@@ -455,6 +474,16 @@ func (s sortedSetNode) Len() int { return len(s) }
455474func (s sortedSetNode ) Less (i , j int ) bool { return s [i ].pathElement .Less (s [j ].pathElement ) }
456475func (s sortedSetNode ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
457476
477+ // Copy returns a copy of the SetNodeMap.
478+ // This is not a full deep copy as any contained value.Value is not copied.
479+ func (s * SetNodeMap ) Copy () SetNodeMap {
480+ out := make (sortedSetNode , len (s .members ))
481+ for i , v := range s .members {
482+ out [i ] = setNode {pathElement : v .pathElement .Copy (), set : v .set .Copy ()}
483+ }
484+ return SetNodeMap {members : out }
485+ }
486+
458487// Descend adds pe to the set if necessary, returning the associated subset.
459488func (s * SetNodeMap ) Descend (pe PathElement ) * Set {
460489 loc := sort .Search (len (s .members ), func (i int ) bool {
@@ -705,6 +734,15 @@ func (s *SetNodeMap) Iterate(f func(PathElement)) {
705734 }
706735}
707736
737+ // All iterates over each PathElement in the set.
738+ func (s * SetNodeMap ) All () iter.Seq [PathElement ] {
739+ return func (yield func (PathElement ) bool ) {
740+ s .Iterate (func (pe PathElement ) {
741+ yield (pe )
742+ })
743+ }
744+ }
745+
708746func (s * SetNodeMap ) iteratePrefix (prefix Path , f func (Path )) {
709747 for _ , n := range s .members {
710748 pe := n .pathElement
0 commit comments