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 ¶
- type Vector
- func (v *Vector[T]) All() iter.Seq2[int, T]
- func (v *Vector[T]) At(i int) T
- func (v *Vector[T]) Cap() int
- func (v *Vector[T]) Clip() error
- func (v *Vector[T]) DeleteFunc(del func(T) bool) int
- func (v *Vector[T]) Free() error
- func (v *Vector[T]) Last() (T, bool)
- func (v *Vector[T]) Len() int
- func (v *Vector[T]) Pop() (T, bool)
- func (v *Vector[T]) Ptr(i int) *T
- func (v *Vector[T]) Push(value T) error
- func (v *Vector[T]) RemoveAt(i int) T
- func (v *Vector[T]) Reserve(n int) error
- func (v *Vector[T]) Set(i int, value T)
- func (v *Vector[T]) Slice() []T
- func (v *Vector[T]) Truncate(n int)
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 (*Vector[T]) DeleteFunc ¶
DeleteFunc removes every element for which del returns true, keeping the order of the rest, and returns how many it removed.
func (*Vector[T]) RemoveAt ¶
RemoveAt removes element i, shifting later elements down, and returns it. It panics if i is out of range.