datastructures

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

datastructures

Generic data structure interfaces and implementations for Go.

Overview

This package defines interfaces for common data structures with support for:

  • Immutability: Freeze() creates immutable snapshots, Unfreeze() creates mutable copies
  • Concurrency: Thread-safe implementations with atomic compute operations
  • Generics: Type-safe collections using Go generics

Interfaces

Maps
  • Map[K, V] - Immutable key-value map
  • MutableMap[K, V] - Mutable key-value map
  • ConcurrentMap[K, V] - Thread-safe map with atomic operations
BiMaps (Bidirectional Maps)
  • BiMap[K, V] - Immutable bidirectional map (unique keys and values)
  • MutableBiMap[K, V] - Mutable bidirectional map
  • ConcurrentBiMap[K, V] - Thread-safe bidirectional map
Sets
  • Set[E] - Immutable set
  • MutableSet[E] - Mutable set with union, intersection, difference operations
  • ConcurrentSet[E] - Thread-safe set with atomic operations
Tables
  • Table[E] - Mutable 2D table with row/column operations
  • ImmutableTable[E] - Immutable 2D table

Subpackages

  • hashmap - Hash-based map implementations
  • hashset - Hash-based set implementations
  • bimap - Bidirectional map implementations

Key Types

// HashCode for custom hashable types
type HashCode uint64

// Hashable interface for types with custom equality and hashing
type Hashable[T any] interface {
    Equal(rhs T) bool
    HashCode() HashCode
}

Usage

import (
    "github.com/bronlabs/bron-crypto/pkg/base/datastructures/hashmap"
    "github.com/bronlabs/bron-crypto/pkg/base/datastructures/hashset"
)

// Create a mutable map
m := hashmap.NewComparable[string, int]()
m.Put("key", 42)

// Create an immutable snapshot
frozen := m.Freeze()

// Create a thread-safe map
concurrent := hashmap.NewConcurrentMap(m)

Documentation

Overview

Package datastructures provides generic data structure interfaces and implementations for Go.

See README.md for details.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidSize = errs.New("invalid size")

ErrInvalidSize is returned when an operation encounters a size mismatch or invalid size constraint.

Functions

This section is empty.

Types

type AbstractMap

type AbstractMap[K, V, C any] interface {
	// Size returns the number of key-value pairs in the map.
	Size() C
	// Iter returns an iterator over all key-value pairs.
	Iter() iter.Seq2[K, V]
}

AbstractMap defines the core operations for any map-like data structure. K is the key type, V is the value type, and C is the type used for size.

type AbstractMatrix

type AbstractMatrix[T, C any] interface {
	// Dimensions returns the number of rows (n) and columns (m) in the matrix.
	Dimensions() (n, m C)
	// Transpose returns a new matrix with rows and columns swapped.
	Transpose() T
	// SubMatrix returns a sub-matrix from row1 to row2 and col1 to col2 (exclusive).
	SubMatrix(row1, row2, col1, col2 int) T
	// IsSquare returns true if the matrix has equal rows and columns.
	IsSquare() bool
	// IsDiagonal returns true if all non-diagonal elements are zero.
	IsDiagonal() bool
}

AbstractMatrix defines basic matrix operations for any matrix-like data structure. T is the concrete matrix type and C is the type used for dimension counts.

type AbstractSet

type AbstractSet[E, C any] interface {
	// Cardinality returns the number of elements in the set.
	Cardinality() C
	// Iter returns an iterator over all elements in the set.
	Iter() iter.Seq[E]
	// Contains returns true if the element is in the set.
	Contains(e E) bool
}

AbstractSet defines the core operations for any set-like data structure. E is the element type and C is the type used for cardinality.

type BiMap

type BiMap[K, V any] interface {

	// Reverse returns a view of this bimap with keys and values swapped.
	Reverse() BiMap[V, K]
	// Unfreeze returns a mutable copy of this bimap.
	Unfreeze() MutableBiMap[K, V]
	// contains filtered or unexported methods
}

BiMap is an immutable bidirectional map where both keys and values are unique. Looking up by key or by value is equally efficient via the Reverse method.

type Clonable

type Clonable[T any] interface {
	// Clone creates and returns a deep copy of the receiver.
	Clone() T
}

Clonable represents a type that can be cloned.

type ConcurrentBiMap

type ConcurrentBiMap[K, V any] interface {

	// Reverse returns a thread-safe view of this bimap with keys and values swapped.
	Reverse() ConcurrentBiMap[V, K]
	// contains filtered or unexported methods
}

ConcurrentBiMap is a thread-safe bidirectional map supporting atomic compute operations. All methods are safe for concurrent use by multiple goroutines.

type ConcurrentMap

type ConcurrentMap[K, V any] interface {

	// Enumerate returns an iterator with index and MapEntry pairs.
	Enumerate() iter.Seq2[int, MapEntry[K, V]]
	// contains filtered or unexported methods
}

ConcurrentMap is a thread-safe map interface supporting atomic compute operations. All methods are safe for concurrent use by multiple goroutines.

type ConcurrentSet

type ConcurrentSet[E any] interface {
	// contains filtered or unexported methods
}

ConcurrentSet is a thread-safe set interface supporting atomic compute operations. All methods are safe for concurrent use by multiple goroutines.

type Equatable

type Equatable[T any] interface {
	// Equal checks if the receiver is equal to rhs.
	Equal(rhs T) bool
}

Equatable represents a type that can be compared for equality.

type HashCode

type HashCode uint64

HashCode represents a 64-bit hash code.

func DeriveHashCode

func DeriveHashCode[T ~[]byte](xs ...T) HashCode

DeriveHashCode derives a HashCode from one or more byte slices.

func (HashCode) Bytes

func (hc HashCode) Bytes() []byte

Bytes returns the byte representation of the HashCode in little-endian order.

func (HashCode) Combine

func (hc HashCode) Combine(xs ...HashCode) HashCode

Combine combines the current HashCode with additional HashCodes and returns a new HashCode.

type Hashable

type Hashable[T any] interface {
	Equatable[T]
	// HashCode returns the hash code of the receiver.
	HashCode() HashCode
}

Hashable represents a type that can be hashed and compared for equality.

type ImmutableTable

type ImmutableTable[E any] immutableTable[E, ImmutableTable[E]]

ImmutableTable is a read-only two-dimensional table of elements.

type Map

type Map[K, V any] interface {

	// Enumerate returns an iterator with index and MapEntry pairs.
	Enumerate() iter.Seq2[int, MapEntry[K, V]]
	// Unfreeze returns a mutable copy of this map.
	Unfreeze() MutableMap[K, V]
	// contains filtered or unexported methods
}

Map is an immutable map interface providing read-only access to key-value pairs. Use Unfreeze to obtain a mutable copy.

type MapEntry

type MapEntry[K any, V any] struct {
	Key   K
	Value V
}

MapEntry represents a key-value pair in a map.

type MutableBiMap

type MutableBiMap[K, V any] interface {

	// Reverse returns a view of this bimap with keys and values swapped.
	Reverse() MutableBiMap[V, K]
	// Freeze returns an immutable snapshot of this bimap.
	Freeze() BiMap[K, V]
	// contains filtered or unexported methods
}

MutableBiMap is a mutable bidirectional map where both keys and values are unique. Looking up by key or by value is equally efficient via the Reverse method.

type MutableMap

type MutableMap[K, V any] interface {

	// Enumerate returns an iterator with index and MapEntry pairs.
	Enumerate() iter.Seq2[int, MapEntry[K, V]]
	// Freeze returns an immutable snapshot of this map.
	Freeze() Map[K, V]
	// contains filtered or unexported methods
}

MutableMap is a mutable map interface supporting put, remove, and clear operations. Use Freeze to obtain an immutable snapshot.

type MutableSet

type MutableSet[E any] interface {

	// Freeze returns an immutable snapshot of this set.
	Freeze() Set[E]
	// contains filtered or unexported methods
}

MutableSet is a mutable set interface supporting add, remove, and clear operations. Use Freeze to obtain an immutable snapshot.

type Set

type Set[E any] interface {

	// Unfreeze returns a mutable copy of this set.
	Unfreeze() MutableSet[E]
	// contains filtered or unexported methods
}

Set is an immutable set interface providing read-only access to set elements. Use Unfreeze to obtain a mutable copy.

type Table

type Table[E any] mutableTable[E, Table[E]]

Table is a mutable two-dimensional table of elements supporting row and column operations.

Directories

Path Synopsis
Package bimap provides bidirectional map implementations for the datastructures interfaces.
Package bimap provides bidirectional map implementations for the datastructures interfaces.
Package hashmap provides hash-based map implementations for the datastructures interfaces.
Package hashmap provides hash-based map implementations for the datastructures interfaces.
Package hashset provides hash-based set implementations for the datastructures interfaces.
Package hashset provides hash-based set implementations for the datastructures interfaces.

Jump to

Keyboard shortcuts

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