set

package module
v1.0.1 Latest Latest
Warning

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

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

README

set Go Reference Go Report Card GoCover

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.

  • Written in vanilla Go with no dependency.
  • Supports thread-safety.
  • Two different set options. Thread-safe and Thread-unsafe.
  • Supports any data type(integer, float, string, byte, and so on).

Installation

go get github.com/gozeloglu/set

Thread Unsafe Set Example

package main

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

func main() {
	unsafeSet := set.New(set.ThreadUnsafe)
	unsafeSet.Add(123)

	exist := unsafeSet.Contains(123)
	if !exist {
		fmt.Println("123 not exist")
	}

	unsafeSet.Append(1, 2, 3, 4, "abc")    // Add multiple values
	values := []interface{}{"github", 100, 640, 0.43, false}
	unsafeSet.Append(values...) // Append the array of elements 

	unsafeSet.Remove(4)
	size := unsafeSet.Size()
	fmt.Println(size)   // Prints 5

	unsafeSet.Pop()    // Returns random value from the set
	unsafeSet.Clear()
	fmt.Println(unsafeSet.Size())   // Prints 0
	
	if unsafeSet.Empty() {
            fmt.Println("set is empty")
        }   
}

Thread Safe Set Example

package main

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

func main() {
	safeSet := set.New(set.ThreadSafe)
	safeSet.Append(1, 2, 3, 4)  // Add multiple values

	exist := safeSet.Contains(2)
	if !exist {
		fmt.Println("2 not exist")
	}

	values := []interface{}{"github", 100, 640, 0.43, false}
	safeSet.Append(values...) // Append the array of elements 

	safeSet.Remove(4)
	size := safeSet.Size()
	fmt.Println(size)

	safeSet.Pop()
	safeSet.Clear()
	fmt.Println(safeSet.Size())
	
	if safeSet.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.

make test   # Runs all tests
make test-v # Runs all tests with -v option
make cover  # Runs all tests with -cover option. Prints out coverage result
make race   # Runs all tests with -race option. 
make bench  # Runs benchmarks

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(set.ThreadUnsafe)
	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

View Source
const (
	// ThreadSafe is used in New() for creating ThreadSafeSet.
	ThreadSafe = iota

	// ThreadUnsafe is used in New() for creating ThreadUnsafeSet.
	ThreadUnsafe
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set 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
}

Set is set interface.

func New

func New(t setType) Set

New creates a set data structure regarding setType. You can call the New function with two different setType.

safeSet := New(set.ThreadSafe)	// Creates a thread-safe set.
unsafeSet := New(set.ThreadUnsafe)	// Creates a thread-unsafe set.

type ThreadSafeSet added in v1.0.0

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

ThreadSafeSet is a set type which provides the thread-safety.

func (*ThreadSafeSet) Add added in v1.0.0

func (s *ThreadSafeSet) Add(val interface{})

Add adds a new values to set.

func (*ThreadSafeSet) Append added in v1.0.0

func (s *ThreadSafeSet) Append(values ...interface{})

Append adds multiple values into set.

func (*ThreadSafeSet) Clear added in v1.0.0

func (s *ThreadSafeSet) Clear()

Clear removes everything from the set.

func (*ThreadSafeSet) Contains added in v1.0.0

func (s *ThreadSafeSet) Contains(val interface{}) bool

Contains checks the value whether exists in the set.

func (*ThreadSafeSet) Difference added in v1.0.0

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

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

func (*ThreadSafeSet) Empty added in v1.0.0

func (s *ThreadSafeSet) Empty() bool

Empty checks whether the set is empty.

func (*ThreadSafeSet) Equal added in v1.0.0

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

Equal checks whether both sets contain exactly the same values.

func (*ThreadSafeSet) Intersection added in v1.0.0

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

Intersection takes the common values from both sets and returns a new set that stores the common ones.

func (*ThreadSafeSet) IsDisjoint added in v1.0.0

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

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

func (*ThreadSafeSet) IsSubset added in v1.0.0

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

IsSubset returns true if all items in the set exist in the given set. Otherwise, it returns false.

func (*ThreadSafeSet) IsSuperset added in v1.0.0

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

IsSuperset returns true if all items in the given set exist in the set. Otherwise, it returns false.

func (*ThreadSafeSet) Pop added in v1.0.0

func (s *ThreadSafeSet) Pop() interface{}

Pop returns a random value from the set. If there is no element in set, it returns nil. It does not remove any elements from the set.

func (*ThreadSafeSet) Remove added in v1.0.0

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

Remove deletes the given value.

func (*ThreadSafeSet) Size added in v1.0.0

func (s *ThreadSafeSet) Size() uint

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

func (*ThreadSafeSet) Slice added in v1.0.0

func (s *ThreadSafeSet) 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 (*ThreadSafeSet) SymmetricDifference added in v1.0.0

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

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

func (*ThreadSafeSet) Union added in v1.0.0

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

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

type ThreadUnsafeSet added in v1.0.0

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

ThreadUnsafeSet is a set type which does not provide the thread-safety.

func (*ThreadUnsafeSet) Add added in v1.0.0

func (s *ThreadUnsafeSet) Add(val interface{})

Add adds a new values to set if there is enough capacity. It is not a thread-safe method. It does not handle the concurrency.

Example:

s.Add("str")
s.Add(12)

func (*ThreadUnsafeSet) Append added in v1.0.0

func (s *ThreadUnsafeSet) Append(values ...interface{})

Append adds multiple values into set. It is not a thread-safe method. It does not handle the concurrency.

Example:

s.Append(1,2,3,4, true, false, "str")

func (*ThreadUnsafeSet) Clear added in v1.0.0

func (s *ThreadUnsafeSet) Clear()

Clear removes everything from the set. It is not a thread-safe method. It does not handle the concurrency.

Example:

s.Clear()

func (ThreadUnsafeSet) Contains added in v1.0.0

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

Contains checks the value whether exists in the set. It is not a thread-safe method. It does not handle the concurrency.

Example:

exist := s.Contains(1)

func (*ThreadUnsafeSet) Difference added in v1.0.0

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

Difference takes the items that only is stored in s, receiver set. It returns a new set. It is not a thread-safe method. It does not handle the concurrency.

Example:

diffSet := s1.Difference(s2)

func (ThreadUnsafeSet) Empty added in v1.0.0

func (s ThreadUnsafeSet) Empty() bool

Empty checks whether the set is empty. It is not a thread-safe method. It does not handle the concurrency.

Example:

empty := s.Empty()

func (*ThreadUnsafeSet) Equal added in v1.0.0

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

Equal checks whether both sets contain exactly the same values. It is not a thread-safe method. It does not handle the concurrency.

Example:

equal := s1.Equal(s2)

func (*ThreadUnsafeSet) Intersection added in v1.0.0

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

Intersection takes the common values from both sets and returns a new set that stores the common ones. It is not a thread-safe method. It does not handle the concurrency.

Example:

intersectionSet := s1.Intersection(s2)

func (*ThreadUnsafeSet) IsDisjoint added in v1.0.0

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

IsDisjoint returns true if none of the items are present in the sets. It is not a thread-safe method. It does not handle the concurrency.

Example:

isDisjoint := s1.IsDisjoint(s2)

func (*ThreadUnsafeSet) IsSubset added in v1.0.0

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

IsSubset returns true if all items in the set exist in the given set. Otherwise, it returns false. It is not a thread-safe method. It does not handle the concurrency.

Example:

isSubset := s1.IsSubset(s2)

func (*ThreadUnsafeSet) IsSuperset added in v1.0.0

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

IsSuperset returns true if all items in the given set exist in the set. Otherwise, it returns false. It is not a thread-safe method. It does not handle the concurrency.

Example:

isSuperset := s1.IsSuperset(s2)

func (ThreadUnsafeSet) Pop added in v1.0.0

func (s ThreadUnsafeSet) Pop() interface{}

Pop returns a random value from the set. If there is no element in set, it returns nil. It is not a thread-safe method. It does not handle the concurrency.

Example:

val := s.Pop()

func (*ThreadUnsafeSet) Remove added in v1.0.0

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

Remove deletes the given value. It is not a thread-safe method. It does not handle the concurrency.

Example:

s.Remove(2)

func (ThreadUnsafeSet) Size added in v1.0.0

func (s ThreadUnsafeSet) Size() uint

Size returns the length of the set which means that number of value of the set. It is not a thread-safe method. It does not handle the concurrency.

Example:

size := s.Size()

func (ThreadUnsafeSet) Slice added in v1.0.0

func (s ThreadUnsafeSet) Slice() []interface{}

Slice returns the elements of the set as a slice. The slice type is interface{}. The elements can be in any order. It is not a thread-safe method. It does not handle the concurrency.

Example:

setSlice := s.Slice()

func (*ThreadUnsafeSet) SymmetricDifference added in v1.0.0

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

SymmetricDifference returns a set that contains from two sets, but not the items are present in both sets. It is not a thread-safe method. It does not handle the concurrency.

Example:

symmetricDiffSet := s1.SymmetricDifference(s2)

func (ThreadUnsafeSet) Union added in v1.0.0

func (s ThreadUnsafeSet) Union(set Set) Set

Union returns a new Set that contains all items from the receiver Set and all items from the given Set. It is not a thread-safe method. It does not handle the concurrency.

Example:

unionSet := s1.Union(s2)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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