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]) Append(values ...T) error
- 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, except on a Vector from NewStable.
func NewStable ¶ added in v0.4.0
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]) Append ¶ added in v0.4.0
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]) 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]) Free ¶
Free releases the elements and leaves v empty. A Vector from NewStable must not be used afterwards.
func (*Vector[T]) RemoveAt ¶
RemoveAt removes element i, shifting later elements down, and returns it. It panics if i is out of range.