ds

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: BSD-2-Clause, ISC Imports: 0 Imported by: 4

Documentation

Overview

Package containers provides core interfaces and functions for data structures.

Container is the base interface for all data structures to implement.

Iterators provide stateful iterators.

Enumerable provides Ruby inspired (each, select, map, find, any?, etc.) container functions.

Serialization provides serializers (marshalers) and deserializers (unmarshalers).

Index

Constants

View Source
const (
	CanOnlyCompareEqualIteratorTypes = "Can only compare iterators of equal concrete type"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BackwardIterator

type BackwardIterator interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// Next moves the iterator backward  by one position.
	Previous() bool

	// NextN moves the iterator backward by n positions.
	PreviousN(n int) bool
}

BackwardIterator defines an Iterator, which can be moved backward according to the indexes ordering.

type BidirectionalIterator

type BidirectionalIterator interface {
	// *********************    Inherited methods    ********************//
	ForwardIterator
	BackwardIterator

	// Next moves the iterator forward/backward by n positions.
	MoveBy(n int) bool
}

BidirectionalIterator defines a ForwardIterator and BackwardIterator, which can be moved forward and backward according to the underlying data structure's ordering.

type BoundsSentinel added in v0.6.0

type BoundsSentinel int
const (
	SentinelBegin BoundsSentinel = iota
	SentinelEnd
	SentinelFirst
	SentinelLast
	SentinelInRange
)

type CollectionIterator

type CollectionIterator[TKey any] interface {
	// *********************    Inherited methods    ********************//
	SizedIterator
	IndexedIterator[TKey]
}

CollectionIterator defines a SizedIterator, which can be said to reference a collection of elements.

type CompIndexIterator added in v0.6.0

type CompIndexIterator[TKey any] interface {
	IndexedIterator[TKey]
	ComparableIterator
}

type ComparableIterator

type ComparableIterator interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// IsEqualTo checks if the iterator's position is said to be equal to other's position.
	IsEqual(other ComparableIterator) bool
}

ComparableIterator defines an Iterator, which can be said to be in a position equal to other's position.

type Container

type Container[TValue any] interface {
	IsEmpty() bool
	Size() int
	Clear()
	GetValues() []TValue
	ToString() string
}

Container defines the minimum functionality required for all other containers.

type ForwardIterator

type ForwardIterator interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// Next moves the iterator forward by one position.
	Next() bool

	// NextN moves the iterator forward by n positions.
	NextN(i int) bool
}

ForwardIterator defines an ordered Iterator, which can be moved forward according to the indexes ordering.

type IndexedIterator added in v0.6.0

type IndexedIterator[TKey any] interface {
	// *********************    Inherited methods    ********************//
	SizedIterator

	// Index returns the index of the iterator's position in the collection.
	Index() (int, bool)
	// GetKey returns the key of the iterator's position in the collection.
	GetKey() (TKey, bool)
}

IndexedIterator defines an Iterator, which defines an an iterator with an index and (optionally different) key.

type Iterator

type Iterator interface {
	// IsBegin checks if the iterator is pointing to one element before it's first element, unless Next() is called, the iterator is in an invalid state.
	IsBegin() bool
	// IsEnd checks if the iterator is pointing to one element past it's last element, unless Previous() is called, the iterator is in an invalid state.
	IsEnd() bool

	// IsFirst checks if the iterator is pointing to it's first element, when Next() is called, the iterator is in an invalid state.
	IsFirst() bool
	// IsBegin checks if the iterator is pointing to it's last element, when Previous() is called, the iterator is in an invalid state.
	IsLast() bool

	// IsValid checks if the iterator is in a valid position and not e.g. out of bounds.
	IsValid() bool
}

Iterator defines the minimum functionality required for all other iterators.

type IteratorWithIndex added in v0.6.0

type IteratorWithIndex[TKey any, TValue any] interface {
	// Next moves the iterator to the next element and returns true if there was a next element in the container.
	// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
	// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
	// Modifies the state of the iterator.
	Next() bool

	// Value returns the current element's value.
	// Does not modify the state of the iterator.
	Value() TValue

	// Index returns the current element's index.
	// Does not modify the state of the iterator.
	Index() int

	// Begin resets the iterator to its initial state (one-before-first)
	// Call Next() to fetch the first element if any.
	Begin()

	// First moves the iterator to the first element and returns true if there was a first element in the container.
	// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	First() bool

	// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
	// passed function, and returns true if there was a next element in the container.
	// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	NextTo(func(index int, value TValue) bool) bool
}

IteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.

type IteratorWithKey added in v0.6.0

type IteratorWithKey[TKey any, TValue any] interface {
	// Next moves the iterator to the next element and returns true if there was a next element in the container.
	// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
	// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
	// Modifies the state of the iterator.
	Next() bool

	// Value returns the current element's value.
	// Does not modify the state of the iterator.
	Value() TValue

	// Key returns the current element's key.
	// Does not modify the state of the iterator.
	Key() TKey

	// Begin resets the iterator to its initial state (one-before-first)
	// Call Next() to fetch the first element if any.
	Begin()

	// First moves the iterator to the first element and returns true if there was a first element in the container.
	// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	First() bool

	// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
	// passed function, and returns true if there was a next element in the container.
	// If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	NextTo(func(key TKey, value TValue) bool) bool
}

IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.

type JSONDeserializer

type JSONDeserializer interface {
	// FromJSON populates containers's elements from the input JSON representation.
	FromJSON([]byte) error
	// UnmarshalJSON @implements json.Unmarshaler
	UnmarshalJSON([]byte) error
}

JSONDeserializer provides JSON deserialization.

type JSONSerializer

type JSONSerializer interface {
	// ToJSON outputs the JSON representation of containers's elements.
	ToJSON() ([]byte, error)
	// MarshalJSON @implements json.Marshaler
	MarshalJSON() ([]byte, error)
}

JSONSerializer provides JSON serialization.

type OrderedIterator

type OrderedIterator interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// IsBefore checks if the iterator's position is said to come before other's position.
	IsBefore(other OrderedIterator) bool
	// IsBefore checks if the iterator's position is said to come after other's position.
	IsAfter(other OrderedIterator) bool

	// DistanceTo returns the signed distance of the iterator's position to other's position.
	DistanceTo(other OrderedIterator) int
}

OrderedIterator defines an Iterator, which can be said to be in a position before or after others's position.

type RandomAccessIterator

type RandomAccessIterator[TKey any] interface {
	// *********************    Inherited methods    ********************//
	CollectionIterator[TKey]

	// MoveTo moves the iterator to the given index, if it is reachable.
	MoveTo(i int) bool
	MoveToKey(i TKey) bool
}

RandomAccessIterator defines a CollectionIterator, which can be moved to every position in the iterable direction. A RandomAccessIterator does not imply bidirectional iteration.

type RandomAccessReadableIterator

type RandomAccessReadableIterator[TKey any, TValue any] interface {
	// *********************    Inherited methods    ********************//
	RandomAccessIterator[TKey]
	ReadableIterator[TValue]

	// GetAt returns the value at the given index of the iterator.
	GetAt(i int) (value TValue, found bool)

	// GetAtKey returns the value at the given key of the iterator.
	GetAtKey(i TKey) (value TValue, found bool)
}

RandomAccessReadableIterator defines a RandomAccessIterator and ReadableIterator, which can read from every index in the iterator.

type RandomAccessWriteableIterator

type RandomAccessWriteableIterator[TKey any, TValue any] interface {
	// *********************    Inherited methods    ********************//
	RandomAccessIterator[TKey]
	WritableIterator[TValue]

	// GetAt sets the value at the given index of the iterator.
	SetAt(i int, value TValue) bool

	// GetAtKey sets the value at the given key of the iterator.
	SetAtKey(i TKey, value TValue) bool
}

RandomAccessWriteableIterator defines a RandomAccessIterator and WritableIterator, which can write from every index in the iterator.

type ReadCompForIndexIterator added in v0.6.0

type ReadCompForIndexIterator[TKey any, TValue any] interface {
	ReadableIterator[TValue]
	IndexedIterator[TKey]
	ComparableIterator
	ForwardIterator
}

type ReadCompForIterator added in v0.6.0

type ReadCompForIterator[TValue any] interface {
	ReadableIterator[TValue]
	ComparableIterator
	ForwardIterator
}

type ReadCompIterator added in v0.6.0

type ReadCompIterator[TValue any] interface {
	ReadableIterator[TValue]
	ComparableIterator
}

type ReadForIndexIterator added in v0.22.0

type ReadForIndexIterator[TKey any, TValue any] interface {
	ReadableIterator[TValue]
	ForwardIterator
	IndexedIterator[TKey]
}

type ReadForIterator added in v0.6.0

type ReadForIterator[TValue any] interface {
	ReadableIterator[TValue]
	ForwardIterator
}

type ReadWriteCompForRandCollIterator added in v0.4.0

type ReadWriteCompForRandCollIterator[TKey any, TValue any] interface {
	ComparableIterator
	CollectionIterator[TKey]
	ForwardIterator
	RandomAccessReadableIterator[TKey, TValue]
	RandomAccessWriteableIterator[TKey, TValue]
}

type ReadWriteOrdCompBidRandCollIterator added in v0.4.0

type ReadWriteOrdCompBidRandCollIterator[TKey any, TValue any] interface {
	ComparableIterator
	OrderedIterator
	BidirectionalIterator
	RandomAccessReadableIterator[TKey, TValue]
	RandomAccessWriteableIterator[TKey, TValue]
}

type ReadWriteOrdCompForRandCollIterator added in v0.7.0

type ReadWriteOrdCompForRandCollIterator[TKey any, TValue any] interface {
	OrderedIterator
	ComparableIterator
	ForwardIterator
	ReadableIterator[TValue]
	WritableIterator[TValue]
	RandomAccessIterator[TKey]
}

type ReadableIterator

type ReadableIterator[TValue any] interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// Get returns the value at the iterator's position.
	// found will be false if the iterator is in an invalid state or the collection is empty.
	Get() (value TValue, found bool)
}

ReadableIterator defines an Iterator, which can be used to read the underlying values.

type ReverseIteratorWithIndex added in v0.6.0

type ReverseIteratorWithIndex[TKey any, TValue any] interface {
	// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
	// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	Prev() bool

	// End moves the iterator past the last element (one-past-the-end).
	// Call Prev() to fetch the last element if any.
	End()

	// Last moves the iterator to the last element and returns true if there was a last element in the container.
	// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	Last() bool

	// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
	// passed function, and returns true if there was a next element in the container.
	// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	PrevTo(func(index int, value TValue) bool) bool

	IteratorWithKey[TKey, TValue]
}

ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.

Essentially it is the same as IteratorWithIndex, but provides additional:

Prev() function to enable traversal in reverse

Last() function to move the iterator to the last element.

End() function to move the iterator past the last element (one-past-the-end).

type ReverseIteratorWithKey added in v0.6.0

type ReverseIteratorWithKey[TKey any, TValue any] interface {
	// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
	// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	Prev() bool

	// End moves the iterator past the last element (one-past-the-end).
	// Call Prev() to fetch the last element if any.
	End()

	// Last moves the iterator to the last element and returns true if there was a last element in the container.
	// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	Last() bool

	// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
	// passed function, and returns true if there was a next element in the container.
	// If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	PrevTo(func(key TKey, value TValue) bool) bool

	IteratorWithKey[TKey, TValue]
}

ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.

Essentially it is the same as IteratorWithKey, but provides additional:

Prev() function to enable traversal in reverse

Last() function to move the iterator to the last element.

type SizedIterator

type SizedIterator interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// Size returns the number of elements in the iterator.
	Size() int
}

SizedIterator defines an Iterator, which can be said to have a fixed size.

type WritableIterator

type WritableIterator[TValue any] interface {
	// *********************    Inherited methods    ********************//
	Iterator

	// Set sets the value at the iterator's position.
	Set(value TValue) bool
}

WritableIterator defines an Iterator, which can be used to write to the underlying values.

Jump to

Keyboard shortcuts

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