Documentation
¶
Overview ¶
Package orderedset provides a red-black tree backed ordered collection of unique items.
Index ¶
- type OrderedSet
- func (s *OrderedSet[T]) Add(value T) bool
- func (s *OrderedSet[T]) AddCollection(collection collections.Collection[T])
- func (s *OrderedSet[T]) AddRange(values []T)
- func (s *OrderedSet[T]) All(predicate functions.PredicateFunc[T]) bool
- func (s *OrderedSet[T]) Any(predicate functions.PredicateFunc[T]) bool
- func (s *OrderedSet[T]) Clear()
- func (s *OrderedSet[T]) Contains(value T) bool
- func (s *OrderedSet[T]) Count() int
- func (s *OrderedSet[T]) Difference(other sets.Set[T]) sets.Set[T]
- func (s *OrderedSet[T]) Empty() bool
- func (s *OrderedSet[T]) Find(predicate functions.PredicateFunc[T]) collections.Element[T]
- func (s *OrderedSet[T]) FindAll(predicate functions.PredicateFunc[T]) []collections.Element[T]
- func (s *OrderedSet[T]) ForEach(f func(collections.Element[T]))
- func (s *OrderedSet[T]) Get(value T) collections.Element[T]
- func (s *OrderedSet[T]) Intersection(other sets.Set[T]) sets.Set[T]
- func (s *OrderedSet[T]) IsEmpty() bool
- func (s *OrderedSet[T]) Iterator() collections.Iterator[T]
- func (s *OrderedSet[T]) Map(f func(T) T) collections.Collection[T]
- func (s *OrderedSet[T]) Max() T
- func (s *OrderedSet[T]) Min() T
- func (s *OrderedSet[T]) Remove(key T) bool
- func (s *OrderedSet[T]) ReverseIterator() collections.Iterator[T]
- func (s *OrderedSet[T]) Select(predicate functions.PredicateFunc[T]) collections.Collection[T]
- func (s *OrderedSet[T]) SelectDeep(predicate functions.PredicateFunc[T]) collections.Collection[T]
- func (s *OrderedSet[T]) String() string
- func (s *OrderedSet[T]) TakeWhile(predicate functions.PredicateFunc[T]) collections.Iterator[T]
- func (s *OrderedSet[T]) ToSlice() []T
- func (s *OrderedSet[T]) ToSliceDeep() []T
- func (s *OrderedSet[T]) TreeWalk(action func(T) bool) bool
- func (s *OrderedSet[T]) Type() collections.CollectionType
- func (s *OrderedSet[T]) Union(other sets.Set[T]) sets.Set[T]
- func (s *OrderedSet[T]) UnlockedContains(value T) bool
- type OrderedSetIterator
- type OrderedSetOptionFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedSet ¶
type OrderedSet[T any] struct { local.InternalImpl // contains filtered or unexported fields }
OrderedSet stores an ordered collection of unique elements.
func New ¶
func New[T any](options ...OrderedSetOptionFunc[T]) *OrderedSet[T]
Constructs a new OrderedSet[T].
func (*OrderedSet[T]) Add ¶
func (s *OrderedSet[T]) Add(value T) bool
Add adds a value into the collection. Returns false if the value already exists; else true if it was added.
func (*OrderedSet[T]) AddCollection ¶
func (s *OrderedSet[T]) AddCollection(collection collections.Collection[T])
AddCollection inserts the values of the given collection into this set.
func (*OrderedSet[T]) AddRange ¶
func (s *OrderedSet[T]) AddRange(values []T)
AddRange adds a slice of values to the set.
func (*OrderedSet[T]) All ¶
func (s *OrderedSet[T]) All(predicate functions.PredicateFunc[T]) bool
All applies the predicate function to every element in the collection, and returns true if all elements match the predicate.
func (*OrderedSet[T]) Any ¶
func (s *OrderedSet[T]) Any(predicate functions.PredicateFunc[T]) bool
Any returns true for the first element found where the predicate function returns true. It returns false if no element matches the predicate.
func (*OrderedSet[T]) Clear ¶
func (s *OrderedSet[T]) Clear()
Clear removes all nodes from the tree.
func (*OrderedSet[T]) Contains ¶
func (s *OrderedSet[T]) Contains(value T) bool
Contains returns true if the given value exists in the set.
func (*OrderedSet[T]) Count ¶
func (s *OrderedSet[T]) Count() int
Count returns the number of values stored in the collection.
func (*OrderedSet[T]) Difference ¶
func (s *OrderedSet[T]) Difference(other sets.Set[T]) sets.Set[T]
Difference returns the difference between two sets. The new set consists of all elements that are in this set, but not other set.
The argument can be any implementation of Set[T]. The result is a new OrderedSet with the same properties as this one. Items are shallow-copied.
func (*OrderedSet[T]) Empty ¶
func (s *OrderedSet[T]) Empty() bool
Empty returns true if tree does not contain any nodes.
func (*OrderedSet[T]) Find ¶
func (s *OrderedSet[T]) Find(predicate functions.PredicateFunc[T]) collections.Element[T]
Find finds the first occurrence of an element matching the predicate.
The function returns nil if no match.
func (*OrderedSet[T]) FindAll ¶
func (s *OrderedSet[T]) FindAll(predicate functions.PredicateFunc[T]) []collections.Element[T]
FindAll finds all occurrences of an element matching the predicate.
The function returns an empty slice if none match.
func (*OrderedSet[T]) ForEach ¶
func (s *OrderedSet[T]) ForEach(f func(collections.Element[T]))
ForEach applies function f to all elements in the collection.
func (*OrderedSet[T]) Get ¶ added in v0.1.1
func (s *OrderedSet[T]) Get(value T) collections.Element[T]
Get returns the collection element that matches the given value, or nil if it is not found. Useful if the set contains struct elements you want to modify in-place.
func (*OrderedSet[T]) Intersection ¶
func (s *OrderedSet[T]) Intersection(other sets.Set[T]) sets.Set[T]
Intersection returns the intersection between two sets. The new set consists of all elements that are in both this set and the other.
The argument can be any implementation of Set[T]. The result is a new OrderedSet with the same properties as this one. Items are shallow-copied.
func (*OrderedSet[T]) IsEmpty ¶
func (s *OrderedSet[T]) IsEmpty() bool
IsEmpty returns true if the collection has no elements.
func (*OrderedSet[T]) Iterator ¶
func (s *OrderedSet[T]) Iterator() collections.Iterator[T]
Iterator returns an iterator that walks the collection in ascending order of values.
func (*OrderedSet[T]) Map ¶
func (s *OrderedSet[T]) Map(f func(T) T) collections.Collection[T]
Map applies function f to all elements in the collection and returns a new OrderedSet containing the result of f.
func (*OrderedSet[T]) Max ¶
func (s *OrderedSet[T]) Max() T
Max returns the maximum value in the collection according to the Comparer function.
func (*OrderedSet[T]) Min ¶
func (s *OrderedSet[T]) Min() T
Min returns the minimum value in the collection according to the Comparer function.
func (*OrderedSet[T]) Remove ¶
func (s *OrderedSet[T]) Remove(key T) bool
Remove removes a value from the set.
Returns true if the value was present and was removed; else false.
func (*OrderedSet[T]) ReverseIterator ¶
func (s *OrderedSet[T]) ReverseIterator() collections.Iterator[T]
ReverseIterator returns an iterator that walks the collection in descending order of values.
func (*OrderedSet[T]) Select ¶
func (s *OrderedSet[T]) Select(predicate functions.PredicateFunc[T]) collections.Collection[T]
Select returns a new OrderedSet containing only the items for which predicate is true.
func (*OrderedSet[T]) SelectDeep ¶
func (s *OrderedSet[T]) SelectDeep(predicate functions.PredicateFunc[T]) collections.Collection[T]
SelectDeep returns a new OrderedSet containing only the items for which predicate is true
Elements are deep copied to the new collection using the provided functions.DeepCopyFunc if any.
func (*OrderedSet[T]) String ¶
func (s *OrderedSet[T]) String() string
String returns a string representation of container.
func (*OrderedSet[T]) TakeWhile ¶
func (s *OrderedSet[T]) TakeWhile(predicate functions.PredicateFunc[T]) collections.Iterator[T]
TakeWhile returns a forward iterater that walks the collection returning only those elements for which predicate returns true.
set := hashset.New[int]() // add values iter := set.TakeWhile(func (val int) bool { return val % 2 == 0 }) for e := iter.Start() ; e != nil; e = iter.Next() { // do something with e.Value() }
func (*OrderedSet[T]) ToSlice ¶
func (s *OrderedSet[T]) ToSlice() []T
ToSlice returns the collection content as a slice. The values will be in ascending order.
func (*OrderedSet[T]) ToSliceDeep ¶
func (s *OrderedSet[T]) ToSliceDeep() []T
ToSliceDeep returns the collection content as a slice. The values will be in ascending order. Elements are deep copied using the provided functions.DeepCopyFunc if any.
func (*OrderedSet[T]) TreeWalk ¶
func (s *OrderedSet[T]) TreeWalk(action func(T) bool) bool
TreeWalk walks the underlying tree implemnetation of this set from smallest to largest value calling the delegate for each value. If the action delegate returns false, stop the walk.
Returns true if the entire tree has been walked. Otherwise returns false.
func (*OrderedSet[T]) Type ¶
func (s *OrderedSet[T]) Type() collections.CollectionType
Type returns the type of the collection (to avoid reflecting).
func (*OrderedSet[T]) Union ¶
func (s *OrderedSet[T]) Union(other sets.Set[T]) sets.Set[T]
Union returns the union of two sets. The new set consists of all elements that are in buth this and the other set.
The argument can be any implementation of Set[T]. The result is a new OrderedSet with the same properties as this one. Items are shallow-copied.
func (*OrderedSet[T]) UnlockedContains ¶
func (s *OrderedSet[T]) UnlockedContains(value T) bool
type OrderedSetIterator ¶
type OrderedSetIterator[T any] struct { util.IteratorBase[T] local.InternalImpl // contains filtered or unexported fields }
func (*OrderedSetIterator[T]) Next ¶
func (i *OrderedSetIterator[T]) Next() collections.Element[T]
Next returns the next element in the set, which will be nil if the end has been reached.
Panics if the set has been modified since creation of the iterator.
func (*OrderedSetIterator[T]) Start ¶
func (i *OrderedSetIterator[T]) Start() collections.Element[T]
Start begins an iteration across the set returning the fisrt element, which will be nil if the collection is empty.
Panics if the set has been modified since creation of the iterator.
type OrderedSetOptionFunc ¶
type OrderedSetOptionFunc[T any] func(*OrderedSet[T])
OrderedSetOptionFunc is the signature of a function for providing options to the OrderedSet constructor.
func WithComparer ¶
func WithComparer[T any](comparer functions.ComparerFunc[T]) OrderedSetOptionFunc[T]
Option function for NewOrderedSet to provide a comparer function for values of type T. Required if the element type is not numeric, bool, pointer or string.
func WithConcurrent ¶
func WithConcurrent[T any]() OrderedSetOptionFunc[T]
Option function to enable concurrency feature.
func WithDeepCopy ¶
func WithDeepCopy[T any](copier functions.DeepCopyFunc[T]) OrderedSetOptionFunc[T]
Option func to provide a deep copy implementation for collection elements.
func WithThreadSafe ¶
func WithThreadSafe[T any]() OrderedSetOptionFunc[T]
Option function for New to make the collection thread-safe. Adds overhead.