set

package module
v0.5.1 Latest Latest
Warning

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

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

README

Build Status Code Coverage Go Report Card GoDoc

golang-map-set

A simple, generic, non-threadsafe set type for the Go language. This package provides an easy-to-use and efficient implementation of sets, tailored for developers looking for an essential set functionality in Go.

Install

Use go get to install this package.

go get github.com/felixenescu/golang-map-set

Import it with:

import set "github.com/felixenescu/golang-map-set"

and use set as the package name inside the code.

Usage

Set works with any comparable type. In Go, comparable types are those that can be compared using equality operators == and !=. These types include:

  • Boolean types
  • Numeric types (integer, float, complex)
  • String types
  • Pointer types
  • Channel types
  • Interface types
  • An array type is comparable if the elements' type is comparable.
  • A struct type is comparable if all its fields are comparable.

Note that slices, maps, and functions are not comparable.

Creating a set
  • Empty Set: Create a new empty set.
// create a set of integers
s1 := set.New[int]()

// create a set of strings
s2 := set.New[string]()
  • From Slice: Create a new set from a slice.
// create a set from a slice of integers
slice := []int{1, 2, 3}
s := set.NewFromSlice(slice)

// create a set from a slice of strings
slice := []string{"a", "b", "c"}
s := set.NewFromSlice(slice)
  • From Map Keys: Create a new set from the keys of a map.
// create a set from the keys of a map of integers
m := map[int]string{1: "a", 2: "b"}
s := set.NewFromMapKeys(m)

// create a set from the keys of a map of strings
m := map[string]int{"a": 1, "b": 2}
s := set.NewFromMapKeys(m)
Basic Operations
  • Add Elements: Add one or more elements to the set.
s1 := set.New[int]()
s.Add(4) // set is now {4}
s.AddAll([]int{5, 6}) // set is now {4, 5, 6}
  • Remove Elements: Remove one or more elements from the set.
s := set.NewFromSlice([]int{1, 2, 3, 4, 5})
s.Remove(2) // set is now {1, 3, 4, 5}
s.RemoveAll([]int{3, 4}) // set is now {1, 5}
  • Contains: Check if an element is in the set.
s := set.New[int]()
s.Add(1)
if s.Contains(1) {
    // element 1 is in the set
    fmt.Println("1 is in the set")
}
  • To Slice: Convert the set to a slice.
s := set.New[int]()
s.AddAll([]int{1, 2, 3})
slice := s.ToSlice()
fmt.Println(slice) // [1 2 3]
Set Operations
  • Union: Combine two sets.
s1 := set.NesFromSlice([]int{1, 2, 3})
s2 := set.NewFromSlice([]int{4, 5, 6})
u := s1.Union(s2) // u is now {1, 2, 3, 4, 5, 6}
  • Intersection: Get the common elements of two sets.
s1 := set.NewFromSlice([]int{1, 2, 3})
s2 := set.NewFromSlice([]int{2, 3, 4})
i := s1.Intersection(s2) // i is now {2, 3}
  • Difference: Get the elements that are in one set but not the other.
s1 := set.NewFromSlice([]int{1, 2, 3})
s2 := set.NewFromSlice([]int{2, 3, 4})
d := s1.Difference(s2) // d is now {1}

Remember, golang-map-set is not threadsafe, so appropriate precautions should be taken when using it in a concurrent environment.

Documentation

Overview

Package set implements a simple generic, not threadsafe set data structure. It provides constructors form slices and maps, and methods for typical set operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] map[T]struct{}

Set is a generic, not threadsafe set data structure.

func New

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

New creates a new Set.

func NewFromMapKeys

func NewFromMapKeys[T comparable, V any](m map[T]V) Set[T]

NewFromMapKeys creates a new Set from a map's keys.

func NewFromSlice

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

NewFromSlice creates a new Set from a slice of comparable.

func (Set[T]) Add

func (set Set[T]) Add(s T)

Add adds an element to a Set.

func (Set[T]) AddAll

func (set Set[T]) AddAll(slice []T)

AddAll adds a slice of elements to a Set.

func (Set[T]) Contains

func (set Set[T]) Contains(s T) bool

Contains returns true if a Set contains an element.

func (Set[T]) Difference

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

Difference returns the difference of two Sets as new Set.

func (Set[T]) Equals

func (set Set[T]) Equals(other Set[T]) bool

Equals returns true if two Sets are equal.

func (Set[T]) Intersection

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

Intersection returns the intersection of two Sets as new Set.

func (Set[T]) IsProperSubsetOf

func (set Set[T]) IsProperSubsetOf(other Set[T]) bool

IsProperSubsetOf returns true if a Set is a proper subset of another Set (they cannot be equal).

func (Set[T]) IsSubsetOf

func (set Set[T]) IsSubsetOf(other Set[T]) bool

IsSubsetOf returns true if a Set is a subset of another Set (they can be equal).

func (Set[T]) Remove

func (set Set[T]) Remove(s T)

Remove removes an element from a Set.

func (Set[T]) RemoveAll

func (set Set[T]) RemoveAll(slice []T)

RemoveAll removes a slice of elements from a Set.

func (Set[T]) ToSlice

func (set Set[T]) ToSlice() []T

ToSlice returns an unordered slice of elements from a Set.

func (Set[T]) Union

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

Union returns the union of two Sets as new Set.

Jump to

Keyboard shortcuts

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