github.com/haraldrudell/parl@v0.4.176/sets/if-set.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 // Set provides a collection of unique elements of a particular type T that are printable, 7 // type convertible and have verifiable validity. 8 // Element represents an element of a set that has a unique value and is printable. 9 // SetElements provides an iterator for all elements of a set intended for SetFactory.NewSet. 10 package sets 11 12 import ( 13 "fmt" 14 15 "github.com/haraldrudell/parl/iters" 16 ) 17 18 // Set is a collection of elements. 19 // 20 // A set has: 21 // - a unique named type for elements that is comparable 22 // - element values and literals providing access to set functions: 23 // - — iterator over all elements 24 // - — IsValid method determining if the element is valid or zero-value 25 // - — String method making the element printable 26 // - — for elements with element ID, access to the element type 27 // 28 // A set element has: 29 // - element or element ID literals, typically typed constants 30 // - series of element or element ID literals are often 31 // typed iota-based constants 32 // - comparable mechanic: 33 // - — an incomparable element must have a comparable element ID 34 // - — element ID may also otherwise be used to refer to an element 35 // - — elements or element IDs may also be ordered 36 // 37 // benefits of using sets include: 38 // - Iterator method iterating over all elements 39 // - IsValid method verifying whether value is valid 40 // - String method ensuring elements to be printable 41 // - make incomparable elements comparable and iterable 42 // - referring to a complex element any-type using 43 // a basic-type comparable element ID 44 // - type safety by: 45 // - — distinguishing the set from all other types and values 46 // - — using elements or element IDs as typed function arguments 47 // 48 // composite or incomparable elements 49 // - If the element type is incomparable, 50 // the element should have a basic-type ID for which 51 // literals exist: 52 // - — Integer, Complex string float boolean 53 // - — slice index or pointers can be used as identity and element iD, 54 // but this complicates element ID literals or storing of element IDs 55 // - incomparables are slice, map, function, and structs or interface 56 // dynamic types of those. In particular, a byte slice is incomparable 57 // - an element must at least have a derived comparable ID type 58 // - a method can be used to obtain a comparable ID from a 59 // composite or incomparable type 60 // 61 // complex elements 62 // - similarly, if element type is large, it may benefit from being referred 63 // by an element ID 64 // 65 // Usage: 66 // 67 // const IsSame NextAction = 0 68 // type NextAction uint8 69 // func (na NextAction) String() (s string) {return nextActionSet.StringT(na)} 70 // var nextActionSet = set.NewSet(yslices.ConvertSliceToInterface[ 71 // set.Element[NextAction], 72 // parli.Element[NextAction], 73 // ]([]set.Element[NextAction]{{ValueV: IsSame, Name: "IsSame"}})) 74 type Set[E comparable] interface { 75 // IsValid returns whether value is part of the set 76 IsValid(value E) (isValid bool) 77 // Iterator allows iteration over all set elements 78 Iterator() (iterator iters.Iterator[E]) 79 // StringT returns a string representation for an element of this set. 80 // If value is not a valid element, a fmt.Printf value is output like ?'%v' 81 StringT(value E) (s string) 82 // Description returns a more elaborate string representation 83 // for an element. Description and StringT may return the same value 84 Description(value E) (s string) 85 fmt.Stringer 86 } 87 88 type SetID[T comparable, E any] interface { 89 Set[T] 90 // Element returns the element representation for value or 91 // nil if value is not an element of the set 92 Element(value T) (elementType *E) 93 // Iterator allows iteration over all set elements 94 EIterator() (iterator iters.Iterator[*E]) 95 } 96 97 // Element represents an element of a set that has a unique value and is printable. 98 // set element values are unique but not necessarily ordered. 99 // set.SetElement is an implementation. 100 type Element[T comparable] interface { 101 Value() (value T) 102 fmt.Stringer 103 } 104 105 type ElementDescription interface { 106 Description() (full string) 107 }