collections

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = fmt.Errorf("not implemented")

Functions

func Equals

func Equals[T any](a, b T) bool

Equals compares two values of the same type. If the type implements the Comparable[T] interface, its Equals method is used for comparison. Otherwise, the standard comparison is used. Panics if the type is not comparable and does not implement Comparable[T].

Types

type Collection

type Collection[T any] interface {
	iterators.Iterable[T]

	// Add ensures that this collection contains the specified elements.
	// Returns true if the collection changed as a result of the operation.
	// (returns false if the collection does not support duplicates and already contained all given elements).
	Add(elements ...T) bool

	// AddAll adds all elements from the given collection to this one.
	// Returns true if the collection changed as a result of the operation.
	AddAll(c Collection[T]) bool

	// Clear removes all of the elements from this collection.
	Clear()

	// Returns true if this collection contains the specified element.
	Contains(element T) bool

	// ContainsAll returns true if this collection contains all of the elements in the specified collection.
	ContainsAll(c Collection[T]) bool

	// Compares the specified object with this collection for equality.
	Equals(c Collection[T]) bool

	// IsEmpty returns true if this collection contains no elements.
	IsEmpty() bool

	// Removes all given elements from the collection, if they are present.
	// Each specified element is removed only once, even if it is contained multiple times in the collection.
	// Returns true if the collection changed as a result of the operation.
	Remove(elements ...T) bool

	// RemoveAllOf removes all instances of the given elements from the collection.
	// Returns true if the collection changed as a result of the operation.
	RemoveAllOf(elements ...T) bool

	// Removes all of this collection's elements that are also contained in the specified collection.
	// Returns true if the collection changed as a result of the operation.
	RemoveAll(c Collection[T]) bool

	// RetainAll removes all elements from the collection that are not contained in the specified collection.
	// Returns true if the collection changed as a result of the operation.
	RetainAll(c Collection[T]) bool

	// RemoveIf removes all elements from the collection that satisfy the given predicate.
	// Returns true if the collection changed as a result of the operation.
	RemoveIf(filter Predicate[T]) bool

	// Size returns the number of elements in this collection.
	Size() int

	// ToSlice returns a slice containing the elements in this collection.
	ToSlice() []T

	// New returns a new Collection of the same type.
	New() Collection[T]
}

Collection represents a collection of elements. Note that T must be either comparable or implement the Comparable interface, otherwise runtime panics could occur.

type Comparable

type Comparable[T any] interface {
	// Equals returns true if the receiver and the argument are equal.
	Equals(T) bool
}

Comparable is an interface that can be implemented to use types as generic argument which don't satisfy the 'comparable' constraint.

type LinkedList

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

func NewLinkedList

func NewLinkedList[T any](elements ...T) *LinkedList[T]

func NewLinkedListFromCollection

func NewLinkedListFromCollection[T any](c Collection[T]) *LinkedList[T]

func (*LinkedList[T]) Add

func (l *LinkedList[T]) Add(elements ...T) bool

Add ensures that this collection contains the specified elements. Returns true if the collection changed as a result of the operation. (returns false if the collection does not support duplicates and already contained all given elements).

func (*LinkedList[T]) AddAll

func (l *LinkedList[T]) AddAll(c Collection[T]) bool

AddAll adds all elements from the given collection to this one. Returns true if the collection changed as a result of the operation.

func (*LinkedList[T]) AddIndex

func (l *LinkedList[T]) AddIndex(element T, idx int) error

func (*LinkedList[T]) Clear

func (l *LinkedList[T]) Clear()

Clear removes all of the elements from this collection.

func (*LinkedList[T]) Element

func (l *LinkedList[T]) Element() (T, error)

func (*LinkedList[T]) Fetch

func (l *LinkedList[T]) Fetch() (T, error)

func (*LinkedList[T]) Get

func (l *LinkedList[T]) Get(idx int) (T, error)

func (*LinkedList[T]) Iterator

func (l *LinkedList[T]) Iterator() iterators.Iterator[T]

Returns an iterator over the elements in this collection.

func (*LinkedList) MarshalJSON

func (al *LinkedList) MarshalJSON() ([]byte, error)

func (*LinkedList[T]) New

func (l *LinkedList[T]) New() Collection[T]

New returns a new Collection of the same type.

func (*LinkedList[T]) Peek

func (l *LinkedList[T]) Peek() T

func (*LinkedList[T]) Poll

func (l *LinkedList[T]) Poll() T

func (*LinkedList[T]) Push

func (l *LinkedList[T]) Push(elements ...T) error

func (*LinkedList[T]) Remove

func (l *LinkedList[T]) Remove(elements ...T) bool

Removes all given elements from the collection, if they are present. Each specified element is removed only once, even if it is contained multiple times in the collection. Returns true if the collection changed as a result of the operation.

func (*LinkedList[T]) RemoveAllOf

func (l *LinkedList[T]) RemoveAllOf(elements ...T) bool

RemoveAllOf removes all instances of the given elements from the collection. Returns true if the collection changed as a result of the operation.

func (*LinkedList[T]) RemoveIf

func (l *LinkedList[T]) RemoveIf(filter Predicate[T]) bool

RemoveIf removes all elements from the collection that satisfy the given predicate. Returns true if the collection changed as a result of the operation.

func (*LinkedList[T]) RemoveIndex

func (l *LinkedList[T]) RemoveIndex(idx int) error

func (*LinkedList[T]) Size

func (l *LinkedList[T]) Size() int

Size returns the number of elements in this collection.

func (*LinkedList[T]) ToSlice

func (l *LinkedList[T]) ToSlice() []T

ToSlice returns a slice containing the elements in this collection.

func (*LinkedList) UnmarshalJSON

func (al *LinkedList) UnmarshalJSON(data []byte) error

type List

type List[T any] interface {
	Collection[T]
	json.Marshaler
	json.Unmarshaler

	// AddIndex adds the given element at the specified index.
	// Returns an IndexOutOfBoundsError if the index is not within the list's size or equal to l.Size().
	AddIndex(element T, idx int) error

	// RemoveIndex removes the element at the specified index.
	// Returns an IndexOutOfBoundsError if the index is not within the list's size.
	RemoveIndex(idx int) error

	// Get returns the element at the specified index.
	// Returns an IndexOutOfBoundsError if the index is not within the list's size.
	Get(idx int) (T, error)
}

type Predicate

type Predicate[T any] func(T) bool

type Queue

type Queue[T any] interface {
	Collection[T]

	// Push adds the given elements to the queue.
	// Returns an error, if the queue's size restriction prevents the elements from being added.
	Push(elements ...T) error
	// Peek returns the first element without removing it.
	// Returns T's zero value, if the queue is empty.
	Peek() T
	// Element returns the first element without removing it.
	// Returns an error, if the queue is empty.
	Element() (T, error)
	// Poll returns the first element and removes it from the queue.
	// Returns T's zero value, if the queue is empty.
	Poll() T
	// Fetch returns the first element and removes it from the queue.
	// Returns an error, if the queue is empty.
	Fetch() (T, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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