Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/campbel/q"
)
func main() {
list := q.NewList(1, 2, 3)
fmt.Println(list)
heap := q.NewHeap(func(a, b int) bool {
return a < b
}, 1, 2, 3)
fmt.Println(heap)
set := q.NewSet(1, 2, 3)
fmt.Println(set)
}
Output:
Index ¶
- func Equal[M comparable](a, b *List[M]) bool
- func IndexOf[M comparable](list *List[M], value M) int
- func Reduce[M any, N any](l *List[M], callback func(N, M) N, initial N) N
- func Remove[M comparable](list *List[M], value M)
- type Counter
- func (c *Counter[T]) Add(elements ...T)
- func (c *Counter[T]) Clear()
- func (c *Counter[T]) Contains(element T) bool
- func (c *Counter[T]) Count(element T) int
- func (c *Counter[T]) Elements() []T
- func (c *Counter[T]) Equal(other *Counter[T]) bool
- func (c *Counter[T]) IsEmpty() bool
- func (c *Counter[T]) Len() int
- func (c *Counter[T]) Remove(elements ...T) bool
- func (c *Counter[T]) String() string
- type Heap
- type List
- func (l *List[M]) All(callback func(int, M) bool) bool
- func (l *List[M]) Any(callback func(int, M) bool) bool
- func (l *List[M]) Copy() *List[M]
- func (l *List[M]) Each(callback func(int, M))
- func (l *List[M]) Elements() []M
- func (l *List[M]) Extend(lists ...*List[M])
- func (l *List[M]) Filter(callback func(int, M) bool) *List[M]
- func (l *List[M]) Find(callback func(int, M) bool) M
- func (l *List[M]) IsSorted(less func(M, M) bool) bool
- func (l *List[M]) Len() int
- func (l *List[M]) Len64() int64
- func (l *List[M]) Pop() M
- func (l *List[M]) PopLeft() M
- func (l *List[M]) PopRight() M
- func (l *List[M]) Push(values ...M)
- func (l *List[M]) PushLeft(values ...M)
- func (l *List[M]) PushRight(values ...M)
- func (l *List[M]) Reverse()
- func (l *List[M]) Slice(start, stop int) *List[M]
- func (l *List[M]) Sort(less func(M, M) bool) *List[M]
- func (l *List[M]) String() string
- type Node
- type Set
- func (s *Set[T]) Add(elements ...T)
- func (s *Set[T]) Clear()
- func (s *Set[T]) Contains(element T) bool
- func (s *Set[T]) Difference(other *Set[T]) *Set[T]
- func (s *Set[T]) Elements() []T
- func (s *Set[T]) Intersection(other *Set[T]) *Set[T]
- func (s *Set[T]) Len() int
- func (s *Set[T]) Remove(element T)
- func (s *Set[T]) String() string
- func (s *Set[T]) Union(other *Set[T]) *Set[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
func Equal[M comparable](a, b *List[M]) bool
Equal returns true if the two lists are equal, false otherwise. Time complexity: O(n), where n is the number of elements in the list.
func IndexOf ¶
func IndexOf[M comparable](list *List[M], value M) int
IndexOf returns the index of the first occurrence of the value in the list, or -1 if not found. Time complexity: O(n), where n is the number of elements in the list.
func Reduce ¶
Reduce applies a callback function to each value in the list and returns a single accumulated value. Time complexity: O(n), where n is the number of elements in the list.
func Remove ¶
func Remove[M comparable](list *List[M], value M)
Remove removes all occurrences of the value from the list. Time complexity: O(n), where n is the number of elements in the list.
Types ¶
type Counter ¶
type Counter[T comparable] struct { // contains filtered or unexported fields }
Counter is a generic counter data structure that stores unique elements of type T and counts occurences.
func NewCounter ¶
func NewCounter[T comparable](elements ...T) *Counter[T]
NewCounter creates a new Counter and returns a pointer to it. Time complexity: O(1).
func (*Counter[T]) Add ¶
func (c *Counter[T]) Add(elements ...T)
Add adds one or more elements to the counter. Time complexity: O(n), where n is the number of elements being added.
func (*Counter[T]) Clear ¶
func (c *Counter[T]) Clear()
Clear removes all elements from the counter. Time complexity: O(1).
func (*Counter[T]) Contains ¶
Contains checks if an element is present in the counter. Time complexity: O(1).
func (*Counter[T]) Count ¶
Count returns the number of occurences of an element in the counter. Time complexity: O(1).
func (*Counter[T]) Elements ¶
func (c *Counter[T]) Elements() []T
Elements returns a slice containing all the elements in the counter. Time complexity: O(n), where n is the number of elements in the counter.
func (*Counter[T]) Equal ¶
Equal checks if the counter is equal to another counter. Time complexity: O(n), where n is the number of elements in the counter.
type Heap ¶
type Heap[M any] struct { // contains filtered or unexported fields }
Heap is a generic implementation of a heap data structure.
func NewHeap ¶
NewHeap creates a new instance of Heap with the specified less function. Time complexity: O(1).
func (*Heap[M]) Empty ¶
Empty returns true if the heap is empty, false otherwise. Time complexity: O(1).
Example ¶
ExampleHeap_Empty demonstrates how to check if the heap is empty.
h := NewHeap(func(a, b int) bool {
return a < b
})
fmt.Println(h.Empty())
h.Push(1, 2, 3)
fmt.Println(h.Empty())
Output: true false
func (*Heap[M]) Len ¶
Len returns the number of elements in the heap. Time complexity: O(1).
Example ¶
ExampleHeap_Len demonstrates how to get the number of elements in the heap.
h := NewHeap(func(a, b int) bool {
return a < b
})
h.Push(5, 2, 7, 1, 9)
fmt.Println(h.Len())
Output: 5
func (*Heap[M]) Pop ¶
func (h *Heap[M]) Pop() M
Pop removes and returns the top element from the heap. Time complexity: O(log n), where n is the number of elements in the heap.
Example ¶
ExampleHeap_Pop demonstrates how to pop the top element from the heap.
h := NewHeap(func(a, b int) bool {
return a < b
})
h.Push(5, 2, 7, 1, 9)
top := h.Pop()
fmt.Println(top)
fmt.Println(h.data)
Output: 1 [2 5 7 9]
func (*Heap[M]) Push ¶
func (h *Heap[M]) Push(values ...M)
Push adds one or more values to the heap. Time complexity: O(log n) for each value, where n is the number of elements in the heap.
Example ¶
ExampleHeap_Push demonstrates how to push elements onto the heap.
h := NewHeap(func(a, b int) bool {
return a < b
})
h.Push(5, 2, 7, 1, 9)
fmt.Println(h.data)
Output: [1 2 7 5 9]
func (*Heap[M]) Top ¶
func (h *Heap[M]) Top() M
Top returns the top element of the heap without removing it. Time complexity: O(1).
Example ¶
ExampleHeap_Top demonstrates how to retrieve the top element without removing it.
h := NewHeap(func(a, b string) bool {
return a < b
})
h.Push("banana", "apple", "orange", "grape")
top := h.Top()
fmt.Println(top)
fmt.Println(h.data)
Output: apple [apple banana orange grape]
type List ¶
type List[M any] struct { // contains filtered or unexported fields }
List represents a generic linked list.
func Join ¶
Join combines multiple lists into a single list. Time complexity: O(m), where m is the total number of elements in all the lists.
func Map ¶
Map applies a callback function to each value in the list and returns a new list with the results. Time complexity: O(n), where n is the number of elements in the list.
func NewList ¶
NewList creates a new List and initializes it with the given elements. Time complexity: O(n), where n is the number of elements.
func (*List[M]) All ¶
All returns true if all values in the list satisfy the callback function, false otherwise. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Any ¶
Any returns true if at least one value in the list satisfies the callback function, false otherwise. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Copy ¶
Copy returns a new list with the same values as the original list. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Each ¶
Each applies a callback function to each value in the list. Time complexity: O(n), where n is the number of elements in the list.
Example ¶
ExampleList_Each demonstrates how to apply a callback function to each element in the list.
list := NewList[int](1, 2, 3, 4, 5)
list.Each(func(i, value int) {
fmt.Print(value*2, " ")
})
Output: 2 4 6 8 10
func (*List[M]) Elements ¶
func (l *List[M]) Elements() []M
Elements returns a slice containing all the values in the list. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Extend ¶
Extend appends other lists to the current list. Time complexity: O(m), where m is the total number of elements in all the lists.
func (*List[M]) Filter ¶
Filter returns a new list containing only the values that satisfy the callback function. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Find ¶
Find returns the first value in the list that satisfies the callback function. Time complexity: O(n), where n is the number of elements in the list.
Example ¶
ExampleList_Find demonstrates how to find the first element that satisfies a condition.
list := NewList[int](1, 2, 3, 4, 5)
even := list.Find(func(i, value int) bool {
return value%2 == 0
})
fmt.Println(even)
Output: 2
func (*List[M]) IsSorted ¶
IsSorted returns true if the list is sorted in non-decreasing order according to the provided less function, false otherwise. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Pop ¶
func (l *List[M]) Pop() M
Pop removes and returns the last value from the list. Time complexity: O(1).
Example ¶
ExampleList_Pop demonstrates how to remove and return the last element from the list.
list := NewList[int](1, 2, 3) last := list.Pop() fmt.Println(last) fmt.Println(list)
Output: 3 [1 2]
func (*List[M]) PopLeft ¶
func (l *List[M]) PopLeft() M
PopLeft removes and returns the first value from the list. Time complexity: O(1).
func (*List[M]) PopRight ¶
func (l *List[M]) PopRight() M
PopRight removes and returns the last value from the list. Time complexity: O(1).
func (*List[M]) Push ¶
func (l *List[M]) Push(values ...M)
Push adds values to the list. Time complexity: O(n), where n is the number of values.
Example ¶
ExampleList_Push demonstrates how to add elements to the list.
list := NewList[int]() list.Push(1, 2, 3) fmt.Println(list)
Output: [1 2 3]
func (*List[M]) PushLeft ¶
func (l *List[M]) PushLeft(values ...M)
PushLeft adds values to the beginning of the list. Time complexity: O(n), where n is the number of values.
func (*List[M]) PushRight ¶
func (l *List[M]) PushRight(values ...M)
PushRight adds values to the end of the list. Time complexity: O(n), where n is the number of values.
func (*List[M]) Reverse ¶
func (l *List[M]) Reverse()
Reverse reverses the order of the list. Time complexity: O(n), where n is the number of elements in the list.
Example ¶
ExampleList_Reverse demonstrates how to reverse the order of elements in the list.
list := NewList[string]("apple", "banana", "orange")
list.Reverse()
fmt.Println(list)
Output: [orange banana apple]
func (*List[M]) Slice ¶
Slice returns a new list containing the elements from the start index to the stop index. Time complexity: O(n), where n is the number of elements in the list.
func (*List[M]) Sort ¶
Sort returns a new sorted list using the provided less function. Time complexity: O(n log n), where n is the number of elements in the list.
Example ¶
ExampleList_Sort demonstrates how to sort the list using a custom less function.
list := NewList[int](5, 2, 4, 1, 3)
sorted := list.Sort(func(a, b int) bool {
return a < b
})
fmt.Println(sorted)
Output: [1 2 3 4 5]
type Node ¶
type Node[M any] struct { // contains filtered or unexported fields }
Node represents a node in the linked list.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a generic set data structure that stores unique elements of type T.
func NewSet ¶
func NewSet[T comparable](elements ...T) *Set[T]
NewSet creates a new Set and returns a pointer to it. Time complexity: O(1).
func (*Set[T]) Add ¶
func (s *Set[T]) Add(elements ...T)
Add adds one or more elements to the set. Time complexity: O(n), where n is the number of elements being added.
Example ¶
Example_Set_Add demonstrates how to add elements to a set.
set := NewSet[int]() set.Add(1, 2, 3) elements := set.Elements() slices.Sort(elements) fmt.Println(elements)
Output: [1 2 3]
func (*Set[T]) Clear ¶
func (s *Set[T]) Clear()
Clear removes all elements from the set. Time complexity: O(1).
func (*Set[T]) Contains ¶
Contains checks if an element is present in the set. Time complexity: O(1).
Example ¶
Example_Set_Contains demonstrates how to check if an element exists in a set.
set := NewSet[string]()
set.Add("apple", "banana", "orange")
fmt.Println(set.Contains("banana"))
fmt.Println(set.Contains("grape"))
Output: true false
func (*Set[T]) Difference ¶
Difference returns a new set that contains the elements present in the current set but not in another set. Time complexity: O(n), where n is the number of elements in the current set.
func (*Set[T]) Elements ¶
func (s *Set[T]) Elements() []T
Elements returns a slice containing all the elements in the set. Time complexity: O(n), where n is the number of elements in the set.
func (*Set[T]) Intersection ¶
Intersection returns a new set that is the intersection of the current set and another set. Time complexity: O(n), where n is the number of elements in the smaller set.
func (*Set[T]) Remove ¶
func (s *Set[T]) Remove(element T)
Remove removes an element from the set. Time complexity: O(1).
func (*Set[T]) Union ¶
Union returns a new set that is the union of the current set and another set. Time complexity: O(n), where n is the total number of elements in both sets.
Example ¶
Example_Set_Union demonstrates how to find the union of two sets.
set1 := NewSet[int]() set1.Add(1, 2, 3) set2 := NewSet[int]() set2.Add(3, 4, 5) unionSet := set1.Union(set2) sorted := sort.IntSlice(unionSet.Elements()) sorted.Sort() fmt.Println(sorted)
Output: [1 2 3 4 5]