ds

package
v0.30.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ds provides generic data structures for use in event sourcing systems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[T MapFactory[T]] struct {
	// contains filtered or unexported fields
}

func NewMap

func NewMap[T MapFactory[T]]() *Map[T]

func (*Map[T]) Data

func (i *Map[T]) Data() map[string]*T

func (*Map[T]) Ensure

func (i *Map[T]) Ensure(id string) (e *T)

func (*Map[T]) Keys

func (i *Map[T]) Keys() *Set[string]

func (*Map[T]) Len

func (i *Map[T]) Len() int

func (*Map[T]) MarshalJSON

func (i *Map[T]) MarshalJSON() ([]byte, error)

func (*Map[T]) Remove

func (i *Map[T]) Remove(id string)

func (*Map[T]) UnmarshalJSON

func (i *Map[T]) UnmarshalJSON(data []byte) error

type MapFactory

type MapFactory[T any] interface{ Create(id string) *T }

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is an ordered set that maintains both O(1) membership testing and insertion order preservation. This is useful for deterministic iteration in event sourcing scenarios.

Mutation Semantics

The following methods mutate the receiver:

  • Add, Extend, Remove, Merge, Clear, SetValues

The following methods return new sets without modifying the receiver:

  • Filter, Copy, Intersect, Additions, Removals, Diff, Values

func NewSet

func NewSet[T comparable](items ...T) *Set[T]

NewSet creates a new set with the given items.

func (*Set[T]) Add

func (s *Set[T]) Add(id T)

Add adds the given id to the set. No-op if already present. (mutates)

func (*Set[T]) Additions

func (s *Set[T]) Additions(other *Set[T]) (add *Set[T])

Additions returns a new set that contains all elements that are present in the other set but not in the receiver (s). In other words: the elements you need to add to s to obtain other. The order follows the other's insertion order.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear removes all elements from the set. (mutates)

func (*Set[T]) Contains

func (s *Set[T]) Contains(v T) bool

Contains returns true if v is present in the set.

func (*Set[T]) ContainsAll

func (s *Set[T]) ContainsAll(other *Set[T]) bool

ContainsAll returns true if all elements of other are present in s.

func (*Set[T]) ContainsAny

func (s *Set[T]) ContainsAny(other *Set[T]) bool

ContainsAny returns true if at least one element of other is present in s.

func (*Set[T]) ContainsValues

func (s *Set[T]) ContainsValues(ids ...T) bool

ContainsValues returns true if all given ids are present in the set.

func (*Set[T]) Copy

func (s *Set[T]) Copy() *Set[T]

Copy returns a new set with the same elements and order.

func (*Set[T]) Diff

func (s *Set[T]) Diff(other *Set[T]) (add *Set[T], remove *Set[T])

Diff computes the transition needed to go from the current set (s) to the target set (other). It returns two ordered sets:

  • add: elements to add to s (present in other but not in s), ordered by other's insertion order
  • remove: elements to remove from s (present in s but not in other), ordered by s's insertion order

func (*Set[T]) Eq

func (s *Set[T]) Eq(other *Set[T]) bool

Eq returns true if both sets contain the same elements (order is ignored).

func (*Set[T]) EqValues

func (s *Set[T]) EqValues(ids ...T) bool

EqValues returns true if the set contains exactly the given ids.

func (*Set[T]) Extend

func (s *Set[T]) Extend(ids ...T) *Set[T]

Extend adds all given ids to the set and returns a new set containing only the elements that were actually added (i.e., not already present). (mutates)

func (*Set[T]) Filter

func (s *Set[T]) Filter(fn func(T) bool) *Set[T]

Filter returns a new set containing only elements for which fn returns true. The order of the resulting set preserves the receiver's insertion order.

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(fn func(T))

ForEach iterates over all elements in insertion order, calling fn for each. This is more efficient than Values() when you don't need a slice copy.

func (*Set[T]) Intersect

func (s *Set[T]) Intersect(other *Set[T]) *Set[T]

Intersect returns a new set that contains all elements that are present in both the receiver (s) and the other set. The order of the resulting set preserves the insertion order of the receiver (left-hand) set.

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

func (*Set[T]) IsSubsetOf

func (s *Set[T]) IsSubsetOf(other *Set[T]) bool

IsSubsetOf returns true if all elements of s are contained in other. An empty set is a subset of any set.

func (*Set[T]) IsSupersetOf

func (s *Set[T]) IsSupersetOf(other *Set[T]) bool

IsSupersetOf returns true if s contains all elements of other. Any set is a superset of the empty set.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of elements in the set.

func (Set[T]) MarshalJSON

func (s Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON serializes the set as an ordered JSON array.

func (*Set[T]) Merge

func (s *Set[T]) Merge(other *Set[T])

Merge adds all elements from other to s. (mutates)

func (*Set[T]) Removals

func (s *Set[T]) Removals(other *Set[T]) (remove *Set[T])

Removals returns a new set that contains all elements that are present in the receiver (s) but not in other. In other words: the elements you need to remove from s to obtain other. The order follows the receiver's insertion order.

func (*Set[T]) Remove

func (s *Set[T]) Remove(ids ...T)

Remove removes the given ids from the set. (mutates) This operation is O(n) where n is the set size.

func (*Set[T]) SetValues

func (s *Set[T]) SetValues(items ...T)

SetValues replaces all elements with the given items. (mutates)

func (*Set[T]) String

func (s *Set[T]) String() string

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes a JSON array into the set.

func (*Set[T]) Values

func (s *Set[T]) Values() []T

Values returns a copy of the elements in insertion order.

type StringSet

type StringSet = Set[string]

func NewStringSet

func NewStringSet(items ...string) *StringSet

NewStringSet creates a new string set with the given items.

Jump to

Keyboard shortcuts

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