collections

package
v1.0.0-dev.145 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set map[SetItem]membership

Set is just a set.

func MakeSet

func MakeSet(si ...SetItem) (s Set)

MakeSet creates a new Set.

func (*Set) Clear

func (s *Set) Clear()

Clear clears the set, removing all values.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set := coll.MakeSet("cat", "dog", "cow")

	set.Clear()
	fmt.Println(set.Size())
}
Output:

0

func (Set) Collect

func (s Set) Collect() (ss SetSlice)

Collect returns a slice of all the elements present in the set in arbitrary order.

func (Set) Contains

func (s Set) Contains(si SetItem) (ok bool)

Contains returns true if the set contains a value.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set := coll.MakeSet("cat", "dog", "cow")

	ok := set.Contains("cat")
	fmt.Println(ok)

	ok = set.Contains("buffalo")
	fmt.Println(ok)
}
Output:

true
false

func (Set) Difference

func (s Set) Difference(other Set) (set Set)

Difference returns the set of values that are in s but not in other.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set1 := coll.MakeSet(1, 2, 3)
	set2 := coll.MakeSet(4, 2, 3, 4)

	diff1 := set1.Difference(set2)
	fmt.Println(diff1.Contains(1))
	fmt.Println(diff1.Contains(2))
	fmt.Println(diff1.Contains(3))
	fmt.Println(diff1.Contains(4))

	diff2 := set2.Difference(set1)
	fmt.Println(diff2.Contains(4))
	fmt.Println(diff2.Contains(1))
	fmt.Println(diff2.Contains(2))
	fmt.Println(diff2.Contains(3))
}
Output:

true
false
false
false
true
false
false
false

func (Set) Insert

func (s Set) Insert(si SetItem) (ok bool)

Insert adds a value to the set. If the set did not have this value present, true is returned. If the set did have this value present, false is returned.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set := coll.MakeSet()

	ok := set.Insert("cat")
	fmt.Println(ok)

	ok = set.Insert("cat")
	fmt.Println(ok)
}
Output:

true
false

func (Set) Intersection

func (s Set) Intersection(other Set) (set Set)

Intersection returns the set of values that are both in s and other.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set1 := coll.MakeSet("cat", "dog", "cow")
	set2 := coll.MakeSet("cat", "duck", "bull")
	intersection := set1.Intersection(set2)

	fmt.Println(intersection.Contains("cat"))
	fmt.Println(intersection.Contains("dog"))
	fmt.Println(intersection.Contains("cow"))
	fmt.Println(intersection.Contains("duck"))
	fmt.Println(intersection.Contains("bull"))
}
Output:

true
false
false
false
false

func (Set) IsEmpty

func (s Set) IsEmpty() bool

IsEmpty returns true if the set contains no elements.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set := coll.MakeSet()
	fmt.Println(set.IsEmpty())

	set.Insert("cat")
	fmt.Println(set.IsEmpty())
}
Output:

true
false

func (Set) IsEqual

func (s Set) IsEqual(other Set) bool

IsEqual returns true if s and other are the same.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set1 := coll.MakeSet("cat", "dog", "cow")
	set2 := coll.MakeSet("cat", "dog")
	set3 := coll.MakeSet("frog")
	set4 := coll.MakeSet("cow", "cat", "dog")

	fmt.Println(set4.IsEqual(set4))
	fmt.Println(set1.IsEqual(set4))
	fmt.Println(set2.IsEqual(set3))
}
Output:

true
true
false

func (Set) Remove

func (s Set) Remove(si SetItem) (ok bool)

Remove a value from the set. Returns true if the value was present in the set.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set := coll.MakeSet("cat", "dog", "cow")

	ok := set.Remove("cat")
	fmt.Println(ok)

	ok = set.Remove("cat")
	fmt.Println(ok)
}
Output:

true
false

func (Set) Size

func (s Set) Size() int

Size returns the cardinality of a set.

func (Set) SubsetOf

func (s Set) SubsetOf(other Set) bool

SubsetOf returns true if s is a subset of other.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set1 := coll.MakeSet("cat", "dog", "cow")
	set2 := coll.MakeSet("cat", "dog")
	set3 := coll.MakeSet("frog")
	set4 := coll.MakeSet("cat", "dog", "frog", "cow")

	fmt.Println(set2.SubsetOf(set1))
	fmt.Println(set1.SubsetOf(set2))
	fmt.Println(set4.SubsetOf(set3))
	fmt.Println(set3.SubsetOf(set4))
}
Output:

true
false
false
true

func (Set) SymmetricDifference

func (s Set) SymmetricDifference(other Set) (set Set)

SymmetricDifference returns the set of values that are in s or in other but not in both.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set1 := coll.MakeSet(1, 2, 3)
	set2 := coll.MakeSet(4, 2, 3, 4)
	symDiff := set1.SymmetricDifference(set2)

	fmt.Println(symDiff.Contains(1))
	fmt.Println(symDiff.Contains(4))
	fmt.Println(symDiff.Contains(2))
	fmt.Println(symDiff.Contains(3))
}
Output:

true
true
false
false

func (Set) ToString

func (s Set) ToString() string

ToString returns a string representation of the set in arbitrary order.

func (Set) Union

func (s Set) Union(other Set) (set Set)

Union returns the set of values that are in s or in other, without duplicates.

Example
package main

import (
	"fmt"

	coll "github.com/xgodev/boost/utils/collections"
)

func main() {
	set1 := coll.MakeSet("cat", "dog", "cow")
	set2 := coll.MakeSet("cat", "duck", "bull")
	union := set1.Union(set2)

	fmt.Println(union.Contains("cat"))
	fmt.Println(union.Contains("dog"))
	fmt.Println(union.Contains("cow"))
	fmt.Println(union.Contains("duck"))
	fmt.Println(union.Contains("bull"))
}
Output:

true
true
true
true
true

type SetItem

type SetItem interface{}

SetItem represent a item present in a Set.

type SetSlice

type SetSlice []SetItem

SetSlice is a slice of items present in a Set.

Jump to

Keyboard shortcuts

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