set

package module
v2.0.0-...-84ec22f Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 4 Imported by: 0

README

GoDocs Go Reference Go Report Card

set

A set implementation for Go using generics.

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

This module contains two set implementations:

  • A generic implementation using interface{} values, for all versions of Go. This is fixed to v1.0.0. No more updates will happen.
  • An type-safe implementation using generics and iterator functions in the v2 directory. This requires Go 1.23 or later.

This documentation is for the version 2 of the package. See the README.md at the repositories top level for the documentation of the version 1 package.

Implementation

A set is implemented as map without values. The elements of a set are stored as maps keys.

Examples

import "github.com/hweidner/set/v2"

a := set.New(1, 3, 5, 7, 9)
b := set.New(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))

for x := range a.All() {
	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

A set library for Go using generics

Package set provides a generic type-safe set library in Go, using the new generics language extension in Go 1.18 and higher.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

The Set is implemented as a map without values.

func New

func New[T comparable](e ...T) Set[T]

New creates a new set and initializes it with the argument values. Note: NewInit() was renamed to New()

func (Set[T]) Add

func (s Set[T]) Add(e ...T)

Add adds one or more elements to the given set.

func (Set[T]) All

func (s Set[T]) All() iter.Seq[T]

All returns an iterator to all elements in the set in an undefined order.

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all elements from the given set.

func (Set[T]) Contains

func (s Set[T]) Contains(e ...T) 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[T]) ContainsAny

func (s Set[T]) ContainsAny(e ...T) bool

ContainsAny checks if a set contains one or more elements. The return value is true if at least one of the given elements is in the set.

func (Set[T]) Copy

func (s Set[T]) Copy() Set[T]

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

func (Set[T]) Diff

func (s Set[T]) Diff(t Set[T]) Set[T]

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

func (Set[T]) Intersect

func (s Set[T]) Intersect(t ...Set[T]) Set[T]

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

func (Set[T]) IsEmpty

func (s Set[T]) IsEmpty() bool

IsEmpty tests if the set is empty.

func (Set[T]) IsEqual

func (s Set[T]) IsEqual(t Set[T]) bool

IsEqual tests if two sets are equal.

func (Set[T]) IsSubsetOf

func (s Set[T]) IsSubsetOf(t Set[T]) 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[T]) IsSupersetOf

func (s Set[T]) IsSupersetOf(t Set[T]) 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[T]) Iterator deprecated

func (s Set[T]) Iterator() (iterch <-chan T, donech 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.

Deprecated: The Iterator method will be removed in a future release. Use the All() method to iterate over the set instead.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the length of the set.

func (Set[T]) List

func (s Set[T]) List() []T

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

func (Set[T]) Remove

func (s Set[T]) Remove(e ...T)

Remove removes one or more elements from the given set.

func (Set[T]) String

func (s Set[T]) String() string

String returns a textual representation of the set in a string. It is there for implementing the fmt.Stringer interface to prettyprint the set.

func (Set[T]) SymDiff

func (s Set[T]) SymDiff(t Set[T]) Set[T]

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

func (Set[T]) Union

func (s Set[T]) Union(t ...Set[T]) Set[T]

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