sets

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package sets contains set operations like union, intersection, and difference.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map added in v0.2.0

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

Map implements sets.Set for map[T]struct{}.

func (Map[T]) Add added in v0.2.0

func (s Map[T]) Add(item T)

func (Map[T]) Contains added in v0.2.0

func (s Map[T]) Contains(item T) bool

func (Map[T]) Iterate added in v0.2.0

func (s Map[T]) Iterate() iterator.Iterator[T]

func (Map[T]) IterateInternal added in v0.5.1

func (s Map[T]) IterateInternal(f func(T) bool)

func (Map[T]) Len added in v0.2.0

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

func (Map[T]) Remove added in v0.2.0

func (s Map[T]) Remove(item T)

type Set

type Set[T any] interface {
	Add(item T)
	Remove(item T)
	Contains(item T) bool
	Len() int
	Iterate() iterator.Iterator[T]
}

Set is a minimal interface to a set. It is implemented by sets.Map and container/tree.Set, among others.

func Difference

func Difference[T comparable](out, a, b Set[T]) Set[T]

Difference adds to out all items that appear in a but not in b and returns out.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/sets"
)

func main() {
	a := sets.Map[int]{
		1: {},
		4: {},
		5: {},
	}
	b := sets.Map[int]{
		3: {},
		4: {},
	}

	out := make(sets.Map[int])

	difference := sets.Difference[int](out, a, b)

	fmt.Println(difference)

}
Output:

map[1:{} 5:{}]

func Intersection

func Intersection[T comparable](out Set[T], sets ...Set[T]) Set[T]

Intersection adds to out all items that appear in all sets and returns out.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/sets"
)

func main() {
	a := sets.Map[int]{
		1: {},
		2: {},
		4: {},
	}
	b := sets.Map[int]{
		1: {},
		3: {},
		4: {},
	}
	c := sets.Map[int]{
		1: {},
		4: {},
		5: {},
	}

	out := make(sets.Map[int])

	intersection := sets.Intersection[int](out, a, b, c)

	fmt.Println(intersection)

}
Output:

map[1:{} 4:{}]

func Union

func Union[T any](out Set[T], sets ...Set[T]) Set[T]

Union adds to out out all items from sets and returns out.

Example
package main

import (
	"fmt"

	"github.com/bradenaw/juniper/sets"
)

func main() {
	a := sets.Map[int]{
		1: {},
		4: {},
	}
	b := sets.Map[int]{
		3: {},
		4: {},
	}
	c := sets.Map[int]{
		1: {},
		5: {},
	}

	out := make(sets.Map[int])

	union := sets.Union[int](out, a, b, c)

	fmt.Println(union)

}
Output:

map[1:{} 3:{} 4:{} 5:{}]

Jump to

Keyboard shortcuts

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