Documentation
¶
Overview ¶
This package aims to provide generic data types to abstract certain operations that may be performed by both server and client.
Index ¶
- type Counter
- type Slice
- type Table
- type Waitlist
- func (w *Waitlist[T]) Cancel(cancel context.CancelFunc)
- func (w *Waitlist[T]) Clear()
- func (w *Waitlist[T]) Get(ctx context.Context, find func(T) bool) (T, error)
- func (w *Waitlist[T]) Insert(v T)
- func (w *Waitlist[T]) Timeout(ctx context.Context)
- func (w *Waitlist[T]) TryGet(find func(T) bool) (T, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter that can increase and decrease up to a certain maximum value. It is implemented so that it is safe to use concurrently.
func NewCounter ¶
Creates a new counter with the maximum value it can have.
func (*Counter) Dec ¶
func (c *Counter) Dec()
Decreases the value of the counter Notifies anyone waiting to increase the counter again.
type Slice ¶
type Slice[T comparable] struct { // contains filtered or unexported fields }
Used to store a slice that is safe to be accessed concurrently. Stored values must be able to be compared together.
func NewSlice ¶
func NewSlice[T comparable](cap uint) Slice[T]
Returns a preallocated slice with 0 elements according to the given capacity.
func (*Slice[T]) Add ¶
func (s *Slice[T]) Add(v T)
Appends a new element to the slice, reallocating it if necessary.
func (*Slice[T]) Copy ¶
Returns a copy of the actual slice data so that it can be safely traversed by a single goroutine. An optional argument of how many elements to retrieve can be provided. To return all elements, "n" must be 0. If the slice is empty or "n" goes out of bounds, nil will be returned.
func (*Slice[T]) Find ¶
Returns the element found that fulfills the given function and a boolean indicating whether it was or not found.
func (*Slice[T]) Get ¶
Returns the element located at a certain index and a boolean indicating if it exists (array indexing starts at 0).
func (*Slice[T]) Has ¶
Returns true if the given element exists in the slice, returns false otherwise.
type Table ¶
type Table[I comparable, T any] struct { // contains filtered or unexported fields }
Table used for storing a map that is safe to use concurrently.
func NewTable ¶
func NewTable[I comparable, T any](size uint) Table[I, T]
Returns an allocated data table according to provided size.
func (*Table[I, T]) GetAll ¶
func (t *Table[I, T]) GetAll() []T
Returns all value elements of the table in an array. It is important to note that order can change.
type Waitlist ¶
type Waitlist[T any] struct { // contains filtered or unexported fields }
Used to store sorted data and make goroutines wait for a specific piece of data under certain conditions.
func NewWaitlist ¶
Returns a preallocated slice with 0 elements according to the given capacity and also sets the function that will sort elements according to slices.SortFunc.
func (*Waitlist[T]) Cancel ¶
func (w *Waitlist[T]) Cancel(cancel context.CancelFunc)
Wakes up all waiting threads and cancels the context.
func (*Waitlist[T]) Get ¶
Tries to retrieve an element that fulfills the given function. If the element is not found the caller goroutine will sleep and wake up when a new element is inserted, repeating this process forever until the element is found. A context must be passed which will be checked whenever the goroutine wakes up and return its error if its not nil.
func (*Waitlist[T]) Insert ¶
func (w *Waitlist[T]) Insert(v T)
Inserts an element into the waitlist, sorts the list and notifies all waiting goroutines.