Documentation
¶
Overview ¶
Collection is a Go library aim to implement some basic data structure such as List, Queue, Stack, Heap and more.
Index ¶
- type ErrIndexOutOfRange
- type ErrIsEmpty
- type ErrNotFound
- type List
- func (l *List[T]) All() iter.Seq2[int, T]
- func (l *List[T]) Append(values ...T)
- func (l *List[T]) Backward() iter.Seq2[int, T]
- func (l *List[T]) Dequeue() (T, error)
- func (l *List[T]) Index(at int) (T, error)
- func (l *List[T]) Insert(value T, at int) error
- func (l *List[T]) Length() int
- func (l *List[T]) Pop() (T, error)
- func (l *List[T]) Prepend(values ...T)
- func (l *List[T]) Remove(at int) (T, error)
- func (l *List[T]) Search(target T, equal func(value, target T) bool) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrIndexOutOfRange ¶
type ErrIndexOutOfRange struct {
// contains filtered or unexported fields
}
func (*ErrIndexOutOfRange) Error ¶
func (err *ErrIndexOutOfRange) Error() string
type ErrIsEmpty ¶
type ErrIsEmpty struct {
// contains filtered or unexported fields
}
func (*ErrIsEmpty) Error ¶
func (err *ErrIsEmpty) Error() string
type ErrNotFound ¶
type ErrNotFound struct {
// contains filtered or unexported fields
}
func (*ErrNotFound) Error ¶
func (err *ErrNotFound) Error() string
type List ¶
type List[T any] struct { // contains filtered or unexported fields }
List is a doubly linked list implementation. All operation on List should be thread-safe, because it only allow one goroutine at a time to access it data.
func NewList ¶
NewList creates a new doubly linked List. All operation on List should be thread-safe, because it only allow one goroutine at a time to access it data.
emptyList := New[int]() initializedList := New(1, 2, 3, 4, 5)
func (*List[T]) All ¶
All return an iterator of elements in list going from head to tail. The iterator returns the index and value of the node.
for idx, val := range list.All() {
// code goes here
}
func (*List[T]) Append ¶
func (l *List[T]) Append(values ...T)
Append adds new nodes to at the end of the list
func (*List[T]) Backward ¶
Backward return an iterator of elements in list going from tail to head. The iterator returns the index and value of the node.
for idx, val := range list.Backward() {
// code goes here
}
func (*List[T]) Dequeue ¶
Dequeue removes and returns the first element of the list. If the list is empty then return ErrIsEmpty as an error.
func (*List[T]) Index ¶
Index gets value at the specified index. If the index is out of range, it will return ErrIndexOutOfRange as error.
func (*List[T]) Insert ¶
Insert adds a new node after the node at a specified index. If the index is less than zero or greater than or equal the current length of the list, then this function will return an ErrIndexOutOfRange error. If you want to insert at the start of the list use List.Prepend instead.
func (*List[T]) Pop ¶
Pop removes and returns the last element of the list. If the list is empty then return ErrIsEmpty as an error.
func (*List[T]) Prepend ¶
func (l *List[T]) Prepend(values ...T)
Prepend adds a new node at the start of the list.
func (*List[T]) Remove ¶
Remove removes and returns the element at the specified index of the list. If the index is out of range then return ErrIndexOutOfRange as an error. Or if the list is empty then return ErrIsEmpty as an error.
func (*List[T]) Search ¶
Search searches for a value in the list. It takes the target to search for and the equal function. The equal function takes two arguments value and target, it should return true if the two arguments is considered to be equal.
It returns an index greater or equal to zero and a nil error if the value existed inside the list, else return the index of -1 and error of ErrNotFound.
type user struct {
name string
}
func main() {
user1 := user{name: "User 1"}
user2 := user{name: "User 2"}
users := NewList(user1, user2)
equal := func (value, target user) bool {
return value.name == target.name
}
idx, err := users.Search(user1, equal)
// code goes here
}