goset

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2022 License: Apache-2.0 Imports: 4 Imported by: 6

README ΒΆ

Go Report Card

GoSet

πŸ† goset is a generic Go implementation of the Set data structure

Inspired by Python's set, this project uses Go 1.18+ generics to implement all the methods you ever wanted for a Set of items!

πŸ“Œ Install

go get github.com/amit7itz/goset@v1

πŸ€“ Usage

Import:

import "github.com/amit7itz/goset"

Initialize:

// create an empty Set of integers
emptySet := goset.NewSet[int]()

// create a Set of strings with items 
mySet := goset.NewSet[string]("c", "d")

// create a Set of strings from a slice
lettersSet := goset.FromSlice([]string{"a", "b", "c", "d"})

Use:

mySet.Add("c", "d", "e", "f", "g")
mySet.Discard("g")
println(mySet.String())
// Set[string]{"c", "d", "e", "f"}

intersectionSet := mySet.Intersection(lettersSet)
// Set[string]{"c", "d"}

unionSet := mySet.Union(lettersSet)
// Set[string]{"a", "b", "c", "d", "e", "f"}

naturals := goset.NewSet[int](1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
primes := goset.NewSet[int](2, 3, 5, 7)

primes.IsSubset(naturals)
// true

primes.Contains(8)
// false

items := primes.Items()
// []int{2, 3, 5, 7}

Set works on any comparable object, so you can use it also for structs! (But don't use it for structs with pointer fields, it will hurt)

type Person struct {
    Name string
}
peopleSet := goset.NewSet(Person{Name: "Amit"}, Person{Name: "Amit"})
println(peopleSet.Len())
// 1

πŸ“– Spec

Full documentation at GoDoc: https://godoc.org/github.com/amit7itz/goset

Constructors:

  • NewSet
  • FromSlice

Methods:

  • Add
  • Contains
  • Copy
  • Discard
  • For
  • ForWithBreak
  • IsEmpty
  • Items
  • Len
  • Pop
  • Remove
  • String
  • Difference
  • Equal
  • Intersection
  • IsDisjoint
  • IsSubset
  • IsSuperset
  • SymmetricDifference
  • Union
  • Update

🀝 Contributing

Open source is awesome! Feel free to fork the project, open issues, and request new features!

πŸ–‹οΈ Authors

  • Amit Itzkovitch

πŸ’« Show your support

If you liked it then you should have put a ⭐ on it! (And let your friends know, so they can enjoy it too!)

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Set ΒΆ

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set represents a set data structure. You should not call it directly, use NewSet() or FromSlice()

func FromSlice ΒΆ

func FromSlice[T comparable](slice []T) *Set[T]

FromSlice returns a new Set with all the items of the slice.

func NewSet ΒΆ

func NewSet[T comparable](items ...T) *Set[T]

NewSet returns a new Set of the given items

func (*Set[T]) Add ΒΆ

func (s *Set[T]) Add(items ...T)

Add adds item(s) to the Set

func (*Set[T]) Contains ΒΆ

func (s *Set[T]) Contains(item T) bool

Contains returns whether an item is in the Set

func (*Set[T]) Copy ΒΆ

func (s *Set[T]) Copy() *Set[T]

Copy returns a new Set with the same items as the current Set

func (*Set[T]) Difference ΒΆ

func (s *Set[T]) Difference(others ...*Set[T]) *Set[T]

Difference returns a new Set of all the items in the current Set that are not in any of the others

func (*Set[T]) Discard ΒΆ

func (s *Set[T]) Discard(items ...T)

Discard removes item(s) from the Set if exist See also: Remove()

func (*Set[T]) Equal ΒΆ

func (s *Set[T]) Equal(other *Set[T]) bool

Equal returns whether the current Set contains the same items as the other one

func (*Set[T]) For ΒΆ added in v1.1.0

func (s *Set[T]) For(f func(item T))

For runs a function on all the items in the Set

func (*Set[T]) ForWithBreak ΒΆ added in v1.1.0

func (s *Set[T]) ForWithBreak(f func(item T) bool)

ForWithBreak runs a function on all the items in the store if f returns false, the iteration stops

func (*Set[T]) Intersection ΒΆ

func (s *Set[T]) Intersection(others ...*Set[T]) *Set[T]

Intersection returns a new Set with the common items of the current set and all others.

func (*Set[T]) IsDisjoint ΒΆ

func (s *Set[T]) IsDisjoint(other *Set[T]) bool

IsDisjoint returns whether the two Sets have no item in common

func (*Set[T]) IsEmpty ΒΆ

func (s *Set[T]) IsEmpty() bool

IsEmpty returns true if there are no items in the Set

func (*Set[T]) IsSubset ΒΆ

func (s *Set[T]) IsSubset(other *Set[T]) bool

IsSubset returns whether all the items of the current set exist in the other one

func (*Set[T]) IsSuperset ΒΆ

func (s *Set[T]) IsSuperset(other *Set[T]) bool

IsSuperset returns whether all the items of the other set exist in the current one

func (*Set[T]) Items ΒΆ

func (s *Set[T]) Items() []T

Items returns a slice of all the Set items

func (*Set[T]) Len ΒΆ

func (s *Set[T]) Len() int

Len returns the number of items in the Set

func (*Set[T]) MarshalJSON ΒΆ added in v1.2.0

func (s *Set[T]) MarshalJSON() ([]byte, error)

func (*Set[T]) Pop ΒΆ

func (s *Set[T]) Pop() (T, error)

Pop removes an arbitrary item from the Set and returns it. Returns error if the Set is empty

func (*Set[T]) Remove ΒΆ

func (s *Set[T]) Remove(item T) error

Remove removes a single item from the Set. Returns error if the item is not in the Set See also: Discard()

func (*Set[T]) String ΒΆ

func (s *Set[T]) String() string

String returns a string that represents the Set

func (*Set[T]) SymmetricDifference ΒΆ

func (s *Set[T]) SymmetricDifference(other *Set[T]) *Set[T]

SymmetricDifference returns all the items that exist in only one of the Sets

func (*Set[T]) Union ΒΆ

func (s *Set[T]) Union(others ...*Set[T]) *Set[T]

Union returns a new Set of the items from the current set and all others

func (*Set[T]) UnmarshalJSON ΒΆ added in v1.2.0

func (s *Set[T]) UnmarshalJSON(b []byte) error

func (*Set[T]) Update ΒΆ

func (s *Set[T]) Update(others ...*Set[T])

Update adds all the items from the other Sets to the current Set

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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