set

package
v0.0.0-...-44cc335 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 5 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. 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 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 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}

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
}

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]) 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]) 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, itemn}

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