Documentation ¶
Overview ¶
Package array provides OOP-style arrays (direct-access sequences).
For better performance, all functions in this package are unsafe for concurrency unless otherwise specified.
Index ¶
- type Array
- type DynamicArray
- type DynamicArraySpecific
- type OrderedArray
- type OrderedDynamicArray
- func WrapFloatSlice[Item constraints.Float](slicePtr *[]Item) OrderedDynamicArray[Item]
- func WrapSliceLess[Item any](slicePtr *[]Item, lessFn compare.LessFunc[Item]) OrderedDynamicArray[Item]
- func WrapTransitiveOrderedSlice[Item constraints.TransitiveOrdered](slicePtr *[]Item) OrderedDynamicArray[Item]
- type SliceDynamicArray
- func (sda *SliceDynamicArray[Item]) Append(s sequence.Sequence[Item])
- func (sda *SliceDynamicArray[Item]) Back() Item
- func (sda *SliceDynamicArray[Item]) Cap() int
- func (sda *SliceDynamicArray[Item]) Clear()
- func (sda *SliceDynamicArray[Item]) Cut(begin, end int)
- func (sda *SliceDynamicArray[Item]) CutWithoutOrder(begin, end int)
- func (sda *SliceDynamicArray[Item]) Expand(i, n int)
- func (sda *SliceDynamicArray[Item]) Extend(n int)
- func (sda *SliceDynamicArray[Item]) Filter(filter func(x Item) (keep bool))
- func (sda *SliceDynamicArray[Item]) Front() Item
- func (sda *SliceDynamicArray[Item]) Get(i int) Item
- func (sda *SliceDynamicArray[Item]) Insert(i int, x Item)
- func (sda *SliceDynamicArray[Item]) InsertSequence(i int, s sequence.Sequence[Item])
- func (sda *SliceDynamicArray[Item]) Len() int
- func (sda *SliceDynamicArray[Item]) Pop() Item
- func (sda *SliceDynamicArray[Item]) Push(x Item)
- func (sda *SliceDynamicArray[Item]) Range(handler func(x Item) (cont bool))
- func (sda *SliceDynamicArray[Item]) Remove(i int) Item
- func (sda *SliceDynamicArray[Item]) RemoveWithoutOrder(i int) Item
- func (sda *SliceDynamicArray[Item]) Reserve(capacity int)
- func (sda *SliceDynamicArray[Item]) Reverse()
- func (sda *SliceDynamicArray[Item]) Set(i int, x Item)
- func (sda *SliceDynamicArray[Item]) SetBack(x Item)
- func (sda *SliceDynamicArray[Item]) SetFront(x Item)
- func (sda *SliceDynamicArray[Item]) Shrink()
- func (sda *SliceDynamicArray[Item]) Slice(begin, end int) Array[Item]
- func (sda *SliceDynamicArray[Item]) Swap(i, j int)
- func (sda *SliceDynamicArray[Item]) Truncate(i int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array[Item any] interface { sequence.Sequence[Item] // Get returns the item with index i. // // It panics if i is out of range. Get(i int) Item // Set sets the item with index i to x. // // It panics if i is out of range. Set(i int, x Item) // Swap exchanges the items with indexes i and j. // // It panics if i or j is out of range. Swap(i, j int) // Slice returns a slice from argument begin (inclusive) to // argument end (exclusive) of the array, as an Array. // // It panics if begin or end is out of range, or begin > end. Slice(begin, end int) Array[Item] }
Array is an interface representing a direct-access sequence.
type DynamicArray ¶
type DynamicArray[Item any] interface { Array[Item] DynamicArraySpecific[Item] }
DynamicArray is an interface representing a dynamic-length direct-access sequence.
type DynamicArraySpecific ¶
type DynamicArraySpecific[Item any] interface { container.Filter[Item] // Cap returns the current capacity of the dynamic array. Cap() int // Push adds x to the back of the dynamic array. Push(x Item) // Pop removes and returns the last item. // // It panics if the dynamic array is nil or empty. Pop() Item // Append adds s to the back of the dynamic array. // // s shouldn't be modified during calling this method, // otherwise, unknown error may occur. Append(s sequence.Sequence[Item]) // Truncate removes the item with index i and all subsequent items. // // It does nothing if i is out of range. Truncate(i int) // Insert adds x as the item with index i. // // It panics if i is out of range, i.e., i < 0 or i > Len(). Insert(i int, x Item) // Remove removes and returns the item with index i. // // It panics if i is out of range, i.e., i < 0 or i >= Len(). Remove(i int) Item // RemoveWithoutOrder removes and returns the item with index i, // without preserving order. // // It panics if i is out of range, i.e., i < 0 or i >= Len(). RemoveWithoutOrder(i int) Item // InsertSequence inserts s to the front of the item with index i. // // It panics if i is out of range, i.e., i < 0 or i > Len(). // // s shouldn't be modified during calling this method, // otherwise, unknown error may occur. InsertSequence(i int, s sequence.Sequence[Item]) // Cut removes items from argument begin (inclusive) to // argument end (exclusive) of the dynamic array. // // It panics if begin or end is out of range, or begin > end. Cut(begin, end int) // CutWithoutOrder removes items from argument begin (inclusive) to // argument end (exclusive) of the dynamic array, without preserving order. // // It panics if begin or end is out of range, or begin > end. CutWithoutOrder(begin, end int) // Extend adds n zero-value items to the back of the dynamic array. // // It panics if n < 0. Extend(n int) // Expand inserts n zero-value items to the front of the item with index i. // // It panics if i is out of range, i.e., i < 0 or i > Len(), or n < 0. Expand(i, n int) // Reserve requests that the capacity of the dynamic array // is at least the specified capacity. // // It does nothing if capacity <= Cap(). Reserve(capacity int) // Shrink reduces the dynamic array to fit, i.e., // requests Cap() equals to Len(). // // Note that it isn't equivalent to operations on Go slice // like s[:len(s):len(s)], // because it allocates a new array and copies the content // if Cap() > Len(). Shrink() // Clear removes all items in the dynamic array and // asks to release the memory. Clear() }
DynamicArraySpecific is an interface that groups the dynamic-array-specific methods.
type OrderedArray ¶
type OrderedArray[Item any] interface { Array[Item] // Less reports whether the item with index i must sort before // the item with index j. // // Less must describe a transitive ordering: // - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well. // - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well. // // Note that floating-point comparison // (the < operator on float32 or float64 values) // is not a transitive ordering when not-a-number (NaN) values are involved. // // It panics if i or j is out of range. Less(i, j int) bool }
OrderedArray is an interface representing a direct-access sequence that can be sorted by integer index.
It conforms to interface sort.Interface.
type OrderedDynamicArray ¶
type OrderedDynamicArray[Item any] interface { OrderedArray[Item] DynamicArraySpecific[Item] }
OrderedDynamicArray is an interface representing a dynamic-length direct-access sequence that can be sorted by integer index.
It conforms to interface sort.Interface.
func WrapFloatSlice ¶
func WrapFloatSlice[Item constraints.Float](slicePtr *[]Item) OrderedDynamicArray[Item]
WrapFloatSlice wraps a pointer to Go slice to an OrderedDynamicArray.
It requires that the items of the slice must be floating-point numbers. See github.com/donyori/gogo/constraints.Float for details.
Operations on the returned OrderedDynamicArray will affect the provided Go slice. Operations on the Go slice will also affect the returned OrderedDynamicArray.
It panics if slicePtr is nil.
func WrapSliceLess ¶
func WrapSliceLess[Item any]( slicePtr *[]Item, lessFn compare.LessFunc[Item]) OrderedDynamicArray[Item]
WrapSliceLess wraps a pointer to a Go slice with github.com/donyori/gogo/function/compare.LessFunc to an OrderedDynamicArray.
The specified LessFunc must describe a transitive ordering:
- if both lessFn(a, b) and lessFn(b, c) are true, then lessFn(a, c) must be true as well.
- if both lessFn(a, b) and lessFn(b, c) are false, then lessFn(a, c) must be false as well.
Note that floating-point comparison (the < operator on float32 or float64 values) is not a transitive ordering when not-a-number (NaN) values are involved.
Operations on the returned OrderedDynamicArray will affect the provided Go slice. Operations on the Go slice will also affect the returned OrderedDynamicArray.
It panics if slicePtr or lessFn is nil.
func WrapTransitiveOrderedSlice ¶
func WrapTransitiveOrderedSlice[Item constraints.TransitiveOrdered]( slicePtr *[]Item) OrderedDynamicArray[Item]
WrapTransitiveOrderedSlice wraps a pointer to Go slice to an OrderedDynamicArray.
It requires that the items of the slice must be transitive ordered. See github.com/donyori/gogo/constraints.TransitiveOrdered for details.
Operations on the returned OrderedDynamicArray will affect the provided Go slice. Operations on the Go slice will also affect the returned OrderedDynamicArray.
It panics if slicePtr is nil.
type SliceDynamicArray ¶
type SliceDynamicArray[Item any] []Item
SliceDynamicArray is a dynamic array wrapped on Go slice. *SliceDynamicArray implements the interface DynamicArray.
The client can convert a Go slice to SliceDynamicArray by type conversion, e.g.:
SliceDynamicArray[int]([]int{1, 2, 3})
Or allocate a new SliceDynamicArray by the slice literal or the built-in function make, e.g.:
SliceDynamicArray[int]{1, 2, 3} make(SliceDynamicArray[int], 2, 10)
func (*SliceDynamicArray[Item]) Append ¶
func (sda *SliceDynamicArray[Item]) Append(s sequence.Sequence[Item])
Append adds s to the back of the slice.
s shouldn't be modified during calling this method, otherwise, unknown error may occur.
func (*SliceDynamicArray[Item]) Back ¶
func (sda *SliceDynamicArray[Item]) Back() Item
Back returns the last item.
It panics if the slice is nil or empty.
func (*SliceDynamicArray[Item]) Cap ¶
func (sda *SliceDynamicArray[Item]) Cap() int
Cap returns the current capacity of the slice.
It returns 0 if the slice is nil.
func (*SliceDynamicArray[Item]) Clear ¶
func (sda *SliceDynamicArray[Item]) Clear()
Clear sets the slice to nil.
func (*SliceDynamicArray[Item]) Cut ¶
func (sda *SliceDynamicArray[Item]) Cut(begin, end int)
Cut removes items from argument begin (inclusive) to argument end (exclusive) of the slice.
It panics if begin or end is out of range, or begin > end.
func (*SliceDynamicArray[Item]) CutWithoutOrder ¶
func (sda *SliceDynamicArray[Item]) CutWithoutOrder(begin, end int)
CutWithoutOrder removes items from argument begin (inclusive) to argument end (exclusive) of the slice, without preserving order.
It panics if begin or end is out of range, or begin > end.
func (*SliceDynamicArray[Item]) Expand ¶
func (sda *SliceDynamicArray[Item]) Expand(i, n int)
Expand inserts n zero-value items to the front of the item with index i.
It panics if i is out of range, i.e., i < 0 or i > Len(), or n < 0.
func (*SliceDynamicArray[Item]) Extend ¶
func (sda *SliceDynamicArray[Item]) Extend(n int)
Extend adds n zero-value items to the back of the slice.
It panics if n < 0.
func (*SliceDynamicArray[Item]) Filter ¶
func (sda *SliceDynamicArray[Item]) Filter(filter func(x Item) (keep bool))
Filter refines items in the slice (in-place).
Its parameter filter is a function to report whether to keep the item x.
func (*SliceDynamicArray[Item]) Front ¶
func (sda *SliceDynamicArray[Item]) Front() Item
Front returns the first item.
It panics if the slice is nil or empty.
func (*SliceDynamicArray[Item]) Get ¶
func (sda *SliceDynamicArray[Item]) Get(i int) Item
Get returns the item with index i.
It panics if i is out of range.
func (*SliceDynamicArray[Item]) Insert ¶
func (sda *SliceDynamicArray[Item]) Insert(i int, x Item)
Insert adds x as the item with index i.
It panics if i is out of range, i.e., i < 0 or i > Len().
func (*SliceDynamicArray[Item]) InsertSequence ¶
func (sda *SliceDynamicArray[Item]) InsertSequence( i int, s sequence.Sequence[Item])
InsertSequence inserts s to the front of the item with index i.
It panics if i is out of range, i.e., i < 0 or i > Len().
s shouldn't be modified during calling this method, otherwise, unknown error may occur.
func (*SliceDynamicArray[Item]) Len ¶
func (sda *SliceDynamicArray[Item]) Len() int
Len returns the number of items in the slice.
It returns 0 if the slice is nil.
func (*SliceDynamicArray[Item]) Pop ¶
func (sda *SliceDynamicArray[Item]) Pop() Item
Pop removes and returns the last item.
It panics if the slice is nil or empty.
func (*SliceDynamicArray[Item]) Push ¶
func (sda *SliceDynamicArray[Item]) Push(x Item)
Push adds x to the back of the slice.
func (*SliceDynamicArray[Item]) Range ¶
func (sda *SliceDynamicArray[Item]) Range(handler func(x Item) (cont bool))
Range accesses the items in the slice from first to last. Each item is accessed once.
Its parameter handler is a function to deal with the item x in the slice and report whether to continue to access the next item.
func (*SliceDynamicArray[Item]) Remove ¶
func (sda *SliceDynamicArray[Item]) Remove(i int) Item
Remove removes and returns the item with index i.
It panics if i is out of range, i.e., i < 0 or i >= Len().
func (*SliceDynamicArray[Item]) RemoveWithoutOrder ¶
func (sda *SliceDynamicArray[Item]) RemoveWithoutOrder(i int) Item
RemoveWithoutOrder removes and returns the item with index i, without preserving order.
It panics if i is out of range, i.e., i < 0 or i >= Len().
func (*SliceDynamicArray[Item]) Reserve ¶
func (sda *SliceDynamicArray[Item]) Reserve(capacity int)
Reserve requests that the capacity of the slice is at least the specified capacity.
It does nothing if capacity <= Cap().
func (*SliceDynamicArray[Item]) Reverse ¶
func (sda *SliceDynamicArray[Item]) Reverse()
Reverse turns items in the slice the other way round.
func (*SliceDynamicArray[Item]) Set ¶
func (sda *SliceDynamicArray[Item]) Set(i int, x Item)
Set sets the item with index i to x.
It panics if i is out of range.
func (*SliceDynamicArray[Item]) SetBack ¶
func (sda *SliceDynamicArray[Item]) SetBack(x Item)
SetBack sets the last item to x.
It panics if the slice is nil or empty.
func (*SliceDynamicArray[Item]) SetFront ¶
func (sda *SliceDynamicArray[Item]) SetFront(x Item)
SetFront sets the first item to x.
It panics if the slice is nil or empty.
func (*SliceDynamicArray[Item]) Shrink ¶
func (sda *SliceDynamicArray[Item]) Shrink()
Shrink reduces the slice to fit, i.e., requests Cap() equals to Len().
Note that it isn't equivalent to operations on Go slice like s[:len(s):len(s)], because it allocates a new array and copies the content if Cap() > Len().
func (*SliceDynamicArray[Item]) Slice ¶
func (sda *SliceDynamicArray[Item]) Slice(begin, end int) Array[Item]
Slice returns a slice from argument begin (inclusive) to argument end (exclusive) of the slice, as an Array.
It panics if begin or end is out of range, or begin > end.
func (*SliceDynamicArray[Item]) Swap ¶
func (sda *SliceDynamicArray[Item]) Swap(i, j int)
Swap exchanges the items with indexes i and j.
It panics if i or j is out of range.
func (*SliceDynamicArray[Item]) Truncate ¶
func (sda *SliceDynamicArray[Item]) Truncate(i int)
Truncate removes the item with index i and all subsequent items.
It does nothing if i is out of range.