hashset

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package hashset implements an immutable set by using a hashdict in the background.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

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

func New

func New[T any](eq hash.EqHash[T], elems ...T) Set[T]

New creates a new set

func (Set[T]) Add

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

Add elements to the set

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s1 := hashset.New(hash.String(), "a", "b", "c")
	s2 := s1.Add("x", "y")
	fmt.Printf("s1 = %v\n", s1)
	fmt.Printf("s2 = %v\n", s2)
}
Output:

s1 = [b, a, c]
s2 = [b, x, a, c, y]

func (Set[T]) Contains

func (s Set[T]) Contains(elem T) bool

Contains checks if the set contains an element

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s := hashset.New(hash.String(), "a", "b", "c")
	fmt.Printf("contains a: %v\n", s.Contains("a"))
	fmt.Printf("contains x: %v\n", s.Contains("x"))
}
Output:

contains a: true
contains x: false

func (Set[T]) EqHash

func (s Set[T]) EqHash() hash.EqHash[T]

EqHash returns the hash instance used in the set

func (Set[T]) Intersect

func (s Set[T]) Intersect(other iterable.Iterable[T]) Set[T]

Intersect two sets, returning only elements contained in both

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s1 := hashset.New(hash.String(), "a", "b", "c")
	s2 := hashset.New(hash.String(), "b", "c", "d")
	s3 := s1.Intersect(s2)
	fmt.Printf("s1 = %v\n", s1)
	fmt.Printf("s2 = %v\n", s2)
	fmt.Printf("s3 = %v\n", s3)
}
Output:

s1 = [b, a, c]
s2 = [b, c, d]
s3 = [b, c]

func (Set[T]) Iterator

func (s Set[T]) Iterator() iterable.Iterator[T]

Iterator for the set

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s := hashset.New(hash.Num[int](), 1, 2, 3)
	it := s.Iterator()
	sum := 0
	for {
		v, ok := it.Next()
		if !ok {
			break
		}
		sum += v
	}
	fmt.Printf("sum = %v\n", sum)
}
Output:

sum = 6

func (Set[T]) Minus

func (s Set[T]) Minus(other iterable.Iterable[T]) Set[T]

Minus removes all elements from the other set from this set

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s1 := hashset.New(hash.String(), "a", "b", "c", "d")
	s2 := hashset.New(hash.String(), "b", "d", "e")
	s3 := s1.Minus(s2)
	fmt.Printf("s1 = %v\n", s1)
	fmt.Printf("s2 = %v\n", s2)
	fmt.Printf("s3 = %v\n", s3)
}
Output:

s1 = [b, a, c, d]
s2 = [e, b, d]
s3 = [a, c]

func (Set[T]) Remove

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

Remove elements from the set

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s1 := hashset.New(hash.String(), "a", "b", "c")
	s2 := s1.Remove("c")
	fmt.Printf("s1 = %v\n", s1)
	fmt.Printf("s2 = %v\n", s2)
}
Output:

s1 = [b, a, c]
s2 = [b, a]

func (Set[T]) String

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

String representation of the set

func (Set[T]) Union

func (s Set[T]) Union(other iterable.Iterable[T]) Set[T]

Union of two sets, returning elements that return in either of the sets

Example
package main

import (
	"fmt"

	"github.com/peterzeller/go-fun/hash"
	"github.com/peterzeller/go-fun/set/hashset"
)

func main() {
	s1 := hashset.New(hash.String(), "a", "b", "c")
	s2 := hashset.New(hash.String(), "b", "d")
	s3 := s1.Union(s2)
	fmt.Printf("s1 = %v\n", s1)
	fmt.Printf("s2 = %v\n", s2)
	fmt.Printf("s3 = %v\n", s3)
}
Output:

s1 = [b, a, c]
s2 = [b, d]
s3 = [b, a, c, d]

Jump to

Keyboard shortcuts

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