Documentation ¶
Index ¶
- func Advance[T any](iter Iter[T], count int)
- func AntiJoin[T comparable](a, b []T) []T
- func AntiJoinFn[T any](a, b []T, cmpFn func(a, b T) bool) (r []T)
- func Avg[T constraints.Integer | constraints.Float](a ...T) (r T)
- func Contains[T comparable](vs []T, e T) bool
- func ContainsFn[T any](vs []T, cmpFn CompareFunc[T]) bool
- func Delete[T comparable](set []T, value T) []T
- func DeleteAll[T comparable](set []T, value T) []T
- func Filter[T any](set []T, cmpFn CompareFunc[T]) []T
- func FilterInPlace[T any](set []T, cmpFn CompareFunc[T]) []T
- func Join[T comparable](a, b []T) []T
- func JoinFn[T any](a, b []T, cmpFn func(a, b T) bool) (r []T)
- func Map[T, E any](set []T, fn func(T) E) []E
- func Max[T constraints.Ordered](numbers ...T) (r T)
- func Merge[T comparable](a []T, more ...[]T) (r []T)
- func MergeFn[T any](cmpFn func(a, b T) bool, a []T, more ...[]T) (r []T)
- func Min[T constraints.Ordered](a, b T) (r T)
- func SearchFn[T any](vs []T, cmpFn CompareFunc[T]) int
- func Sum[T constraints.Integer | constraints.Float](a ...T) (r T)
- type Bytes
- func (b *Bytes) Append(others ...byte)
- func (b Bytes) Cap() int
- func (b *Bytes) CopyFrom(other Bytes)
- func (b Bytes) Index(other Bytes) int
- func (b Bytes) IndexByte(c byte) int
- func (b Bytes) Len() int
- func (b *Bytes) LimitReadFrom(r io.Reader, n int) (int64, error)
- func (b Bytes) LimitWriteTo(w io.Writer, n int) (int64, error)
- func (b *Bytes) Push(bts ...byte)
- func (b Bytes) ReadFrom(r io.Reader) (int64, error)
- func (b *Bytes) Reserve(n int)
- func (b *Bytes) Resize(n int)
- func (b Bytes) Slice(low, max int) Bytes
- func (b Bytes) String() string
- func (b Bytes) UnsafeString() string
- func (b Bytes) WriteTo(w io.Writer) (int64, error)
- type CompareFunc
- type Iter
- type IterBidir
- type IterDrop
- type IterDropBidir
- type List
- func (list *List[T]) Back() (opt OptionalPtr[T])
- func (list *List[T]) Front() (opt OptionalPtr[T])
- func (list *List[T]) Iter() IterDropBidir[T]
- func (list *List[T]) PopBack() (opt OptionalPtr[T])
- func (list *List[T]) PopFront() (opt OptionalPtr[T])
- func (list *List[T]) PushBack(v T) *ListElement[T]
- func (list *List[T]) PushFront(v T) *ListElement[T]
- func (list *List[T]) Reset()
- func (list *List[T]) Size() int
- type ListElement
- type Optional
- type OptionalPtr
- type Pair
- type Queue
- type Ring
- type Tree
- func (tree *Tree[Key, Value]) Data() Optional[Value]
- func (tree *Tree[Key, Value]) Del(path ...Key)
- func (tree *Tree[Key, Value]) Depth() int
- func (tree *Tree[Key, Value]) Fetch(path ...Key) (opt Optional[Value])
- func (tree *Tree[Key, Value]) Get(path ...Key) (depth int, opt Optional[Value])
- func (tree *Tree[Key, Value]) GetTree(path ...Key) *Tree[Key, Value]
- func (tree *Tree[Key, Value]) Name() Key
- func (tree *Tree[Key, Value]) Path() []Key
- func (tree *Tree[Key, Value]) Range(fn func(*Tree[Key, Value]) bool, path ...Key)
- func (tree *Tree[Key, Value]) RangeAll(fn func(*Tree[Key, Value]) bool)
- func (tree *Tree[Key, Value]) RangeLevel(fn func(*Tree[Key, Value]) bool, level int)
- func (tree *Tree[Key, Value]) RangeLimit(fn func(*Tree[Key, Value]) bool, maxDepth int)
- func (tree *Tree[Key, Value]) Set(data Value, path ...Key)
- func (tree *Tree[Key, Value]) SetRange(data Value, lvl int)
- func (tree *Tree[Key, Value]) Trees() []*Tree[Key, Value]
- type Vec
- func (vc *Vec[T]) Append(elmnts ...T)
- func (vc Vec[T]) Back() (opt OptionalPtr[T])
- func (vc Vec[T]) Cap() int
- func (vc Vec[T]) Contains(cmpFn CompareFunc[T]) bool
- func (vc *Vec[T]) DelByIndex(i int) (val T, erased bool)
- func (vc *Vec[T]) Filter(cmpFn CompareFunc[T]) (val T, erased bool)
- func (vc Vec[T]) Front() (opt OptionalPtr[T])
- func (vc Vec[T]) Get(i int) T
- func (vc Vec[T]) Index(cmpFn CompareFunc[T]) int
- func (vc Vec[T]) Len() int
- func (vc *Vec[T]) PopBack() (opt OptionalPtr[T])
- func (vc *Vec[T]) PopFront() (opt OptionalPtr[T])
- func (vc *Vec[T]) Push(elmnts ...T)
- func (vc *Vec[T]) Reserve(n int)
- func (vc *Vec[T]) Resize(n int)
- func (vc Vec[T]) Search(cmpFn CompareFunc[T]) (v T, ok bool)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AntiJoin ¶
func AntiJoin[T comparable](a, b []T) []T
AntiJoin returns a slice containing the elements that are present in `a` but not in `b`.
Example ¶
package main import ( "fmt" "github.com/dgrr/tl" ) func main() { a := []int{1, 2, 3, 4, 5, 6, 7, 8} b := []int{5, 7, 9, 11, 13} antijoined := tl.AntiJoin(a, b) fmt.Println("All", antijoined) }
Output:
func AntiJoinFn ¶
AntiJoinFn performs an AntiJoin operation for non-comparable types.
See AntiJoin for more.
func Avg ¶
func Avg[T constraints.Integer | constraints.Float](a ...T) (r T)
func Contains ¶
func Contains[T comparable](vs []T, e T) bool
Contains returns where `e` is present in `vs`.
func ContainsFn ¶
func ContainsFn[T any](vs []T, cmpFn CompareFunc[T]) bool
ContainsFn returns where `vs` contains an element using `cmpFn`.
func Delete ¶
func Delete[T comparable](set []T, value T) []T
Delete removes an element from a set. Notice that this function only removes the first occurrence.
func DeleteAll ¶ added in v0.2.1
func DeleteAll[T comparable](set []T, value T) []T
DeleteAll deletes all occurrences of a matching value.
func Filter ¶
func Filter[T any](set []T, cmpFn CompareFunc[T]) []T
Filter filters a set of values using `cmpFn`.
Example ¶
package main import ( "fmt" "strings" "github.com/dgrr/tl" ) func main() { filterThis := []string{ "Each type parameter has a type", "constraint that acts as a kind of", "meta-type for the type parameter", "Each type constraint specifies the permissible", "type arguments that calling", "code can use for the respective", "type parameter", } // filter out the strings containing `type`. afterFilter := tl.Filter(filterThis, func(x string) bool { return !strings.Contains(x, "type") }) fmt.Println(strings.Join(filterThis, "\n")) fmt.Println("--- after ----") fmt.Println(strings.Join(afterFilter, "\n")) }
Output:
func FilterInPlace ¶
func FilterInPlace[T any](set []T, cmpFn CompareFunc[T]) []T
FilterInPlace filters the set in-place, meaning that it will not create a new slice like in Filter.
Example ¶
package main import ( "fmt" "strings" "github.com/dgrr/tl" ) func main() { filterThis := []string{ "Each type parameter has a type", "constraint that acts as a kind of", "meta-type for the type parameter", "Each type constraint specifies the permissible", "type arguments that calling", "code can use for the respective", "type parameter", } // filter out the strings containing `type`. filterThis = tl.FilterInPlace(filterThis, func(x string) bool { return !strings.Contains(x, "type") }) fmt.Println(strings.Join(filterThis, "\n")) }
Output:
func Join ¶
func Join[T comparable](a, b []T) []T
Join returns a slice containing the elements that are present in `a` and `b`.
Example ¶
package main import ( "fmt" "github.com/dgrr/tl" ) func main() { a := []int{1, 2, 3, 4, 5, 6, 7, 8} b := []int{5, 7, 9, 11, 13} joined := tl.Join(a, b) fmt.Println("All", joined) }
Output:
func Map ¶
func Map[T, E any](set []T, fn func(T) E) []E
Map maps the values of `set` using `fn`.
Example ¶
package main import ( "fmt" "strconv" "github.com/dgrr/tl" ) func main() { numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} numbersToString := tl.Map(numbers, func(number int) string { return strconv.Itoa(number) }) fmt.Println(numbers) fmt.Println(numbersToString) }
Output:
func Max ¶
func Max[T constraints.Ordered](numbers ...T) (r T)
Max returns the maximum instance of a set of numbers.
func Merge ¶
func Merge[T comparable](a []T, more ...[]T) (r []T)
Merge merges multiple slices into one avoiding duplicates.
Returns a slice containing all the elements in a, and more... without duplicates.
Example ¶
package main import ( "fmt" "github.com/dgrr/tl" ) func main() { a := []int{1, 2, 3, 4, 5, 6, 7, 8} b := []int{5, 7, 9, 11, 13} merged := tl.Merge(a, b) fmt.Println("All", merged) }
Output:
func Min ¶
func Min[T constraints.Ordered](a, b T) (r T)
func SearchFn ¶
func SearchFn[T any](vs []T, cmpFn CompareFunc[T]) int
SearchFn iterates over `vs` comparing the values using `cmpFn`.
Returns the index to the element if found, -1 otherwise.
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](a ...T) (r T)
Types ¶
type Bytes ¶
Bytes wraps the behavior of Vec as Vec[byte] adding new methods on top.
func (Bytes) Index ¶
Index returns the index of `other` in `b`.
This function is equivalent to bytes.Index(b, other).
func (Bytes) IndexByte ¶ added in v0.2.1
IndexByte returns the index of `c` in `b`.
This function is equivalent to bytes.IndexByte(b, c).
func (*Bytes) LimitReadFrom ¶
LimitReadFrom is like ReadFrom but it is limited to `n` bytes.
func (Bytes) LimitWriteTo ¶
LimitWriteTo writes a limited amount of `n` bytes to an io.Writer.
func (Bytes) ReadFrom ¶
ReadFrom reads bytes from an io.Reader into `b`.
This function does NOT append bytes, it uses the existing buffer.
Implements the io.ReaderFrom interface.
func (*Bytes) Reserve ¶
Reserve increases the size of Bytes if needed.
The call doesn't produce any allocation if `n` < cap(b).
func (Bytes) Slice ¶
Slice returns a sliced Bytes using indexes starting from `low` and ending in `max`.
func (Bytes) String ¶
String converts `b` to a string.
This function produces an allocation. To avoid the allocation use UnsafeString.
func (Bytes) UnsafeString ¶
UnsafeString converts `b` to a string without producing allocations.
type CompareFunc ¶
type Iter ¶
type Iter[T any] interface { // Next is called to iterate to the next element. // // Returns true if a next element is available, false otherwise. Next() bool // Get returns the current element. Get() T // GetPtr returns a pointer to the current element. GetPtr() *T }
Iter defines an iterator interface.
type IterBidir ¶
type IterBidir[T any] interface { Iter[T] // Back is like next but for going backwards. It moves the iterator to the previous position. // For some implementations that might mean going in reverse mode (not going backwards). Back() bool }
IterBidir defines a bidirectional iterator.
type IterDrop ¶
type IterDrop[T any] interface { Iter[T] // Drop removes the current element from the iterator. Drop() }
IterDrop implements an iterator which current element can be dropped.
type IterDropBidir ¶
IterDropBidir merges IterBidir and IterDrop in one interface.
type List ¶
type List[T any] struct { // contains filtered or unexported fields }
List defines a doubly linked list.
func (*List[T]) Back ¶
func (list *List[T]) Back() (opt OptionalPtr[T])
Back returns an optional to the last element of the queue.
func (*List[T]) Front ¶
func (list *List[T]) Front() (opt OptionalPtr[T])
Front returns an optional to the first element of the queue.
func (*List[T]) Iter ¶
func (list *List[T]) Iter() IterDropBidir[T]
Iter returns an iterator for the List.
func (*List[T]) PopBack ¶
func (list *List[T]) PopBack() (opt OptionalPtr[T])
PopBack pops the last element if any.
func (*List[T]) PopFront ¶
func (list *List[T]) PopFront() (opt OptionalPtr[T])
PopFront pops the first element if any.
func (*List[T]) PushBack ¶
func (list *List[T]) PushBack(v T) *ListElement[T]
PushBack appends an element to the back of the queue.
func (*List[T]) PushFront ¶
func (list *List[T]) PushFront(v T) *ListElement[T]
PushFront pushes an element to the front of the queue.
type ListElement ¶
type ListElement[T any] struct { // contains filtered or unexported fields }
ListElement is an element in a List.
func (*ListElement[T]) Drop ¶
func (e *ListElement[T]) Drop()
Drop drops the current element from the list.
func (*ListElement[T]) GetPtr ¶
func (e *ListElement[T]) GetPtr() *T
GetPtr returns a pointer to the value of the element.
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional defines a value that can be optional. So, if might be defined or not.
func MakeOptional ¶
MakeOptional returns an Optional with a value `v`.
func NewOptional ¶
NewOptional returns an Optional with a value `v` if `v` is not nil.
func (Optional[T]) Get ¶
func (opt Optional[T]) Get() T
Get returns the value. The function doesn't check whether the value is set or not.
type OptionalPtr ¶
type OptionalPtr[T any] struct { // contains filtered or unexported fields }
OptionalPtr is like Optional but instead of holding a copy of the value, it holds a pointer.
func MakeOptionalPtr ¶
func MakeOptionalPtr[T any](v *T) (opt OptionalPtr[T])
MakeOptionalPtr returns an OptionalPtr using the value `v`.
func (OptionalPtr[T]) Get ¶
func (opt OptionalPtr[T]) Get() *T
Get returns a pointer to the value, because the value is a pointer already.
func (*OptionalPtr[T]) GetValue ¶
func (opt *OptionalPtr[T]) GetValue() T
GetValue returns the value inside the Optional's pointer. This function might panic if the value is not defined (aka value == nil).
func (OptionalPtr[T]) HasValue ¶
func (opt OptionalPtr[T]) HasValue() bool
HasValue returns true if a value is present.
func (OptionalPtr[T]) Or ¶
func (opt OptionalPtr[T]) Or(v *T) OptionalPtr[T]
Or sets the value `v` to the Optional only if not value is already present.
func (*OptionalPtr[T]) Reset ¶
func (opt *OptionalPtr[T]) Reset()
Reset resets the OptionalPtr's value.
func (*OptionalPtr[T]) Set ¶
func (opt *OptionalPtr[T]) Set(v *T)
Set sets the value `v` to the OptionalPtr.
type Pair ¶
type Pair[T, U any] struct { // contains filtered or unexported fields }
Pair defines a pair of values.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue defines a queue data structure.
func (*Queue[T]) PushBack ¶
func (q *Queue[T]) PushBack(data T)
PushBack pushes to the back of the queue.
type Ring ¶
type Ring[T any] struct { // contains filtered or unexported fields }
Ring is a Multiple-Producer Multiple-Consumer (MPMC) circular buffer data structure.
type Tree ¶
type Tree[Key comparable, Value any] struct { // contains filtered or unexported fields }
func (*Tree[Key, Value]) RangeLevel ¶
func (*Tree[Key, Value]) RangeLimit ¶
type Vec ¶
type Vec[T any] []T
Vec defines a slice of type T.
func MakeVecSize ¶
MakeVecSize returns a vector with size `size` and capacity `capacity`.
func (Vec[T]) Back ¶
func (vc Vec[T]) Back() (opt OptionalPtr[T])
func (Vec[T]) Contains ¶
func (vc Vec[T]) Contains(cmpFn CompareFunc[T]) bool
func (*Vec[T]) DelByIndex ¶
func (*Vec[T]) Filter ¶
func (vc *Vec[T]) Filter(cmpFn CompareFunc[T]) (val T, erased bool)
func (Vec[T]) Front ¶
func (vc Vec[T]) Front() (opt OptionalPtr[T])
func (Vec[T]) Index ¶
func (vc Vec[T]) Index(cmpFn CompareFunc[T]) int
func (*Vec[T]) PopBack ¶
func (vc *Vec[T]) PopBack() (opt OptionalPtr[T])
func (*Vec[T]) PopFront ¶
func (vc *Vec[T]) PopFront() (opt OptionalPtr[T])
func (Vec[T]) Search ¶
func (vc Vec[T]) Search(cmpFn CompareFunc[T]) (v T, ok bool)