Documentation ¶
Index ¶
- Variables
- func B2S(b []byte) string
- func ChanMerge[T any](ctx context.Context, bufSize uint, sources ...<-chan T) chan T
- func ChanRelay[T any](ctx context.Context, source <-chan T, sink chan<- T)
- func ChanStream[T any](ctx context.Context, bufSize uint, values ...T) <-chan T
- func CompareOrdered[T constraints.Ordered](a, b T) int
- func HeapPop[T any](heapPtr *[]T, compare Compare[T]) T
- func HeapPopTail[T any](heapPtr *[]T, compare Compare[T]) T
- func HeapPush[T any](heapPtr *[]T, x T, compare Compare[T]) []T
- func Heapify[T any](input []T, compare Compare[T])
- func PartitionFat[T any](x []T, pivot T, compare Compare[T]) (less, greater int)
- func S2B(s string) []byte
- func SlicePop[T any](ptr *[]T) T
- func SliceReshape[T any](input []T, newLen int) []T
- func SliceSwap[T any](values []T, i, j int)
- func SortHeap[T any](input []T, compare Compare[T])
- type ChanBroadcast
- func (b *ChanBroadcast[T]) Add(sink chan<- T)
- func (b *ChanBroadcast[T]) AddContext(broadcast context.Context, sink chan<- T)
- func (b *ChanBroadcast[T]) Delete(sink chan<- T)
- func (b *ChanBroadcast[T]) DeleteContext(delete context.Context, sink chan<- T)
- func (b *ChanBroadcast[T]) Wait()
- func (b *ChanBroadcast[T]) WaitContext(wait context.Context) error
- type Compare
- type PriorityQueue
- type PriorityQueueOptions
- type Spinlock
Constants ¶
This section is empty.
Variables ¶
var ErrBadOptions = errors.New("bad options")
Functions ¶
func B2S ¶
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 ¶
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 ¶
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 ¶
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 ¶
HeapPop pops the max value from the heap.
heapPtr: Heap pointer. compare: Comparator function.
Returns the max value.
func HeapPopTail ¶
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 ¶
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 ¶
Heapify makes its input a heap.
input: Slice to heapify. compare: Comparator function.
func PartitionFat ¶
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 ¶
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 ¶
SliceReshape reshapes the slice to the new length.
input: Slice to reshape. newLen: New length.
Returns the reshaped slice.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Compare is a comparator func. See bounded methods for more.
func CompareReverse ¶
CompareReverse returns the negated comparator.
func (Compare[T]) GreaterOrEqual ¶
GreaterOrEqual is `greater than or equal to`.
func (Compare[T]) LessOrEqual ¶
LessOrEqual is `less than or 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.
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.