Documentation
¶
Overview ¶
A package that adds a variable-length Vector datatype, similar to the Rust implementation.
Index ¶
- Constants
- func Contains[T comparable](v *Vector[T], val T) bool
- func Find[T comparable](v *Vector[T], val T) int
- type Vector
- func (v *Vector[T]) AddCapacity(amount int) error
- func (v *Vector[T]) AllocAmount() int
- func (v *Vector[T]) Capacity() int
- func (v *Vector[T]) Copy(dst []T) int
- func (v *Vector[T]) Data() []T
- func (v *Vector[T]) Get(index int) (*T, error)
- func (v *Vector[T]) GetUnchecked(index int) *T
- func (v *Vector[T]) Insert(index int, val T) (bool, error)
- func (v *Vector[T]) IsInBounds(index int) bool
- func (v *Vector[T]) Len() int
- func (v *Vector[T]) PushBack(val T) bool
- func (v *Vector[T]) PushFront(val T) bool
- func (v *Vector[T]) Remove(index int) (*T, error)
- func (v *Vector[T]) RemoveUnchecked(index int) *T
- func (v *Vector[T]) Set(index int, val T) error
- func (v *Vector[T]) SetAllocAmount(newVal int) error
- func (v *Vector[T]) SetUnchecked(index int, val T)
Constants ¶
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 EmptyWithCapacity ¶
Create an empty Vector with a specified capacity.
Returns an error if allocAmount <= 0.
func FromSlice ¶
Create a Vector from a slice with capacity len(slice).
Returns an error if allocAmount <= 0.
func (*Vector[T]) AddCapacity ¶
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 ¶
Get the current allocation amount.
func (*Vector[T]) Copy ¶
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 ¶
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 ¶
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 ¶
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 ¶
Returns true if the index is valid. Returns false if using it would return an index out of bounds error.
func (*Vector[T]) PushBack ¶
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 ¶
Inserts an element at index 0. Takes the same amount of time as insertion.
Returns whether an allocation has occurred.
func (*Vector[T]) Remove ¶
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 ¶
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 ¶
Sets the value at the specified index.
Returns an error if the index is out of bounds.
func (*Vector[T]) SetAllocAmount ¶
Set the allocation amount to newVal.
Returns an error if newVal <= 0.
func (*Vector[T]) SetUnchecked ¶
Sets the value at the specified index without checking whether the index is in bounds.