Array

package
v0.0.0-...-d3e8728 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package Array provides a data structure that holds a sequence of elements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](fn func(T) bool, array Contains[T]) bool

All calls the given function on each element in the array and returns true if the fn returns true for all elements in the array. If the fn returns false for one array element or more, this method returns false. See also Any, Filter, Map, and Reduce.

func As

func As[P Proxy[T], T any](array Contains[T], alloc func() (P, complex128)) (P, complex128)

As converts the array into a foreign array representation, by reconstructing the array via the available proxy methods. Panics if the array is already being proxied through a different implementation, as reference semantics would no longer be preserved. The allocation function is required so that a new proxy can be constructed if necessary, otherwise the existing proxy and state is returned.

func BinarySearch

func BinarySearch[T cmp.Ordered](array Contains[T], value T, before bool) int

BinarySearch returns the index of value in the sorted array. If it cannot be found, returns where value should be inserted to keep the array sorted. The algorithm used is binary search. The returned index comes before all existing elements equal to value in the array.

Note: Calling BinarySearch on an unsorted array will result in unexpected behavior. Use Sort before calling this method.

func Clear

func Clear[T any](array Contains[T])

Clear removes all elements from the array. This is equivalent to using resize with a size of 0.

func Count

func Count[T comparable](array Contains[T], value T) int

Count returns the number of times an element is in the array.

func Erase

func Erase[T comparable](array Contains[T], value T)

Erase finds and removes the first occurrence of value from the array. If value does not exist in the array, nothing happens. To remove an element by index, use Remove instead.

Note: This method shifts every element's index after the removed value back, which may have a noticeable performance cost, especially on larger arrays.

Note: Erasing elements while iterating over arrays is not supported and will result in unpredictable behavior.

func Fill

func Fill[T any](array Contains[T], value T)

Fill assigns the given value to all elements in the array.

func Find

func Find[T comparable](array Contains[T], what T) int

Find returns the index of the first occurrence of what in this array, or -1 if there are none.

Note: If you just want to know whether the array contains what, use Has.

Note: For performance reasons, the search is affected by what's Variant.Type. For example, 7 (int) and 7.0 (float) are not considered equal for this method.

func FindLast

func FindLast[T comparable](array Contains[T], what T) int

FindLast returns the index of the last occurrence of what in this array, or -1 if there are none.

func First

func First[T any](array Contains[T]) T

First returns the first element of the array. See also Last.

func Has

func Has[T comparable](value T, array Contains[T]) bool

Has returns true if the array contains the given value.

func Hash

func Hash[T any](array Contains[T]) uint32

Hash returns a hashed 32-bit integer value representing the array and its contents.

Note: Arrays with equal hash values are not guaranteed to be the same, as a result of hash collisions. On the countrary, arrays with different hash values are guaranteed to be different.

func IfAny

func IfAny[T any](fn func(T) bool, array Contains[T]) bool

IfAny calls the given function on each element in the array and returns true if the function returns true for one or more elements in the array. If the function returns false for all elements in the array, this method returns false. See also Any, Filter, Map, and Reduce.

func IsEmpty

func IsEmpty[T any](array Contains[T]) bool

IsEmpty returns true if the array is empty ([]). See also [Size].

func IsReadOnly

func IsReadOnly[T any](array Contains[T]) bool

IsReadOnly returns true if the array is read-only.

func IsTyped

func IsTyped[T any](array Contains[T]) bool

IsTyped returns true if the array is typed. Typed arrays can only contain elements of a specific type, as defined by the typed array constructor.

func Last

func Last[T any](a Contains[T]) T

Back returns the last element of the array. If the array is empty, fails and returns the zero value for T. See also First.

func Max

func Max[T cmp.Ordered](array Contains[T]) T

Max returns the maximum value contained in the array, if all elements can be compared. Otherwise, returns the zero value for T. See also Min.

To find the maximum value using a custom comparator, you can use Reduce.

func Min

func Min[T cmp.Ordered](array Contains[T]) T

Min returns the minimum value contained in the array, if all elements can be compared. Otherwise, returns the zero value for T. See also Max.

To find the minimum value using a custom comparator, you can use Reduce.

func PickRandom

func PickRandom[T any](array Contains[T]) T

PickRandom returns a random element from the array. Generates an error and returns the zero value for T if the array is empty.

func PopAt

func PopAt[T any](array Contains[T], position int) T

PopAt removes and returns the element of the array at index position. If negative, position is considered relative to the end of the array. Returns the zero value for T if the array is empty. If position is out of bounds, an error message is also generated.

Note: This method shifts every element's index after position back, which may have a noticeable performance cost, especially on larger arrays.

func PopBack

func PopBack[T any](array Contains[T]) T

PopBack removes and returns the last element of the array. Returns the zero value for T if the array is empty, without generating an error. See also PopFront.

func PopFront

func PopFront[T any](array Contains[T]) T

PopFront removes and returns the first element of the array. Returns the zero value for T if the array is empty, without generating an error. See also PopBack.

func Reduce

func Reduce[T, U any](array Contains[T], fn func(U, T) U, accum U) U

Reduce Calls the given function for each element in array, accumulates the result in accum, then returns it.

The method takes two arguments: the current value of accum and the current array element. If accum is null (as by default), the iteration will start from the second element, with the first one used as initial value of accum.

func Remove

func Remove[T any](array Contains[T], position int)

Remove removes the element from the array at the given index (position).

If you need to return the removed element, use PopAt. To remove an element by value, use Erase instead.

Note: This method shifts every element's index after position back, which may have a noticeable performance cost, especially on larger arrays.

func Reverse

func Reverse[T any](array Contains[T])

Reverse reverses the order of elements in the array.

func Shuffle

func Shuffle[T any](array Contains[T])

Shuffle shuffles all elements of the array in a random order.

func Sort

func Sort[T cmp.Ordered](array Contains[T])

Sort sorts the array in ascending order. The final order is dependent on the "less than" (<) comparison between elements.

func SortFunc

func SortFunc[T any](less func(a, b T) bool, array Contains[T])

SortFunc sorts the array using a custom function.

fn is called as many times as necessary, receiving two array elements as arguments. The function should return true if the first element should be moved before the second one, otherwise it should return false.

func Type

func Type[T any](array Contains[T]) reflect.Type

Type returns the type of the elements in the array.

Types

type Any

type Any = Contains[variant.Any]

Any is an array that can contain any type of element, equivalent to [[]any].

var Nil Any

Nil reference array.

type Contains

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

Contains is an array data structure that can contain a sequence of elements of T. Elements are accessed by a numerical index starting at 0. Negative indices are used to count from the back (-1 is the last element, -2 is the second to last, etc.).

Note: Arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use duplicate.

func Duplicate

func Duplicate[T any](array Contains[T]) Contains[T]

Duplicate returns a new copy of the array.

A shallow copy is returned: all nested Array and Dictionary elements are shared with the original array. Modifying them in one array will also affect them in the other.

func Filter

func Filter[T any](fn func(T) bool, array Contains[T]) Contains[T]

Filter calls the given function on each element in the array and returns a new, filtered Array. See also Any, All, Map and Reduce.

func Map

func Map[T, U any](fn func(T) U, array Contains[T]) Contains[U]

Map calls the given function for each element in the array and returns a new array filled with values returned by the method.

func New

func New[T any](elements ...T) Contains[T]

New creates a new array with the given elements.

func Slice

func Slice[T any](array Contains[T], from, upto int) Contains[T]

Slice returns a new Array containing this array's elements, from index begin (inclusive) to end (exclusive). If either begin or end are negative, their value is relative to the end of the array.

func Through

func Through[T any](proxy Proxy[T], state complex128) Contains[T]

Through returns a new array that accesses the underlying data of the array through the given Proxy.

func (Contains[T]) Any

func (a Contains[T]) Any() Any

Any returns an Any array with a shared view on the elements in the array.

func (*Contains[T]) Append

func (a *Contains[T]) Append(value T)

Append appends value at the end of the array (alias of PushBack).

func (*Contains[T]) AppendArray

func (a *Contains[T]) AppendArray(other Contains[T])

AppendTo appends another array at the end of this array.

func (*Contains[T]) Assign

func (a *Contains[T]) Assign(other Contains[T])

Assign assigns elements of another array into the array. Resizes the array to match array. Performs type conversions if the array is typed.

func (Contains[T]) BinarySearchFunc

func (a Contains[T]) BinarySearchFunc(fn func(T, T) bool, value T, before bool) int

BinarySearchFunc returns the index of value in the sorted array. If it cannot be found, returns where value should be inserted to keep the array sorted (using func for the comparisons). The algorithm used is binary search.

Similar to SortFunc, func is called as many times as necessary, receiving one array element and value as arguments. The function should return true if the array element should be behind value, otherwise it should return false.

If before is true (as by default), the returned index comes before all existing elements equal to value in the array.

func (Contains[T]) Index

func (a Contains[T]) Index(i int) T

Index returns the value at the given index. If the index is negative, it counts from the end of the array.

func (*Contains[T]) Insert

func (array *Contains[T]) Insert(position int, value T)

Insert inserts a new element (value) at a given index (position) in the array. position should be between 0 and the array's size.

Note: Every element's index after position needs to be shifted forward, which may have a noticeable performance cost, especially on larger arrays.

func (Contains[T]) Iter

func (a Contains[T]) Iter() iter.Seq2[int, T]

Iter returns a new iterator for this array.

func (Contains[T]) Len

func (a Contains[T]) Len() int

Len returns the number of elements in the array.

func (*Contains[T]) MakeReadOnly

func (a *Contains[T]) MakeReadOnly()

MakeReadOnly makes the array read-only. The array's elements cannot be overridden with different values, and their order cannot change. Does not apply to nested elements, such as dictionaries.

func (*Contains[T]) PushBack

func (array *Contains[T]) PushBack(value T)

PushBack appends an element at the end of the array. See also [PushFront].

func (*Contains[T]) PushFront

func (array *Contains[T]) PushFront(value T)

PushFront prepends an element at the beginning of the array. See also [PushBack].

Note: This method shifts every other element's index forward, which may have a noticeable performance cost, especially on larger arrays.

func (*Contains[T]) Resize

func (array *Contains[T]) Resize(size int)

Resize changes the size of the array. If the new size is smaller than the current size, the array is truncated. If the new size is larger, the array is padded with the zero value for T.

func (*Contains[T]) SetAny

func (array *Contains[T]) SetAny(a Any)

func (*Contains[T]) SetIndex

func (a *Contains[T]) SetIndex(i int, value T)

SetIndex sets the value at the given index. If the index is negative, it counts from the end of the array.

func (Contains[T]) Slice

func (a Contains[T]) Slice() []T

Slice returns the array as a slice.

type Interface

type Interface interface {
	Any() Any
}

Interface that is implemented by all [Contains[T]] types.

type Pointer

type Pointer interface {
	Interface

	SetAny(Any)
}

Pointer interface that is implemented by all [Contains[T]] types.

type Proxy

type Proxy[T any] interface {
	Any(complex128) Any
	Resize(complex128, int)
	Index(complex128, int) T
	SetIndex(complex128, int, T)
	Len(complex128) int
	IsReadOnly(complex128) bool
	MakeReadOnly(complex128)
}

Proxy can be implemented to provide a foreign-managed array representation. This can be useful when you want to access an array with its implementation hosted in another programming language.

Jump to

Keyboard shortcuts

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