vector

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 2 Imported by: 0

README

vector-go

Go Reference

A variable-length collection datatype that allocates as needed. It is based on the Rust implementation and aims to provide an efficient, high-level API.

Installation

To add this as a dependency, simply run go get github.com/HyperCodec/vector-go.

Basic Example
package main

import (
	"fmt"

	"github.com/HyperCodec/vector-go"
)

func main() {
    // create a vector with an initial capacity of 3 and an allocation amount of 5.
    v := vector.EmptyWithCapacity[int](3, 5)

    v.PushBack(1)
    v.PushBack(2)
    v.PushBack(3)

    fmt.Println(v.Data())
}
License

This project uses the MIT license.

Documentation

Overview

A package that adds a variable-length Vector datatype, similar to the Rust implementation.

Index

Constants

View Source
const (
	OutOfBounds        = "index out of bounds"
	InvalidAllocAmount = "invalid `allocAmount`"
	CannotAddAmount    = "cannot add this amount"
)

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](v *Vector[T], val T) bool

Returns whether or not the value exists in the vector. Requires the type in the vector to be comparable.

func Find

func Find[T comparable](v *Vector[T], val T) int

Returns the index of the first instance of val in v. If that value does not exist, it returns -1. Requires the type in the vector to be comparable.

Types

type Vector

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

A variable-length collection datatype that allows for simple/efficient pushing and insertion.

func Empty

func Empty[T any](allocAmount int) *Vector[T]

Create an empty Vector with a capacity of 0.

Returns an error if allocAmount <= 0.

func EmptyWithCapacity

func EmptyWithCapacity[T any](capacity, allocAmount int) *Vector[T]

Create an empty Vector with a specified capacity.

Returns an error if allocAmount <= 0.

func FromSlice

func FromSlice[T any](slice []T, allocAmount int) *Vector[T]

Create a Vector from a slice with capacity len(slice).

Returns an error if allocAmount <= 0.

func (*Vector[T]) AddCapacity

func (v *Vector[T]) AddCapacity(amount int) error

Add new capacity to the vector. Takes O(newCapacity) time to copy the vector's elements to a larger allocation.

Returns an error if amount <= 0.

func (*Vector[T]) AllocAmount

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

Get the current allocation amount.

func (*Vector[T]) Capacity

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

Get the current capacity of the vector.

func (*Vector[T]) Copy

func (v *Vector[T]) Copy(dst []T) int

Copies the data of the Vector to another slice.

Returns the amount of elements written.

func (*Vector[T]) Data

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

Get a slice of the data that is within the correct range. Mutating this value will mutate the original Vector.

Warning: Do not use this value after modifying the vector elsewhere (especially if the capacity changes) as it likely will not point to the correct data anymore.

func (*Vector[T]) Get

func (v *Vector[T]) Get(index int) (*T, error)

Gets a pointer to the value at a specified index.

Returns the pointer to the value. Otherwise it returns an error if the index is out of bounds.

func (*Vector[T]) GetUnchecked

func (v *Vector[T]) GetUnchecked(index int) *T

Gets a pointer to the value at the specified index without checking that the index is in bounds (panics if out of bounds).

func (*Vector[T]) Insert

func (v *Vector[T]) Insert(index int, val T) (bool, error)

Inserts an element at the index. Takes O(capacity) time without an allocation, or O(newCapacity) with an allocation.

Returns whether an allocation has occurred. Otherwise it returns an error if the index is out of bounds.

func (*Vector[T]) IsInBounds

func (v *Vector[T]) IsInBounds(index int) bool

Returns true if the index is valid. Returns false if using it would return an index out of bounds error.

func (*Vector[T]) Len

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

Get the length of the vector. Runs in O(1) time.

func (*Vector[T]) PushBack

func (v *Vector[T]) PushBack(val T) bool

Appends an item to the end of the Vector. Runs in O(1) time if there is no allocation. Otherwise takes O(newCapacity) time to copy values to a bigger allocation.

Returns whether an allocation has occurred.

func (*Vector[T]) PushFront

func (v *Vector[T]) PushFront(val T) bool

Inserts an element at index 0. Takes the same amount of time as insertion.

Returns whether an allocation has occurred.

func (*Vector[T]) Remove

func (v *Vector[T]) Remove(index int) (*T, error)

Removes the value at the specified index.

Returns the removed value. Returns an error if the index is out of bounds.

func (*Vector[T]) RemoveUnchecked

func (v *Vector[T]) RemoveUnchecked(index int) *T

Removes the value at the specified index and returns it without checking that the index is in bounds (panics if out of bounds).

func (*Vector[T]) Set

func (v *Vector[T]) Set(index int, val T) error

Sets the value at the specified index.

Returns an error if the index is out of bounds.

func (*Vector[T]) SetAllocAmount

func (v *Vector[T]) SetAllocAmount(newVal int) error

Set the allocation amount to newVal.

Returns an error if newVal <= 0.

func (*Vector[T]) SetUnchecked

func (v *Vector[T]) SetUnchecked(index int, val T)

Sets the value at the specified index without checking whether the index is in bounds.

Jump to

Keyboard shortcuts

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