collections

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2025 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package collections provides a set of various useful collection implementations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectByKey

func CollectByKey[T comparable, U any](items []U, mapper func(U) T) map[T][]U

CollectByKey extracts key for passed slice elements and collects them into mapp of small slices.

func FilterInplace

func FilterInplace[S ~[]E, E any](items S, keepFunc func(E) bool) S

FilterInplace filters incmoming slice by using provided func an adjusts origin slice.

func FindFirstMatch

func FindFirstMatch[S ~[]E, E any](items S, match func(E) bool) int

func ForEach

func ForEach[S ~[]E, E any](items S, f func(E))

ForEach applies function to each element of passed slice.

func GroupByUniqueKey

func GroupByUniqueKey[T comparable, U any](items []U, mapper func(U) T) map[T]U

GroupByUniqueKey extracts key from passed slice and uses it as key for map to group values. Note that same key for values will override output map with latest value.

func IteratorFromSlice

func IteratorFromSlice[T any](values []T) iterator[T]

func Map

func Map[T, U any](items []T, f func(T) U) []U

func Reduce

func Reduce[S ~[]E, E any, U constraints.Ordered](items S, f func(E) U) (result U)

func SortOutput

func SortOutput[K cmp.Ordered, T ~[]K](items T)

SortOutput sorts output items after converting set to slice.

Types

type AsSliceOpt

type AsSliceOpt[K comparable, T ~[]K] func(T)

AsSliceOpt allows to adjust behaviour of the AsSlice method.

type Optional

type Optional[T any] struct {
	// contains filtered or unexported fields
}

func (Optional[T]) Get

func (o Optional[T]) Get() T

func (Optional[T]) IsValid

func (o Optional[T]) IsValid() bool

func (*Optional[T]) Set

func (o *Optional[T]) Set(value T)

type Set

type Set[K comparable] map[K]empty

Set is a typed map with keys only. It does not stores any values and only keys.

func NewSet

func NewSet[K comparable, T ~[]K](items T) Set[K]

NewSet converts incoming slice into set.

func NewSetExtract

func NewSetExtract[U comparable, T any](items []T, mapper func(T) U) Set[U]

NewSetExtract maps incoming slice types into another type prior to building a set.

func (Set[K]) Add

func (s Set[K]) Add(value K)

Add element to set.

func (Set[K]) AsSlice

func (s Set[K]) AsSlice(opts ...AsSliceOpt[K, []K]) []K

AsSlice converts set to slice.

func (Set[K]) Contains

func (s Set[K]) Contains(item K) bool

Contains check if set contains specific item.

func (Set[K]) Empty

func (s Set[K]) Empty() bool

Empty checks if set contains at least one value.

func (Set[K]) Pops

func (s Set[K]) Pops(items ...K) (subtracted []K)

Pops pop multiple items from set returning them back to the caller.

func (Set[K]) Remove

func (s Set[K]) Remove(item K)

Remove item from set. It does not checks the existance of item before deleting.

type SkiplistNode

type SkiplistNode[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

type SkiplistRoot

type SkiplistRoot[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

SkiplistRoot implements Tree interface

func NewSkiplist

func NewSkiplist[K cmp.Ordered, V any]() *SkiplistRoot[K, V]

func (*SkiplistRoot[K, V]) Contains

func (r *SkiplistRoot[K, V]) Contains(key K) bool

func (*SkiplistRoot[K, V]) Get

func (r *SkiplistRoot[K, V]) Get(key K) (result Optional[V])

func (*SkiplistRoot[K, V]) Height

func (r *SkiplistRoot[K, V]) Height() uint64

func (*SkiplistRoot[K, V]) Insert

func (r *SkiplistRoot[K, V]) Insert(key K, value V)

func (*SkiplistRoot[K, V]) Size

func (r *SkiplistRoot[K, V]) Size() uint64

type Tree

type Tree[K cmp.Ordered, V any] interface {
	Contains(K) bool
	Get(K) Optional[V]
	Insert(K, V)
	Size() uint64
}

Jump to

Keyboard shortcuts

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