collections

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewStackIsEmptyNativeError

func NewStackIsEmptyNativeError() error

NewStackIsEmptyNativeError creates an untranslated error to indicate stack is empty (internal error)

Types

type Iterator added in v0.4.0

type Iterator[T any] interface {
	IteratorCtrl[T]

	// Start returns the first element of the sequence and moves the current
	// position to the next item.
	Start() T

	// Next moves the iterator to the next item in the sequence then returns
	// that item. If the iterator is already at the end, then the zero
	// value of T is returned. However, when used properly, this situation
	// should never occur, as Valid would indicate that iteration with
	// Next should no longer occur.
	Next() T

	// Reset is designed to be used in high frequency applications. The client
	// can reuse this iterator for a new collection rather that having to throw this
	// instance away and create a new one. This helps to reduce the number of
	// allocations in a high frequency application.
	Reset(entries []T)
}

Iterator represents an iterator over a slice. The underlying slice may be empty. When created, the iterator does not point to a valid slice entry. To begin iteration, the client invokes Start. At any stage after the iteration has started, the iterator, points to the current item in the sequence. The client can query the validity of the current item using the Valid method. To obtain successive elements, the client invokes the Next method and this can be continued until Valid returns false. Once Valid returns false, the client should no longer call Next. Doing so in this scenario, returns the zero value for the value T. It should also be noted that the iterator does not in itself implement the loop operation, it merely provides the logic to enable the client to implement the looping operation.

func ForwardIt added in v0.5.0

func ForwardIt[T any](elements []T, zero T) Iterator[T]

ForwardIt creates a forward iterator over a non empty slice. If the provided slice is empty, then a nil iterator is returned.

The zero value represents the value that is returned if the Next method on the iterator is incorrectly invoked after Valid has returned false. If the collection contains interfaces, or pointers just pass in nil as the zero value.

If the collection contains scalars, pass in 0 cast to the appropriate type, eg int32(0). It doesn't matter if 0 is a valid value in the collection, because this value is only ever return in an invalid scenario, ie, calling Next after Valid has returned false. This is preferable than generating a panic. If the collection contains structs, then pass in an empty struct as the nil value.

func ReverseIt added in v0.4.0

func ReverseIt[T any](elements []T, zero T) Iterator[T]

ReverseIt creates a reverse iterator over a non empty slice. If the provided slice is empty, then a nil iterator is returned. (NB: please remember to check for a nil interface correctly; see the helper function IsNil in utils).

type IteratorCtrl added in v0.4.0

type IteratorCtrl[T any] interface {
	Valid() bool
}

IteratorCtrl represents a narrow view of the iterator that exposes just the method, Valid which indicates when all items in the sequence have been consumed. The purpose of this is to allow a client abstraction, which implements the looping logic to provide a condition which halts its iteration. There are 2 scenarios: 1) the client wants to iterate the entire sequence; in this case the client just needs to continue the iteration until Valid returns false. 2) the client only wants to iterate the sequence until a certain other condition arises; in this case, the client combines the result of Valid() with another predicate within the for statement.

type OrderedKeysMap added in v0.4.0

type OrderedKeysMap[K cmp.Ordered, V any] map[K]V

OrderedKeysMap provides a deterministic way to iterate the contents of a map. The problem with a regular iteration is that the order the elements are presented is not guaranteed. This can be detrimental when consistency is paramount. Any map that needs to be iterated deterministically, should use the Keys() function for the iteration loop and obtain the value using this key to index into the map.

func (*OrderedKeysMap[K, V]) Keys added in v0.4.0

func (m *OrderedKeysMap[K, V]) Keys() []K

type RunEach added in v0.5.0

type RunEach[T any, R any] func(T) R

RunEach is a client defined function which is invoked for each element in the sequence.

type RunWhile added in v0.5.0

type RunWhile[T any, R any] func(T, R) bool

RunWhile is a client defined function that denotes that the iteration can continue. When it returns false, iteration lapses.

type RunnableIterator added in v0.5.0

type RunnableIterator[T any, R any] interface {
	Iterator[T]

	// RunAll invokes the predicate for every member of the sequence, while
	// Valid and while both holds true.
	RunAll(each RunEach[T, R], while RunWhile[T, R])
}

RunnableIterator implements a looping mechanism for the Iterator. The runnable version is intended to make iteration just that little bit easier. The key feature of the runnable iterator is to be able to process a sequence while a certain condition holds true (defined, by the client using the while function.) The runnable iterator passes the return value for that particular item, to the while function, along with the item itself, so that it can define logic based on the return result. If the client always want to invoke all items in a sequence, then the better solution would be just to use a standard for/range statement, rather than use this iterator.

func ForwardRunIt added in v0.5.0

func ForwardRunIt[T any, R any](elements []T, zero T) RunnableIterator[T, R]

ForwardRunIt creates a forward runnable iterator

func ReverseRunIt added in v0.5.0

func ReverseRunIt[T any, R any](elements []T, zero T) RunnableIterator[T, R]

ReverseRunIt creates a reverse runnable iterator

type Stack

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

Stack

func NewStack

func NewStack[T any]() *Stack[T]

NewStack

func NewStackWith

func NewStackWith[T any](with []T) *Stack[T]

func (*Stack[T]) Content added in v0.1.7

func (s *Stack[T]) Content() []T

Content

func (*Stack[T]) Current

func (s *Stack[T]) Current() (T, error)

Current

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty

func (*Stack[T]) MustPop

func (s *Stack[T]) MustPop() T

MustPop

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, error)

Pop

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Push

func (*Stack[T]) Size

func (s *Stack[T]) Size() uint

Size

Jump to

Keyboard shortcuts

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