set

package
v0.0.0-...-56b3168 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 7 Imported by: 0

README

Set

Set data structure implementation for the Go programming language. Implement Set Theory solutions to your code with this easy to use package.

Features

  • Easy to use
  • 100% test coverage
  • Almost all built-in data types available

High level overview

  • Add/remove items from the Set
  • Checks existence of elements
  • Operations with other sets like union and intersection
  • Easy to interpret string representation when printed with the fmt library

Usage

Import

After installation, add this import statement to your Go file:

import "github.com/joselws/go-utils/set"
Data types

These are the available data types for Set:

  • All int and uint types
    • int, int8, int16, int32, and int64
    • uint, uint8, uint16, uint32, and uint64
  • float32 and float64
  • string
  • rune
  • byte

For the time being, you cannot use Set with maps, slices, structs, Set, or pointer values.

Initialization

Create new sets with the NewSet[T]() constructor, where T is any of the available data types. It returns a *Set[T] type. For example:

mySet := set.NewSet[int]()
String representation

A Set has an easy to interpret print format when shown using the fmt module. For example, given a string set with names:

fmt.Println(nameSet)
// Set[string]{"Jose", "Luis"}
Operations

Add

Add individual items to the set with the Add(item T) method. If the item already exists in the set, it does nothing. This function returns nothing. For example:

fmt.Println(nameSet) // Set[string]{"Jose"}
nameSet.Add("Luis") // Luis is successfully added into the set
nameSet.Add("Jose") // Nothing happens because "Jose" already exists!

AddFromSlice

Add multiple items to the set with the AddFromSlice(items []T) method. This function returns nothing. The same rules applies as with the Add(item T) method: if some of the items already exist within the set, nothing happens.

nameSet := set.NewSet[string]()
nameSlice := []string{"Jose", "Luis"}
nameSet.AddFromSlice(nameSlice) // Jose and Luis are added into the set

Remove

Remove an individual item from the set with Remove(item T). If the item doesn't exist, nothing happens. This function doesn't return anything.

fmt.Println(nameSet) // Set[string]{"Jose"}
nameSet.Remove("Jose") 
fmt.Println(nameSet) // Set[string]{}

Length

Length() int returns the length of the set as an int:

fmt.Println(numberSet) // Set[int]{1, 2, 3, 4, 5}
numberSet.Length() // 5

Contains

Contains(item T) checks if an item exists withing the slice. Returns true if item exists, else returns false:

fmt.Println(nameSet) // Set[string]{"Jose"}
nameSet.Contains("Jose") // true
nameSet.Contains("Luis") // false

ToSlice

ToSlice() []T returns the items within the set in a slice of the same type T as the set in ascending order:

fmt.Println(nameSet) // Set[string]{"Jose", "Luis"}
nameSlice := nameSet.ToSlice()
fmt.Println(nameSlice) // ["Jose" "Luis"]

IsSupersetOf

The function IsSupersetOf(other *Set[T]) bool function takes in another Set of the same type and checks whether the Set is a super set of this other Set. Returns true if it's a superset, and false otherwise:

fmt.Println(numberSet1) // Set[int]{1, 2, 3, 4, 5}
fmt.Println(numberSet2) // Set[int]{2, 4}
numberSet1.IsSupersetOf(numberSet2) // true

IsSubsetOf

The function IsSubsetOf(other *Set[T]) bool function takes in another Set of the same type and checks whether the Set is a subset of this other Set. Returns true if it's a subset, and false otherwise:

fmt.Println(numberSet1) // Set[int]{2, 4}
fmt.Println(numberSet2) // Set[int]{1, 2, 3, 4, 5}
numberSet1.IsSubsetOf(numberSet2) // true

Intersection

Intersection(other *Set[T]) *Set[T] takes in another set and computes the intersection of both sets, returning a new pointer to a Set with the intersected items:

fmt.Println(numberSet1) // Set[int]{2, 4, 6, 8}
fmt.Println(numberSet2) // Set[int]{1, 2, 3, 4, 5}
intersection := numberSet1.Intersection(numberSet2)
fmt.Println(intersection) // Set[int]{2, 4}

Union

Union(other *Set[T]) *Set[T] takes in another set and computes the Union of both sets, returning a new pointer to a Set with the resulting items:

fmt.Println(numberSet1) // Set[int]{2, 4, 6, 8}
fmt.Println(numberSet2) // Set[int]{1, 2, 3, 4, 5}
union := numberSet1.Union(numberSet2)
fmt.Println(union) // Set[int]{1, 2, 3, 4, 5, 6, 8}

IsDisjoint

IsDisjoint(other *Set[T]) bool takes in another set and returns true only if both sets have no elements in common:

fmt.Println(numberSet1) // Set[int]{1, 3, 5}
fmt.Println(numberSet2) // Set[int]{2, 4, 6}
fmt.Println(numberSet1.IsDisjoint(numberSet2)) // true

Copy

Copy() *Set[T] returns an copy of the set:

fmt.Println(numberSet1) // Set[int]{1, 3, 5}
numberSet2 := numberSet1.Copy()
fmt.Println(numberSet2) // Set[int]{1, 3, 5}

Difference

Difference(other *Set[T]) *Set[T] takes in another set and returns the difference of the original set substracted by the other set:

fmt.Println(numberSet1) // Set[int]{1, 2, 3}
fmt.Println(numberSet2) // Set[int]{3}
difference := numberSet1.Difference(numberSet2)
fmt.Println(difference) // Set[int]{1, 2}

Equal

Equal(other *Set[T]) bool returns true if both sets contain the exact same elements. It's different from the == operator because it doesn't check whether both sets refer to the same location in memory:

fmt.Println(numberSet1) // Set[int]{1, 2, 3}
fmt.Println(numberSet2) // Set[int]{1, 2, 3}
fmt.Println(numberSet1.Equal(numberSet2)) // true
fmt.Println(numberSet1 == numberSet2) // false

SymmetricDifference

SymmetricDifference(other *Set[T]) *Set[T] performs the symmetric difference operation between both sets:

fmt.Println(numberSet1) // Set[int]{1, 2}
fmt.Println(numberSet2) // Set[int]{2, 3}
fmt.Println(numberSet1.SymmetricDifference(numberSet2)) // Set[int]{1, 3}

Documentation

Overview

Basic implementation of a Set data structure

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T SetTypes] struct {
	Items map[T]bool
	// contains filtered or unexported fields
}

The Set is a data structure that works with a map under the hood

func NewSet

func NewSet[T SetTypes]() *Set[T]

Use this function whenever you want a new set

func (*Set[T]) Add

func (thisSet *Set[T]) Add(item T)

Add an new item to the set. Nothing happens if it already exists

func (*Set[T]) AddFromSlice

func (thisSet *Set[T]) AddFromSlice(items []T)

Takes in a slice of items and add them to the set

func (*Set[T]) Contains

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

Checks whether an item is in the set. Returns true if the item is in the set, and false otherwise

func (*Set[T]) Copy

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

Returns a copy of the set

func (*Set[T]) Difference

func (thisSet *Set[T]) Difference(otherSet *Set[T]) *Set[T]

Performs thisSet - otherSet

func (*Set[T]) Equal

func (thisSet *Set[T]) Equal(otherSet *Set[T]) bool

Returns true if both sets contain the same elements.

func (*Set[T]) Intersection

func (thisSet *Set[T]) Intersection(otherSet *Set[T]) *Set[T]

Calculates the intersection of your set and another set you pass as arguments, and returns these common values in a new set object

func (*Set[T]) IsDisjoint

func (thisSet *Set[T]) IsDisjoint(otherSet *Set[T]) bool

Returns true if both sets are disjointed, otherwise returns false. Two sets are disjointed if they have no elements in common.

func (*Set[T]) IsSubsetOf

func (thisSet *Set[T]) IsSubsetOf(otherSet *Set[T]) bool

Takes in another set of the same type and checks whether your set (the one you're calling the method with) is a subset of the set you passed as an argument

Returns true if it is a subset, and false otherwise

func (*Set[T]) IsSupersetOf

func (thisSet *Set[T]) IsSupersetOf(otherSet *Set[T]) bool

Takes in another set of the same type and checks whether your set (the one you're calling the method with) is a superset of the set you passed as an argument

Returns true if it is a superset, and false otherwise

func (*Set[T]) Length

func (thisSet *Set[T]) Length() int

Returns the length of the set

func (*Set[T]) Remove

func (thisSet *Set[T]) Remove(item T)

Remove item from set. Does nothing if it doesn't exist

func (*Set[T]) String

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

Outputs set in this format when printed: Set[T]{item1, item2}

func (*Set[T]) SymmetricDifference

func (thisSet *Set[T]) SymmetricDifference(otherSet *Set[T]) *Set[T]

Returns a set with the symmetric difference between both sets

func (*Set[T]) ToSlice

func (thisSet *Set[T]) ToSlice() []T

Returns a slice of the items of the set sorted in ascending order

func (*Set[T]) Union

func (thisSet *Set[T]) Union(otherSet *Set[T]) *Set[T]

Calculates the union of your set and another set you pass as arguments, and returns these common values in a new set object

type SetTypes

type SetTypes interface {
	mytypes.Number | string
}

A Set can only be initialized with the following built-in types.

Jump to

Keyboard shortcuts

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