Documentation
¶
Overview ¶
Package set is a set package provides set data structure for Go. It is written without any dependency.
package main
import (
"fmt"
"github.com/gozeloglu/set"
)
func main() {
s := set.New()
s.Add(123)
exist := s.Contains(123)
if !exist {
fmt.Println("123 not exist")
}
s.Append(1, 2, 3, 4, "abc") // Add multiple values
values := []interface{}{"github", 100, 640, 0.43, false}
s.Append(values...) // Append the array of elements
s.Remove(4)
size := s.Size()
fmt.Println(size) // Prints 5
s.Pop() // Returns random value from the set
s.Clear()
fmt.Println(s.Size()) // Prints 0
}
You can call the Union() method for creating a new Set which contains the all items from the set1 and set2. It concatenates two sets and creates a new one.
union := set1.Union(set2)
In order to take intersection of the sets, you can call the Intersection() method.
intersect := set1.Intersection(set2)
You can find the difference between sets by Difference() method.
// Returns a set that contains the items which are only contained in the set1. diffSet := set1.Difference(set2)
You can check the set which is subset of the another set with IsSubset() method.
isSubset := set1.IsSubset(set2) // Returns true is set1 is subset of set2.
You can check the set which is superset of the another set with IsSuperset() method.
isSuperset := set1.IsSuperset(set2) // Returns true is set1 is superset of set2.
You can check whether the sets are equal with the Equal() method.
equal := set1.Equal(set2) // Returns true if set1 and set2 values are exactly the same
You can check whether the sets are disjoint with IsDisjoint() method.
isDisjoint := set1.IsDisjoint(set2) // Returns true is set1 and set2 are disjoint.
You can get the symmetric difference of two sets by SymmetricDifference() method.
// Returns a set which is the symmetric difference of the two sets. symDiffSet := set1.SymmetricDifference(set2)
Index ¶
- type S
- type Set
- func (s *Set) Add(val interface{})
- func (s *Set) Append(values ...interface{})
- func (s *Set) Clear()
- func (s Set) Contains(val interface{}) bool
- func (s *Set) Difference(set *Set) *Set
- func (s Set) Empty() bool
- func (s *Set) Equal(set *Set) bool
- func (s *Set) Intersection(set *Set) *Set
- func (s *Set) IsDisjoint(set *Set) bool
- func (s *Set) IsSubset(set *Set) bool
- func (s *Set) IsSuperset(set *Set) bool
- func (s Set) Pop() interface{}
- func (s *Set) Remove(val interface{})
- func (s Set) Size() uint
- func (s Set) Slice() []interface{}
- func (s *Set) SymmetricDifference(set *Set) *Set
- func (s Set) Union(set *Set) *Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type S ¶
type S interface {
Add(val interface{})
Append(val ...interface{})
Remove(val interface{})
Contains(val interface{}) bool
Size() uint
Pop() interface{}
Clear()
Empty() bool
Slice() []interface{}
Union(set *Set) *Set
Intersection(set *Set) *Set
Difference(set *Set) *Set
IsSubset(set *Set) bool
IsSuperset(set *Set) bool
IsDisjoint(set *Set) bool
Equal(set *Set) bool
SymmetricDifference(set *Set) *Set
}
S is set interface.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is the data structure which provides some functionalities.
func (*Set) Add ¶
func (s *Set) Add(val interface{})
Add adds a new values to set if there is enough capacity.
func (*Set) Append ¶
func (s *Set) Append(values ...interface{})
Append adds multiple values into set.
func (*Set) Difference ¶ added in v0.4.0
Difference takes the items that only is stored in s, receiver set. It returns a new set.
func (*Set) Intersection ¶ added in v0.3.0
Intersection takes the common values from both sets and returns a new set that stores the common ones.
func (*Set) IsDisjoint ¶ added in v0.8.0
IsDisjoint returns true if none of the items are present in the sets.
func (*Set) IsSubset ¶ added in v0.5.0
IsSubset returns true if all items in the set exist in the given set. Otherwise, it returns false.
func (*Set) IsSuperset ¶ added in v0.6.0
IsSuperset returns true if all items in the given set exist in the set. Otherwise, it returns false.
func (Set) Pop ¶
func (s Set) Pop() interface{}
Pop returns a random value from the set. If there is no element in set, it returns nil.
func (Set) Slice ¶ added in v0.2.0
func (s Set) Slice() []interface{}
Slice returns the elements of the set as a slice. The slice type is interface{}. The elements can be in any order.
func (*Set) SymmetricDifference ¶ added in v0.8.0
SymmetricDifference returns a set that contains from two sets, but not the items are present in both sets.