set

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2022 License: MIT Imports: 1 Imported by: 0

README

Go Set

Go Reference

This is a Set based on Generic and Map implementations.

Installation

go get github.com/min0625/set

Quick start

package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])

	s.Store(1, 2, 3)
	fmt.Println(s.Has(1))

	s.Delete(1)
	fmt.Println(s.Has(1))

	// Output:
	// true
	// false
}

Example

See: ./set_example_test.go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type None

type None = struct{}

type Set

type Set[Elem comparable] map[Elem]None
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])

	s.Store(1, 2, 3)
	fmt.Println(s.Has(1))

	s.Delete(1)
	fmt.Println(s.Has(1))

}
Output:

true
false
Example (String)
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[string])

	s.Store("a")
	s.Store("b", "c")

	s.Range(func(elem string) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

a
b
c

func (Set[Elem]) Clear

func (s Set[Elem]) Clear()
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])
	s.Store(1, 2, 3)

	s.Clear()

	fmt.Println(s.Len())

}
Output:

0

func (Set[Elem]) Clone

func (s Set[Elem]) Clone() Set[Elem]
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s1 := make(set.Set[int])
	s1.Store(1, 2, 3)

	s2 := s1.Clone()

	s1.Store(4, 5)

	s2.Range(func(elem int) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

1
2
3

func (Set[Elem]) Delete

func (s Set[Elem]) Delete(elems ...Elem)
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])

	s.Store(1, 2, 3)

	s.Delete(2)

	s.Range(func(elem int) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

1
3

func (Set[Elem]) Difference

func (s Set[Elem]) Difference(s2 Set[Elem]) Set[Elem]
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s1 := make(set.Set[int])
	s1.Store(1, 2)

	s2 := make(set.Set[int])
	s2.Store(2, 3)

	s3 := s1.Difference(s2)

	s3.Range(func(elem int) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

1

func (Set[Elem]) Equal

func (s Set[Elem]) Equal(s2 Set[Elem]) (equal bool)
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s1 := make(set.Set[int])
	s1.Store(1, 2, 3)

	s2 := make(set.Set[int])
	s2.Store(1, 2, 3)

	s3 := make(set.Set[int])
	s3.Store(1, 2)

	fmt.Println(s1.Equal(s2))
	fmt.Println(s1.Equal(s3))

}
Output:

true
false

func (Set[Elem]) Has

func (s Set[Elem]) Has(elem Elem) (has bool)
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])

	s.Store(1, 2)

	fmt.Println(s.Has(1))
	fmt.Println(s.Has(2))
	fmt.Println(s.Has(3))
	fmt.Println(s.Has(4))

}
Output:

true
true
false
false

func (Set[Elem]) Intersection

func (s Set[Elem]) Intersection(s2 Set[Elem]) Set[Elem]
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s1 := make(set.Set[int])
	s1.Store(1, 2)

	s2 := make(set.Set[int])
	s2.Store(2, 3)

	s3 := s1.Intersection(s2)

	s3.Range(func(elem int) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

2

func (Set[Elem]) IsSubset

func (s Set[Elem]) IsSubset(s2 Set[Elem]) (isSubset bool)
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s1 := make(set.Set[int])
	s1.Store(1, 2)

	s2 := make(set.Set[int])
	s2.Store(1)

	s3 := make(set.Set[int])
	s3.Store(3)

	s4 := make(set.Set[int])
	s4.Store(1, 2, 3)

	fmt.Println(s1.IsSubset(s1))
	fmt.Println(s1.IsSubset(s2))
	fmt.Println(s1.IsSubset(s3))
	fmt.Println(s1.IsSubset(s4))

}
Output:

true
true
false
false

func (Set[Elem]) Len

func (s Set[Elem]) Len() (length int)
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])

	fmt.Println(s.Len())
	s.Store(1, 2)
	fmt.Println(s.Len())
	s.Delete(2)
	fmt.Println(s.Len())

}
Output:

0
2
1

func (Set[Elem]) Range

func (s Set[Elem]) Range(f func(elem Elem) bool)
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])

	s.Store(1, 2, 3)

	s.Range(func(elem int) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

1
2
3
Example (Break)
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[string])

	s.Store("a", "b", "c")

	var cnt int
	s.Range(func(elem string) bool {
		cnt++
		return false
	})

	fmt.Println(cnt)

}
Output:

1

func (Set[Elem]) Slice

func (s Set[Elem]) Slice() []Elem
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s := make(set.Set[int])
	s.Store(1, 2, 3)

	for _, e := range s.Slice() {
		fmt.Println(e)
	}

}
Output:

1
2
3

func (Set[Elem]) Store

func (s Set[Elem]) Store(elems ...Elem)

func (Set[Elem]) Union

func (s Set[Elem]) Union(s2 Set[Elem]) Set[Elem]
Example
package main

import (
	"fmt"

	"github.com/min0625/set"
)

func main() {
	s1 := make(set.Set[int])
	s1.Store(1, 2)

	s2 := make(set.Set[int])
	s2.Store(2, 3)

	s3 := s1.Union(s2)

	s3.Range(func(elem int) bool {
		fmt.Println(elem)
		return true
	})

}
Output:

1
2
3

Jump to

Keyboard shortcuts

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