vector

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 3 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 vector provides a growable slice whose elements live off the Go heap.

Example
package main

import (
	"fmt"

	memory "github.com/stubbedev/memory-go"
	"github.com/stubbedev/memory-go/vector"
)

func main() {
	var a memory.Local
	defer a.Close()

	v, err := vector.New[int](&a, 0)
	if err != nil {
		panic(err)
	}
	defer v.Free()
	for i := range 5 {
		if err := v.Push(i * i); err != nil {
			panic(err)
		}
	}
	v.RemoveAt(0)
	last, _ := v.Pop()
	fmt.Println(v.Slice(), last, v.Len())
}
Output:
[1 4 9] 16 3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Vector

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

Vector is a growable sequence of off-heap Ts. It is not safe for concurrent use. Pointers and slices into it are valid until the next call that may grow or shrink it, except on a Vector from NewStable.

func From added in v0.5.0

func From[T any](a memory.Allocator, values []T) (*Vector[T], error)

From returns a Vector holding a copy of values.

func New

func New[T any](a memory.Allocator, capacity int) (*Vector[T], error)

New returns an empty Vector with room for capacity elements.

func NewStable added in v0.4.0

func NewStable[T any](maxLen int) (*Vector[T], error)

NewStable returns an empty Vector that never moves its elements, so pointers into it stay valid as it grows, and growing never copies. It reserves address space for maxLen elements, using memory only for those it holds, and growing past maxLen returns memory.ErrRegionFull. maxLen may be much larger than the Vector is likely to get.

func (*Vector[T]) All

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

All yields each index and element in order.

func (*Vector[T]) Append added in v0.4.0

func (v *Vector[T]) Append(values ...T) error

Append adds every value with one capacity check and one copy, which beats calling Push in a loop by a wide margin on large inputs.

func (*Vector[T]) At

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

At returns element i. It panics if i is out of range.

func (*Vector[T]) Cap

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

Cap returns the number of elements v can hold without growing.

func (*Vector[T]) Clip

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

Clip shrinks the capacity of v to its length.

func (*Vector[T]) DeleteFunc

func (v *Vector[T]) DeleteFunc(del func(T) bool) int

DeleteFunc removes every element for which del returns true, keeping the order of the rest, and returns how many it removed.

func (*Vector[T]) Free

func (v *Vector[T]) Free() error

Free releases the elements and leaves v empty. A Vector from NewStable must not be used afterwards.

func (*Vector[T]) Last

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

Last returns the last element, or reports false if v is empty.

func (*Vector[T]) Len

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

Len returns the number of elements.

func (*Vector[T]) Pop

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

Pop removes and returns the last element, or reports false if v is empty.

func (*Vector[T]) Ptr

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

Ptr returns a pointer to element i. It panics if i is out of range.

func (*Vector[T]) Push

func (v *Vector[T]) Push(value T) error

Push appends value.

func (*Vector[T]) RemoveAt

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

RemoveAt removes element i, shifting later elements down, and returns it. It panics if i is out of range.

func (*Vector[T]) Reserve

func (v *Vector[T]) Reserve(n int) error

Reserve grows v, if needed, to hold at least n elements. On a Vector from NewStable, n past its maxLen returns memory.ErrRegionFull and leaves v as it was.

func (*Vector[T]) Set

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

Set replaces element i. It panics if i is out of range.

func (*Vector[T]) Slice

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

Slice returns the elements as a Go slice.

func (*Vector[T]) Truncate

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

Truncate shortens v to n elements. It panics if n is out of range.

func (*Vector[T]) Values added in v0.5.0

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

Values yields each element in order.

Jump to

Keyboard shortcuts

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