ListLike

package
v0.2.17 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ListLike provides a Lister interface that defines methods for a list data structure.

Package errors provides error handling for list operations. It includes error types for empty and full list conditions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayList

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

ArrayList is a generic type that represents a list data structure with or without a limited capacity. It is implemented using an array.

func NewArrayList

func NewArrayList[T any](values ...T) *ArrayList[T]

NewArrayList is a function that creates and returns a new instance of a ArrayList.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the list.

Returns:

  • *ArrayList[T]: A pointer to the newly created ArrayList.

func (*ArrayList[T]) Append

func (list *ArrayList[T]) Append(value T) error

Append is a method of the ArrayList type. It is used to add an element to the end of the list.

Panics with an error of type *ErrFullList if the list is full.

Parameters:

  • value: A pointer to an element of type T to be added to the list.

func (*ArrayList[T]) Capacity

func (list *ArrayList[T]) Capacity() optional.Int

Capacity is a method of the ArrayList type. It returns the maximum number of elements the list can hold.

Returns:

  • optional.Int: An optional integer that represents the maximum number of elements the list can hold.

func (*ArrayList[T]) Clear

func (list *ArrayList[T]) Clear()

Clear is a method of the ArrayList type. It is used to remove all elements from the list.

func (*ArrayList[T]) Copy added in v0.2.15

func (list *ArrayList[T]) Copy() itf.Copier

Copy is a method of the ArrayList type. It is used to create a shallow copy of the list.

Returns:

  • itf.Copier: A copy of the list.

func (*ArrayList[T]) CutNilValues

func (list *ArrayList[T]) CutNilValues()

CutNilValues is a method of the ArrayList type. It is used to remove all nil values from the list.

func (*ArrayList[T]) DeleteFirst

func (list *ArrayList[T]) DeleteFirst() (T, error)

DeleteFirst is a method of the ArrayList type. It is used to remove and return the first element in the list.

Panics with an error of type *ErrInvalidOperation if the list is empty.

Returns:

  • T: The first element in the list.

func (*ArrayList[T]) DeleteLast

func (list *ArrayList[T]) DeleteLast() (T, error)

DeleteLast is a method of the ArrayList type. It is used to remove and return the last element in the list.

Panics with an error of type *ErrInvalidOperation if the list is empty.

Returns:

  • T: The last element in the list.

func (*ArrayList[T]) IsEmpty

func (list *ArrayList[T]) IsEmpty() bool

IsEmpty is a method of the ArrayList type. It checks if the list is empty.

Returns:

  • bool: A boolean value that is true if the list is empty, and false otherwise.

func (*ArrayList[T]) IsFull

func (list *ArrayList[T]) IsFull() (isFull bool)

IsFull is a method of the ArrayList type. It checks if the list is full.

Returns:

  • isFull: A boolean value that is true if the list is full, and false otherwise.

func (*ArrayList[T]) Iterator

func (list *ArrayList[T]) Iterator() itf.Iterater[T]

Iterator is a method of the ArrayList type. It returns an iterator for the list.

Returns:

  • itf.Iterater[T]: An iterator for the list.

func (*ArrayList[T]) PeekFirst

func (list *ArrayList[T]) PeekFirst() (T, error)

PeekFirst is a method of the ArrayList type. It is used to return the first element in the list without removing it.

Panics with an error of type *ErrInvalidOperation if the list is empty.

Returns:

  • T: A pointer to the first element in the list.

func (*ArrayList[T]) PeekLast

func (list *ArrayList[T]) PeekLast() (T, error)

PeekLast is a method of the ArrayList type. It is used to return the last element in the list without removing it.

Panics with an error of type *ErrInvalidOperation if the list is empty.

Returns:

  • T: The last element in the list.

func (*ArrayList[T]) Prepend

func (list *ArrayList[T]) Prepend(value T) error

Prepend is a method of the ArrayList type. It is used to add an element to the end of the list.

Panics with an error of type *ErrFullList if the list is full.

Parameters:

  • value: A pointer to an element of type T to be added to the list.

func (*ArrayList[T]) Size

func (list *ArrayList[T]) Size() int

Size is a method of the ArrayList type. It returns the number of elements in the list.

Returns:

  • int: An integer that represents the number of elements in the list.

func (*ArrayList[T]) Slice added in v0.2.10

func (list *ArrayList[T]) Slice() []T

Slice is a method of the ArrayList type that returns a slice of type T containing the elements of the list.

Returns:

  • []T: A slice of type T containing the elements of the list.

func (*ArrayList[T]) String

func (list *ArrayList[T]) String() string

String is a method of the ArrayList type. It returns a string representation of the list with information about its size, capacity, and elements.

Returns:

  • string: A string representation of the list.

func (*ArrayList[T]) WithCapacity

func (list *ArrayList[T]) WithCapacity(capacity int) (Lister[T], error)

WithCapacity is a method of the ArrayList type. It is used to set the maximum number of elements the list can hold.

Panics with an error of type *ErrCallFailed if the capacity is already set or with an error of type *ErrInvalidParameter if the provided capacity is negative or less than the current number of elements in the list.

Parameters:

  • capacity: An integer that represents the maximum number of elements the list can hold.

Returns:

  • Lister[T]: A pointer to the list with the new capacity set.

type ErrEmptyList

type ErrEmptyList struct {
	// contains filtered or unexported fields
}

ErrEmptyList represents an error when attempting to perform a list operation on an empty list.

func NewErrEmptyList

func NewErrEmptyList[T any](list Lister[T]) *ErrEmptyList

NewErrEmptyList creates a new ErrEmptyList.

Parameters:

  • list: the type of list that caused the error.

Returns:

  • *ErrEmptyList: a pointer to the new ErrEmptyList.

func (*ErrEmptyList) Error

func (e *ErrEmptyList) Error() string

Error implements the error interface for the ErrEmptyList type.

Returns:

  • string: a string representation of the error.

type ErrFullList

type ErrFullList struct {
	// contains filtered or unexported fields
}

ErrFullList represents an error when attempting to prepend an element into a full list.

func NewErrFullList

func NewErrFullList[T any](list Lister[T]) *ErrFullList

NewErrFullList creates a new ErrFullList.

Parameters:

  • list: the type of list that caused the error.

Returns:

  • *ErrFullList: a pointer to the new ErrFullList.

func (*ErrFullList) Error

func (e *ErrFullList) Error() string

Error implements the error interface for the ErrFullList type.

Returns:

  • string: a string representation of the error.

type LinkedList

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

LinkedList is a generic type that represents a list data structure with or without a limited capacity, implemented using a linked list.

func NewLinkedList

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

NewLinkedList is a function that creates and returns a new instance of a LinkedList.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the list.

Returns:

  • *LinkedList[T]: A pointer to the newly created LinkedList.

func (*LinkedList[T]) Append

func (list *LinkedList[T]) Append(value T) error

Append is a method of the LinkedList type. It is used to add an element to the end of the list.

Panics with an error of type *ErrFullList if the list is full.

Parameters:

  • value: An element of type T to be added to the list.

func (*LinkedList[T]) Capacity

func (list *LinkedList[T]) Capacity() optional.Int

Capacity is a method of the LinkedList type. It is used to return the maximum number of elements the list can hold.

Returns:

  • optional.Int: An optional integer that represents the maximum number of elements the list can hold.

func (*LinkedList[T]) Clear

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

Clear is a method of the LinkedList type. It is used to remove all elements from the list.

func (*LinkedList[T]) Copy added in v0.2.15

func (list *LinkedList[T]) Copy() itf.Copier

Copy is a method of the LinkedList type. It is used to create a shallow copy of the list.

Returns:

  • itf.Copier: A copy of the list.

func (*LinkedList[T]) CutNilValues

func (list *LinkedList[T]) CutNilValues()

CutNilValues is a method of the LinkedList type. It is used to remove all nil values from the list.

func (*LinkedList[T]) DeleteFirst

func (list *LinkedList[T]) DeleteFirst() (T, error)

DeleteFirst is a method of the LinkedList type. It is used to remove and return the first element in the list.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • T: The first element in the list.

func (*LinkedList[T]) DeleteLast

func (list *LinkedList[T]) DeleteLast() (T, error)

DeleteLast is a method of the LinkedList type. It is used to remove and return the last element in the list.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • T: The last element in the list.

func (*LinkedList[T]) IsEmpty

func (list *LinkedList[T]) IsEmpty() bool

IsEmpty is a method of the LinkedList type. It is used to check if the list is empty.

Returns:

  • bool: A boolean value that is true if the list is empty, and false otherwise.

func (*LinkedList[T]) IsFull

func (list *LinkedList[T]) IsFull() (isFull bool)

IsFull is a method of the LinkedList type. It is used to check if the list is full.

Returns:

  • isFull: A boolean value that is true if the list is full, and false otherwise.

func (*LinkedList[T]) Iterator

func (list *LinkedList[T]) Iterator() itf.Iterater[T]

Iterator is a method of the LinkedList type. It is used to return an iterator for the list.

Returns:

  • itf.Iterater[T]: An iterator for the list.

func (*LinkedList[T]) PeekFirst

func (list *LinkedList[T]) PeekFirst() (T, error)

PeekFirst is a method of the LinkedList type. It is used to return the first element in the list without removing it.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • value: The first element in the list.

func (*LinkedList[T]) PeekLast

func (list *LinkedList[T]) PeekLast() (T, error)

PeekLast is a method of the LinkedList type. It is used to return the last element in the list without removing it.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • value: The last element in the list.

func (*LinkedList[T]) Prepend

func (list *LinkedList[T]) Prepend(value T) error

Prepend is a method of the LinkedList type. It is used to add an element to the end of the list.

Panics with an error of type *ErrInvalidOperation if the list is full.

Parameters:

  • value: An element of type T to be added to the list.

func (*LinkedList[T]) Size

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

Size is a method of the LinkedList type. It is used to return the current number of elements in the list.

Returns:

  • int: An integer that represents the current number of elements in the list.

func (*LinkedList[T]) Slice added in v0.2.10

func (list *LinkedList[T]) Slice() []T

Slice is a method of the LinkedList type that returns a slice of type T

Returns:

  • []T: A slice of type T.

func (*LinkedList[T]) String

func (list *LinkedList[T]) String() string

String is a method of the LinkedList type. It returns a string representation of the list with information about its size, capacity, and elements.

Returns:

  • string: A string representation of the list.

func (*LinkedList[T]) WithCapacity

func (list *LinkedList[T]) WithCapacity(capacity int) (Lister[T], error)

WithCapacity is a method of the LinkedList type. It is used to set the maximum number of elements the list can hold.

Panics with an error of type *ErrCallFailed if the capacity is already set or with an error of type *ErrInvalidParameter if the provided capacity is negative or less than the current number of elements in the list.

Parameters:

  • capacity: An integer that represents the maximum number of elements the list can hold.

Returns:

  • Lister[T]: A pointer to the list with the new capacity set.

type Lister

type Lister[T any] interface {
	// The Append method adds a value of type T to the end of the list.
	Append(value T) error

	// The DeleteFirst method is a convenience method that deletefirsts an element from
	// the list and returns it.
	// If the list is empty, it will panic.
	DeleteFirst() (T, error)

	// PeekFirst is a method that returns the value at the front of the list without
	// removing it.
	// If the list is empty, it will panic.
	PeekFirst() (T, error)

	// The Prepend method adds a value of type T to the end of the list.
	Prepend(value T) error

	// The DeleteLast method is a convenience method that deletelasts an element from the
	// list and returns it.
	// If the list is empty, it will panic.
	DeleteLast() (T, error)

	// PeekLast is a method that returns the value at the front of the list without
	// removing it.
	// If the list is empty, it will panic.
	PeekLast() (T, error)

	// WithCapacity is a special function that modifies an existing queue data
	// structure to have a specific capacity. Panics if the list already has a capacity
	// set or if the new capacity is less than the current size of the list-like data
	// structure.
	//
	// As a result, it is recommended to use this function only when creating a new
	// list-like data structure.
	WithCapacity(int) (Lister[T], error)

	// ListLike[T] is an interface that defines methods for a list data structure.
	ListLike.ListLike[T]
}

Lister is an interface that defines methods for a list data structure. It includes methods to add and remove elements, check if the list is empty or full, get the size of the list, convert the list to a slice, clear the list, and get a string representation of the list.

type SafeList

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

SafeList is a generic type that represents a thread-safe list data structure with or without a maximum capacity, implemented using a linked list.

func NewSafeList

func NewSafeList[T any](values ...T) *SafeList[T]

NewSafeList is a function that creates and returns a new instance of a SafeList.

Parameters:

  • values: A variadic parameter of type T, which represents the initial values to be stored in the list.

Returns:

  • *SafeList[T]: A pointer to the newly created SafeList.

func (*SafeList[T]) Append

func (list *SafeList[T]) Append(value T) error

Append is a method of the SafeList type. It is used to add an element to the end of the list.

Panics with an error of type *ErrCallFailed if the list is full.

Parameters:

  • value: The value of type T to be added to the list.

func (*SafeList[T]) Capacity

func (list *SafeList[T]) Capacity() optional.Int

Capacity is a method of the SafeList type. It returns the maximum number of elements that the list can hold.

Returns:

  • optional.Int: An optional integer that represents the maximum number of elements the list can hold.

func (*SafeList[T]) Clear

func (list *SafeList[T]) Clear()

Clear is a method of the SafeList type. It is used to remove all elements from the list.

func (*SafeList[T]) Copy added in v0.2.15

func (list *SafeList[T]) Copy() itf.Copier

Copy is a method of the SafeList type. It is used to create a shallow copy of the list.

Returns:

  • itf.Copier: A copy of the list.

func (*SafeList[T]) CutNilValues

func (list *SafeList[T]) CutNilValues()

CutNilValues is a method of the SafeList type. It is used to remove all nil values from the list.

func (*SafeList[T]) DeleteFirst

func (list *SafeList[T]) DeleteFirst() (T, error)

DeleteFirst is a method of the SafeList type. It is used to remove and return the first element from the list.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • T: The first element in the list.

func (*SafeList[T]) DeleteLast

func (list *SafeList[T]) DeleteLast() (T, error)

DeleteLast is a method of the SafeList type. It is used to remove and return the last element from the list.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • T: The last element in the list.

func (*SafeList[T]) IsEmpty

func (list *SafeList[T]) IsEmpty() bool

IsEmpty is a method of the SafeList type. It checks if the list is empty.

Returns:

  • bool: A boolean value that is true if the list is empty, and false otherwise.

func (*SafeList[T]) IsFull

func (list *SafeList[T]) IsFull() (isFull bool)

IsFull is a method of the SafeList type. It checks if the list is full.

Returns:

  • isFull: A boolean value that is true if the list is full, and false otherwise.

func (*SafeList[T]) Iterator

func (list *SafeList[T]) Iterator() itf.Iterater[T]

Iterator is a method of the SafeList type. It is used to return an iterator for the list. However, the iterator does not share the list's thread safety.

Returns:

  • itf.Iterater[T]: An iterator for the list.

func (*SafeList[T]) PeekFirst

func (list *SafeList[T]) PeekFirst() (T, error)

PeekFirst is a method of the SafeList type. It is used to return the first element from the list without removing it.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • T: The first element in the list.

func (*SafeList[T]) PeekLast

func (list *SafeList[T]) PeekLast() (T, error)

PeekLast is a method of the SafeList type. It is used to return the last element from the list without removing it.

Panics with an error of type *ErrCallFailed if the list is empty.

Returns:

  • T: The last element in the list.

func (*SafeList[T]) Prepend

func (list *SafeList[T]) Prepend(value T) error

Prepend is a method of the SafeList type. It is used to add an element to the front of the list.

Panics with an error of type *ErrCallFailed if the list is full.

Parameters:

  • value: The value of type T to be added to the list.

func (*SafeList[T]) Size

func (list *SafeList[T]) Size() int

Size is a method of the SafeList type. It returns the number of elements in the list.

Returns:

  • int: An integer that represents the number of elements in the list.

func (*SafeList[T]) Slice added in v0.2.10

func (list *SafeList[T]) Slice() []T

Slice is a method of the SafeList type. It is used to return a slice of the elements in the list.

Returns:

  • []T: A slice of type T containing the elements of the list.

func (*SafeList[T]) String

func (list *SafeList[T]) String() string

String is a method of the SafeList type. It returns a string representation of the list including information about its size, capacity, and elements.

Returns:

  • string: A string representation of the list.

func (*SafeList[T]) WithCapacity

func (list *SafeList[T]) WithCapacity(capacity int) (Lister[T], error)

WithCapacity is a method of the SafeList type. It is used to set the maximum number of elements the list can hold.

Panics with an error of type *ErrCallFailed if the capacity is already set or with an error of type *ErrInvalidParameter if the provided capacity is negative or less than the current number of elements in the list.

Parameters:

  • capacity: An integer that represents the maximum number of elements the list can hold.

Returns:

  • Lister[T]: A pointer to the list with the new capacity set.

Jump to

Keyboard shortcuts

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