go-rust-std

collections

import "github.com/avivatedgi/go-rust-std/collections"

Index

func Dedup

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

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.

type Iterator

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 (*Iterator[T]) Close

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 (*Iterator[T]) IntoVector

func (ch *Iterator[T]) IntoVector() *Vec[T]

Convert an iterator into a vector of the same type.

func (*Iterator[T]) Push

func (ch *Iterator[T]) Push(value *T)

Push a value into the channel\, passing nil as the value will be ignored.

type Map

type Map[K comparable, V any] map[K]V

func (*Map[K\, V]) Clear

func (m *Map[K, V]) Clear()

Clears the map\, removing all key-value pairs.

func (Map[K\, V]) ContainsKey

func (m Map[K, V]) ContainsKey(key K) bool

Returns true if the map contains a value for the specified key.

func (*Map[K\, V]) Drain

func (m *Map[K, V]) Drain() Iterator[Pair[K, V]]

Clears the map\, returning all key-value pairs as an iterator.

func (*Map[K\, V]) Entry

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 (*Map[K\, V]) ForEach

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 (Map[K\, V]) Get

func (m Map[K, V]) Get(key K) option.Option[V]

Returns the value corresponding to the key.

func (Map[K\, V]) GetKeyValue

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 (*Map[K\, V]) Insert

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 (Map[K\, V]) Iter

func (m Map[K, V]) Iter() Iterator[Pair[K, V]]

Return the map iterator.

func (Map[K\, _]) Keys

func (m Map[K, _]) Keys() Iterator[K]

Retreive all the keys of the map.

func (Map[_\, V]) Values

func (m Map[_, V]) Values() Iterator[V]

Retreive all the values of the map

type MapEntry

This struct is constructed from the Entry method on Map.

type MapEntry[K comparable, V any] struct {
    // contains filtered or unexported fields
}

func (MapEntry[K\, V]) AndModify

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 (MapEntry[K\, V]) Key

func (m MapEntry[K, V]) Key() K

Returns this entry’s key.

func (MapEntry[K\, V]) OrDefault

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 (MapEntry[K\, V]) OrInsert

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 (MapEntry[K\, V]) OrInsertWith

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 (MapEntry[K\, V]) OrInsertWithKey

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.

type Pair

A struct that represents a pair of values.

type Pair[T any, U any] struct {
    First  T
    Second U
}

type Vec

type Vec[T any] []T

func (*Vec[T]) Append

func (vec *Vec[T]) Append(other *Vec[T])

Moves all the elements of other into vec\, leaving other empty.

func (Vec[T]) Capacity

func (vec Vec[T]) Capacity() int

Returns the number of elements the vector can hold without reallocating.

func (*Vec[T]) Clear

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[T]) DedupBy

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[T]) Drain

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[T]) Extend

func (vec *Vec[T]) Extend(other *Vec[T])

Appends all elements in a slice to the Vec.

func (*Vec[T]) Insert

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[T]) IsEmpty

func (vec Vec[T]) IsEmpty() bool

Returns true if the vector contains no elements.

func (*Vec[T]) Iter

func (vec *Vec[T]) Iter() Iterator[T]

Returns an Iterator to the vector elements.

func (Vec[T]) Len

func (vec Vec[T]) Len() int

Returns the number of elements in the vector\, also referred to as its ‘length’.

func (*Vec[T]) Pop

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[T]) Push

func (vec *Vec[T]) Push(item T)

Appends an element to the back of a collection.

func (*Vec[T]) Remove

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[T]) Resize

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[T]) ResizeWith

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[T]) Retain

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[T]) Splice

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[T]) SplitOff

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[T]) SwapRemove

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[T]) Truncate

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