sly

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadOptions = errors.New("bad options")

Functions

func B2S

func B2S(b []byte) string

B2S converts the byte slice to string with no allocations.

b: Byte slice to convert.

Returns the string representation of the byte slice.

func ChanMerge

func ChanMerge[T any](ctx context.Context, bufSize uint, sources ...<-chan T) chan T

ChanMerge takes multiple channels and merges their outputs into one.

ctx: Cancellation context. If nil, defaults to context.Background().
bufSize: Buffer size for the merged channel.
sources: Channels to merge.

Returns the merged channel.

func ChanRelay

func ChanRelay[T any](ctx context.Context, source <-chan T, sink chan<- T)

ChanRelay routes data from the source channel to the sink channel.

ctx: Cancellation context. If nil, defaults to context.Background().
source: Channel to read from.
sink: Channel to write to.

func ChanStream

func ChanStream[T any](ctx context.Context, bufSize uint, values ...T) <-chan T

ChanStream produces a stream of values on a channel.

ctx: Cancellation context. If nil, defaults to context.Background().
bufSize: Buffer size for the channel returned.
values: Values to stream.

Returns the stream channel.

func CompareOrdered

func CompareOrdered[T constraints.Ordered](a, b T) int

CompareOrdered is a built-in comparator for ordered types.

func HeapPop

func HeapPop[T any](heapPtr *[]T, compare Compare[T]) T

HeapPop pops the max value from the heap.

heapPtr: Heap pointer.
compare: Comparator function.

Returns the max value.

func HeapPopTail

func HeapPopTail[T any](heapPtr *[]T, compare Compare[T]) T

HeapPopTail pops the smallest value from the heap and places it on top.

heapPtr: Heap pointer.
compare: Comparator function.

Returns the smallest value.

func HeapPush

func HeapPush[T any](heapPtr *[]T, x T, compare Compare[T]) []T

HeapPush pushes the value onto the heap.

heapPtr: Heap pointer.
x: Value to push.
compare: Comparator function.

Returns the heap with the value pushed onto it.

func Heapify

func Heapify[T any](input []T, compare Compare[T])

Heapify makes its input a heap.

input: Slice to heapify.
compare: Comparator function.

func PartitionFat

func PartitionFat[T any](x []T, pivot T, compare Compare[T]) (less, greater int)

PartitionFat executes Fat (aka Dutch flag) partition of x around the pivot.

x: Slice to be partitioned.
pivot: Value to partition around.
compare: Comparator function.

Returns a pair of indices:

less: all(y < pivot for y in x[:less]).
greater: all (y > pivot for y in x[greater+1:]).

all(y == pivot for y in x[less:greater+1])

func S2B

func S2B(s string) []byte

S2B converts the string to a byte slice with no allocations.

s: String to convert.

Returns the byte slice representation of the string.

func SlicePop

func SlicePop[T any](ptr *[]T) T

SlicePop pops the last value from the slice.

ptr: Slice to pop from.

Returns the popped value.

func SliceReshape

func SliceReshape[T any](input []T, newLen int) []T

SliceReshape reshapes the slice to the new length.

input: Slice to reshape.
newLen: New length.

Returns the reshaped slice.

func SliceSwap

func SliceSwap[T any](values []T, i, j int)

SliceSwap swaps two slice items by their indices.

values: Slice to swap the items in.
i: First item index.
j: Second item index.

func SortHeap

func SortHeap[T any](input []T, compare Compare[T])

SortHeap is an in-place heap sort.

input: Slice to sort.
compare: Comparator function.

Types

type ChanBroadcast

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

ChanBroadcast allows for broadcasting values from the source channel to multiple sink channels. Ensure that sink channels have adequate capacity to keep up with the broadcasts.

func NewChanBroadcast

func NewChanBroadcast[T any](accept context.Context, source <-chan T, nsinks uint) ChanBroadcast[T]

NewChanBroadcast creates a new ChanBroadcast with the given source channel and initial capacity for sinks.

accept: The context that will be used to cancel the broadcaster.
source: The channel to read values from.
sinkQueueCapacity: The initial capacity of the sink queue.

func (*ChanBroadcast[T]) Add

func (b *ChanBroadcast[T]) Add(sink chan<- T)

Add a new sink channel to receive broadcasts.

This is a convenience function for AddContext with background context.

See AddContext for more details.

func (*ChanBroadcast[T]) AddContext

func (b *ChanBroadcast[T]) AddContext(broadcast context.Context, sink chan<- T)

AddContext registers a new sink channel to receive broadcasts.

broadcast: The context that will be used to cancel the subscription.
sink: The sink channel.

If the broadcast is already canceled, the subscription is ignored.

If the sink channel is already registered, the subscription is replaced.

func (*ChanBroadcast[T]) Delete

func (b *ChanBroadcast[T]) Delete(sink chan<- T)

Delete the sink from broadcasting queue.

This is a convenience function for DeleteContext with background delete context.

See DeleteContext for more details.

func (*ChanBroadcast[T]) DeleteContext

func (b *ChanBroadcast[T]) DeleteContext(delete context.Context, sink chan<- T)

DeleteContext deletes the sink from broadcasting queue.

delete: The context that will be used to cancel the deletion.
sink: The sink channel.

If the broadcast is already canceled, the deletion is ignored.

If the sink channel is not registered, the deletion is ignored.

func (*ChanBroadcast[T]) Wait

func (b *ChanBroadcast[T]) Wait()

Wait blocks until the broadcaster has finished.

This is a convenience function for WaitContext with background wait context.

See WaitContext for more details.

func (*ChanBroadcast[T]) WaitContext

func (b *ChanBroadcast[T]) WaitContext(wait context.Context) error

WaitContext blocks until the broadcaster has finished.

wait: The context that will be used to cancel the wait.

Returns an error if the wait was canceled.

type Compare

type Compare[T any] func(T, T) int

Compare is a comparator func. See bounded methods for more.

func CompareReverse

func CompareReverse[T any](compare Compare[T]) Compare[T]

CompareReverse returns the negated comparator.

func (Compare[T]) Equal

func (c Compare[T]) Equal(a, b T) bool

Equal is `equal to`.

func (Compare[T]) Greater

func (c Compare[T]) Greater(a, b T) bool

Greater is `greater than`.

func (Compare[T]) GreaterOrEqual

func (c Compare[T]) GreaterOrEqual(a, b T) bool

GreaterOrEqual is `greater than or equal to`.

func (Compare[T]) Less

func (c Compare[T]) Less(a T, b T) bool

Less is `less than`.

func (Compare[T]) LessOrEqual

func (c Compare[T]) LessOrEqual(a, b T) bool

LessOrEqual is `less than or equal to`.

func (Compare[T]) NotEqual

func (c Compare[T]) NotEqual(a, b T) bool

NotEqual is `not equal to`.

type PriorityQueue

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

The PriorityQueue is a thread-safe priority queue.

func NewPriorityQueue

func NewPriorityQueue[T any](opts PriorityQueueOptions[T]) (*PriorityQueue[T], error)

NewPriorityQueue creates a new priority queue.

opts: See PriorityQueueOptions.

Returns a pointer to the newly created priority queue, or an error if the options are invalid.

func (*PriorityQueue[T]) Close

func (pq *PriorityQueue[T]) Close() []T

Close the priority queue.

Returns the remaining elements in order of priority.

func (*PriorityQueue[T]) Pop

func (pq *PriorityQueue[T]) Pop() (T, bool)

Pop the highest priority element from the priority queue.

Blocks indefinitely until there's either something to pop or the queue is closed.

Returns the default value and false in case the queue is closed and nothing remains to pop.

func (*PriorityQueue[T]) TryPush

func (pq *PriorityQueue[T]) TryPush(x T) bool

TryPush attempts to push an element onto the priority queue.

x: Element to push.

Returns true if the element was pushed, or false if the queue is either full or closed.

type PriorityQueueOptions

type PriorityQueueOptions[T any] struct {
	InitialCap uint
	MaxCap     uint
	Lock       sync.Locker
	Compare    Compare[T]
}

PriorityQueueOptions are used to construct a new priority queue.

InitialCap: Initial capacity.
MaxCap: Max capacity. If 0, then unlimited.
Lock: Queue lock. If nil, then Spinlock.
Compare: Comparator function.

type Spinlock

type Spinlock int32

Spinlock is an atomic based active lock.

func (*Spinlock) Lock

func (v *Spinlock) Lock()

Lock acquires the lock.

func (*Spinlock) Unlock

func (v *Spinlock) Unlock()

Unlock releases the lock.

Jump to

Keyboard shortcuts

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