iter

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package iter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bound

func Bound(i, n int) (bound int, valid bool)

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 Each

func Each[Iter Core[Elem], Elem any](
	iter Iter, fn func(i int, v Elem) bool,
) int

Each calls fn on each element from the 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

func Index(i, n int) (index int, valid bool)

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.

func Range

func Range(start, end, n int) (int, int, bool)

Range returns the [start:end] in range [0, n]

func SliceNth

func SliceNth[Elem any](s []Elem, i int) (elem Elem, ok bool)

func Step

func Step[Elem any](
	iter Core[Elem], from, step int, fn func(i int, v Elem) bool,
) int

Step calls fn on every next step element from the iter.

func StepEx

func StepEx[Elem, Arg any](
	iter Core[Elem], from, step int, arg Arg, fn func(i int, v Elem, arg Arg) bool,
)

StepEx is like Step but calls fn with an extra arg.

It panics if from or step is invalid for the iter.

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 CoreEx

type CoreEx[Arg, Elem any] interface {
	NthEx(Arg, int) (Elem, bool)
}

CoreEx is like Core but accepts an extra argument.

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

type DivisibleEx[Arg, Self any] interface {
	SliceFromEx(arg Arg, start int) Self
}

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 FiniteEx

type FiniteEx[Arg any] interface {
	LenEx(Arg) int
}

FiniteEx is like Finite but takes an extra argument.

type Func

type Func[Elem any] func(int) (Elem, bool)

Func implements Core for function.

func (Func[Elem]) Nth

func (fn Func[Elem]) Nth(i int) (Elem, bool)

type FuncEx

type FuncEx[Arg, Elem any] func(Arg, int) (Elem, bool)

FuncEx implements CoreEx for function.

func (FuncEx[Arg, Elem]) NthEx

func (fn FuncEx[Arg, Elem]) NthEx(arg Arg, i int) (Elem, bool)

type Interface

type Interface[T, Self any] interface {
	Core[T]
	Divisible[Self]
	Finite
}

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

func Slice

func Slice[Elem any](x []Elem) SliceIter[Elem]

Slice returns an iterator for the given slice.

func (SliceIter[E]) Len

func (s SliceIter[E]) Len() int

Len implements Finite.

func (SliceIter[E]) Nth

func (s SliceIter[E]) Nth(i int) (elem E, ok bool)

Nth implements Core[T]

func (SliceIter[E]) SliceFrom

func (s SliceIter[E]) SliceFrom(start int) SliceIter[E]

SliceFrom implements Divisible[SliceIter[E]]

type SliceIterEx

type SliceIterEx[Elem any, T []Elem] int

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]) Len

func (s SliceReverseIter[E]) Len() int

Len implements Finite.

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

type SliceReverseIterEx[Elem any, T []Elem] int

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]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL