Documentation
¶
Overview ¶
Package iter.
Index ¶
- func Bound(i, n int) (bound int, valid bool)
- func Contains[T comparable, Iter Core[T]](iter Iter, x T) bool
- func Each[Iter Core[Elem], Elem any](iter Iter, fn func(i int, v Elem) bool) int
- func EachEx[Iter Core[Elem], Elem, Arg any](iter Iter, arg Arg, fn func(i int, v Elem, arg Arg) bool) int
- func Index(i, n int) (index int, valid bool)
- func Range(start, end, n int) (int, int, bool)
- func SliceNth[Elem any](s []Elem, i int) (elem Elem, ok bool)
- func Step[Elem any](iter Core[Elem], from, step int, fn func(i int, v Elem) bool) int
- func StepEx[Elem, Arg any](iter Core[Elem], from, step int, arg Arg, fn func(i int, v Elem, arg Arg) bool)
- type CanBackward
- type CanIndexFromEnd
- type CanSkip
- type Core
- type CoreEx
- type Divisible
- type DivisibleEx
- type Finite
- type FiniteEx
- type Func
- type FuncEx
- type Interface
- type InterfaceEx
- type MarkCanBackward
- type MarkCanIndexFromEnd
- type MarkCanSkip
- type SliceIter
- type SliceIterEx
- type SliceReverseIter
- type SliceReverseIterEx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bound ¶
Bound returns value used for slice bounding.
assuming n >= 0:
- i >= 0 && i < n: Bound(i, n) = Index(i, n)
- i == n: returns (i, true)
- i < 0: Bound(i, n) = Index(i, n) + 1
NOTE: the returned bound is only valid when valid is true.
func Contains ¶
func Contains[T comparable, Iter Core[T]](iter Iter, x T) bool
Contains returns true if there is x in iter.
func EachEx ¶
func EachEx[Iter Core[Elem], Elem, Arg any]( iter Iter, arg Arg, fn func(i int, v Elem, arg Arg) bool, ) int
EachEx is like Each but calls fn with an extra arg.
func Index ¶
Index returns the index in range [0, n) from i.
assuming n > 0:
- when i >= 0, validate it
- when i < 0, index from the n
NOTE: the returned index is only valid when valid is true.
Types ¶
type CanBackward ¶
type CanBackward interface {
IterCanBackward()
}
CanBackward should be implemented by iterators support backward iteration (from higher to lower).
Hint: embed iter.MarkCanBackward.
type CanIndexFromEnd ¶
type CanIndexFromEnd interface {
IterCanIndexFromEnd()
}
CanIndexFromEnd should be implemented by iterators support negative argument to Nth(int) method.
Hint: embed iter.MarkCanIndexFromEnd.
type CanSkip ¶
type CanSkip interface {
IterCanSkip()
}
CanSkip should be implemented by iterators support skipping elements during a single iteration.
Hint: embed iter.MarkCanSkip.
type Core ¶
type Core[Elem any] interface { // Nth returns the n-th element, the returned bool value indicates whether // the element exists. // // It is required to support the 1-step [0, end) range as input sequence, // in which case Nth(int) MUST function exactly the same way as Next() would. // // Caller SHOULD assume the implementation only supports the iteration // style implied by the above requirement: // // for i := 0; ; i++ { // elem, ok := iter.Nth(i) // if !ok { // break // } // // _ = elem // } // // Following features are optional: // // - Negative n (index from end) // - Step more than 1 (calling with non-consecutive n) // - Backward iteration (calling with n from high to low) // - Arbitrary iteration (calling with unordered n) // // Rationales choosing Nth(int) instead of Next() as core iterator function: // // - Passing an integer argument is cheap (a register in ABIInternal). // - Nth(int) is Next() when called with a sequence of ascending numbers. // (per implementation requirement) // - Nth(int) may skip unwanted elements without extraneous call. // - Nth(int) may step-backward with no external caching. // - Nth(int) is more friendly to slices. // - Nth(int) may be used with binary search to get the total length // of the iterator (when unknown). // - Stateless values can use the integer passed in to decide when to // stop iteration, otherwise an additional Next() style iterator // should be implemented. Nth(int) (Elem, bool) }
Core is the minimal interface of an iterator.
Rationales choosing interface over function for iterator definition:
- Implicit allocations: interface values can use mark.Noescape to avoid allocation but functions cannot, closures always allocate unless properly inlined by the compiler, which is not something controlled by application developers (at the time of go1.21).
Drawbacks of interface:
- One concrete type can only implement one iterator interface, some types may need multiple iterator implementations. A workaround is to define `type OtherTree Tree` and use pointer type cast for iterator implementation.
type Divisible ¶
type Divisible[Self any] interface { // SliceFrom returns a sub-iterator by slicing at the `start` of current // itertor, it resembles the native `x[start:]` operation. SliceFrom(start int) Self }
Divisible is implemented by iterators can be divided into multiple sub-iterators.
type DivisibleEx ¶
DivisibleEx is Divisible with an extra argument.
type Finite ¶
type Finite interface {
// Len returns the count of elements a iterator may produce.
Len() int
}
Finite is implemented by iterators with a finite number of elements.
type Interface ¶
Interface is the most complete iterator interface covers most use cases of an iterator.
type InterfaceEx ¶
type InterfaceEx[Arg, Elem, Self any] interface { CoreEx[Arg, Elem] FiniteEx[Arg] DivisibleEx[Arg, Self] }
type MarkCanBackward ¶
type MarkCanBackward struct{}
MarkCanBackward is a zero size implementation of CanBackward.
func (MarkCanBackward) IterCanBackward ¶
func (MarkCanBackward) IterCanBackward()
type MarkCanIndexFromEnd ¶
type MarkCanIndexFromEnd struct{}
MarkCanIndexFromEnd is a zero size implementation of CanIndexFromEnd.
func (MarkCanIndexFromEnd) IterCanIndexFromEnd ¶
func (MarkCanIndexFromEnd) IterCanIndexFromEnd()
type MarkCanSkip ¶
type MarkCanSkip struct{}
MarkCanSkip is a zero size implementation of CanSkip.
func (MarkCanSkip) IterCanSkip ¶
func (MarkCanSkip) IterCanSkip()
type SliceIter ¶
type SliceIter[Elem any] []Elem
type SliceIterEx ¶
func (SliceIterEx[Elem, T]) LenEx ¶
func (x SliceIterEx[Elem, T]) LenEx(s T) int
func (SliceIterEx[Elem, T]) NthEx ¶
func (x SliceIterEx[Elem, T]) NthEx(s T, i int) (e Elem, ok bool)
func (SliceIterEx[Elem, T]) SliceFromEx ¶
func (x SliceIterEx[Elem, T]) SliceFromEx(s T, start int) SliceIterEx[Elem, T]
type SliceReverseIter ¶
type SliceReverseIter[Elem any] []Elem
func SliceReverse ¶
func SliceReverse[Elem any](x []Elem) SliceReverseIter[Elem]
func (SliceReverseIter[E]) Nth ¶
func (s SliceReverseIter[E]) Nth(i int) (elem E, ok bool)
Nth implements Core[T]
func (SliceReverseIter[E]) SliceFrom ¶
func (s SliceReverseIter[E]) SliceFrom(start int) SliceReverseIter[E]
SliceFrom implements Divisible[SliceReverseIter[E]]
type SliceReverseIterEx ¶
func (SliceReverseIterEx[Elem, T]) LenEx ¶
func (x SliceReverseIterEx[Elem, T]) LenEx(s T) int
Len implements Finite.
func (SliceReverseIterEx[Elem, T]) NthEx ¶
func (x SliceReverseIterEx[Elem, T]) NthEx(s T, i int) (e Elem, ok bool)
Nth implements Core[T]