Documentation
¶
Overview ¶
Package coll provides utilities for collection types.
Index ¶
- type OrderedMap
- func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]
- func (m *OrderedMap[K, V]) Get(key K) (V, bool)
- func (m *OrderedMap[K, V]) Keys() iter.Seq[K]
- func (m *OrderedMap[K, V]) Put(key K, value V)
- func (m *OrderedMap[K, V]) Update(key K, update func(prev V, alreadyExist bool) V)
- func (m *OrderedMap[K, V]) Values() iter.Seq[V]
- type OrderedSet
- func (s *OrderedSet[E]) Append(el E)
- func (s *OrderedSet[E]) Contains(el E) bool
- func (s *OrderedSet[E]) Diff(other *OrderedSet[E]) *OrderedSet[E]
- func (s *OrderedSet[E]) Intersect(other *OrderedSet[E]) *OrderedSet[E]
- func (s *OrderedSet[E]) Len() int
- func (s *OrderedSet[E]) Remove(removedEl E)
- func (s *OrderedSet[E]) Union(other *OrderedSet[E]) *OrderedSet[E]
- func (s *OrderedSet[E]) Values() iter.Seq[E]
- type Set
- func (s *Set[E]) Append(el E)
- func (s *Set[E]) Contains(el E) bool
- func (s *Set[E]) Diff(other *Set[E]) *Set[E]
- func (s *Set[E]) Intersect(other *Set[E]) *Set[E]
- func (s *Set[E]) Len() int
- func (s *Set[E]) Remove(removedEl E)
- func (s *Set[E]) Union(other *Set[E]) *Set[E]
- func (s *Set[E]) Values() iter.Seq[E]
- type SetLike
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedMap ¶ added in v0.2.0
type OrderedMap[K comparable, V any] struct { // contains filtered or unexported fields }
OrderedMap represents a map that preserves insertion order of keys. It is safe for concurrent use.
func NewOrderedMap ¶ added in v0.2.0
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]
NewOrderedMap returns a new instance of OrderedMap.
func (*OrderedMap[K, V]) All ¶ added in v0.2.0
func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]
All returns an iterator over key-value pairs in insertion order. It is safe for concurrent use.
func (*OrderedMap[K, V]) Get ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Get(key K) (V, bool)
Get retrieves the value associated with the given key. The second return value indicates whether the key was found. It is safe for concurrent use.
func (*OrderedMap[K, V]) Keys ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Keys() iter.Seq[K]
Keys returns an iterator over the keys in insertion order. It is safe for concurrent use.
func (*OrderedMap[K, V]) Put ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Put(key K, value V)
Put inserts the key-value pair into the map if the key does not already exist. The insertion order of keys is preserved. It is safe for concurrent use.
func (*OrderedMap[K, V]) Update ¶ added in v0.3.0
func (m *OrderedMap[K, V]) Update(key K, update func(prev V, alreadyExist bool) V)
Update updates the value associated with the key using the provided function. The updater function receives the current value (or zero value if not found) and a boolean indicating existence. It is safe for concurrent use.
func (*OrderedMap[K, V]) Values ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Values() iter.Seq[V]
Values returns an iterator over the values in insertion order of their corresponding keys. It is safe for concurrent use.
type OrderedSet ¶
type OrderedSet[E comparable] struct { // contains filtered or unexported fields }
OrderedSet represents a set of comparable elements that maintains insertion order. It is safe for concurrent use.
Example ¶
package main
import (
"fmt"
"github.com/aereal/coll"
)
func main() {
nums := coll.NewOrderedSet(3, 1, 2)
fmt.Printf("nums contains 1?: %v\n", nums.Contains(1))
fmt.Printf("nums contains 42?: %v\n", nums.Contains(42))
nums.Append(42)
fmt.Printf("appended nums contains 42?: %v\n", nums.Contains(42))
fmt.Print("values:")
for n := range nums.Values() {
fmt.Printf(" %d", n)
}
fmt.Println()
}
Output: nums contains 1?: true nums contains 42?: false appended nums contains 42?: true values: 3 1 2 42
func NewOrderedSet ¶
func NewOrderedSet[E comparable](els ...E) *OrderedSet[E]
NewOrderedSet returns a new OrderedSet containing the provided elements. Duplicates in the input are ignored, and insertion order is preserved.
func (*OrderedSet[E]) Append ¶
func (s *OrderedSet[E]) Append(el E)
Append adds the element to the set if it does not already exist. The insertion order is preserved. It is safe for concurrent use.
func (*OrderedSet[E]) Contains ¶
func (s *OrderedSet[E]) Contains(el E) bool
Contains reports whether the element is present in the set. It is safe for concurrent use.
func (*OrderedSet[E]) Diff ¶ added in v0.5.0
func (s *OrderedSet[E]) Diff(other *OrderedSet[E]) *OrderedSet[E]
Diff returns a new OrderedSet containing elements that are in s or other but not in both.
func (*OrderedSet[E]) Intersect ¶ added in v0.5.0
func (s *OrderedSet[E]) Intersect(other *OrderedSet[E]) *OrderedSet[E]
Intersect returns a new OrderedSet containing elements that are present in both s and other.
func (*OrderedSet[E]) Len ¶
func (s *OrderedSet[E]) Len() int
Len returns the number of elements in the set.
func (*OrderedSet[E]) Remove ¶ added in v0.4.0
func (s *OrderedSet[E]) Remove(removedEl E)
func (*OrderedSet[E]) Union ¶ added in v0.5.0
func (s *OrderedSet[E]) Union(other *OrderedSet[E]) *OrderedSet[E]
Union returns a new OrderedSet containing all elements from both s and other.
func (*OrderedSet[E]) Values ¶
func (s *OrderedSet[E]) Values() iter.Seq[E]
Values returns an iterator over the elements of the set in insertion order.
type Set ¶ added in v0.6.0
type Set[E comparable] struct { // contains filtered or unexported fields }
Set represents a set of comparable elements. It is safe for concurrent use.
func NewSet ¶ added in v0.6.0
func NewSet[E comparable](els ...E) *Set[E]
NewSet returns a new Set containing the provided elements. Duplicates in the input are ignored.
func (*Set[E]) Append ¶ added in v0.6.0
func (s *Set[E]) Append(el E)
Append adds the element to the set if it does not already exist. It is safe for concurrent use.
func (*Set[E]) Contains ¶ added in v0.6.0
Contains reports whether the element is present in the set. It is safe for concurrent use.
func (*Set[E]) Diff ¶ added in v0.6.0
Diff returns a new Set containing elements that are in s or other but not in both.
func (*Set[E]) Intersect ¶ added in v0.6.0
Intersect returns a new Set containing elements that are present in both s and other.
type SetLike ¶ added in v0.6.0
type SetLike[E comparable] interface { Len() int Values() iter.Seq[E] Contains(E) bool }
func Diff ¶ added in v0.6.0
func Diff[E comparable](xs, ys SetLike[E]) SetLike[E]
Diff returns a new Set containing elements that are in xs or ys but not in both.