set

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Set

CircleCI branch License GitHub tag (latest SemVer)

Set is a threadsafe abstract data structure library for representing collection of distinct values, without any particular order.

Examples
package main

import (
    "fmt"
	
    "github.com/trivigy/set"
)

func main() {
    m := set.New("b", "b", "c", "d")
    fmt.Println(m.Contains("b", "c", "d"))
	
    m1 := set.New("b", "b", "c", "d")
    m2 := set.New("c", "b", "d", "b")
    fmt.Println(m1.Equals(m2))
}

Documentation

Overview

Package set implements a map-based set data structure.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set represents the set data structure.

func New

func New(l ...interface{}) *Set

New creates and returns a reference to an empty set. Operations on the resulting set are thread-safe.

Example
m := New("a", "a", "b")
var list []string
for elem := range m.Iter() {
	list = append(list, elem.(string))
}
sort.Strings(list)
fmt.Println(list)
Output:
[a b]

func (*Set) Add

func (s *Set) Add(l ...interface{})

Add adds occurrences of each of the specified elements to this set.

Example
m := New("a", "a", "a")
m.Add("b", "b", "c", "d")
var list []string
for elem := range m.Iter() {
	list = append(list, elem.(string))
}
sort.Strings(list)
fmt.Println(list)
Output:
[a b c d]

func (*Set) Clear

func (s *Set) Clear()

Clear removes all of the elements from this set. The collection will be empty after this method returns.

Example
m := New("b", "b", "c", "d")
m.Clear()
fmt.Println(m)
Output:
[]

func (*Set) Contains

func (s *Set) Contains(l ...interface{}) bool

Contains determines whether this set contains the occurrence of each of the specified elements. If at least one occurrence missing returns false; otherwise true.

Example
m := New("b", "b", "c", "d")
fmt.Println(m.Contains("b", "c", "d"))
Output:
true

func (*Set) Diff added in v1.1.0

func (s *Set) Diff(o *Set) *Set

Diff returns a new set with items in the current set but not in the other set.

Example
m1 := New("a", "b", "c")
m2 := New("c", "d", "e")
m := m1.Diff(m2)

var list []string
for elem := range m.Iter() {
	list = append(list, elem.(string))
}
sort.Strings(list)
fmt.Println(list)
Output:
[a b]

func (*Set) Equals

func (s *Set) Equals(o *Set) bool

Equals compares the specified set with this set for equality. Returns true if this set contains equal elements; false otherwise.

Example
m1 := New("b", "b", "c", "d")
m2 := New("c", "b", "d", "b")
fmt.Println(m1.Equals(m2))
Output:
true

func (*Set) Intersect added in v1.1.0

func (s *Set) Intersect(o *Set) *Set

Intersect returns a new set with items that exist only in both sets.

Example
m1 := New("a", "b", "c")
m2 := New("c", "d", "e")
m := m1.Intersect(m2)

var list []string
for elem := range m.Iter() {
	list = append(list, elem.(string))
}
sort.Strings(list)
fmt.Println(list)
Output:
[c]

func (*Set) IsEmpty

func (s *Set) IsEmpty() bool

IsEmpty returns true if this set contains no elements; false otherwise.

Example
m := New("b", "b", "c", "d")
fmt.Println(m.IsEmpty())
Output:
false

func (*Set) Iter

func (s *Set) Iter() <-chan interface{}

Iter returns a channel of elements that can be ranged over.

Example
m := New("a", "a", "b")
for elem := range m.Iter() {
	fmt.Println(elem)
}
Output:
a
b

func (*Set) Remove

func (s *Set) Remove(l ...interface{}) bool

Remove removes each of the specified elements from this set, if present. Returns true if this set changed as a result of the call

Example
m := New("a", "a", "a", "b", "b")
m.Remove("a", "a")
fmt.Println(m)
Output:
[b]

func (*Set) Size

func (s *Set) Size() int

Size returns the total number of elements in this set.

Example
m := New("b", "b", "c", "d")
fmt.Println(m.Size())
Output:
3

func (*Set) String

func (s *Set) String() string

String returns a string representation of the set.

func (*Set) Subset added in v1.1.0

func (s *Set) Subset(o *Set) bool

Subset determines if every item in the other set is in this set.

Example
m1 := New("a", "b", "c")
m2 := New("c", "b")
fmt.Println(m2.Subset(m1))
Output:
true

func (*Set) Superset added in v1.1.0

func (s *Set) Superset(o *Set) bool

Superset determines if every item of this set is in the other set.

Example
m1 := New("a", "b", "c")
m2 := New("c", "b")
fmt.Println(m1.Superset(m2))
Output:
true

func (*Set) SymDiff added in v1.1.0

func (s *Set) SymDiff(o *Set) *Set

SymDiff returns a new set with items in the current set or the other set but not in both.

Example
m1 := New("a", "b", "c")
m2 := New("c", "d", "e")
m := m1.SymDiff(m2)

var list []string
for elem := range m.Iter() {
	list = append(list, elem.(string))
}
sort.Strings(list)
fmt.Println(list)
Output:
[a b d e]

func (*Set) ToSlice

func (s *Set) ToSlice() []interface{}

ToSlice returns a slice containing all elements of this set.

func (*Set) Union added in v1.1.0

func (s *Set) Union(o *Set) *Set

Union returns a new set with all items in both sets.

Example
m1 := New("a", "b", "c")
m2 := New("c", "d", "e")
m := m1.Union(m2)

var list []string
for elem := range m.Iter() {
	list = append(list, elem.(string))
}
sort.Strings(list)
fmt.Println(list)
Output:
[a b c d e]

Jump to

Keyboard shortcuts

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