sets

package
v0.0.5 Latest Latest
Warning

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

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

Documentation

Overview

Package sets provides several implementation of a Set and related helpers.

Index

Examples

Constants

This section is empty.

Variables

View Source
var U = api.U

U is the unit value used to populate the set.

Functions

This section is empty.

Types

type Set

type Set[C comparable] map[C]Unit

Set uses a go map to represent a set of elements of type C as keys with no value.

Example
package main

import (
	"fmt"
	"sort"

	"github.com/toolvox/utilgo/pkg/sets"
)

func main() {
	// Initialize a new set of integers
	s := sets.NewSet(1, 2, 3)

	// Add an element to the set
	s.Add(4)
	var resultValues []int = []int{1, 2, 3, 4}

	// Display the set
	fmt.Println("Set:", resultValues)

	// Create another set
	other := sets.NewSet(3, 4, 5)

	// UnionWith of s and other
	UnionWith := s.Union(other)
	resultValues = UnionWith.Elements()
	sort.Ints(resultValues)
	fmt.Println("UnionWith:", resultValues)

	// IntersectionWith of s and other
	IntersectionWith := s.Intersection(other)
	resultValues = IntersectionWith.Elements()
	sort.Ints(resultValues)
	fmt.Println("IntersectionWith:", resultValues)

	// DifferenceWith of s and other
	DifferenceWith := s.Difference(other)
	resultValues = DifferenceWith.Elements()
	sort.Ints(resultValues)
	fmt.Println("DifferenceWith:", resultValues)

	// Check if an element is in the set
	fmt.Println("Contains 3:", s.Contains(3))
	fmt.Println("Contains 6:", s.Contains(6))

}
Output:

Set: [1 2 3 4]
UnionWith: [1 2 3 4 5]
IntersectionWith: [3 4]
DifferenceWith: [1 2]
Contains 3: true
Contains 6: false

func NewSet

func NewSet[C comparable](elements ...C) Set[C]

NewSet initializes a new Set with the given elements, ensuring uniqueness.

func (Set[C]) Add

func (set Set[C]) Add(elements ...C)

Add unique elements to the Set. Repeated elements will be discarded.

func (Set[C]) Contains

func (set Set[C]) Contains(elements ...C) bool

Contains checked whether all elements are in the Set.

func (Set[C]) Difference

func (set Set[C]) Difference(other Set[C]) Set[C]

Difference creates a set of elements in the first set but not in the second.

func (Set[C]) DifferenceWith

func (set Set[C]) DifferenceWith(elements ...C) Set[C]

DifferenceWith removes specified elements from the set.

func (Set[C]) Elements

func (set Set[C]) Elements() []C

Elements returns the unique elements of the Set.

func (Set[C]) Intersection

func (set Set[C]) Intersection(other Set[C]) Set[C]

Intersection creates a set of elements common to both sets.

func (Set[C]) IntersectionWith

func (set Set[C]) IntersectionWith(elements ...C) Set[C]

IntersectionWith forms a set from common elements of the set and the provided elements.

func (Set[C]) Len

func (set Set[C]) Len() int

Len counts the elements in the Set.

func (Set[T]) MarshalJSON

func (set Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON converts the Set to a JSON array.

func (Set[T]) MarshalYAML

func (set Set[T]) MarshalYAML() (interface{}, error)

MarshalYAML converts the Set to a YAML array.

func (Set[C]) Remove

func (set Set[C]) Remove(elements ...C)

Remove any existing elements from the Set.

func (Set[C]) String

func (set Set[C]) String() string

String returns the string representation of the Set.

func (Set[C]) ThreeWay

func (set Set[C]) ThreeWay(other Set[C]) [3]Set[C]

ThreeWay splits elements into three sets: common, only in the first set, and only in the second set.

func (Set[C]) Union

func (set Set[C]) Union(other Set[C]) Set[C]

Union combines two sets into a new one containing elements from both.

func (Set[C]) UnionWith

func (set Set[C]) UnionWith(elements ...C) Set[C]

UnionWith adds multiple elements to the set and returns the resulting set.

func (*Set[T]) UnmarshalJSON

func (set *Set[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON converts the JSON []byte to a Set.

func (*Set[T]) UnmarshalYAML

func (set *Set[T]) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML converts the YAML yaml.Node (representing a YAML list) to a Set.

type TinySet

type TinySet[C comparable] []C

TinySet represents a set of elements of type C using a slice for storage. It is optimized for small sets where the overhead of a map is not justified.

func NewTinySet

func NewTinySet[C comparable](elements ...C) *TinySet[C]

NewTinySet initializes a new TinySet with the given elements, ensuring uniqueness.

func (*TinySet[C]) Add

func (tinySet *TinySet[C]) Add(elements ...C)

Add unique elements to the TinySet. Repeated elements will be discarded.

func (TinySet[C]) Contains

func (tinySet TinySet[C]) Contains(elements ...C) bool

Contains checked whether all elements are in the TinySet.

func (TinySet[C]) Difference

func (tinySet TinySet[C]) Difference(other TinySet[C]) TinySet[C]

Difference returns a new TinySet containing elements present in this set but not in the other set.

func (TinySet[C]) DifferenceWith

func (tinySet TinySet[C]) DifferenceWith(elements ...C) TinySet[C]

DifferenceWith returns a new TinySet containing elements present in this set but not among the provided elements.

func (TinySet[C]) Elements

func (tinySet TinySet[C]) Elements() []C

Elements returns the unique elements of the TinySet.

func (TinySet[C]) Intersection

func (tinySet TinySet[C]) Intersection(other TinySet[C]) TinySet[C]

Intersection returns a new TinySet containing elements that exist in both sets.

func (TinySet[C]) IntersectionWith

func (tinySet TinySet[C]) IntersectionWith(elements ...C) TinySet[C]

IntersectionWith returns a new TinySet containing elements that exist in both this set and the provided elements.

func (TinySet[C]) Len

func (tinySet TinySet[C]) Len() int

Len counts the elements in the TinySet.

func (TinySet[T]) MarshalJSON

func (tinySet TinySet[T]) MarshalJSON() ([]byte, error)

MarshalJSON converts the TinySet to a JSON array.

func (TinySet[T]) MarshalYAML

func (tinySet TinySet[T]) MarshalYAML() (interface{}, error)

MarshalYAML converts the TinySet to a YAML array.

func (*TinySet[C]) Remove

func (tinySet *TinySet[C]) Remove(elements ...C)

Remove any existing elements from the TinySet.

func (TinySet[C]) String

func (tinySet TinySet[C]) String() string

String returns the string representation of the TinySet.

func (TinySet[C]) ThreeWay

func (tinySet TinySet[C]) ThreeWay(other TinySet[C]) [3]TinySet[C]

ThreeWay splits elements into three [TinySets]: common to both, only in the first, and only in the second.

func (TinySet[C]) Union

func (tinySet TinySet[C]) Union(other TinySet[C]) TinySet[C]

Union merges the current set with another set and returns the result as a new TinySet.

func (TinySet[C]) UnionWith

func (tinySet TinySet[C]) UnionWith(elements ...C) TinySet[C]

UnionWith adds multiple elements to the set and returns the resulting set. This operation does not modify the original set but returns a new one.

func (*TinySet[T]) UnmarshalJSON

func (tinySet *TinySet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON converts the JSON []byte to a TinySet.

func (*TinySet[T]) UnmarshalYAML

func (tinySet *TinySet[T]) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML converts the YAML yaml.Node (representing a YAML list) to a TinySet.

type Unit

type Unit = api.Unit

Unit type for the set implementation.

Jump to

Keyboard shortcuts

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