set

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: MIT Imports: 0 Imported by: 0

README

set Go Reference

set is a data structure package written by Go. It provides some basic set functionalities of the user. It uses map data structure under the hood. It does not have any dependency.

Installation

go get github.com/gozeloglu/set

Example

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
	
	if s.Empty() {
            fmt.Println("set is empty")
        }   
}

Supported methods

  • Add(val interface{})
  • Append(val ...interface{})
  • Remove(val interface{})
  • Contains(val interface{})
  • Size()
  • Pop()
  • Clear()
  • Empty()
  • Slice()
  • Union()
  • Intersection()
  • Difference()
  • IsSubset()
  • IsSuperset()
  • IsDisjoint()
  • Equal()
  • SymmetricDifference()

Tests

You can run the tests with the following command.

go test ./...

You can check the code coverage with the following commands:

go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html

LICENSE

MIT

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

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 New

func New() *Set

New creates a set data structure.

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) Clear

func (s *Set) Clear()

Clear removes everything from the set.

func (Set) Contains

func (s Set) Contains(val interface{}) bool

Contains checks the value whether exists in the set.

func (*Set) Difference added in v0.4.0

func (s *Set) Difference(set *Set) *Set

Difference takes the items that only is stored in s, receiver set. It returns a new set.

func (Set) Empty

func (s Set) Empty() bool

Empty checks whether the set is empty.

func (*Set) Equal added in v0.7.0

func (s *Set) Equal(set *Set) bool

Equal checks whether both sets contain exactly the same values.

func (*Set) Intersection added in v0.3.0

func (s *Set) Intersection(set *Set) *Set

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

func (s *Set) IsDisjoint(set *Set) bool

IsDisjoint returns true if none of the items are present in the sets.

func (*Set) IsSubset added in v0.5.0

func (s *Set) IsSubset(set *Set) bool

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

func (s *Set) IsSuperset(set *Set) bool

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) Remove

func (s *Set) Remove(val interface{})

Remove deletes the given value.

func (Set) Size

func (s Set) Size() uint

Size returns the length of the set which means that number of value of the set.

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

func (s *Set) SymmetricDifference(set *Set) *Set

SymmetricDifference returns a set that contains from two sets, but not the items are present in both sets.

func (Set) Union added in v0.3.0

func (s Set) Union(set *Set) *Set

Union returns a new Set that contains all items from the receiver Set and all items from the given Set.

Jump to

Keyboard shortcuts

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