hashset

package
v0.0.0-...-e62b5e0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: 0BSD Imports: 7 Imported by: 2

Documentation

Overview

Package hashset implements an immutable Set datastructure on top of hashmap

A note about Value equality. If you would like to override the default go equality operator for values in this library implement the Equal(other interface{}) bool function for the type. Otherwise '==' will be used with all its restrictions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is a persistent unordered set implementation.

func Empty

func Empty() *Set

Empty returns the empty set.

func From

func From(value interface{}) *Set

From will convert many different go types to an immutable map. Converting some types is more efficient than others and the mechanisms are described below.

*Set:

Returned directly as it is already immutable.

*TSet:

AsPersistent is called on it and the result is returned.

map[interface{}]struct{}:

Converted directly by looping over the map and calling Add starting with an empty transient set. The transient set is the converted to a persistent one and returned.

[]interface{}:

The elements are passed to New.

map[kT]vT:

Reflection is used to loop over the keys of the map and add them to an empty transient set. The transient set is converted to a persistent map and then returned.

[]T:

Reflection is used to convert the slice to add the elements to the set.

seq.Sequence:

The sequence is reduced into a transient set that is made persistent on return.

seq.Sequable:

A sequence is obtained using Seq() and then the sequence is reduced into a transient set that is made persistent on return.

func New

func New(elems ...interface{}) *Set

New returns a set containing the supplied elements.

func (*Set) Add

func (s *Set) Add(elem interface{}) *Set

Add adds an element to the set and a new set is returned.

func (*Set) Apply

func (s *Set) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows map to be called as a function by the 'dyn' library.

func (*Set) AsTransient

func (s *Set) AsTransient() *TSet

AsTransient returns a mutable copy on write version of the set.

func (*Set) At

func (s *Set) At(elem interface{}) interface{}

At returns the elem if it exists in the set otherwise it returns nil.

func (*Set) Conj

func (s *Set) Conj(elem interface{}) interface{}

Conj adds an element to the set. Conj implements a generic mechanism for building collections.

func (*Set) Contains

func (s *Set) Contains(elem interface{}) bool

Contains returns true if the element is in the set, false otherwise.

func (*Set) Delete

func (s *Set) Delete(elem interface{}) *Set

Delete removes an element from the set returning a new Set without the element.

func (*Set) Equal

func (s *Set) Equal(o interface{}) bool

Equal tests if two sets are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons when there are sets of sets

func (*Set) Find

func (s *Set) Find(elem interface{}) (interface{}, bool)

Find will return the key if it exists in the set and whether the key exists in the set. If the key is not in the set, (nil, false) is returned.

func (*Set) Length

func (s *Set) Length() int

Length returns the elements in the set.

func (*Set) MakeTransient

func (s *Set) MakeTransient() interface{}

MakeTransient is a generic version of AsTransient.

func (*Set) Range

func (s *Set) Range(do interface{})

Range calls the passed in function on each element of the set. The function passed in may be of many types:

func(value interface{}) bool:

Takes a value of any type and returns if the loop should continue.
Useful to avoid reflection where not needed and to support
heterogenous sets.

func(value interface{})

Takes a value of any type.
Useful to avoid reflection where not needed and to support
heterogenous sets.

func(value T) bool:

Takes a value of the type of element stored in the set and
returns if the loop should continue. Useful for homogeneous sets.
Is called with reflection and will panic if the type is incorrect.

func(value T)

Takes a value of the type of element stored in the set and
returns if the loop should continue. Useful for homogeneous sets.
Is called with reflection and will panic if the type is incorrect.

Range will panic if passed anything that doesn't match one of these signatures

func (*Set) Reduce

func (s *Set) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Map. Reduce can take the following types as the fn:

func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT

Reduce will panic if given any other function type.

func (*Set) Seq

func (s *Set) Seq() seq.Sequence

Seq returns a seralized sequence of interface{} corresponding to the set's elements.

func (*Set) String

func (s *Set) String() string

String returns a string serialization of the set.

func (*Set) Transform

func (s *Set) Transform(xfrms ...func(*TSet) *TSet) *Set

Transform takes a set of actions and performs them on the persistent set. It does this by making a transient set and calling each action on it, then converting it back to a persistent set.

type TSet

type TSet struct {
	// contains filtered or unexported fields
}

TSet is a transient copy on write version of Set. Changes made to a transient set will not effect the original persistent structure. Changes to a transient set occur as mutations. These mutations are then made persistent when the transient is transformed into a persistent structure. These are useful when appling multiple transforms to a persistent set where the intermediate results will not be seen or stored anywhere.

func (*TSet) Add

func (s *TSet) Add(elem interface{}) *TSet

Add adds an element to the set as a mutation and original TSet is returned.

func (*TSet) Apply

func (s *TSet) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows map to be called as a function by the 'dyn' library.

func (*TSet) AsPersistent

func (s *TSet) AsPersistent() *Set

AsPersistent will transform this transient set into a persistent set. Once this occurs any additional actions on the transient set will fail.

func (*TSet) At

func (s *TSet) At(elem interface{}) interface{}

At returns the elem if it exists in the set otherwise it returns nil.

func (*TSet) Conj

func (s *TSet) Conj(elem interface{}) interface{}

Conj adds an element to the set. Conj implements a generic mechanism for building collections.

func (*TSet) Contains

func (s *TSet) Contains(elem interface{}) bool

Contains returns true if the element is in the set, false otherwise.

func (*TSet) Delete

func (s *TSet) Delete(elem interface{}) *TSet

Delete removes an element from the set as a mutation returning the original TSet.

func (*TSet) Equal

func (s *TSet) Equal(o interface{}) bool

Equal tests if two sets are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons when there are sets of sets

func (*TSet) Length

func (s *TSet) Length() int

Length returns the elements in the set.

func (*TSet) MakePersistent

func (s *TSet) MakePersistent() interface{}

MakePersistent is a generic version of AsTransient.

func (*TSet) Range

func (s *TSet) Range(do interface{})

Range calls the passed in function on each element of the set. The function passed in may be of many types:

func(value interface{}) bool:

Takes a value of any type and returns if the loop should continue.
Useful to avoid reflection where not needed and to support
heterogenous sets.

func(value interface{})

Takes a value of any type.
Useful to avoid reflection where not needed and to support
heterogenous sets.

func(value T) bool:

Takes a value of the type of element stored in the set and
returns if the loop should continue. Useful for homogeneous sets.
Is called with reflection and will panic if the type is incorrect.

func(value T)

Takes a value of the type of element stored in the set and
returns if the loop should continue. Useful for homogeneous sets.
Is called with reflection and will panic if the type is incorrect.

Range will panic if passed anything that doesn't match one of these signatures

func (*TSet) Reduce

func (s *TSet) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Map. Reduce can take the following types as the fn:

func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT

Reduce will panic if given any other function type.

func (*TSet) String

func (s *TSet) String() string

String returns a string serialization of the set.

Jump to

Keyboard shortcuts

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