vector

package
v0.0.0-...-749c54f Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package vector provides a simple vector implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](v1, v2 *Vector[T]) bool

Equal reports whether two vectors are equal.

Types

type Vector

type Vector[T any] struct {
	// contains filtered or unexported fields
}

Vector represent a growable collection of elements that are stored contiguously in memory.

func Collect

func Collect[T any](seq iter.Seq[T]) *Vector[T]

Collect collects values from an iterator and returns a new vector.

func New

func New[T any]() *Vector[T]

New creates and initializes a new Vector

func NewWithCapacity

func NewWithCapacity[T any](capacity int) *Vector[T]

NewWithCapacity creates and initializes a new Vector with a specified capacity

func Of

func Of[T any](v ...T) *Vector[T]

Of constructs a new Vector with initial values

func (*Vector[T]) All

func (v *Vector[T]) All() iter.Seq2[int, T]

All returns an iterator over index-value pairs in the vector.

func (*Vector[T]) Back

func (v *Vector[T]) Back() (value T, ok bool)

Back returns the last element of v, if any.

func (*Vector[T]) Backward

func (v *Vector[T]) Backward() iter.Seq2[int, T]

Backward returns an iterator over index-value pairs in the vector, traversing it backward with decreasing indices.

func (*Vector[T]) Cap

func (v *Vector[T]) Cap() int

Cap returns the number of elements that the vector can hold without further allocation.

func (*Vector[T]) Clear

func (v *Vector[T]) Clear()

Clear removes all elements from the vector

func (*Vector[T]) Clip

func (v *Vector[T]) Clip()

Clip reduces the capacity of the vector as much as possible.

func (*Vector[T]) Empty

func (v *Vector[T]) Empty() bool

Empty reports whether the vector is empty.

func (*Vector[T]) Front

func (v *Vector[T]) Front() (value T, ok bool)

Front returns the first element of v, if any.

func (*Vector[T]) Get

func (v *Vector[T]) Get(i int) (value T, ok bool)

Get returns the zero-indexed ith element of v, if any.

func (*Vector[T]) Grow

func (v *Vector[T]) Grow(n int)

Grow increases the capacity of the vector, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the vector without another allocation.

If n is negative, Grow panics.

func (*Vector[T]) Insert

func (v *Vector[T]) Insert(i int, vals ...T)

Insert inserts the values vals... into v at index i.

Insert panics if i is out of range. This function is O(v.Len() + len(vals))

func (*Vector[T]) Len

func (v *Vector[T]) Len() int

Len returns the number of elements in the vector.

func (*Vector[T]) PopBack

func (v *Vector[T]) PopBack() (T, bool)

PopBack removes and returns the last element from the vector. If the vector is empty, it returns a zero value and false.

func (*Vector[T]) PushBack

func (v *Vector[T]) PushBack(val T)

PushBack adds an element to the end of the vector

func (*Vector[T]) Remove

func (v *Vector[T]) Remove(i int)

Remove removes the element at index i from v.

Remove panics if i is out of range.

func (*Vector[T]) RemoveRange

func (v *Vector[T]) RemoveRange(i, j int)

RemoveRange removes the elements with indices in range [i, j) from v. Removing an empty range (i >= j) is a no-op.

RemoveRange panics if either i or j is out of range.

func (*Vector[T]) Resize

func (v *Vector[T]) Resize(n int)

Resize changes the size of the vector to n. If n is less than the current size, the vector is truncated to the first n elements. If n is greater than the current size, the vector is grown to n elements, with the additional elements initialized to zero.

If n is negative, Resize panics.

func (*Vector[T]) Set

func (v *Vector[T]) Set(i int, value T)

Set sets the zero-indexed ith element of v to value.

Set panics if n is negative or greater than or equal to the length of v.

func (*Vector[T]) Values

func (v *Vector[T]) Values() iter.Seq[T]

Values returns an iterator that yields the vector elements in order.

Jump to

Keyboard shortcuts

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