seq

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package seq provides an interface for sequence-like types that can be indexed and inserted into.

Protocompile avoids storing slices of its public types as a means of achieving memory efficiency. However, this means that it cannot return those slices, and thus must use proxy types that implement the interfaces in this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](seq Indexer[T]) iter.Seq2[int, T]

All returns an iterator over the elements in seq, like slices.All.

func Append

func Append[T any](seq Inserter[T], values ...T)

Append appends values to an Inserter.

func Backward

func Backward[T any](seq Indexer[T]) iter.Seq2[int, T]

Backward returns an iterator over the elements in seq in reverse, like slices.Backward.

func Map

func Map[T, U any](seq Indexer[T], f func(T) U) iter.Seq[U]

Map is like [slicesx.Map].

func ToSlice

func ToSlice[T any](seq Indexer[T]) []T

ToSlice copies an Indexer into a slice.

func Values

func Values[T any](seq Indexer[T]) iter.Seq[T]

Values returns an iterator over the elements in seq, like slices.Values.

Types

type Func

type Func[T any] struct {
	Count int
	Get   func(int) T
}

Func implements Indexer[T] using an access function as underlying storage.

It uses a function that gets the value at the given index. Attempting to Set a value will panic.

func NewFunc

func NewFunc[T any](count int, get func(int) T) Func[T]

NewFunc constructs a new Func.

This method exists because Go currently will not infer type parameters of a type.

func (Func[T]) At

func (s Func[T]) At(idx int) T

At implements Indexer.

func (Func[T]) Len

func (s Func[T]) Len() int

Len implements Indexer.

func (Func[T]) SetAt

func (s Func[T]) SetAt(int, T)

SetAt implements Setter by panicking.

type Indexer

type Indexer[T any] interface {
	// Len returns the length of this sequence.
	Len() int

	// At returns the element at the given index.
	//
	// Should panic if idx < 0 or idx => Len().
	At(idx int) T
}

Indexer is a type that can be indexed like a slice.

type Inserter

type Inserter[T any] interface {
	Setter[T]

	// Insert inserts an element at the given index.
	//
	// Should panic if idx < 0 or idx > Len().
	Insert(idx int, value T)

	// Delete deletes the element at the given index.
	//
	// Should panic if idx < 0 or idx => Len().
	Delete(idx int)
}

Inserter is an Indexer that can be mutated by inserting or deleting values.

type Setter

type Setter[T any] interface {
	Indexer[T]

	// SetAt sets the value of the element at the given index.
	//
	// Should panic if idx < 0 or idx => Len().
	SetAt(idx int, value T)
}

Setter is an Indexer that can be mutated by modifying already present values.

type Slice

type Slice[T, E any] struct {
	Slice  []E
	Wrap   func(int, E) T
	Unwrap func(int, T) E
}

Slice implements Indexer[T] using an ordinary slice as the backing storage, and using the given functions to perform the conversion to and from the underlying raw values.

The first argument of Wrap/Unwrap given is the index the value has/will have in the slice.

func NewFixedSlice

func NewFixedSlice[T, E any](
	slice []E,
	wrap func(int, E) T,
) Slice[T, E]

NewFixedSlice constructs a new Slice whose Set method panics. This function is intended for cases where the Slice will immediately be turned into an Indexer.

This method exists because Go currently will not infer type parameters of a type.

func NewSlice

func NewSlice[T, E any](
	slice []E,
	wrap func(int, E) T,
	unwrap func(int, T) E,
) Slice[T, E]

NewSlice constructs a new Slice.

This method exists because Go currently will not infer type parameters of a type.

func (Slice[T, _]) At

func (s Slice[T, _]) At(idx int) T

At implements Indexer.

func (Slice[T, _]) Len

func (s Slice[T, _]) Len() int

Len implements Indexer.

func (Slice[T, _]) SetAt

func (s Slice[T, _]) SetAt(idx int, value T)

SetAt implements Setter.

type Slice2

type Slice2[T, E1, E2 any] struct {
	// All functions on Slice2 assume that these two slices are
	// always of equal length.
	Slice1 []E1
	Slice2 []E2

	Wrap   func(int, E1, E2) T
	Unwrap func(int, T) (E1, E2)
}

Slice2 is like Slice, but it uses a pair of slices instead of one.

This is useful for cases where the raw data is represented in a struct-of-arrays format for memory efficiency.

func NewSlice2

func NewSlice2[T, E1, E2 any](
	slice1 []E1,
	slice2 []E2,
	wrap func(int, E1, E2) T,
	unwrap func(int, T) (E1, E2),
) Slice2[T, E1, E2]

NewSlice2 constructs a new Slice2.

This method exists because Go currently will not infer type parameters of a type.

func (Slice2[T, _, _]) At

func (s Slice2[T, _, _]) At(idx int) T

At implements Indexer.

func (Slice2[T, _, _]) Len

func (s Slice2[T, _, _]) Len() int

Len implements Indexer.

func (Slice2[T, _, _]) SetAt

func (s Slice2[T, _, _]) SetAt(idx int, value T)

SetAt implements Setter.

type SliceInserter

type SliceInserter[T, E any] struct {
	Slice  *[]E
	Wrap   func(int, E) T
	Unwrap func(int, T) E
}

SliceInserter is like Slice, but also implements Inserter[T].

func EmptySliceInserter

func EmptySliceInserter[T, E any]() SliceInserter[T, E]

EmptySliceInserter returns a SliceInserter that is always empty and whose insertion operations panic.

func NewSliceInserter

func NewSliceInserter[T, E any](
	slice *[]E,
	wrap func(int, E) T,
	unwrap func(int, T) E,
) SliceInserter[T, E]

NewSliceInserter constructs a new SliceInserter.

This method exists because Go currently will not infer type parameters of a type.

func (SliceInserter[T, _]) At

func (s SliceInserter[T, _]) At(idx int) T

At implements Indexer.

func (SliceInserter[T, _]) Delete

func (s SliceInserter[T, _]) Delete(idx int)

Delete implements Inserter.

func (SliceInserter[T, _]) Insert

func (s SliceInserter[T, _]) Insert(idx int, value T)

Insert implements Inserter.

func (SliceInserter[T, _]) Len

func (s SliceInserter[T, _]) Len() int

Len implements Indexer.

func (SliceInserter[T, _]) SetAt

func (s SliceInserter[T, _]) SetAt(idx int, value T)

SetAt implements Setter.

type SliceInserter2

type SliceInserter2[T, E1, E2 any] struct {
	// All functions on SliceInserter2 assume that these two slices are
	// always of equal length.
	Slice1 *[]E1
	Slice2 *[]E2

	Wrap   func(int, E1, E2) T
	Unwrap func(int, T) (E1, E2)
}

SliceInserter2 is like Slice2, but also implements Inserter[T].

func NewSliceInserter2

func NewSliceInserter2[T, E1, E2 any](
	slice1 *[]E1,
	slice2 *[]E2,
	wrap func(int, E1, E2) T,
	unwrap func(int, T) (E1, E2),
) SliceInserter2[T, E1, E2]

NewSliceInserter2 constructs a new SliceInserter2.

This method exists because Go currently will not infer type parameters of a type.

func (SliceInserter2[T, _, _]) At

func (s SliceInserter2[T, _, _]) At(idx int) T

At implements Indexer.

func (SliceInserter2[T, _, _]) Delete

func (s SliceInserter2[T, _, _]) Delete(idx int)

Delete implements Inserter.

func (SliceInserter2[T, _, _]) Insert

func (s SliceInserter2[T, _, _]) Insert(idx int, value T)

Insert implements Inserter.

func (SliceInserter2[T, _, _]) Len

func (s SliceInserter2[T, _, _]) Len() int

Len implements Indexer.

func (SliceInserter2[T, _, _]) SetAt

func (s SliceInserter2[T, _, _]) SetAt(idx int, value T)

SetAt implements Setter.

Jump to

Keyboard shortcuts

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