vector

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 2 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.

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 (*Vector[T]) All

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

All yields each index and element in order.

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.

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.

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.

Jump to

Keyboard shortcuts

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