set

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 2 Imported by: 0

README

GoDocs Go Reference Go Report Card Total alerts

set

A generic set implementation for Go.

See http://en.wikipedia.org/wiki/Set_%28mathematics%29 for a full discussion of sets.

This package implements a set as a map without values. Keys can have any arbitrary type, as long as there is equality defined on it.

This version of the package is fixed to v1.0.0. No more updates will happen. There will be a new version using Generics soon.

Examples

a := set.NewInit(1, 3, 5, 7, 9)
b := set.NewInit(2, 4, 6, 8, 10)

fmt.Println(a, b)

union := a.Union(b)
intersect := a.Intersect(b)
difference := a.Diff(b)

fmt.Println(union, intersect, difference)

a.Add(2)
b.Add(5)
fmt.Println(a.Intersect(b).Contains(2))

ch, _ := a.Iterator()
for x := range ch {
	fmt.Println(x)
}

Rationale

All operations are invoked in an object oriented style. Only the Add, Remove and Clear methods modify the receiver.

License

This package is released under the MIT license. The full license text can be found in the LICENSE file.

Documentation

Overview

Package set provides a generic set implementation for Go.

See http://en.wikipedia.org/wiki/Set_%28mathematics%29 for a full discussion of sets.

This package implements a set as a map without values. Keys can have any arbitrary type, as long as there is equality defined on it.

Examples

a := set.NewInit(1, 3, 5, 7, 9)
b := set.NewInit(2, 4, 6, 8, 10)

fmt.Println(a, b)

union := a.Union(b)
intersect := a.Intersect(b)
difference := a.Diff(b)

fmt.Println(union, intersect, difference)

a.Add(2)
b.Add(5)
fmt.Println(a.Intersect(b).Contains(2))

ch, _ := a.Iterator()
for x := range ch {
	fmt.Println(x)
}

Rationale

All operations are invoked in an object oriented style. Only the Add, Remove and Clear methods modify the receiver.

License

This package is released under the GNU Lesser General Public License, Version 3. The full license text can be found in the LICENSE file of the source code distribution.

Index

Constants

View Source
const VERSION = "0.1"

VERSION is the version number of the set package

Variables

This section is empty.

Functions

This section is empty.

Types

type AnySlice

type AnySlice []interface{}

AnySlice is a slice type for arbitrary values, which implements the sort.Interface. We use it for returning a list of (sorted or unsorted) set elements.

func (AnySlice) Len

func (s AnySlice) Len() int

The Len function for sort.Interface.

func (AnySlice) Less

func (s AnySlice) Less(i, j int) bool

The Less function for sort.Interface.

func (AnySlice) Swap

func (s AnySlice) Swap(i, j int)

The Swap function for sort.Interface.

type Set

type Set map[interface{}]struct{}

The Set is implemented as a map without values.

func New

func New() Set

New creates a new (empty) set.

func NewInit

func NewInit(e ...interface{}) Set

NewInit creates a new set and initializes it with the argument values.

func (Set) Add

func (s Set) Add(e ...interface{})

Add adds one or more elements to the given set.

func (Set) Clear

func (s Set) Clear()

Clear removes all elements from the given set.

func (Set) Contains

func (s Set) Contains(e ...interface{}) bool

Contains checks if a set contains one or more elements. The return value is true only if all given elements are in the set.

func (Set) Copy

func (s Set) Copy() Set

Copy returns a copy of a set. The set s is not modified.

func (Set) Diff

func (s Set) Diff(t Set) Set

Diff returns a new set which represents the difference of two sets. The sets themselves are not modified.

func (Set) Intersect

func (s Set) Intersect(t ...Set) Set

Intersect returns a new set which represents the intersection of two or more sets. The sets themselves are not modified.

func (Set) IsEmpty

func (s Set) IsEmpty() bool

IsEmpty tests if the set is empty.

func (Set) IsEqual

func (s Set) IsEqual(t Set) bool

IsEqual tests if two sets are equal.

func (Set) IsSubsetOf

func (s Set) IsSubsetOf(t Set) bool

IsSubsetOf returns true if the set s is a subset of the set t, e.g. if all elements of s are also in t.

func (Set) IsSupersetOf

func (s Set) IsSupersetOf(t Set) bool

IsSupersetOf returns true if the set s is a superset of the set t, e.g. if all elements of t are also in s.

func (Set) Iterator

func (s Set) Iterator() (<-chan interface{}, chan<- struct{})

Iterator returns a channel that can be used to iterate over the set. A second "done" channel can be used to preliminarily terminate the iteration by closing the done channel.

func (Set) Len

func (s Set) Len() int

Len returns the length of the set.

func (Set) List

func (s Set) List() AnySlice

List returns a list of the set elements in a slice.

func (Set) Remove

func (s Set) Remove(e ...interface{})

Remove removes one or more elements from the given set.

func (Set) SortedList

func (s Set) SortedList() AnySlice

SortedList returns a sorted list of the set elements in a slice.

func (Set) String

func (s Set) String() string

Function for implementing the fmt.Stringer interface to prettyprint the set.

func (Set) SymDiff

func (s Set) SymDiff(t Set) Set

SymDiff returns a new set which represents the symmetric difference of two sets. The sets themselves are not modified.

func (Set) Union

func (s Set) Union(t ...Set) Set

Union returns a new set, which represents the union of two or more sets. The sets themselves are not modified.

Jump to

Keyboard shortcuts

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