containers

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteComparator

func ByteComparator(a, b interface{}) int

ByteComparator provides a basic comparison on byte

func Float32Comparator

func Float32Comparator(a, b interface{}) int

Float32Comparator provides a basic comparison on float32

func Float64Comparator

func Float64Comparator(a, b interface{}) int

Float64Comparator provides a basic comparison on float64

func GetSortedValues

func GetSortedValues(container Container, comparator Comparator) []interface{}

GetSortedValues returns sorted container's elements with respect to the passed comparator. Does not effect the ordering of elements within the container.

func Int16Comparator

func Int16Comparator(a, b interface{}) int

Int16Comparator provides a basic comparison on int16

func Int32Comparator

func Int32Comparator(a, b interface{}) int

Int32Comparator provides a basic comparison on int32

func Int64Comparator

func Int64Comparator(a, b interface{}) int

Int64Comparator provides a basic comparison on int64

func Int8Comparator

func Int8Comparator(a, b interface{}) int

Int8Comparator provides a basic comparison on int8

func IntComparator

func IntComparator(a, b interface{}) int

IntComparator provides a basic comparison on int

func RuneComparator

func RuneComparator(a, b interface{}) int

RuneComparator provides a basic comparison on rune

func Sort

func Sort(values []interface{}, comparator Comparator)

Sort sorts values (in-place) with respect to the given comparator.

Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices).

func StringComparator

func StringComparator(a, b interface{}) int

StringComparator provides a fast comparison on strings

func TimeComparator

func TimeComparator(a, b interface{}) int

TimeComparator provides a basic comparison on time.Time

func UInt16Comparator

func UInt16Comparator(a, b interface{}) int

UInt16Comparator provides a basic comparison on uint16

func UInt32Comparator

func UInt32Comparator(a, b interface{}) int

UInt32Comparator provides a basic comparison on uint32

func UInt64Comparator

func UInt64Comparator(a, b interface{}) int

UInt64Comparator provides a basic comparison on uint64

func UInt8Comparator

func UInt8Comparator(a, b interface{}) int

UInt8Comparator provides a basic comparison on uint8

func UIntComparator

func UIntComparator(a, b interface{}) int

UIntComparator provides a basic comparison on uint

Types

type Comparator

type Comparator func(a, b interface{}) int

Comparator will make type assertion (see IntComparator for example), which will panic if a or b are not of the asserted type.

Should return a number:

negative , if a < b
zero     , if a == b
positive , if a > b

type Container

type Container interface {
	Empty() bool
	Size() int
	Clear()
	Values() []interface{}
}

Container is base interface that all data structures implement.

type IteratorWithIndex

type IteratorWithIndex 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() interface{}

	// 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
}

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

type IteratorWithKey

type IteratorWithKey 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() interface{}

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

	// 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
}

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

type ReverseIteratorWithIndex

type ReverseIteratorWithIndex 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

	IteratorWithIndex
}

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

type ReverseIteratorWithKey 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

	IteratorWithKey
}

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.

Jump to

Keyboard shortcuts

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