generic

package
v0.0.0-...-b0c7fd6 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaxHeap

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

MaxHeap is a comparator function to implement max-heap for any type that supports ordering.

func MinHeap

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

MinHeap is a comparator function to implement min-heap for any type that supports ordering.

Types

type Heap

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

Heap is a generic port of container.Heap. Not safe to use concurrently.

func NewHeap

func NewHeap[T any](less comparator[T]) *Heap[T]

NewHeap creates new heap data structure with given comparator.

func (*Heap[T]) Len

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

Len returns current number of elements on the structure.

func (*Heap[T]) Peek

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

Peek returns the top value without removing it from the heap. Will panic if the heap is empty.

func (*Heap[T]) Pop

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

Pop returns and removes the top element from the heap. Panics if there are no elements.

func (*Heap[T]) Push

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

Push adds new element to the heap in O(log n) time.

func (*Heap[T]) Reset

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

Reset removes all elements from the heap (without reducing the capacity).

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SyncMap is a generic wrapper around sync.Map that allows you to specify the key and value types to avoid type assertions and casting.

func (*SyncMap[K, V]) Delete

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

Delete deletes the value for a key.

func (*SyncMap[K, V]) Load

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

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*SyncMap[K, V]) LoadAndDelete

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

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*SyncMap[K, V]) LoadOrStore

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

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) Range

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

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

func (*SyncMap[K, V]) Store

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

Store sets the value for a key.

Jump to

Keyboard shortcuts

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