set

package
v0.0.0-...-a4a72b7 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

package set implements a Set using a golang map. This implies that only the types that are accepted as valid map keys can be used as set elements. For instance, do not try to Add a slice, or the program will panic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] interface {
	// Add: adds new element to the set
	Add(item T)
	// Delete: deletes the passed element from the set if present
	Delete(item T)
	// Len: gives the length of the set (total no. of elements in set)
	Len() int
	// GetItems: gives the array( []T ) of elements of the set.
	GetItems() []T
	// In: checks whether item is present in set or not.
	In(item T) bool
	// IsSubsetOf: checks whether set is subset of set2 or not.
	IsSubsetOf(set2 Set[T]) bool
	// IsProperSubsetOf: checks whether set is proper subset of set2 or not.
	// ex: [1,2,3] proper subset of [1,2,3,4] -> true
	IsProperSubsetOf(set2 Set[T]) bool
	// IsSupersetOf: checks whether set is superset of set2 or not.
	IsSupersetOf(set2 Set[T]) bool
	// IsProperSupersetOf: checks whether set is proper superset of set2 or not.
	// ex: [1,2,3,4] proper superset of [1,2,3] -> true
	IsProperSupersetOf(set2 Set[T]) bool
	// Union: gives new union set of both sets.
	// ex: [1,2,3] union [3,4,5] -> [1,2,3,4,5]
	Union(set2 Set[T]) Set[T]
	// Intersection: gives new intersection set of both sets.
	// ex: [1,2,3] Intersection [3,4,5] -> [3]
	Intersection(set2 Set[T]) Set[T]
	// Difference: gives new difference set of both sets.
	// ex: [1,2,3] Difference [3,4,5] -> [1,2]
	Difference(set2 Set[T]) Set[T]
	// SymmetricDifference: gives new symmetric difference set of both sets.
	// ex: [1,2,3] SymmetricDifference [3,4,5] -> [1,2,4,5]
	SymmetricDifference(set2 Set[T]) Set[T]
}

Set is an interface of possible methods on 'set'.

Example
set := New(1, 2, 3)
fmt.Println(set.Len()) // 3
set.Add(3)
fmt.Println(set.Len()) // 3
set.Add(4)
fmt.Println(set.Len()) // 4
Output:

3
3
4

func New

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

New gives new set.

Jump to

Keyboard shortcuts

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