Comparer allows the comparison of two keys for the purpose of sorting.
type Comparer[K any] interface { // Returns -1 if a is less than b, returns 1 if a is greater than b, // and returns 0 if a is equal to b. Compare(a, b K) int }
func NewComparer[K any](key K) Comparer[K]
NewComparer returns the built-in comparer for a given key type. Note that only int-ish and string-ish types are supported, despite the 'comparable' constraint. Attempts to use other types will result in a panic - users should define their own Comparers for these cases.
Hasher hashes keys and checks them for equality.
type Hasher[K any] interface { // Computes a hash for key. Hash(key K) uint32 // Returns true if a and b are equal. Equal(a, b K) bool }
func NewHasher[K any](key K) Hasher[K]
NewHasher returns the built-in hasher for a given key type.
List is a dense, ordered, indexed collections. They are analogous to slices in Go. They can be updated by appending to the end of the list, prepending values to the beginning of the list, or updating existing indexes in the list.
type List[T any] struct { // contains filtered or unexported fields }
func NewList[T any](values ...T) *List[T]
NewList returns a new empty instance of List.
func (l *List[T]) Append(value T) *List[T]
Append returns a new list with value added to the end of the list.
▹ Example
func (l *List[T]) Get(index int) T
Get returns the value at the given index. Similar to slices, this method will panic if index is below zero or is greater than or equal to the list size.
func (l *List[T]) Iterator() *ListIterator[T]
Iterator returns a new iterator for this list positioned at the first index.
▹ Example
▹ Example (Reverse)
func (l *List[T]) Len() int
Len returns the number of elements in the list.
func (l *List[T]) Prepend(value T) *List[T]
Prepend returns a new list with value(s) added to the beginning of the list.
▹ Example
func (l *List[T]) Set(index int, value T) *List[T]
Set returns a new list with value set at index. Similar to slices, this method will panic if index is below zero or if the index is greater than or equal to the list size.
▹ Example
func (l *List[T]) Slice(start, end int) *List[T]
Slice returns a new list of elements between start index and end index. Similar to slices, this method will panic if start or end are below zero or greater than the list size. A panic will also occur if start is greater than end.
Unlike Go slices, references to inaccessible elements will be automatically removed so they can be garbage collected.
▹ Example
ListBuilder represents an efficient builder for creating new Lists.
type ListBuilder[T any] struct { // contains filtered or unexported fields }
func NewListBuilder[T any]() *ListBuilder[T]
NewListBuilder returns a new instance of ListBuilder.
func (b *ListBuilder[T]) Append(value T)
Append adds value to the end of the list.
▹ Example
func (b *ListBuilder[T]) Get(index int) T
Get returns the value at the given index. Similar to slices, this method will panic if index is below zero or is greater than or equal to the list size.
func (b *ListBuilder[T]) Iterator() *ListIterator[T]
Iterator returns a new iterator for the underlying list.
func (b *ListBuilder[T]) Len() int
Len returns the number of elements in the underlying list.
func (b *ListBuilder[T]) List() *List[T]
List returns the current copy of the list. The builder should not be used again after the list after this call.
func (b *ListBuilder[T]) Prepend(value T)
Prepend adds value to the beginning of the list.
▹ Example
func (b *ListBuilder[T]) Set(index int, value T)
Set updates the value at the given index. Similar to slices, this method will panic if index is below zero or if the index is greater than or equal to the list size.
▹ Example
func (b *ListBuilder[T]) Slice(start, end int)
Slice updates the list with a sublist of elements between start and end index. See List.Slice() for more details.
▹ Example
ListIterator represents an ordered iterator over a list.
type ListIterator[T any] struct { // contains filtered or unexported fields }
func (itr *ListIterator[T]) Done() bool
Done returns true if no more elements remain in the iterator.
func (itr *ListIterator[T]) First()
First positions the iterator on the first index. If source list is empty then no change is made.
func (itr *ListIterator[T]) Last()
Last positions the iterator on the last index. If source list is empty then no change is made.
func (itr *ListIterator[T]) Next() (index int, value T)
Next returns the current index and its value & moves the iterator forward. Returns an index of -1 if the there are no more elements to return.
func (itr *ListIterator[T]) Prev() (index int, value T)
Prev returns the current index and value and moves the iterator backward. Returns an index of -1 if the there are no more elements to return.
func (itr *ListIterator[T]) Seek(index int)
Seek moves the iterator position to the given index in the list. Similar to Go slices, this method will panic if index is below zero or if the index is greater than or equal to the list size.
Map represents an immutable hash map implementation. The map uses a Hasher to generate hashes and check for equality of key values.
It is implemented as an Hash Array Mapped Trie.
type Map[K, V any] struct { // contains filtered or unexported fields }
func NewMap[K, V any](hasher Hasher[K]) *Map[K, V]
NewMap returns a new instance of Map. If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. Default hasher implementations only exist for int, string, and byte slice types.
func NewMapOf[K comparable, V any](hasher Hasher[K], entries map[K]V) *Map[K, V]
NewMapOf returns a new instance of Map, containing a map of provided entries.
If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. Default hasher implementations only exist for int, string, and byte slice types.
func (m *Map[K, V]) Delete(key K) *Map[K, V]
Delete returns a map with the given key removed. Removing a non-existent key will cause this method to return the same map.
▹ Example
func (m *Map[K, V]) Get(key K) (value V, ok bool)
Get returns the value for a given key and a flag indicating whether the key exists. This flag distinguishes a nil value set on a key versus a non-existent key in the map.
func (m *Map[K, V]) Iterator() *MapIterator[K, V]
Iterator returns a new iterator for the map.
▹ Example
func (m *Map[K, V]) Len() int
Len returns the number of elements in the map.
func (m *Map[K, V]) Set(key K, value V) *Map[K, V]
Set returns a map with the key set to the new value. A nil value is allowed.
This function will return a new map even if the updated value is the same as the existing value because Map does not track value equality.
▹ Example
MapBuilder represents an efficient builder for creating Maps.
type MapBuilder[K, V any] struct { // contains filtered or unexported fields }
func NewMapBuilder[K, V any](hasher Hasher[K]) *MapBuilder[K, V]
NewMapBuilder returns a new instance of MapBuilder.
func (b *MapBuilder[K, V]) Delete(key K)
Delete removes the given key. See Map.Delete() for additional details.
▹ Example
func (b *MapBuilder[K, V]) Get(key K) (value V, ok bool)
Get returns the value for the given key.
func (b *MapBuilder[K, V]) Iterator() *MapIterator[K, V]
Iterator returns a new iterator for the underlying map.
func (b *MapBuilder[K, V]) Len() int
Len returns the number of elements in the underlying map.
func (b *MapBuilder[K, V]) Map() *Map[K, V]
Map returns the underlying map. Only call once. Builder is invalid after call. Will panic on second invocation.
func (b *MapBuilder[K, V]) Set(key K, value V)
Set sets the value of the given key. See Map.Set() for additional details.
▹ Example
MapIterator represents an iterator over a map's key/value pairs. Although map keys are not sorted, the iterator's order is deterministic.
type MapIterator[K, V any] struct { // contains filtered or unexported fields }
func (itr *MapIterator[K, V]) Done() bool
Done returns true if no more elements remain in the iterator.
func (itr *MapIterator[K, V]) First()
First resets the iterator to the first key/value pair.
func (itr *MapIterator[K, V]) Next() (key K, value V, ok bool)
Next returns the next key/value pair. Returns a nil key when no elements remain.
Set represents a collection of unique values. The set uses a Hasher to generate hashes and check for equality of key values.
Internally, the Set stores values as keys of a Map[T,struct{}]
type Set[T any] struct { // contains filtered or unexported fields }
func NewSet[T any](hasher Hasher[T], values ...T) Set[T]
NewSet returns a new instance of Set.
If hasher is nil, a default hasher implementation will automatically be chosen based on the first key added. Default hasher implementations only exist for int, string, and byte slice types. NewSet can also take some initial values as varargs.
func (s Set[T]) Add(value T) Set[T]
Add returns a set containing the new value.
This function will return a new set even if the set already contains the value.
func (s Set[T]) Delete(value T) Set[T]
Delete returns a set with the given key removed.
func (s Set[T]) Has(val T) bool
Has returns true when the set contains the given value
func (s Set[T]) Items() []T
Items returns a slice of the items inside the set
func (s Set[T]) Iterator() *SetIterator[T]
Iterator returns a new iterator for this set positioned at the first value.
func (s Set[K]) Len() int
Len returns the number of elements in the underlying map.
type SetBuilder[T any] struct { // contains filtered or unexported fields }
func NewSetBuilder[T any](hasher Hasher[T]) *SetBuilder[T]
func (s SetBuilder[T]) Delete(val T)
func (s SetBuilder[T]) Has(val T) bool
func (s SetBuilder[T]) Len() int
func (s SetBuilder[T]) Set(val T)
SetIterator represents an iterator over a set. Iteration can occur in natural or reverse order based on use of Next() or Prev().
type SetIterator[T any] struct { // contains filtered or unexported fields }
func (itr *SetIterator[T]) Done() bool
Done returns true if no more values remain in the iterator.
func (itr *SetIterator[T]) First()
First moves the iterator to the first value.
func (itr *SetIterator[T]) Next() (val T, ok bool)
Next moves the iterator to the next value.
SortedMap represents a map of key/value pairs sorted by key. The sort order is determined by the Comparer used by the map.
This map is implemented as a B+tree.
type SortedMap[K, V any] struct { // contains filtered or unexported fields }
func NewSortedMap[K, V any](comparer Comparer[K]) *SortedMap[K, V]
NewSortedMap returns a new instance of SortedMap. If comparer is nil then a default comparer is set after the first key is inserted. Default comparers exist for int, string, and byte slice keys.
func NewSortedMapOf[K comparable, V any](comparer Comparer[K], entries map[K]V) *SortedMap[K, V]
NewSortedMapOf returns a new instance of SortedMap, containing a map of provided entries.
If comparer is nil then a default comparer is set after the first key is inserted. Default comparers exist for int, string, and byte slice keys.
func (m *SortedMap[K, V]) Delete(key K) *SortedMap[K, V]
Delete returns a copy of the map with the key removed. Returns the original map if key does not exist.
▹ Example
func (m *SortedMap[K, V]) Get(key K) (V, bool)
Get returns the value for a given key and a flag indicating if the key is set. The flag can be used to distinguish between a nil-set key versus an unset key.
func (m *SortedMap[K, V]) Iterator() *SortedMapIterator[K, V]
Iterator returns a new iterator for this map positioned at the first key.
▹ Example
func (m *SortedMap[K, V]) Len() int
Len returns the number of elements in the sorted map.
func (m *SortedMap[K, V]) Set(key K, value V) *SortedMap[K, V]
Set returns a copy of the map with the key set to the given value.
▹ Example
SortedMapBuilder represents an efficient builder for creating sorted maps.
type SortedMapBuilder[K, V any] struct { // contains filtered or unexported fields }
func NewSortedMapBuilder[K, V any](comparer Comparer[K]) *SortedMapBuilder[K, V]
NewSortedMapBuilder returns a new instance of SortedMapBuilder.
func (b *SortedMapBuilder[K, V]) Delete(key K)
Delete removes the given key. See SortedMap.Delete() for additional details.
▹ Example
func (b *SortedMapBuilder[K, V]) Get(key K) (value V, ok bool)
Get returns the value for the given key.
func (b *SortedMapBuilder[K, V]) Iterator() *SortedMapIterator[K, V]
Iterator returns a new iterator for the underlying map positioned at the first key.
func (b *SortedMapBuilder[K, V]) Len() int
Len returns the number of elements in the underlying map.
func (b *SortedMapBuilder[K, V]) Map() *SortedMap[K, V]
SortedMap returns the current copy of the map. The returned map is safe to use even if after the builder continues to be used.
func (b *SortedMapBuilder[K, V]) Set(key K, value V)
Set sets the value of the given key. See SortedMap.Set() for additional details.
▹ Example
SortedMapIterator represents an iterator over a sorted map. Iteration can occur in natural or reverse order based on use of Next() or Prev().
type SortedMapIterator[K, V any] struct { // contains filtered or unexported fields }
func (itr *SortedMapIterator[K, V]) Done() bool
Done returns true if no more key/value pairs remain in the iterator.
func (itr *SortedMapIterator[K, V]) First()
First moves the iterator to the first key/value pair.
func (itr *SortedMapIterator[K, V]) Last()
Last moves the iterator to the last key/value pair.
func (itr *SortedMapIterator[K, V]) Next() (key K, value V, ok bool)
Next returns the current key/value pair and moves the iterator forward. Returns a nil key if the there are no more elements to return.
func (itr *SortedMapIterator[K, V]) Prev() (key K, value V, ok bool)
Prev returns the current key/value pair and moves the iterator backward. Returns a nil key if the there are no more elements to return.
func (itr *SortedMapIterator[K, V]) Seek(key K)
Seek moves the iterator position to the given key in the map. If the key does not exist then the next key is used. If no more keys exist then the iteartor is marked as done.
type SortedSet[T any] struct { // contains filtered or unexported fields }
func NewSortedSet[T any](comparer Comparer[T], values ...T) SortedSet[T]
NewSortedSet returns a new instance of SortedSet.
If comparer is nil then a default comparer is set after the first key is inserted. Default comparers exist for int, string, and byte slice keys. NewSortedSet can also take some initial values as varargs.
func (s SortedSet[T]) Add(value T) SortedSet[T]
Add returns a set containing the new value.
This function will return a new set even if the set already contains the value.
func (s SortedSet[T]) Delete(value T) SortedSet[T]
Delete returns a set with the given key removed.
func (s SortedSet[T]) Has(val T) bool
Has returns true when the set contains the given value
func (s SortedSet[T]) Items() []T
Items returns a slice of the items inside the set
func (s SortedSet[T]) Iterator() *SortedSetIterator[T]
Iterator returns a new iterator for this set positioned at the first value.
func (s SortedSet[K]) Len() int
Len returns the number of elements in the underlying map.
type SortedSetBuilder[T any] struct { // contains filtered or unexported fields }
func NewSortedSetBuilder[T any](comparer Comparer[T]) *SortedSetBuilder[T]
func (s SortedSetBuilder[T]) Delete(val T)
func (s SortedSetBuilder[T]) Has(val T) bool
func (s SortedSetBuilder[T]) Len() int
func (s SortedSetBuilder[T]) Set(val T)
SortedSetIterator represents an iterator over a sorted set. Iteration can occur in natural or reverse order based on use of Next() or Prev().
type SortedSetIterator[T any] struct { // contains filtered or unexported fields }
func (itr *SortedSetIterator[T]) Done() bool
Done returns true if no more values remain in the iterator.
func (itr *SortedSetIterator[T]) First()
First moves the iterator to the first value.
func (itr *SortedSetIterator[T]) Last()
Last moves the iterator to the last value.
func (itr *SortedSetIterator[T]) Next() (val T, ok bool)
Next moves the iterator to the next value.
func (itr *SortedSetIterator[T]) Prev() (val T, ok bool)
Next moves the iterator to the previous value.
func (itr *SortedSetIterator[T]) Seek(val T)
Next moves the iterator to the given value.
If the value does not exist then the next value is used. If no more keys exist then the iterator is marked as done.