github.com/haraldrudell/parl@v0.4.176/sets/set-id.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package sets
     7  
     8  import "github.com/haraldrudell/parl/iters"
     9  
    10  // SetID0 is a set of elements set.Element[T] where element type E is accessible
    11  type SetID0[T comparable, E any] struct {
    12  	Set0[T, E]
    13  	fTE        func(comparable T) (ep *E)
    14  	epIterator func() (iterator iters.Iterator[*E])
    15  }
    16  
    17  // NewSetID returns a set of elements set.Element[T] where element type E is accessible
    18  func NewSetID[T comparable, E any](elements []E) (set SetID[T, E]) {
    19  	s := SetID0[T, E]{}
    20  	NewSetFieldp(elements, &s.Set0, &s.fTE, &s.epIterator)
    21  	return &s
    22  }
    23  
    24  // Element returns the element representation for value or
    25  // nil if value is not an element of the set
    26  func (s *SetID0[T, E]) Element(value T) (element *E) { return s.fTE(value) }
    27  
    28  // Iterator allows iteration over all set elements
    29  func (s *SetID0[T, E]) EIterator() (iterator iters.Iterator[*E]) { return s.epIterator() }