Documentation
¶
Overview ¶
Package Array provides a data structure that holds a sequence of elements.
Index ¶
- func All[T any](fn func(T) bool, array Contains[T]) bool
- func As[P Proxy[T], T any](array Contains[T], alloc func() (P, complex128)) (P, complex128)
- func BinarySearch[T cmp.Ordered](array Contains[T], value T, before bool) int
- func Clear[T any](array Contains[T])
- func Count[T comparable](array Contains[T], value T) int
- func Erase[T comparable](array Contains[T], value T)
- func Fill[T any](array Contains[T], value T)
- func Find[T comparable](array Contains[T], what T) int
- func FindLast[T comparable](array Contains[T], what T) int
- func First[T any](array Contains[T]) T
- func Has[T comparable](value T, array Contains[T]) bool
- func Hash[T any](array Contains[T]) uint32
- func IfAny[T any](fn func(T) bool, array Contains[T]) bool
- func IsEmpty[T any](array Contains[T]) bool
- func IsReadOnly[T any](array Contains[T]) bool
- func IsTyped[T any](array Contains[T]) bool
- func Last[T any](a Contains[T]) T
- func Max[T cmp.Ordered](array Contains[T]) T
- func Min[T cmp.Ordered](array Contains[T]) T
- func PickRandom[T any](array Contains[T]) T
- func PopAt[T any](array Contains[T], position int) T
- func PopBack[T any](array Contains[T]) T
- func PopFront[T any](array Contains[T]) T
- func Reduce[T, U any](array Contains[T], fn func(U, T) U, accum U) U
- func Remove[T any](array Contains[T], position int)
- func Reverse[T any](array Contains[T])
- func Shuffle[T any](array Contains[T])
- func Sort[T cmp.Ordered](array Contains[T])
- func SortFunc[T any](less func(a, b T) bool, array Contains[T])
- func Type[T any](array Contains[T]) reflect.Type
- type Any
- type Contains
- func Duplicate[T any](array Contains[T]) Contains[T]
- func Filter[T any](fn func(T) bool, array Contains[T]) Contains[T]
- func Map[T, U any](fn func(T) U, array Contains[T]) Contains[U]
- func New[T any](elements ...T) Contains[T]
- func Slice[T any](array Contains[T], from, upto int) Contains[T]
- func Through[T any](proxy Proxy[T], state complex128) Contains[T]
- func (a Contains[T]) Any() Any
- func (a *Contains[T]) Append(value T)
- func (a *Contains[T]) AppendArray(other Contains[T])
- func (a *Contains[T]) Assign(other Contains[T])
- func (a Contains[T]) BinarySearchFunc(fn func(T, T) bool, value T, before bool) int
- func (a Contains[T]) Index(i int) T
- func (array *Contains[T]) Insert(position int, value T)
- func (a Contains[T]) Iter() iter.Seq2[int, T]
- func (a Contains[T]) Len() int
- func (a *Contains[T]) MakeReadOnly()
- func (array *Contains[T]) PushBack(value T)
- func (array *Contains[T]) PushFront(value T)
- func (array *Contains[T]) Resize(size int)
- func (array *Contains[T]) SetAny(a Any)
- func (a *Contains[T]) SetIndex(i int, value T)
- func (a Contains[T]) Slice() []T
- type Interface
- type Pointer
- type Proxy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
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 ¶
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 ¶
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 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 Has ¶
func Has[T comparable](value T, array Contains[T]) bool
Has returns true if the array contains the given value.
func Hash ¶
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 ¶
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 IsReadOnly ¶
IsReadOnly returns true if the array is read-only.
func IsTyped ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Sort ¶
Sort sorts the array in ascending order. The final order is dependent on the "less than" (<) comparison between elements.
Types ¶
type 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 ¶
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 ¶
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 ¶
Map calls the given function for each element in the array and returns a new array filled with values returned by the method.
func Slice ¶
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]) Append ¶
func (a *Contains[T]) Append(value T)
Append appends value at the end of the array (alias of PushBack).
func (*Contains[T]) AppendArray ¶
AppendTo appends another array at the end of this array.
func (*Contains[T]) Assign ¶
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 ¶
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 ¶
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 ¶
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]) 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 ¶
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.
type Interface ¶
type Interface interface {
Any() Any
}
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.