commoncollections

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: AGPL-3.0 Imports: 4 Imported by: 1

README

Common Collections for Go

Well known collections implemented in Go 1.18 with generics. commmoncollections contains regular mutable collections. safecollections contains immutable collections which are therefore inherently concurrent safe.

Currently Implemented

  • Queue
  • Set
  • Pool
  • Map [+safe]
  • Stack [+safe]
  • Slab Allocator

Planned

  • Balanced Binary Tree
  • BK Tree
  • B Tree

Documentation

Index

Constants

View Source
const (
	// Offset64 is the offset for FNV64
	Offset64 = 14695981039346656037
	// Prime64 is the prime for FNV64
	Prime64 = 1099511628211
)

Variables

This section is empty.

Functions

func FNV64

func FNV64(key []byte) uint64

FNV64 hash function

func Max

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

Max function for any type. Returns the bigger value of a and b. Returns b if equal.

func Min

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

Min function for any type. Returns the smaller value of a and b. Returns b if equal.

func SliceEquals

func SliceEquals[T comparable](a, b []T) bool

SliceEquals returns if two slices are equal

Types

type AllocatorRef

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

AllocatorRef is a reference to an allocated object allocated by a SlabAllocator. The object is guaranteed to be valid until the AllocatorRef is freed. The value of the object is undefined after its Freed. The value is not zeroed.

func (*AllocatorRef[T]) Get

func (ref *AllocatorRef[T]) Get() *T

Get returns the object referenced by the AllocatorRef.

type IntMap

type IntMap[V any] struct {
	// contains filtered or unexported fields
}

IntMap is an int to any map which might provide performance benefits over the std map. Insert and lookup speeds are often slightly faster. Also delete returns the value it held avoiding aditional lookups in some situations.

func NewIntMap

func NewIntMap[V any]() IntMap[V]

NewIntMap initialises a new intmap

func (*IntMap[V]) Delete

func (im *IntMap[V]) Delete(key uint64) (V, bool)

Delete removes a value from this map returns value,true or 0, false if the key wasnt in this map

func (*IntMap[V]) Get

func (im *IntMap[V]) Get(key uint64) (V, bool)

Get retrieves an item from the intmap and returns value, true or 0, false if the item isn't in this map.

func (*IntMap[V]) Put

func (im *IntMap[V]) Put(key uint64, val V)

Put adds an item to the int map

type NoLock

type NoLock byte

NoLock satisfies sync.Locker interface without locking

func NewNoLock

func NewNoLock() *NoLock

NewNoLock creates a new lock which satisfies sync.Locker interface but each operation is a no-op

func (NoLock) Lock

func (NoLock) Lock()

Lock is a no-op

func (NoLock) Unlock

func (NoLock) Unlock()

Unlock is a no-op

type OptLock

type OptLock uint32

OptLock type is used for an optimistic concurrency design. The lock provides a Read/Write facility. Writers are not prevented from acquiring the lock while readers are holding it. Readers must verify that the lock is still valid after reading their data.

Readers must first acquire the state by calling RLock() and then verify that the state is still valid after reading by calling RVerify().

Writers must first acquire the state by calling Lock() and release their lock after writing by calling Unlock().

The null value of the lock is usable.

Data protected by the lock must not panic when reads and writes happen concurrently even if those actions result in invalid data. The std map therefore cant be protected by the lock.

func NewOptLock

func NewOptLock() *OptLock

NewOptLock creates a new OptLock.

func (*OptLock) Lock

func (lock *OptLock) Lock()

Lock acquires the write lock. This operation blocks until the lock is acquired using exponential backoff.

func (*OptLock) RLock

func (lock *OptLock) RLock() (uint32, bool)

RLock acquires the read lock. It returns the state and if the lock was acquired. After reading readers must verify that the state is still valid by calling RVerify().

func (*OptLock) RVerify

func (lock *OptLock) RVerify(expected uint32) bool

RVerify verifies that the state is still valid. It returns if read data is still valid (true) or might be corrupted by reads (false).

func (*OptLock) Unlock

func (lock *OptLock) Unlock()

Unlock releases the write lock. It panics when the lock wasn't locked.

type Pool

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

Pool stores a set of items. It can returned a pooled or a new one when an item is retrieved. A pool might be synced for parallel access see NewSyncPool.

func NewPool

func NewPool[T any](factory func() T) Pool[T]

NewPool creates a new Pool which is not safe to access from multiple goroutines. For a safe implementation us NewSyncPool

func NewSyncPool

func NewSyncPool[T any](factory func() T) Pool[T]

NewSyncPool creates a new Pool which is safe to access from multiple goroutines simultaneously.

The Pool requires locking for this and might therefore be slower for

func (*Pool[T]) Get

func (pool *Pool[T]) Get() T

Get returns an item from the pool if any is available or creates a new one.

func (*Pool[T]) Put

func (pool *Pool[T]) Put(elem T)

Put adds a the given element to the Pool.

type Queue

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

Queue type for an unbound FIFO queue

func NewQueue

func NewQueue[T any]() Queue[T]

NewQueue creates a new Queue.

func (*Queue[T]) Len

func (queue *Queue[T]) Len() int

func (*Queue[T]) Peek

func (queue *Queue[T]) Peek() (T, bool)

Peek reads the last element from the queue without changing the queue. Returns the element and true if an element is present and returns the nilvalue and false if no element is left.

func (*Queue[T]) Pop

func (queue *Queue[T]) Pop() (T, bool)

Pop reads the last element from the queue removing it. It returns the element and true if an element is present and returns the nilvalue and false if no element is left.

func (*Queue[T]) Push

func (queue *Queue[T]) Push(elem T)

Push adds an element to the back of the queue. This might increase the size of the internal buffer.

type RWSpinLock

type RWSpinLock int32

RWSpinLock type is a read/write lock. It is more efficient than sync.RWMutex but has less safety checks.

func NewRWSpinLock

func NewRWSpinLock() RWSpinLock

NewRWSpinLock creates a new RWSpinLock.

func (*RWSpinLock) Lock

func (lock *RWSpinLock) Lock()

Lock locks the RWSpinLock for writing. It blocks until no readers are reading.

func (*RWSpinLock) RLock

func (lock *RWSpinLock) RLock()

RLock locks the RWSpinLock for reading. Blocks if the lock is held for writing until the lock is unlocked.

func (*RWSpinLock) RUnlock

func (lock *RWSpinLock) RUnlock()

RUnlock unlocks the RWSpinLock for reading.

func (*RWSpinLock) Unlock

func (lock *RWSpinLock) Unlock()

Unlock unlocks the RWSpinLock for writing.

type RingBuffer

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

A RingBuffer is a FIFO queue with a fixed size. If the buffer is full, the oldest element is overwritten.

func NewRingBuffer

func NewRingBuffer[T any](capacity uint64) RingBuffer[T]

NewRingBuffer creates a new RingBuffer. The capacity must be greater than zero.

func (*RingBuffer[T]) Cap

func (ring *RingBuffer[T]) Cap() uint64

Cap returns the maximum number of elements in the buffer.

func (*RingBuffer[T]) Get

func (ring *RingBuffer[T]) Get() (T, bool)

Get reads the last element from the queue removing it. If no item is present an invalid value and false is returned. Otherwise the last item is returned and true.

func (*RingBuffer[T]) Put

func (ring *RingBuffer[T]) Put(value T)

Puts adds an element to the buffer.

func (*RingBuffer[T]) Size

func (ring *RingBuffer[T]) Size() uint64

Size returns the number of elements in the buffer.

type Set

type Set[T comparable] map[T]struct{}

Set type using builtin map as container. A set contains every item at most once.

func NewSet

func NewSet[T comparable]() Set[T]

NewSet creates a new set

func (Set[T]) Contains

func (set Set[T]) Contains(elem T) bool

Contains returns true if the set contains elem and false otherwise.

func (Set[T]) Insert

func (set Set[T]) Insert(elem T)

Insert adds the item elem to the set.

func (Set[T]) Len

func (set Set[T]) Len() int

Len returs the size of the set

func (Set[T]) Remove

func (set Set[T]) Remove(elem T)

Remove removes the given element from the map

func (Set[T]) Values

func (set Set[T]) Values() []T

Values returns all values in the set It returns a new slice with a copy of the elements in it

type SlabAllocator

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

SlabAllocator is a memory allocator that allocates a fixed size block of memory and returns references to the allocated values.

The allocator might grow in size if more memory is needed. It is not threadsafe. Locks and bound checks are not implemented and must be done by the caller if needed.

func NewSlabAllocator

func NewSlabAllocator[T any](size int) SlabAllocator[T]

NewSlabAllocator creates a new SlabAllocator which holds size number of elements.

func (*SlabAllocator[T]) Allocate

func (allocator *SlabAllocator[T]) Allocate() *AllocatorRef[T]

Allocate allocates a new object from the allocator. Might trigger a reallocation if the allocator is exhausted.

func (*SlabAllocator[T]) Free

func (allocator *SlabAllocator[T]) Free(ref *AllocatorRef[T])

Free returns the object to the allocator.

type SpinLock

type SpinLock uint32

SpinLock implements and statisfies sync.Locker using a SpinLock as described here https://en.wikipedia.org/wiki/Spinlock with an exponential backoff described here https://en.wikipedia.org/wiki/Exponential_backoff

The zero-value of the spinlock is valid and can be initialised with SpinLock(0) SpinLock must not be copied after first use.

func NewSpinLock

func NewSpinLock() *SpinLock

NewSpinLock creates a new spinlock

func (*SpinLock) Lock

func (s *SpinLock) Lock()

Lock locks the SpinLock or waits until its available using exponential backoff.

func (*SpinLock) Unlock

func (s *SpinLock) Unlock()

Unlock unlocks the locks. Panics when the lock wasn't locked

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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