models

package
v0.0.0-...-f701b90 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

This package aims to provide generic data types to abstract certain operations that may be performed by both server and client.

Index

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

func NewCounter(max int) Counter

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.

func (*Counter) Get

func (c *Counter) Get() int

Returns the current value of the counter.

func (*Counter) Inc

func (c *Counter) Inc()

Increases the value of the counter Will block the goroutine when the value is max until it can be increased again.

func (*Counter) TryInc

func (c *Counter) TryInc() error

Tries to increase the value, if the value is max an error will be returned.

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]) Clear

func (s *Slice[T]) Clear()

Clears all elements from the slice.

func (*Slice[T]) Copy

func (s *Slice[T]) Copy(n uint) []T

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

func (s *Slice[T]) Find(find func(T) bool) (T, bool)

Returns the element found that fulfills the given function and a boolean indicating whether it was or not found.

func (*Slice[T]) Get

func (s *Slice[T]) Get(index uint) (T, bool)

Returns the element located at a certain index and a boolean indicating if it exists (array indexing starts at 0).

func (*Slice[T]) Has

func (s *Slice[T]) Has(v T) bool

Returns true if the given element exists in the slice, returns false otherwise.

func (*Slice[T]) Len

func (s *Slice[T]) Len() int

Returns the amount of elements present in the slice.

func (*Slice[T]) Remove

func (s *Slice[T]) Remove(val T)

Removes an element from the slice by reallocating the indexes, which means that, depending on the size of the slice, it might be a costly operation.

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]) Add

func (t *Table[I, T]) Add(i I, v T)

Adds an element to the table.

func (*Table[I, T]) Clear

func (t *Table[I, T]) Clear()

Clears all elements from the table.

func (*Table[I, T]) Get

func (t *Table[I, T]) Get(i I) (T, bool)

Returns an element from the table and a boolean specifying if it exists.

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.

func (*Table[I, T]) Indexes

func (t *Table[I, T]) Indexes() []I

Returns all indexes of the table

func (*Table[I, T]) Len

func (t *Table[I, T]) Len() int

Returns the amount of elements present in the table

func (*Table[I, T]) Remove

func (t *Table[I, T]) Remove(i I)

Removes an element from the table, no error will be reported if its not found.

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

func NewWaitlist[T any](cap uint, sort func(T, T) int) Waitlist[T]

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]) Clear

func (w *Waitlist[T]) Clear()

Clears all elements from the waitlist.

func (*Waitlist[T]) Get

func (w *Waitlist[T]) Get(ctx context.Context, find func(T) bool) (T, error)

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.

func (*Waitlist[T]) Timeout

func (w *Waitlist[T]) Timeout(ctx context.Context)

Wakes up all threads once the context timeout ends

func (*Waitlist[T]) TryGet

func (w *Waitlist[T]) TryGet(find func(T) bool) (T, bool)

Tries to retrieve an element from the waitlist that fulfills the given function and returns true/false depending on if the element was or not found.

Jump to

Keyboard shortcuts

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