github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/c/iface.go (about) 1 // Package c provides common types of containers, utility types and functions 2 package c 3 4 import ( 5 "errors" 6 7 "golang.org/x/exp/constraints" 8 ) 9 10 // Break is the 'break' statement of the For, Track methods 11 var Break = errors.New("Break") 12 13 // Continue is an alias of the nil value used to continue iterating by For, Track methods. 14 var Continue error = nil 15 16 // Iterable is a loop supplier interface 17 type Iterable[T any, Loop ~func() (T, bool)] interface { 18 Loop() Loop 19 } 20 21 // KeyVal provides extracing of a keys or values collection from key/value pairs 22 type KeyVal[Keys any, Vals any] interface { 23 Keys() Keys 24 Values() Vals 25 } 26 27 // Collection is the base interface of non-associative collections 28 type Collection[T any] interface { 29 For[T] 30 ForEach[T] 31 SliceFactory[T] 32 33 Reduce(merger func(T, T) T) T 34 HasAny(func(T) bool) bool 35 } 36 37 // Filterable provides filtering content functionality 38 type Filterable[T any, Loop ~func() (T, bool), LoopErr ~func() (T, bool, error)] interface { 39 Filter(predicate func(T) bool) Loop 40 Filt(predicate func(T) (bool, error)) LoopErr 41 } 42 43 // Convertable provides converaton of collection elements functionality 44 type Convertable[T any, Loop ~func() (T, bool), LoopErr ~func() (T, bool, error)] interface { 45 Convert(converter func(T) T) Loop 46 Conv(converter func(T) (T, error)) LoopErr 47 } 48 49 // SliceFactory collects the elements of the collection into a slice 50 type SliceFactory[T any] interface { 51 Slice() []T 52 Append([]T) []T 53 } 54 55 // MapFactory collects the key/value pairs of the collection into a map 56 type MapFactory[K comparable, V any, Map map[K]V | map[K][]V] interface { 57 Map() Map 58 } 59 60 // Iterator provides iterate over elements of a collection 61 type Iterator[T any] interface { 62 // Next returns the next element. 63 // The ok result indicates whether the element was returned by the iterator. 64 // If ok == false, then the iteration must be completed. 65 Next() (out T, ok bool) 66 67 For[T] 68 ForEach[T] 69 } 70 71 // Sized - storage interface with measurable size 72 type Sized interface { 73 // returns an estimated internal storage size or -1 if the size cannot be calculated 74 Size() int 75 } 76 77 // PrevIterator is the Iterator that provides reverse iteration over elements of a collection 78 type PrevIterator[T any] interface { 79 Iterator[T] 80 //retrieves a prev element and true or zero value of T and false if no more elements 81 Prev() (T, bool) 82 } 83 84 // DelIterator is the Iterator provides deleting of current element. 85 type DelIterator[T any] interface { 86 Iterator[T] 87 Delete() 88 } 89 90 // For is the interface of a collection that provides traversing of the elements. 91 type For[IT any] interface { 92 //For takes elements of the collection. Can be interrupt by returning Break. 93 For(func(element IT) error) error 94 } 95 96 // ForEach is the interface of a collection that provides traversing of the elements without error checking. 97 type ForEach[T any] interface { 98 // ForEach takes all elements of the collection 99 ForEach(func(element T)) 100 } 101 102 // All provides the `All` function used for iterating over a sequence of elements by `for e := range collection.All`. Supported since go 1.22. 103 type All[T any] interface { 104 All(consumer func(T) bool) 105 } 106 107 // KVAll provides the `All` function used for iterating over a sequence of key\value pairs by `for k, v := range collection.All`. Supported since go 1.22. 108 type KVAll[K, V any] interface { 109 All(consumer func(K, V) bool) 110 } 111 112 // Track is the interface of a collection that provides traversing of the elements with position tracking (index, key, coordinates, etc.). 113 type Track[P any, T any] interface { 114 // return Break for loop breaking 115 Track(func(position P, element T) error) error 116 } 117 118 // TrackEach is the interface of a collection that provides traversing of the elements with position tracking (index, key, coordinates, etc.) without error checking 119 type TrackEach[P any, T any] interface { 120 TrackEach(func(position P, element T)) 121 } 122 123 // Checkable is container with ability to check if an element is present. 124 type Checkable[T any] interface { 125 Contains(T) bool 126 } 127 128 // Access provides access to an element by its pointer (index, key, coordinate, etc.) 129 // Where: 130 // 131 // P - a type of pointer to a value (index, map key, coordinates) 132 // V - any arbitrary type of the value 133 type Access[P any, V any] interface { 134 Get(P) (V, bool) 135 } 136 137 // Addable provides appending the collection by elements. 138 type Addable[T any] interface { 139 Add(...T) 140 AddOne(T) 141 } 142 143 // AddableNew provides appending the collection by elements. 144 type AddableNew[T any] interface { 145 AddNew(...T) bool 146 AddOneNew(T) bool 147 } 148 149 // AddableAll provides appending the collection by elements retrieved from another collection 150 type AddableAll[Iterable any] interface { 151 AddAll(Iterable) 152 } 153 154 // AddableAllNew provides appending the collection by elements retrieved from another collection 155 type AddableAllNew[Iterable any] interface { 156 AddAllNew(Iterable) bool 157 } 158 159 // Settable provides element insertion or replacement by its pointer (index or key). 160 type Settable[P any, V any] interface { 161 Set(key P, value V) 162 } 163 164 // SettableNew provides element insertion by its pointer (index or key) only if the specified place is not occupied. 165 type SettableNew[P any, V any] interface { 166 SetNew(key P, value V) bool 167 } 168 169 // SettableMap provides element insertion or replacement with an equal key element of a map. 170 type SettableMap[Map any] interface { 171 SetMap(m Map) 172 } 173 174 // Deleteable provides removing any elements from the collection. 175 type Deleteable[k any] interface { 176 Delete(...k) 177 DeleteOne(k) 178 } 179 180 // DeleteableVerify provides removing any elements from the collection. 181 type DeleteableVerify[k any] interface { 182 DeleteActual(...k) bool 183 DeleteActualOne(k) bool 184 } 185 186 // ImmutableMapConvert provides converting to an immutable map instance. 187 type ImmutableMapConvert[M any] interface { 188 Immutable() M 189 } 190 191 // Removable provides removing an element by its pointer (index or key). 192 type Removable[P any, V any] interface { 193 Remove(P) (V, bool) 194 } 195 196 // Summable is a type that supports the operator + 197 type Summable interface { 198 constraints.Ordered | constraints.Complex | string 199 } 200 201 // Number is a type that supports the operators +, -, /, * 202 type Number interface { 203 constraints.Integer | constraints.Float | constraints.Complex 204 }