densemap

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 1 Imported by: 0

README

densemap

Go Reference Go Report Card codecov

Package densemap provides a generic, dense, ID-based mapping structure for fast, contiguous lookups by integer ID. Unlike Go’s built-in map, densemap stores values in a slice indexed by a bounded integer range [minID, maxID]. This design ensures O(1) access, efficient iteration, and predictable memory usage, making it well-suited for cases where IDs are compact and naturally bounded (e.g., enums, small identifier spaces, event codes).

Install

Run go get github.com/jokruger/densemap

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DenseMap

type DenseMap[ID Integer, T any] struct {
	// contains filtered or unexported fields
}

DenseMap provides a generic, ID-based lookup structure optimized for fast, contiguous access to values of type T by integer IDs.

func New

func New[ID Integer, T any](minID, maxID ID) *DenseMap[ID, T]

New creates a new DenseMap with a range from minID to maxID (inclusive).

func (*DenseMap[ID, T]) Cap

func (dm *DenseMap[ID, T]) Cap() int

Cap returns the total number of possible values (capacity).

func (*DenseMap[ID, T]) Clear added in v1.0.2

func (dm *DenseMap[ID, T]) Clear()

Clear resets the DenseMap, removing all set values.

func (*DenseMap[ID, T]) Contains

func (dm *DenseMap[ID, T]) Contains(id ID) bool

Contains returns true if value is set for a given ID.

func (*DenseMap[ID, T]) Delete added in v1.0.2

func (dm *DenseMap[ID, T]) Delete(id ID) error

Delete removes the value associated with the ID and marks it as invalid.

func (*DenseMap[ID, T]) First added in v1.0.2

func (dm *DenseMap[ID, T]) First() (ID, *T)

First returns the first set ID and its associated value, or (zeroID, nil) if none are set.

func (*DenseMap[ID, T]) ForEach

func (dm *DenseMap[ID, T]) ForEach(fn func(id ID, value T))

ForEach iterates over all set values and applies the provided function to each value.

func (*DenseMap[ID, T]) IsEmpty added in v1.0.2

func (dm *DenseMap[ID, T]) IsEmpty() bool

IsEmpty returns true if no elements are set.

func (*DenseMap[ID, T]) Last added in v1.0.2

func (dm *DenseMap[ID, T]) Last() (ID, *T)

Last returns the last set ID and its associated value, or (zeroID, nil) if none are set.

func (*DenseMap[ID, T]) Len

func (dm *DenseMap[ID, T]) Len() int

Len returns the number of actually set elements.

func (*DenseMap[ID, T]) MaxID added in v1.0.3

func (dm *DenseMap[ID, T]) MaxID() ID

MaxID returns the maximum ID in the DenseMap.

func (*DenseMap[ID, T]) MinID added in v1.0.3

func (dm *DenseMap[ID, T]) MinID() ID

MinID returns the minimum ID in the DenseMap.

func (*DenseMap[ID, T]) Ptr added in v1.0.3

func (dm *DenseMap[ID, T]) Ptr(id ID) *T

Ptr retrieves pointer to the value associated with the ID. Returns *value if set, otherwise nil.

func (*DenseMap[ID, T]) Range added in v1.0.2

func (dm *DenseMap[ID, T]) Range(min, max ID, fn func(id ID, value T))

Range iterates over a specified range of IDs, applying the provided function to each set value within that range.

func (*DenseMap[ID, T]) Set

func (dm *DenseMap[ID, T]) Set(id ID, value T) error

Set stores a value for the given ID and marks it as valid.

func (*DenseMap[ID, T]) Value added in v1.0.3

func (dm *DenseMap[ID, T]) Value(id ID) (T, bool)

Value retrieves the value associated with the ID. Returns (value, true) if set, otherwise (zero, false).

type Integer

type Integer interface {
	~int8 | ~int16 | ~int32 | ~uint8 | ~uint16 | ~uint32
}

Jump to

Keyboard shortcuts

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