sets

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2022 License: MIT Imports: 2 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 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 maps.Set 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/maps"
	"github.com/bradenaw/juniper/sets"
)

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

	out := make(maps.Set[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/maps"
	"github.com/bradenaw/juniper/sets"
)

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

	out := make(maps.Set[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/maps"
	"github.com/bradenaw/juniper/sets"
)

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

	out := make(maps.Set[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