set

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2023 License: MIT Imports: 7 Imported by: 1

README

Set

Usage

import (
"github.com/jamestrandung/go-data-structure/set"
)

go get "github.com/jamestrandung/go-data-structure/set"

Implemented interface

// Set is an unordered set of T.
type Set[T comparable] interface {
    Collection[T]
    // Difference returns all elements of this set that are not in `other`.
    Difference(other Set[T]) []T
    // SymmetricDifference returns all elements that are in either this set or `other` but not in both.
    SymmetricDifference(other Set[T]) []T
    // Intersect returns all elements that exist in both sets.
    Intersect(other Set[T]) []T
    // Union returns all elements that are in both sets.
    Union(other Set[T]) []T
    // IsProperSubset returns whether all elements in this set are in `other` but they are not equal.
    IsProperSubset(other Set[T]) bool
}

// Collection is a collection of T.
type Collection[T comparable] interface {
    // Add an element to this collection.
    Add(element T)
    // AddAll adds all elements from the given slice to this collection.
    AddAll(elements []T)
    // Count returns the size of this collection.
    Count() int
    // IsEmpty returns whether this collection is empty.
    IsEmpty() bool
    // Has returns whether given element is in this collection.
    Has(element T) bool
    // Contains returns whether all elements in `other` are in this collection.
    Contains(other Collection[T]) bool
    // Equals returns whether this and `other` sets have the same size and contain the same elements.
    Equals(other Collection[T]) bool
    // Pop removes and returns an element from this collection.
    Pop() (T, bool)
    // Remove removes an element from this collection, returns whether this collection
    // changed as a result of the call.
    Remove(element T) bool
    // RemoveAll removes the given elements from this collection, returns whether this collection
    // changed as a result of the call.
    RemoveAll(elements []T) bool
    // RemoveIf removes the given element from this collection based on some condition, then returns
    // true if the element was removed. If the given element doesn't exist in this collection or the
    // element was not removed because of the condition func, false will be returned.
    RemoveIf(element T, conditionFn func() bool) bool
    // Clear removes all elements from this collection.
    Clear()
    // Iter returns a channel which could be used in a for range loop. The capacity of the returned
    // channel is the same as the size of the collection at the time Iter is called.
    Iter() <-chan T
    // Items returns all elements of this collection as a slice.
    Items() []T
    // ForEach executes the given doEachFn on every element in this collection. If `doEachFn` returns
    // true, stop execution immediately.
    ForEach(doEachFn func(element T) bool)
    // MarshalJSON returns the JSON bytes of this collection.
    MarshalJSON() ([]byte, error)
    // UnmarshalJSON consumes a slice of JSON bytes to populate this collection.
    UnmarshalJSON(b []byte) error
    // String returns a string representation of the current state of the collection.
    String() string
}

Hash Set

Go does not have a built-in data type for Set. This package aims to fill in this gap by providing a HashSet implementation that is backed by the basic map type.

// Create a new set
hs := set.NewHashSet[string]()

// Add element to set
hs.Add("foo")

// Check if the set contains "bar"
found := hs.Has("bar")

// Remove "foo" element
hs.Remove("foo")

For more examples, have a look at hset_test.go.

Concurrent Set

This is a thread-safe alternative for HashSet that is backed by emap.ConcurrentMap.

// Create a new set
cs := set.NewConcurrentSet[string]()

// Add element to set
cs.Add("foo")

// Check if the set contains "bar"
found := cs.Has("bar")

// Remove "foo" element
cs.Remove("foo")

For more examples, have a look at cset_test.go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentSet

type ConcurrentSet[T comparable] emap.ConcurrentMap[T, struct{}]

func NewConcurrentSet

func NewConcurrentSet[T comparable](elements ...T) ConcurrentSet[T]

NewConcurrentSet returns a new instance of ConcurrentSet with the given elements.

func NewConcurrentSetWithConcurrencyLevel

func NewConcurrentSetWithConcurrencyLevel[T comparable](concurrencyLevel int, elements ...T) ConcurrentSet[T]

NewConcurrentSetWithConcurrencyLevel returns a new instance of ConcurrentSet with the given amount of shards and contains the given elements.

func (ConcurrentSet[T]) Add

func (cs ConcurrentSet[T]) Add(element T)

Add an element to this set.

func (ConcurrentSet[T]) AddAll

func (cs ConcurrentSet[T]) AddAll(elements []T)

AddAll adds all elements from the given slice to this set.

func (ConcurrentSet[T]) Clear

func (cs ConcurrentSet[T]) Clear()

Clear removes all elements from this set.

func (ConcurrentSet[T]) Contains

func (cs ConcurrentSet[T]) Contains(other ds.Collection[T]) bool

Contains returns whether all elements in `other` are in this set.

func (ConcurrentSet[T]) Count

func (cs ConcurrentSet[T]) Count() int

Count returns the size of this set.

func (ConcurrentSet[T]) Difference

func (cs ConcurrentSet[T]) Difference(other ds.Set[T]) []T

Difference returns all elements of this set that are not in `other`.

func (ConcurrentSet[T]) Equals

func (cs ConcurrentSet[T]) Equals(other ds.Collection[T]) bool

Equals returns whether this and `other` collections have the same size and contain the same elements.

func (ConcurrentSet[T]) ForEach

func (cs ConcurrentSet[T]) ForEach(doEachFn func(element T) bool)

ForEach executes the given doEachFn on every element in this set. If `doEachFn` returns true, stop execution immediately.

Note: doEachFn is called while lock is held. Hence, it must NOT access this set as it may lead to deadlock since sync.RWLock in the underlying emap.ConcurrentMap is not reentrant.

func (ConcurrentSet[T]) Has

func (cs ConcurrentSet[T]) Has(element T) bool

Has returns whether given element is in this set.

func (ConcurrentSet[T]) Intersect

func (cs ConcurrentSet[T]) Intersect(other ds.Set[T]) []T

Intersect returns all elements that exist in both sets.

func (ConcurrentSet[T]) IsEmpty

func (cs ConcurrentSet[T]) IsEmpty() bool

IsEmpty returns whether this set is empty.

func (ConcurrentSet[T]) IsProperSubset

func (cs ConcurrentSet[T]) IsProperSubset(other ds.Set[T]) bool

IsProperSubset returns whether all elements in this set are in `other` but they are not equal.

func (ConcurrentSet[T]) Items

func (cs ConcurrentSet[T]) Items() []T

Items returns all elements of this set as a slice.

func (ConcurrentSet[T]) Iter

func (cs ConcurrentSet[T]) Iter() <-chan T

Iter returns a channel which could be used in a for range loop. The capacity of the returned channel is the same as the size of the set at the time Iter is called.

func (ConcurrentSet[T]) MarshalJSON

func (cs ConcurrentSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON bytes of this set.

func (ConcurrentSet[T]) Pop

func (cs ConcurrentSet[T]) Pop() (v T, ok bool)

Pop removes and returns an element from this set.

func (ConcurrentSet[T]) Remove

func (cs ConcurrentSet[T]) Remove(element T) bool

Remove removes an element from this set, returns whether this set changed as a result of the call.

func (ConcurrentSet[T]) RemoveAll

func (cs ConcurrentSet[T]) RemoveAll(elements []T) bool

RemoveAll removes the given elements from this set, returns whether this set changed as a result of the call.

func (ConcurrentSet[T]) RemoveIf

func (cs ConcurrentSet[T]) RemoveIf(element T, conditionFn func() bool) bool

RemoveIf removes the given element from this set based on some condition, then returns true if the element was removed. If the given element doesn't exist in this set or the element was not removed because of the condition func, false will be returned.

Note: Condition func is called while lock is held. Hence, it must NOT access this set as it may lead to deadlock since sync.RWLock in the underlying emap.ConcurrentMap is not reentrant.

func (ConcurrentSet[T]) String

func (cs ConcurrentSet[T]) String() string

String returns a string representation of the current state of the set.

func (ConcurrentSet[T]) SymmetricDifference

func (cs ConcurrentSet[T]) SymmetricDifference(other ds.Set[T]) []T

SymmetricDifference returns all elements that are in either this set or `other` but not in both.

func (ConcurrentSet[T]) Union

func (cs ConcurrentSet[T]) Union(other ds.Set[T]) []T

Union returns all elements that are in both sets.

func (ConcurrentSet[T]) UnmarshalJSON

func (cs ConcurrentSet[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON consumes a slice of JSON bytes to populate this set.

type HashSet

type HashSet[T comparable] map[T]struct{}

func NewHashSet

func NewHashSet[T comparable](elements ...T) HashSet[T]

NewHashSet returns a new instance of HashSet with the given elements.

func NewHashSetWithInitialSize

func NewHashSetWithInitialSize[T comparable](initialSize int) HashSet[T]

NewHashSetWithInitialSize returns a new instance of HashSet with the given initial size.

func (HashSet[T]) Add

func (hs HashSet[T]) Add(element T)

Add an element to this set.

func (HashSet[T]) AddAll

func (hs HashSet[T]) AddAll(elements []T)

AddAll adds all elements from the given slice to this set.

func (HashSet[T]) Clear

func (hs HashSet[T]) Clear()

Clear removes all elements from this set.

func (HashSet[T]) Contains

func (hs HashSet[T]) Contains(other ds.Collection[T]) bool

Contains returns whether all elements in `other` are in this set.

func (HashSet[T]) Count

func (hs HashSet[T]) Count() int

Count returns the size of this set.

func (HashSet[T]) Difference

func (hs HashSet[T]) Difference(other ds.Set[T]) []T

Difference returns all elements of this set that are not in `other`.

func (HashSet[T]) Equals

func (hs HashSet[T]) Equals(other ds.Collection[T]) bool

Equals returns whether this and `other` collections have the same size and contain the same elements.

func (HashSet[T]) ForEach

func (hs HashSet[T]) ForEach(doEachFn func(element T) bool)

ForEach executes the given doEachFn on every element in this set. If `doEachFn` returns true, stop execution immediately.

func (HashSet[T]) Has

func (hs HashSet[T]) Has(element T) bool

Has returns whether given element is in this set.

func (HashSet[T]) Intersect

func (hs HashSet[T]) Intersect(other ds.Set[T]) []T

Intersect returns all elements that exist in both sets.

func (HashSet[T]) IsEmpty

func (hs HashSet[T]) IsEmpty() bool

IsEmpty returns whether this set is empty.

func (HashSet[T]) IsProperSubset

func (hs HashSet[T]) IsProperSubset(other ds.Set[T]) bool

IsProperSubset returns whether all elements in this set are in `other` but they are not equal.

func (HashSet[T]) Items

func (hs HashSet[T]) Items() []T

Items returns all elements of this set as a slice.

func (HashSet[T]) Iter

func (hs HashSet[T]) Iter() <-chan T

Iter returns a channel which could be used in a for range loop. The capacity of the returned channel is the same as the size of the set at the time Iter is called.

func (HashSet[T]) MarshalJSON

func (hs HashSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON bytes of this set.

func (HashSet[T]) Pop

func (hs HashSet[T]) Pop() (v T, ok bool)

Pop removes and returns an element from this set.

func (HashSet[T]) Remove

func (hs HashSet[T]) Remove(element T) bool

Remove removes an element from this set, returns whether this set changed as a result of the call.

func (HashSet[T]) RemoveAll

func (hs HashSet[T]) RemoveAll(elements []T) bool

RemoveAll removes the given elements from this set, returns whether this set changed as a result of the call.

func (HashSet[T]) RemoveIf

func (hs HashSet[T]) RemoveIf(element T, conditionFn func() bool) bool

RemoveIf removes the given element from this set based on some condition, then returns true if the element was removed. If the given element doesn't exist in this set or the element was not removed because of the condition func, false will be returned.

func (HashSet[T]) String

func (hs HashSet[T]) String() string

String returns a string representation of the current state of the set.

func (HashSet[T]) SymmetricDifference

func (hs HashSet[T]) SymmetricDifference(other ds.Set[T]) []T

SymmetricDifference returns all elements that are in either this set or `other` but not in both.

func (HashSet[T]) Union

func (hs HashSet[T]) Union(other ds.Set[T]) []T

Union returns all elements that are in both sets.

func (HashSet[T]) UnmarshalJSON

func (hs HashSet[T]) UnmarshalJSON(b []byte) error

UnmarshalJSON consumes a slice of JSON bytes to populate this set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL