Documentation ¶
Overview ¶
Package concurrentset provides a generic implementation of a thread-safe concurrent set.
A Set is a collection of unique elements, implemented using Go's built-in map type. The Set is parameterized with a type T, which must be comparable.
This package offers several functions and methods to manipulate and work with sets, including the ability to iterate over the elements, map and filter them, and collect them back into a new Set. (It's been influened by Rust)
Index ¶
- type ConcurrentSet
- func (s *ConcurrentSet[T]) Clear()
- func (s *ConcurrentSet[T]) Clone() *ConcurrentSet[T]
- func (s *ConcurrentSet[T]) Contains(k T) bool
- func (s *ConcurrentSet[T]) Delete(k T)
- func (s *ConcurrentSet[T]) Empty() bool
- func (s *ConcurrentSet[T]) Insert(k T)
- func (s *ConcurrentSet[T]) Iter() *concurrentSetIter[T]
- func (s *ConcurrentSet[T]) Keys() []T
- func (s *ConcurrentSet[T]) Len() int
- func (s *ConcurrentSet[T]) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConcurrentSet ¶
type ConcurrentSet[T comparable] struct { // contains filtered or unexported fields }
A `ConcurrentSet` is implemented as a `map[T]struct{}` and an `RWMutex`.
As with maps, a ConcurrentSet requires T to be a comparable, meaning it can accept structs if and only if they don't have a type like a slice/map/anything that is not comparable
Examples:
package main import ( "fmt" "github.com/Jamlie/set/concurrentset" ) type Person struct { Id int Name string Age int } func main() { intsSet := concurrentset.New[int]() intsSet.Insert(1) intsSet.Insert(2) intsSet.Insert(3) intsSet.Delete(1) fmt.Println(intsSet.Len()) fmt.Println(intsSet) if intsSet.Contains(2) { fmt.Println("Set contains number 2") } uniquePeople := concurrentset.New[Person]() uniquePeople.Insert(Person{Id: 21, Name: "John", Age:30}) uniquePeople.Insert(Person{Id: 22, Name: "Jane", Age:30}) uniquePeople.Insert(Person{Id: 23, Name: "Roland", Age:30}) newUnique := uniquePeople.Clone() if !newUnique.Empty() { newUnique.Clear() } uniquePeople. Iter(). Map(func(k Person) Person { return Person{ Id: k.Id * 3, Name: k.Name, Age: k.Age, } }). Filter(func(k Person) bool { return k.Id%2 == 1 }). Collect() fmt.Println(uniquePeople) }
func New ¶
func New[T comparable]() *ConcurrentSet[T]
func WithCapacity ¶
func WithCapacity[T comparable](capacity int) *ConcurrentSet[T]
func (*ConcurrentSet[T]) Clear ¶
func (s *ConcurrentSet[T]) Clear()
func (*ConcurrentSet[T]) Clone ¶
func (s *ConcurrentSet[T]) Clone() *ConcurrentSet[T]
func (*ConcurrentSet[T]) Contains ¶
func (s *ConcurrentSet[T]) Contains(k T) bool
func (*ConcurrentSet[T]) Delete ¶
func (s *ConcurrentSet[T]) Delete(k T)
func (*ConcurrentSet[T]) Empty ¶
func (s *ConcurrentSet[T]) Empty() bool
func (*ConcurrentSet[T]) Insert ¶
func (s *ConcurrentSet[T]) Insert(k T)
func (*ConcurrentSet[T]) Iter ¶
func (s *ConcurrentSet[T]) Iter() *concurrentSetIter[T]
func (*ConcurrentSet[T]) Keys ¶
func (s *ConcurrentSet[T]) Keys() []T
func (*ConcurrentSet[T]) Len ¶
func (s *ConcurrentSet[T]) Len() int
func (*ConcurrentSet[T]) String ¶
func (s *ConcurrentSet[T]) String() string