list

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package list implements dynamic lists.

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 provides a generic list implemented with a slice.

It implements the interface List.

func NewArrayList

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

NewArrayList returns a new ArrayList containing the elements c.

if no argument is passed, it will be created an empty ArrayList.

func NewArrayListFromSlice

func NewArrayListFromSlice[T any](c []T) *ArrayList[T]

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

func (l *ArrayList[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 (*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

func (l *ArrayList[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 (*ArrayList[T]) Clear

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

Clear removes all element from l.

func (*ArrayList[T]) Compare added in v0.5.0

func (l *ArrayList[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 (*ArrayList[T]) Contains

func (l *ArrayList[T]) Contains(e T) bool

Contains returns if e is present in l.

func (*ArrayList[T]) Copy

func (l *ArrayList[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 an ArrayList.

This method uses util.Copy to make copies of the elements.

func (*ArrayList[T]) Each added in v0.8.0

func (l *ArrayList[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 (*ArrayList[T]) Equal added in v0.5.0

func (l *ArrayList[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 a LinkedList, but the elements of l and the elements of st are equals, this method returns anyway true.

func (*ArrayList[T]) Get

func (l *ArrayList[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 (*ArrayList[T]) Hash added in v0.5.0

func (l *ArrayList[T]) Hash() string

Hash returns the hash code of l.

func (*ArrayList[T]) IndexOf

func (l *ArrayList[T]) IndexOf(e T) int

IndexOf returns the first position of e in l. If e is not present, the result is -1.

func (*ArrayList[T]) IsEmpty

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

IsEmpty returns a bool which indicates if l is empty or not.

func (*ArrayList[T]) Iter

func (l *ArrayList[T]) Iter() Iterator[T]

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

func (l *ArrayList[T]) IterReverse() Iterator[T]

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

func (l *ArrayList[T]) LastIndexOf(e T) int

LastIndexOf returns the last position of e in l. If e is not present, the result is -1.

func (*ArrayList[T]) Len

func (l *ArrayList[T]) Len() int

Len returns the length of l.

func (*ArrayList[T]) Remove

func (l *ArrayList[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 (*ArrayList[T]) RemoveElement

func (l *ArrayList[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 (*ArrayList[T]) Set

func (l *ArrayList[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 (*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

func (l *ArrayList[T]) SortFunc(less func(i T, j T) bool)

SortFunc sorts the elements of l as determined by the less function.

func (*ArrayList[T]) Stream added in v0.8.0

func (l *ArrayList[T]) Stream() *Stream[T]

Stream returns a Stream rapresenting l.

func (*ArrayList[T]) String

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

String returns a rapresentation of l in the form of a string.

func (*ArrayList[T]) ToSlice

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

ToSlice returns a slice which contains all elements of l.

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

func NewArrayListIterator[T any](list *ArrayList[T]) Iterator[T]

NewArrayListIterator returns a new ArrayListIterator associated at the list parameter.

func NewArrayListReverseIterator added in v0.7.0

func NewArrayListReverseIterator[T any](list *ArrayList[T]) Iterator[T]

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]) Clear

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

Clear removes all element from l.

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]) Len

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

Len returns the length of l.

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

func NewStream[T any](list List[T], constructor reflect.Value) *Stream[T]

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]) All added in v0.8.0

func (s *Stream[T]) All(fun func(index int, element T) bool) bool

All returns true if all elements of s satisfy fun.

func (*Stream[T]) Any added in v0.8.0

func (s *Stream[T]) Any(fun func(index int, element T) bool) bool

Any returns true if at least one element of s satisfies fun.

func (*Stream[T]) Collect added in v0.8.0

func (s *Stream[T]) Collect() List[T]

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]) Count added in v0.8.0

func (s *Stream[T]) Count(fun func(index int, element T) bool) int

Count returns the number of elements that satisfy fun.

func (*Stream[T]) Filter added in v0.8.0

func (s *Stream[T]) Filter(fun func(index int, element T) bool) *Stream[T]

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

func (s *Stream[T]) FilterMap(fun func(index int, element T) (T, bool)) *Stream[T]

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.

func (*Stream[T]) Map added in v0.8.0

func (s *Stream[T]) Map(fun func(index int, element T) T) *Stream[T]

Map executes fun for all elements of s and returns a Stream containing the resulting elements.

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]) None added in v0.8.0

func (s *Stream[T]) None(fun func(index int, element T) bool) bool

None returns true if none of the elements of s satisfies fun.

Jump to

Keyboard shortcuts

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