set

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 1 Imported by: 0

README

goutils/set

Go Reference

Contains the utility functions to deal with sets.

Inspired by JavaScript's set methods


📖 Usage

import (
    "fmt"
    "github.com/Shresht7/goutils/set"
)

func main() {
    s := []int{0, 1, 2, 3}

    set.Add(s, 4)
    set.Add(s, 4) // No effect as 4 is already present
    set.Add(s, 5)

    set.ForEach(s, func (value, idx int) {
        fmt.Println(value)
    }) // Output: 0 1 2 3 4 5
}

To use the method syntax, create a new Set

s := set.New([]int{0, 1, 2, 3})

s.Add(4)
s.Add(4) // No effect as 4 is already present
s.Add(5)

s.ForEach(func (value, idx int) {
    fmt.Println(value)
}) // Output: 0 1 2 3 4 5

Alternatives, just typecast the []int into Set[int] provided by the package.

s := set.Set[int]([]int{0, 1, 2, 3})

s.Add(4)
s.Add(4) // No effect as 4 is already present
s.Add(5)

s.ForEach(func (value, index int) {
    fmt.Println(index, value)
}) // Output: 0 1 2 3 4 5

⬆️ Back to Top


📘 API Reference

Go Reference

Len

Returns the length of the set.

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

Example:

s := set.New([]int{0, 1, 2, 3})
s.Len() // Output: 4

⬆️ Back to Top

Cap

Returns the capacity of the set.

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

Example:

s := set.New([]int{0, 1, 2, 3})
s.Cap() // Output: 4

⬆️ Back to Top

AsSlice

Returns the set as a slice.

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

Example:

s := set.New([]int{0, 1, 2, 3})
s.AsSlice() // Output: [0 1 2 3]

⬆️ Back to Top

Size

Returns the size of the set.

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

Example:

s := set.New([]int{0, 1, 2, 3})
s.Size() // Output: 4

⬆️ Back to Top

Has

Checks if the set has the given element.

func (s *Set[T]) Has(value T) bool

Example:

s := set.New([]int{0, 1, 2, 3})
s.Has(2) // Output: true
s.Has(4) // Output: false

⬆️ Back to Top

Add

Adds an element to the set.

func (s *Set[T]) Add(value T) *Set[T]

Example:

s := set.New([]int{0, 1, 2, 3})
s.Add(4)
s.Add(4) // No effect as 4 is already present
s.Add(5)
s.AsSlice() // Output: [0 1 2 3 4 5]

⬆️ Back to Top

Delete

Deletes an element from the set.

func (s *Set[T]) Delete(value T) *Set[T]

Example:

s := set.New([]int{0, 1, 2, 3})
s.Delete(2)
s.AsSlice() // Output: [0 1 3]

⬆️ Back to Top

Clear

Clears the set.

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

Example:

s := set.New([]int{0, 1, 2, 3})
s.Clear()
s.AsSlice() // Output: []

⬆️ Back to Top

ForEach

Iterates over the set and calls the given function for each element.

func (s *Set[T]) ForEach(fn slice.Callback[T])

Example:

s := set.New([]int{0, 1, 2, 3})
s.ForEach(func (value, index int) {
    fmt.Println(index, value)
}) // Output: 0 1 2 3

⬆️ Back to Top


📑 License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] slice.Slice[T]

Set is a collection of unique values

Example (SliceMethods)
// Create a Set
set := New([]int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4})

// Cast the set to a slice
slice := slice.Slice[int](set)

slice.ForEach(func(v, i int) {
	fmt.Println(v)
})

mapped := slice.Map(func(v, i int) int {
	return v * 2
})

fmt.Println(mapped)
Output:

1
2
3
4
[2 4 6 8]

func New

func New[T comparable](slice []T) Set[T]

New creates a new set from a slice

Example
// Create a slice
slice := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}

// Create a set from the slice
set := New(slice)

// Print the set
fmt.Println(set)
Output:

[1 2 3 4]

func (*Set[T]) Add

func (s *Set[T]) Add(value T) *Set[T]

Adds another element to the Set

Example
// Create a Set
set := New([]int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4})

set.Add(5)
set.Add(7)

// Print the set
fmt.Println(set)
Output:

[1 2 3 4 5 7]

func (*Set[T]) AsSlice

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

Returns the Set as a normal slice

func (*Set[T]) Cap

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

Returns the capacity of the Set

func (*Set[T]) Clear

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

Clears the entire Set

Example
// Create a Set
set := New([]int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4})

// Clear the set
set.Clear()

// Print the set
fmt.Println(set)
Output:

[]

func (*Set[T]) Delete

func (s *Set[T]) Delete(value T) *Set[T]

Delete an element from the Set

Example
// Create a Set
set := New([]int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4})

set.Delete(2)
set.Delete(4)

// Print the set
fmt.Println(set)
Output:

[1 3]

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(f slice.Callback[T])

Executes a callback function for each element in the Set

Example
// Create a Set
set := New([]int{1, 2, 3, 4})

// Print the set
set.ForEach(func(v, i int) {
	fmt.Println(v)
})
Output:

1
2
3
4

func (*Set[T]) Has

func (s *Set[T]) Has(value T) bool

Checks if the set has the given value

Example
// Create a slice
slice := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}

// Create a set from the slice
set := New(slice)

// Check if the set has the value 2
fmt.Println(set.Has(2))
Output:

true

func (*Set[T]) Len

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

Returns the length of the Set

func (*Set[T]) Size

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

Returns the size of the Set

Jump to

Keyboard shortcuts

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