github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/cols/readcol/wrapper.go (about)

     1  package readcol
     2  
     3  import (
     4  	"github.com/djordje200179/extendedlibrary/datastructures/cols"
     5  	"github.com/djordje200179/extendedlibrary/datastructures/iter"
     6  	"github.com/djordje200179/extendedlibrary/misc/functions/predication"
     7  )
     8  
     9  // Wrapper is a wrapper around a cols.Collection that provides read-only access to it.
    10  type Wrapper[T any] struct {
    11  	collection cols.Collection[T]
    12  }
    13  
    14  // From creates a new Wrapper around the given cols.Collection.
    15  func From[T any](collection cols.Collection[T]) Wrapper[T] {
    16  	return Wrapper[T]{collection}
    17  }
    18  
    19  // Size returns the number of elements.
    20  func (w Wrapper[T]) Size() int {
    21  	return w.collection.Size()
    22  }
    23  
    24  // Get returns the element at the given index.
    25  func (w Wrapper[T]) Get(index int) T {
    26  	return w.collection.Get(index)
    27  }
    28  
    29  // Clone returns a copy of a Wrapper with the same underlying cols.Collection.
    30  func (w Wrapper[T]) Clone() Wrapper[T] {
    31  	clonedCollection := w.collection.Clone()
    32  	return Wrapper[T]{clonedCollection}
    33  }
    34  
    35  // Iterator returns an iter.Iterator over the elements.
    36  func (w Wrapper[T]) Iterator() iter.Iterator[T] {
    37  	return w.collection.Iterator()
    38  }
    39  
    40  // Stream streams the elements of the collection.
    41  func (w Wrapper[T]) Stream(yield func(T) bool) {
    42  	w.collection.Stream(yield)
    43  }
    44  
    45  // FindIndex returns the index of the first element that satisfies the given predicate.
    46  // If no element satisfies the predicate, 0 and false are returned.
    47  func (w Wrapper[T]) FindIndex(predicate predication.Predicate[T]) (int, bool) {
    48  	return w.collection.FindIndex(predicate)
    49  }