gwrap

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 4 Imported by: 5

README

gwrap - golang generic wrappers for standard library functions

GoDoc unit tests report card codecov

Install:

go get github.com/muir/gwrap

This package is a collection of generic functions that wrap standard library functions. Hopefully, this packge will quickly become obsolete because the library functions will support generics directly. Until then, there is this.

SyncMap

SyncMap is a wrapper around sync.Map supporting the go 1.18 sync.Map

CompareMap

CompareMap is a wrapper around sync.Map supporting the go 1.20 sync.Map. CompareMap is only available when compiling with go 1.20 and above.

AtomicValue

AtomicValue is a wrapper around sync/atomic.Value.

SyncPool

SyncPool is a wrapper for sync.Pool

Heap

Heap is a heap-like wrapper for container/heap

PriorityQueue

PriorityQueue is a wrapper for container/heap that implements priority queues.

PriorityQueue supports removing arbitrary items from the queue at any point. To support that, the items in the queue must implement the PQItem interface. The simplest way to implement that interface is to embed PQItemEmbed in the items that will be in the priority queue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicComparable added in v0.1.0

type AtomicComparable[T comparable] struct {
	AtomicValue[T]
}

func (*AtomicComparable[T]) CompareAndSwap added in v0.1.0

func (av *AtomicComparable[T]) CompareAndSwap(old T, new T) (swapped bool)

CompareAndSwap exchanges values if the current value matches the passed in old value.

type AtomicValue added in v0.1.0

type AtomicValue[T any] struct {
	Value atomic.Value
}

Atomic value is a wrapper around sync/atomic.Value. AtomicValues should not be copied after first use

func (*AtomicValue[T]) Load added in v0.1.0

func (av *AtomicValue[T]) Load() T

Load fetches a value

func (*AtomicValue[T]) Store added in v0.1.0

func (av *AtomicValue[T]) Store(v T)

Store stores a value

func (*AtomicValue[T]) Swap added in v0.1.0

func (av *AtomicValue[T]) Swap(new T) T

Swap exchanges a value for the current value. If there is no current value, the zero value of T is returned. Unlike sync/atomic.Value_Swap, the nil value is allowed if T is a pointer type.

type CompareMap

type CompareMap[K comparable, V comparable] struct {
	SyncMap[K, V]
}

CompareMap is a wrapper of sync.Map. It uses generics so that casting is not required. All methods directly correspond to sync.Map methods. Do not copy a SyncMap after you start using it.

CompareMap is only available when compiling with go 1.20 and above.

CompareMap requires that values be comparable which is not true of SyncMap.

func (*CompareMap[K, V]) CompareAndDelete

func (m *CompareMap[K, V]) CompareAndDelete(key K, old V) (deleted bool)

CompareAndDelete will delete a value from the map if the current value matches the provided value.

func (*CompareMap[K, V]) CompareAndSwap

func (m *CompareMap[K, V]) CompareAndSwap(key K, old V, new V) bool

CompareAndSwap will replace a value in the map if the current value matches the provided "old" value.

type Heap added in v0.3.0

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

Heap is implemented with container/heap and is not thread-safe

func NewHeap added in v0.3.0

func NewHeap[T any](less func(a T, b T) bool) *Heap[T]

NewHeap creates a Heap from a comparison function.

func (Heap[T]) Len added in v0.3.0

func (h Heap[T]) Len() int

func (*Heap[T]) Pop added in v0.3.0

func (h *Heap[T]) Pop() T

Pop is O(log n)

func (*Heap[T]) Push added in v0.3.0

func (h *Heap[T]) Push(x T)

Push is O(log n)

type PQItem added in v0.3.0

type PQItem[P constraints.Ordered] interface {
	PQItem() *PQItemEmbed[P]
}

PQItem is the interface that items in PriorityQueue must implement. Embedding PQItemEmbed is enough to satisfy the interface.

type PQItemEmbed added in v0.3.0

type PQItemEmbed[P constraints.Ordered] struct {
	// contains filtered or unexported fields
}

PQItemEmbed may be embeded in the priority queue item's data structure, or the data structure can have a PQItemEmbed that it returns when the PQItem() method is called.

func (*PQItemEmbed[P]) PQItem added in v0.3.0

func (pqe *PQItemEmbed[P]) PQItem() *PQItemEmbed[P]

type PriorityQueue added in v0.3.0

type PriorityQueue[P constraints.Ordered, T PQItem[P]] struct {
	// contains filtered or unexported fields
}

PriorityQueue is implemented with container/heap and is not thread-safe.

func NewPriorityQueue added in v0.3.0

func NewPriorityQueue[P constraints.Ordered, T PQItem[P]]() *PriorityQueue[P, T]

NewPriorityQueue creates a PriorityQueue from a comparison function.

func (*PriorityQueue[P, T]) Dequeue added in v0.3.0

func (h *PriorityQueue[P, T]) Dequeue() T

Dequeue is O(log n)

func (*PriorityQueue[P, T]) Enqueue added in v0.3.0

func (h *PriorityQueue[P, T]) Enqueue(x T, priority P)

Push is O(log n)

func (PriorityQueue[P, T]) Len added in v0.3.0

func (h PriorityQueue[P, T]) Len() int

func (*PriorityQueue[P, T]) Remove added in v0.3.0

func (h *PriorityQueue[P, T]) Remove(x T)

Remove takes an item out of the priority queue

type SyncMap

type SyncMap[K comparable, V any] struct {
	sync.Map
}

SyncMap is a wrapper of sync.Map. It uses generics so that casting is not required. All methods directly correspond to sync.Map methods. Do not copy a SyncMap after you start using it.

func (*SyncMap[K, V]) Delete

func (m *SyncMap[K, V]) Delete(key K)

Delete removes an item from the map

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (value V, ok bool)

Load looks up an item in the map. The bool is true if the value was found in the map.

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete looks up an item in the map, removes it from the map, and returns it. The boolean is true if the item was present in the map.

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore looks up an item in the map. If present, it returns it and the returned boolean is true. If not present, the provided value is stored in the map and also returned.

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)

Range iterates over the values in the map. Iteration stops if the provided function returns false. The provided value is not safe to use because it can be already overwritten at the time it is received. (Or at least that's my reading of the sync.Map documentation).

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

Store puts a value into the map

func (*SyncMap[K, V]) Swap

func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap will exchange the provided value with the value in the map.

Swap is only available with go 1.20 and above

type SyncPool added in v0.2.0

type SyncPool[T any] struct {
	Pool sync.Pool
	New  func() T
}

func NewSyncPool added in v0.2.0

func NewSyncPool[T any](new func() T) *SyncPool[T]

func (*SyncPool[T]) Get added in v0.2.0

func (p *SyncPool[T]) Get() T

func (*SyncPool[T]) Put added in v0.2.0

func (p *SyncPool[T]) Put(i T)

Jump to

Keyboard shortcuts

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