vector

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/joetifa2003/mm-go/allocator"
	"github.com/joetifa2003/mm-go/vector"
)

func main() {
	alloc := allocator.NewC()
	v := vector.New[int](alloc)
	v.Push(1)
	v.Push(2)
	v.Push(3)

	fmt.Println("Length:", v.Len())
	for i := 0; i < v.Len(); i++ {
		fmt.Println(v.At(i))
	}

	for _, k := range v.Iter() {
		fmt.Println(k)
	}

}
Output:

Length: 3
1
2
3
1
2
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 a contiguous growable array type

func Init

func Init[T any](alloc allocator.Allocator, values ...T) *Vector[T]

Init initializes a new vector with the T elements provided and sets it's len and cap to len(values)

func New

func New[T any](aloc allocator.Allocator, args ...int) *Vector[T]

New creates a new empty vector, if args not provided it will create an empty vector, if only one arg is provided it will init a vector with len and cap equal to the provided arg, if two args are provided it will init a vector with len = args[0] cap = args[1]

func (*Vector[T]) At

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

At gets element T at specified index

func (*Vector[T]) AtPtr

func (v *Vector[T]) AtPtr(idx int) *T

AtPtr gets element a pointer of T at specified index

func (*Vector[T]) Cap

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

Cap gets vector capacity (underling memory length).

func (*Vector[T]) Free

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

Free deallocats the vector

func (*Vector[T]) Iter added in v0.8.0

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

Iter iterates over the vector

func (*Vector[T]) Last

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

Last gets the last element from a vector

func (*Vector[T]) Len

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

Len gets vector length

func (*Vector[T]) Pop

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

Pop pops value T from the vector and returns it

func (*Vector[T]) Push

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

Push pushes value T to the vector, grows if needed.

func (*Vector[T]) RemoveAt added in v0.8.0

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

func (*Vector[T]) Set

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

Set sets element T at specified index

func (*Vector[T]) Slice

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

Slice gets a slice representing the vector CAUTION: don't append to this slice, this is only used if you want to loop on the vec elements

func (*Vector[T]) UnsafeAt added in v0.8.0

func (v *Vector[T]) UnsafeAt(idx int) T

UnsafeAT gets element T at specified index without bounds checking

Jump to

Keyboard shortcuts

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