github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/cols/interfaces.go (about) 1 package cols 2 3 import ( 4 "github.com/djordje200179/extendedlibrary/datastructures/iter" 5 "github.com/djordje200179/extendedlibrary/misc" 6 "github.com/djordje200179/extendedlibrary/misc/functions/comparison" 7 "github.com/djordje200179/extendedlibrary/misc/functions/predication" 8 ) 9 10 // Iterator is a special iter.Iterator that can modify the collection it iterates over. 11 type Iterator[T any] interface { 12 iter.Iterator[T] 13 // GetRef returns a reference to the current element. 14 GetRef() *T 15 // Set sets the current element to the given value. 16 Set(value T) 17 18 // InsertBefore inserts the given value before the current element. 19 InsertBefore(value T) 20 // InsertAfter inserts the given value after the current element. 21 InsertAfter(value T) 22 23 // Remove removes the current element. 24 Remove() 25 26 // Index returns the Index of the current element. 27 Index() int 28 } 29 30 // Collection is a special iter.Iterable that represents a collection of elements. 31 type Collection[T any] interface { 32 // Size returns the number of elements. 33 Size() int 34 35 // Get returns the element at the given index. 36 Get(index int) T 37 // GetRef returns a reference to the element at the given Index. 38 GetRef(index int) *T 39 // Set sets the element at the given Index to the given value. 40 Set(index int, value T) 41 42 // Prepend prepends the given value to the collection. 43 Prepend(value T) 44 // Append appends the given value to the collection. 45 Append(value T) 46 // Insert inserts the given value at the given index. 47 Insert(index int, value T) 48 // Remove removes the element at the given index. 49 Remove(index int) 50 51 // Clear clears the collection. 52 Clear() 53 // Reverse reverses the collection. 54 Reverse() 55 // Sort sorts the collection using the given comparator. 56 Sort(comparator comparison.Comparator[T]) 57 // Join joins the collection with the given collection. 58 Join(other Collection[T]) 59 60 misc.Cloner[Collection[T]] 61 62 iter.Iterable[T] 63 // CollectionIterator creates and returns a new Iterator. 64 CollectionIterator() Iterator[T] 65 // Stream streams the elements of the Collection. 66 Stream(yield func(T) bool) 67 // RefsStream streams references to the elements of the Collection. 68 RefsStream(yield func(*T) bool) 69 70 // FindIndex returns the index of the first element that satisfies the given predicate. 71 // If no element satisfies the predicate, 0 and false are returned. 72 FindIndex(predicate predication.Predicate[T]) (int, bool) 73 // FindRef returns a reference to the first element that satisfies the given predicate. 74 // If no element satisfies the predicate, nil and false are returned. 75 FindRef(predicate predication.Predicate[T]) (*T, bool) 76 }