Documentation
¶
Overview ¶
Package ds provides generic data structures for use in event sourcing systems.
Index ¶
- type Map
- type MapFactory
- type Set
- func (s *Set[T]) Add(id T)
- func (s *Set[T]) Additions(other *Set[T]) (add *Set[T])
- func (s *Set[T]) Clear()
- func (s *Set[T]) Contains(v T) bool
- func (s *Set[T]) ContainsAll(other *Set[T]) bool
- func (s *Set[T]) ContainsAny(other *Set[T]) bool
- func (s *Set[T]) ContainsValues(ids ...T) bool
- func (s *Set[T]) Copy() *Set[T]
- func (s *Set[T]) Diff(other *Set[T]) (add *Set[T], remove *Set[T])
- func (s *Set[T]) Eq(other *Set[T]) bool
- func (s *Set[T]) EqValues(ids ...T) bool
- func (s *Set[T]) Extend(ids ...T) *Set[T]
- func (s *Set[T]) Filter(fn func(T) bool) *Set[T]
- func (s *Set[T]) ForEach(fn func(T))
- func (s *Set[T]) Intersect(other *Set[T]) *Set[T]
- func (s *Set[T]) IsEmpty() bool
- func (s *Set[T]) IsSubsetOf(other *Set[T]) bool
- func (s *Set[T]) IsSupersetOf(other *Set[T]) bool
- func (s *Set[T]) Len() int
- func (s Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Merge(other *Set[T])
- func (s *Set[T]) Removals(other *Set[T]) (remove *Set[T])
- func (s *Set[T]) Remove(ids ...T)
- func (s *Set[T]) SetValues(items ...T)
- func (s *Set[T]) String() string
- func (s *Set[T]) UnmarshalJSON(data []byte) error
- func (s *Set[T]) Values() []T
- type StringSet
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]) MarshalJSON ¶
func (*Map[T]) UnmarshalJSON ¶
type MapFactory ¶
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 ¶
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]) ContainsAll ¶
ContainsAll returns true if all elements of other are present in s.
func (*Set[T]) ContainsAny ¶
ContainsAny returns true if at least one element of other is present in s.
func (*Set[T]) ContainsValues ¶
ContainsValues returns true if all given ids are present in the set.
func (*Set[T]) Diff ¶
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]) Extend ¶
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 ¶
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 ¶
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]) IsSubsetOf ¶
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 ¶
IsSupersetOf returns true if s contains all elements of other. Any set is a superset of the empty set.
func (Set[T]) MarshalJSON ¶
MarshalJSON serializes the set as an ordered JSON array.
func (*Set[T]) Removals ¶
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]) UnmarshalJSON ¶
UnmarshalJSON deserializes a JSON array into the set.