import "github.com/avivatedgi/go-rust-std/collections"
func Dedup[T comparable](vec *Vec[T])
Removes consecutive repeated elements in the vector. If the vector is sorted\, this removes all duplicates.
NOTE: This function isn’t a method of the vector because it can only work on comparable types\, and I didn’t wanted to limit the Vector structure to hold only comparable types.
func DedupByKey[T comparable](vec *Vec[T], key func(T) T)
Removes all but the first of consecutive elements in the vector that resolve to the same key. If the vector is sorted\, this removes all duplicates.
NOTE: This function isn’t a method of the vector because it can only work on comparable types\, and I didn’t wanted to limit the Vector structure to hold only comparable types.
A channel based iterator\, used to abstarct a channel as an iterator. Usage example (taken from collections.Vector[T]): for value := range vec.Iter() { fmt.Println(value) }
type Iterator[T any] chan T
func (ch *Iterator[T]) Close()
Close the channel\, this function MUST be called after we are done use Push for all of the values we want to iterate.
func (ch *Iterator[T]) IntoVector() *Vec[T]
Convert an iterator into a vector of the same type.
func (ch *Iterator[T]) Push(value *T)
Push a value into the channel\, passing nil as the value will be ignored.
type Map[K comparable, V any] map[K]V
func (m *Map[K, V]) Clear()
Clears the map\, removing all key-value pairs.
func (m Map[K, V]) ContainsKey(key K) bool
Returns true if the map contains a value for the specified key.
func (m *Map[K, V]) Drain() Iterator[Pair[K, V]]
Clears the map\, returning all key-value pairs as an iterator.
func (m *Map[K, V]) Entry(key K) MapEntry[K, V]
Gets the given key’s corresponding entry in the map for in-place manipulation. WARNING: In difference from rust\, this method does not return a reference to the value (!). But\, it does match the signatures of MapEntry in rust.
func (m *Map[K, V]) ForEach(f func(*K, *V) bool)
Executes the f function once for each map entry. There is an option to stop the iteration in the middle\, if the handler function returns false.
func (m Map[K, V]) Get(key K) option.Option[V]
Returns the value corresponding to the key.
func (m Map[K, V]) GetKeyValue(key K) option.Option[Pair[K, V]]
Returns the key-value pair corresponding to the supplied key in a Pair (first is key\, second is value).
func (m *Map[K, V]) Insert(key K, value V) option.Option[V]
Inserts a key-value pair into the map. If the map did not have this key present\, option.None is returned. If the map did have this key present\, the value is updated\, and the old value is returned. The key is not updated\, though;
func (m Map[K, V]) Iter() Iterator[Pair[K, V]]
Return the map iterator.
func (m Map[K, _]) Keys() Iterator[K]
Retreive all the keys of the map.
func (m Map[_, V]) Values() Iterator[V]
Retreive all the values of the map
This struct is constructed from the Entry method on Map.
type MapEntry[K comparable, V any] struct {
// contains filtered or unexported fields
}
func (m MapEntry[K, V]) AndModify(f func(*V)) MapEntry[K, V]
Provides access to an occupied entry before any potential inserts into the map.
func (m MapEntry[K, V]) Key() K
Returns this entry’s key.
func (m MapEntry[K, V]) OrDefault() V
Ensures a value is in the entry by inserting the default value if empty. Returns the entry’s value.
func (m MapEntry[K, V]) OrInsert(value V) V
Ensures a value is in the entry by inserting the default if empty. Returns the entry’s value.
func (m MapEntry[K, V]) OrInsertWith(f func() V) V
Ensures a value is in the entry by inserting the result of the default function if empty. Returns the entry’s value.
func (m MapEntry[K, V]) OrInsertWithKey(f func(K) V) V
Ensures a value is in the entry by inserting\, if empty\, the result of the default function.
A struct that represents a pair of values.
type Pair[T any, U any] struct {
First T
Second U
}
type Vec[T any] []T
func (vec *Vec[T]) Append(other *Vec[T])
Moves all the elements of other into vec\, leaving other empty.
func (vec Vec[T]) Capacity() int
Returns the number of elements the vector can hold without reallocating.
func (vec *Vec[T]) Clear()
Clears the vector\, removing all values. Note that this method has no effect on the allocated capacity of the vector.
func (vec *Vec[T]) DedupBy(f func(T, T) bool)
Removes all but the first of consecutive elements in the vector satisfying a given equality relation. The f function is passed the two elements from the vector and must determine if the elements compare equal. The elements are passed in opposite order from their order in the slice\, so if f(a\, b) returns true\, a is removed.
func (vec *Vec[T]) Drain(start, end int) Iterator[T]
Removes the specified range from the vector in bulk\, returning all removed elements as an Iterator.
func (vec *Vec[T]) Extend(other *Vec[T])
Appends all elements in a slice to the Vec.
func (vec *Vec[T]) Insert(index int, item T)
Inserts an element at position index within the vector\, shifting all elements after it to the right. Panics if index > len.
func (vec Vec[T]) IsEmpty() bool
Returns true if the vector contains no elements.
func (vec *Vec[T]) Iter() Iterator[T]
Returns an Iterator to the vector elements.
func (vec Vec[T]) Len() int
Returns the number of elements in the vector\, also referred to as its ‘length’.
func (vec *Vec[T]) Pop() option.Option[T]
Removes the last element from a vector and returns it\, or None if it is empty.
func (vec *Vec[T]) Push(item T)
Appends an element to the back of a collection.
func (vec *Vec[T]) Remove(index int) T
Removes and returns the element at position index within the vector\, shifting all elements after it to the left. Note: Because this shifts over the remaining elements\, it has a worst-case performance of O(n). If you don’t need the order of elements to be preserved\, use SwapRemove instead. Panics if index is out of bounds.
func (vec *Vec[T]) Resize(newLength int, value T)
Resizes the Vec in-place so that len is equal to newLength. If newLength is greater than len\, the Vec is extended by the difference\, with each additional slot filled with value. If newLength is less than len\, the Vec is simply truncated. Panics if index is less than zero
func (vec *Vec[T]) ResizeWith(newLength int, f func() T)
Resizes the Vec in-place so that len is equal to newLength. If newLength is greater than len\, the Vec is extended by the difference\, with each additional slot filled with the result of calling the function f. The return values from f will end up in the Vec in the order they have been generated.
If new_len is less than len\, the Vec is simply truncated. Panics if index is less than zero
func (vec *Vec[T]) Retain(f func(T) bool)
Retains only the elements specified by the predicate. In other words\, remove all elements e such that f(&e) returns false. This method operates in place\, visiting each element exactly once in the original order\, and preserves the order of the retained elements.
func (vec *Vec[T]) Splice(start, end int, replaceWith Vec[T]) Vec[T]
Creates a splicing vector that replaces the specified range in the vector with the given replaceWith vector and returns the removed items. replaceWith does not need to be the same length as range. range is removed even if the vector is not consumed until the end.
func (vec Vec[T]) SplitOff(at int) Vec[T]
Splits the collection into two at the given index. Returns a newly allocated vector containing the elements in the range [at\, len]. After the call\, the original vector will be left containing the elements [0\, at] with its previous capacity unchanged.
func (vec *Vec[T]) SwapRemove(index int) T
Removes an element from the vector and returns it. The removed element is replaced by the last element of the vector. This does not preserve ordering\, but is O(1). If you need to preserve the element order\, use remove instead. Panics if index is out of bounds.
func (vec *Vec[T]) Truncate(len int)
Shortens the vector\, keeping the first len elements and dropping the rest. If len is greater than the vector’s current length\, this has no effect. The drain method can emulate truncate\, but causes the excess elements to be returned instead of dropped. Note that this method has no effect on the allocated capacity of the vector. Panics if index is negative.
Generated by gomarkdoc