Documentation ¶
Overview ¶
Package list implements dynamic lists.
Index ¶
- type ArrayList
- func (l *ArrayList[T]) Add(e ...T)
- func (l *ArrayList[T]) AddAtIndex(index int, e ...T) error
- func (l *ArrayList[T]) AddSlice(e []T)
- func (l *ArrayList[T]) AddSliceAtIndex(index int, e []T) error
- func (l *ArrayList[T]) Clear()
- func (l *ArrayList[T]) Compare(st any) int
- func (l *ArrayList[T]) Contains(e T) bool
- func (l *ArrayList[T]) Copy() List[T]
- func (l *ArrayList[T]) Each(fun func(index int, element T))
- func (l *ArrayList[T]) Equal(st any) bool
- func (l *ArrayList[T]) Get(index int) (T, error)
- func (l *ArrayList[T]) Hash() string
- func (l *ArrayList[T]) IndexOf(e T) int
- func (l *ArrayList[T]) IsEmpty() bool
- func (l *ArrayList[T]) Iter() Iterator[T]
- func (l *ArrayList[T]) IterReverse() Iterator[T]
- func (l *ArrayList[T]) LastIndexOf(e T) int
- func (l *ArrayList[T]) Len() int
- func (l *ArrayList[T]) Remove(index int) (T, error)
- func (l *ArrayList[T]) RemoveElement(e T) bool
- func (l *ArrayList[T]) Set(index int, e T) (T, error)
- func (l *ArrayList[T]) Sort()
- func (l *ArrayList[T]) SortFunc(less func(i T, j T) bool)
- func (l *ArrayList[T]) Stream() *Stream[T]
- func (l *ArrayList[T]) String() string
- func (l *ArrayList[T]) ToSlice() []T
- type ArrayListIterator
- type Iterator
- type LinkedList
- func (l *LinkedList[T]) Add(e ...T)
- func (l *LinkedList[T]) AddAtIndex(index int, e ...T) error
- func (l *LinkedList[T]) AddSlice(e []T)
- func (l *LinkedList[T]) AddSliceAtIndex(index int, e []T) error
- func (l *LinkedList[T]) Clear()
- func (l *LinkedList[T]) Compare(st any) int
- func (l *LinkedList[T]) Contains(e T) bool
- func (l *LinkedList[T]) Copy() List[T]
- func (l *LinkedList[T]) Each(fun func(index int, element T))
- func (l *LinkedList[T]) Equal(st any) bool
- func (l *LinkedList[T]) Get(index int) (T, error)
- func (l *LinkedList[T]) Hash() string
- func (l *LinkedList[T]) IndexOf(e T) int
- func (l *LinkedList[T]) IsEmpty() bool
- func (l *LinkedList[T]) Iter() Iterator[T]
- func (l *LinkedList[T]) IterReverse() Iterator[T]
- func (l *LinkedList[T]) LastIndexOf(e T) int
- func (l *LinkedList[T]) Len() int
- func (l *LinkedList[T]) Remove(index int) (T, error)
- func (l *LinkedList[T]) RemoveElement(e T) bool
- func (l *LinkedList[T]) Set(index int, e T) (T, error)
- func (l *LinkedList[T]) Sort()
- func (l *LinkedList[T]) SortFunc(less func(i T, j T) bool)
- func (l *LinkedList[T]) Stream() *Stream[T]
- func (l *LinkedList[T]) String() string
- func (l *LinkedList[T]) ToSlice() []T
- type LinkedListIterator
- type List
- type Stream
- func (s *Stream[T]) All(fun func(index int, element T) bool) bool
- func (s *Stream[T]) Any(fun func(index int, element T) bool) bool
- func (s *Stream[T]) Collect() List[T]
- func (s *Stream[T]) Count(fun func(index int, element T) bool) int
- func (s *Stream[T]) Filter(fun func(index int, element T) bool) *Stream[T]
- func (s *Stream[T]) FilterMap(fun func(index int, element T) (T, bool)) *Stream[T]
- func (s *Stream[T]) Map(fun func(index int, element T) T) *Stream[T]
- func (s *Stream[T]) None(fun func(index int, element T) bool) bool
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 provides a generic list implemented with a slice.
It implements the interface List.
func NewArrayList ¶
NewArrayList returns a new ArrayList containing the elements c.
if no argument is passed, it will be created an empty ArrayList.
func NewArrayListFromSlice ¶
NewArrayListFromSlice returns a new ArrayList containing the elements of slice c.
func NewArrayListFromStructure ¶
func NewArrayListFromStructure[T any](c structures.Structure[T]) *ArrayList[T]
NewArrayListFromStructure is a wrapper for NewArrayListFromSlice(c.ToSlice()).
func (*ArrayList[T]) Add ¶
func (l *ArrayList[T]) Add(e ...T)
Add adds the elements e at the end of l.
func (*ArrayList[T]) AddAtIndex ¶
AddAtIndex adds the elements e at the specified index. It returns an error if the the index is out of bounds.
func (*ArrayList[T]) AddSlice ¶
func (l *ArrayList[T]) AddSlice(e []T)
AddSlice adds the elements of e at the end of l.
func (*ArrayList[T]) AddSliceAtIndex ¶
AddSliceAtIndex adds the elements of e at the specified index. It returns an error if the the index is out of bounds.
func (*ArrayList[T]) Compare ¶ added in v0.5.0
Compare returns 0 if l and st are equals, -1 if l is shorten than st, 1 if l is longer than st, -2 if st is not a List or if one between l and st is nil.
If l and st have the same length, the result is the comparison between the first different element of the two lists if T implemets util.Comparer, otherwhise the result is 0.
func (*ArrayList[T]) Copy ¶
Copy returns a list containing a copy of the elements of l. The result of this method is of type List, but the effective list which is created is an ArrayList.
This method uses util.Copy to make copies of the elements.
func (*ArrayList[T]) Each ¶ added in v0.8.0
Each executes fun for all elements of l.
This method should be used to remove elements. Use Iter insted.
func (*ArrayList[T]) Equal ¶ added in v0.5.0
Equal returns true if l and st are both lists and their elements are equals. In any other case, it returns false.
Equal does not take into account the effective type of st. This means that if st is a LinkedList, but the elements of l and the elements of st are equals, this method returns anyway true.
func (*ArrayList[T]) Get ¶
Get returns the elements at the specifies index. It returns an error if the the index is out of bounds.
func (*ArrayList[T]) IndexOf ¶
IndexOf returns the first position of e in l. If e is not present, the result is -1.
func (*ArrayList[T]) Iter ¶
Iter returns an Iterator which permits to iterate an ArrayList.
for i := l.Iter(); !i.End(); i = i.Next() { element := i.Element() index := i.Index() // Code }
func (*ArrayList[T]) IterReverse ¶ added in v0.2.0
IterReverse returns an Iterator which permits to iterate an ArrayList in reverse order.
for i := l.IterReverse(); !i.End(); i = i.Prev() { element := i.Element() index := i.Index() // Code }
func (*ArrayList[T]) LastIndexOf ¶
LastIndexOf returns the last position of e in l. If e is not present, the result is -1.
func (*ArrayList[T]) Remove ¶
Remove removes the element at specified index and return the removed value. It returns an error if the the index is out of bounds.
func (*ArrayList[T]) RemoveElement ¶
RemoveElement removes the element e from l if it is present. In that case, the method returns true, otherwhise it returns false.
func (*ArrayList[T]) Set ¶
Set sets the value of element at the specified index and returns the overwritten value. It returns an error if the the index is out of bounds.
func (*ArrayList[T]) Sort ¶ added in v0.8.0
func (l *ArrayList[T]) Sort()
Sort sorts the elements of l.
This method panics if T does not implement util.Comparer
func (*ArrayList[T]) SortFunc ¶ added in v0.8.0
SortFunc sorts the elements of l as determined by the less function.
type ArrayListIterator ¶ added in v0.7.0
type ArrayListIterator[T any] struct { // contains filtered or unexported fields }
ArrayListIterator is an iterator of an ArrayList.
func (*ArrayListIterator[T]) Element ¶ added in v0.7.0
func (i *ArrayListIterator[T]) Element() T
Elements returns the element of i.
func (*ArrayListIterator[T]) End ¶ added in v0.7.0
func (i *ArrayListIterator[T]) End() bool
End checks if the iteration is finished.
func (*ArrayListIterator[T]) Index ¶ added in v0.7.0
func (i *ArrayListIterator[T]) Index() int
Index returns the index of the element of i.
func (*ArrayListIterator[T]) Next ¶ added in v0.7.0
func (i *ArrayListIterator[T]) Next() Iterator[T]
Next returns the iterator of the next element.
func (*ArrayListIterator[T]) Prev ¶ added in v0.7.0
func (i *ArrayListIterator[T]) Prev() Iterator[T]
Prev returns the iterator of the previous element.
func (*ArrayListIterator[T]) Remove ¶ added in v0.7.0
func (i *ArrayListIterator[T]) Remove() Iterator[T]
Remove removes the element from the list and returns the iterator of the next element.
The result of this method must be assigned in most cases to himself.
i = i.Remove()
An example of the use of the method is the following:
for i := list.Iter(); !i.End(); i = i.Next() { // Code if /*some condition*/ { i = i.Remove() } // Code }
The following code, instead, can lead to undefined program behavior:
for i := list.Iter(); !i.End(); i = i.Next() { // Code if /*some condition*/ { i.Remove() } // Code }
type Iterator ¶ added in v0.7.0
type Iterator[T any] interface { // Elements returns the element of the iterator. Element() T // Index returns the index of the element the iterator. Index() int // Remove removes the element from the list and returns the iterator of the next element. // // The result of this method must be assigned in most cases to himself. // // i = i.Remove() // // An example of the use of the method is the following: // // for i := list.Iter(); !i.End(); i = i.Next() { // // Code // if /*some condition*/ { // i = i.Remove() // } // // Code // } // // The following code, instead, can lead to undefined program behavior: // // for i := list.Iter(); !i.End(); i = i.Next() { // // Code // if /*some condition*/ { // i.Remove() // } // // Code // } // Remove() Iterator[T] // Prev returns the iterator of the previous element. Prev() Iterator[T] // Next returns the iterator of the next element. Next() Iterator[T] // End checks if the iteration is finished. End() bool }
Iterator provides the methods to iterate over a List.
func NewArrayListIterator ¶ added in v0.7.0
NewArrayListIterator returns a new ArrayListIterator associated at the list parameter.
func NewArrayListReverseIterator ¶ added in v0.7.0
NewArrayListReverseIterator returns a new reverse ArrayListIterator associated at the list parameter.
func NewLinkedListIterator ¶ added in v0.7.0
func NewLinkedListIterator[T any](list *LinkedList[T]) Iterator[T]
NewLinkedListIterator returns a new LinkedListIterator associated at the list parameter.
func NewLinkedListReverseIterator ¶ added in v0.7.0
func NewLinkedListReverseIterator[T any](list *LinkedList[T]) Iterator[T]
NewLinkedListReverseIterator returns a new reverse LinkedListIterator associated at the list parameter.
type LinkedList ¶
type LinkedList[T any] struct { // contains filtered or unexported fields }
LinkedList provides a generic double linked list. The list is implemented through a series of linked structures.Entry.
It implements the interface List.
func NewLinkedList ¶
func NewLinkedList[T any](c ...T) *LinkedList[T]
NewLinkedList returns a new LinkedList containing the elements c.
if no argument is passed, it will be created an empty LinkedList.
func NewLinkedListFromSlice ¶
func NewLinkedListFromSlice[T any](c []T) *LinkedList[T]
NewLinkedListFromSlice returns a new LinkedList containing the elements of slice c.
func NewLinkedListFromStructure ¶
func NewLinkedListFromStructure[T any](c structures.Structure[T]) *LinkedList[T]
NewLinkedListFromStructure is a wrapper for NewLinkedListFromSlice(c.ToSlice()).
func (*LinkedList[T]) Add ¶
func (l *LinkedList[T]) Add(e ...T)
Add adds the elements e at the end of l.
func (*LinkedList[T]) AddAtIndex ¶
func (l *LinkedList[T]) AddAtIndex(index int, e ...T) error
AddAtIndex adds the elements e at the specified index. It returns an error if the the index is out of bounds.
func (*LinkedList[T]) AddSlice ¶
func (l *LinkedList[T]) AddSlice(e []T)
AddSlice adds the elements of e at the end of l.
func (*LinkedList[T]) AddSliceAtIndex ¶
func (l *LinkedList[T]) AddSliceAtIndex(index int, e []T) error
AddSliceAtIndex adds the elements of e at the specified index. It returns an error if the the index is out of bounds.
func (*LinkedList[T]) Compare ¶ added in v0.5.0
func (l *LinkedList[T]) Compare(st any) int
Compare returns 0 if l and st are equals, -1 if l is shorten than st, 1 if l is longer than st, -2 if st is not a List or if one between l and st is nil.
If l and st have the same length, the result is the comparison between the first different element of the two lists if T implemets util.Comparer, otherwhise the result is 0.
func (*LinkedList[T]) Contains ¶
func (l *LinkedList[T]) Contains(e T) bool
Contains returns if e is present in l.
func (*LinkedList[T]) Copy ¶
func (l *LinkedList[T]) Copy() List[T]
Copy returns a list containing a copy of the elements of l. The result of this method is of type List, but the effective list which is created is a LinkedList.
This method uses util.Copy to make copies of the elements.
func (*LinkedList[T]) Each ¶ added in v0.8.0
func (l *LinkedList[T]) Each(fun func(index int, element T))
Each executes fun for all elements of l.
This method should be used to remove elements. Use Iter insted.
func (*LinkedList[T]) Equal ¶ added in v0.5.0
func (l *LinkedList[T]) Equal(st any) bool
Equal returns true if l and st are both lists and their elements are equals. In any other case, it returns false.
Equal does not take into account the effective type of st. This means that if st is an ArrayList, but the elements of l and the elements of st are equals, this method returns anyway true.
func (*LinkedList[T]) Get ¶
func (l *LinkedList[T]) Get(index int) (T, error)
Get returns the elements at the specifies index. It returns an error if the the index is out of bounds.
func (*LinkedList[T]) Hash ¶ added in v0.5.0
func (l *LinkedList[T]) Hash() string
Hash returns the hash code of l.
func (*LinkedList[T]) IndexOf ¶
func (l *LinkedList[T]) IndexOf(e T) int
IndexOf returns the first position of e in l. If e is not present, the result is -1.
func (*LinkedList[T]) IsEmpty ¶
func (l *LinkedList[T]) IsEmpty() bool
IsEmpty returns a bool which indicates if l is empty or not.
func (*LinkedList[T]) Iter ¶
func (l *LinkedList[T]) Iter() Iterator[T]
Iter returns an Iterator which permits to iterate a LinkedList.
for i := l.Iter(); !i.End(); i = i.Next() { element := i.Element() index := i.Index() // Code }
func (*LinkedList[T]) IterReverse ¶ added in v0.2.0
func (l *LinkedList[T]) IterReverse() Iterator[T]
IterReverse returns an Iterator which permits to iterate a LinkedList in reverse order.
for i := l.IterReverse(); !i.End(); i = i.Prev() { element := i.Element() index := i.Index() // Code }
func (*LinkedList[T]) LastIndexOf ¶
func (l *LinkedList[T]) LastIndexOf(e T) int
LastIndexOf returns the last position of e in l. If e is not present, the result is -1.
func (*LinkedList[T]) Remove ¶
func (l *LinkedList[T]) Remove(index int) (T, error)
Remove removes the element at specified index and return the removed value. It returns an error if the the index is out of bounds.
func (*LinkedList[T]) RemoveElement ¶
func (l *LinkedList[T]) RemoveElement(e T) bool
RemoveElement removes the element e from l if it is present. In that case, the method returns true, otherwhise it returns false.
func (*LinkedList[T]) Set ¶
func (l *LinkedList[T]) Set(index int, e T) (T, error)
Set sets the value of element at the specified index and returns the overwritten value. It returns an error if the the index is out of bounds.
func (*LinkedList[T]) Sort ¶ added in v0.8.0
func (l *LinkedList[T]) Sort()
Sort sorts the elements of l.
This method panics if T does not implement util.Comparer
func (*LinkedList[T]) SortFunc ¶ added in v0.8.0
func (l *LinkedList[T]) SortFunc(less func(i T, j T) bool)
SortFunc sorts the elements of l as determined by the less function.
func (*LinkedList[T]) Stream ¶ added in v0.8.0
func (l *LinkedList[T]) Stream() *Stream[T]
Stream returns a Stream rapresenting l.
func (*LinkedList[T]) String ¶
func (l *LinkedList[T]) String() string
String returns a rapresentation of l in the form of a string.
func (*LinkedList[T]) ToSlice ¶
func (l *LinkedList[T]) ToSlice() []T
ToSlice returns a slice which contains all elements of l.
type LinkedListIterator ¶ added in v0.7.0
type LinkedListIterator[T any] struct { // contains filtered or unexported fields }
LinkedListIterator is an iterator of a LinkedList.
func (*LinkedListIterator[T]) Element ¶ added in v0.7.0
func (i *LinkedListIterator[T]) Element() T
Elements returns the element of i.
func (*LinkedListIterator[T]) End ¶ added in v0.7.0
func (i *LinkedListIterator[T]) End() bool
End checks if the iteration is finished.
func (*LinkedListIterator[T]) Index ¶ added in v0.7.0
func (i *LinkedListIterator[T]) Index() int
Index returns the index of the element of i.
func (*LinkedListIterator[T]) Next ¶ added in v0.7.0
func (i *LinkedListIterator[T]) Next() Iterator[T]
Next returns the iterator of the next element.
func (*LinkedListIterator[T]) Prev ¶ added in v0.7.0
func (i *LinkedListIterator[T]) Prev() Iterator[T]
Prev returns the iterator of the previous element.
func (*LinkedListIterator[T]) Remove ¶ added in v0.7.0
func (i *LinkedListIterator[T]) Remove() Iterator[T]
Remove removes the element from the list and returns the iterator of the next element.
The result of this method must be assigned in most cases to himself.
i = i.Remove()
An example of the use of the method is the following:
for i := list.Iter(); !i.End(); i = i.Next() { // Code if /*some condition*/ { i = i.Remove() } // Code }
The following code, instead, can lead to undefined program behavior:
for i := list.Iter(); !i.End(); i = i.Next() { // Code if /*some condition*/ { i.Remove() } // Code }
type List ¶
type List[T any] interface { structures.Structure[T] util.Copier[List[T]] // Contains returns if e is present in the list. Contains(e T) bool // IndexOf returns the first position of e in the list. // If e is not present, the result is -1. IndexOf(e T) int // LastIndexOf returns the last position of e in the list. // If e is not present, the result is -1. LastIndexOf(e T) int // Get returns the elements at the specifies index. // It returns an error if the the index is out of bounds. Get(index int) (T, error) // Set sets the value of element at the specified index and returns the overwritten value. // It returns an error if the the index is out of bounds. Set(index int, e T) (T, error) // Add adds the elements e at the end if the list. Add(e ...T) // AddAtIndex adds the elements e at the specified index. // It returns an error if the the index is out of bounds. AddAtIndex(index int, e ...T) error // AddSlice adds the elements of e at the end of the list. AddSlice(e []T) // AddSliceAtIndex adds the elements of e at the specified index. // It returns an error if the the index is out of bounds. AddSliceAtIndex(index int, e []T) error // Remove removes the element at specified index and return the removed value. // It returns an error if the the index is out of bounds. Remove(index int) (T, error) // RemoveElement removes the element e from the list if it is presentt. // In that case, the method returns true, otherwhise it returns false. RemoveElement(e T) bool // Each executes fun for all elements of the list. // // This method should be used to remove elements. Use Iter insted. Each(fun func(index int, element T)) // Stream returns a [Stream] rapresenting the list. Stream() *Stream[T] // Sort sorts the elements of the list. // // This method panics if T does not implement [util.Comparer] Sort() // SortFunc sorts the elements of the list as determined by the less function. SortFunc(less func(i T, j T) bool) // Iter returns an [Iterator] which permits to iterate a [List]. // // for i := list.Iter(); !i.End(); i = i.Next() { // element := i.Element() // index := i.Index() // // Code // } Iter() Iterator[T] // IterReverse returns an [Iterator] which permits to iterate a [List] in reverse order. // // for i := list.IterReverse(); !i.End(); i = i.Prev() { // element := i.Element() // index := i.Index() // // Code // } IterReverse() Iterator[T] }
List provides all methods to use a generic dynamic list. A list contains all the methods of structures.Structure.
A list is indexed starting from 0.
The check on the equality of the elements is done with the Equal method if T implements util.Equaler, otherwise it is done with reflect.DeepEqual.
type Stream ¶ added in v0.8.0
type Stream[T any] struct { // contains filtered or unexported fields }
Stream provides aggregate operations for a List.
func NewStream ¶ added in v0.8.0
NewStream returns a new Stream associated at the list parameter.
Constructor a reflect.Value rapresenting the function that create the resulting list from the stream. This function must have no parameters or must be a variadic function and must returns a List[T].
func (*Stream[T]) Any ¶ added in v0.8.0
Any returns true if at least one element of s satisfies fun.
func (*Stream[T]) Collect ¶ added in v0.8.0
Collect returns a List from s.
the effective type of the result is the same the constructor.
This method panics if constructor have wrong parameters or not returns a List[T].
func (*Stream[T]) Filter ¶ added in v0.8.0
Filter returns a Stream containing the elements that satisfy fun.
This method modifies the state of s, so it is not necessary to assign to resulting stream, but it can be useful for concatenate operations in a single instruction.
func (*Stream[T]) FilterMap ¶ added in v0.8.0
FilterMap executes fun for all elements of s and returns a Stream containing the resulting elements that satisfy fun.
This method modifies the state of s, so it is not necessary to assign to resulting stream, but it can be useful for concatenate operations in a single instruction.