set

package module
v1.0.0 Latest Latest
Warning

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

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

README

Set

Simple implementation of a set data structure in Go.

Documentation

Please refer to DOCS.md for the package documentation.

Usage Example

package main

import (
	"fmt"

	"github.com/marvinpeter95/set"
)

func main() {
    s := set.New(1, 2, 2, 3)

    for v := range s.Values() {
        println(v)
    }

    s.Insert(4)

    s.Delete(2)
}

Documentation

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 defines a set of comparable values.

func From

func From[T comparable](collection []T) Set[T]

From creates a Set from the given collection.

func FromSeq

func FromSeq[T comparable](seq iter.Seq[T]) Set[T]

FromSeq creates a Set from the given iterator.

func New

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

New creates a new Set from the given values.

func (Set[T]) Contains

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

Contains checks if the Set contains the given value.

func (Set[T]) Delete

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

Delete removes the given value from the Set.

func (Set[T]) Insert

func (s Set[T]) Insert(value T)

Insert adds the given value to the Set.

func (Set[T]) Len

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

Len returns the number of elements in the Set.

func (Set[T]) String

func (s Set[T]) String() string

String returns the string representation of the StableSet.

func (Set[T]) ToSlice

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

ToSlice converts the Set to a slice.

func (Set[T]) Values

func (s Set[T]) Values() iter.Seq[T]

Values returns an iterator over the Set values.

type SetLike

type SetLike[T comparable] interface {
	fmt.Stringer

	// Len returns the number of elements in the Set.
	Len() int

	// Values returns an iterator over the Set values.
	Values() iter.Seq[T]

	// ToSlice converts the Set to a slice.
	ToSlice() []T

	// Contains checks if the Set contains the given value.
	Contains(value T) bool

	// Insert adds the given value to the Set.
	Insert(value T)

	// Delete removes the given value from the Set.
	Delete(value T)
	// contains filtered or unexported methods
}

type StableSet

type StableSet[T comparable] struct {
	// contains filtered or unexported fields
}

StableSet defines a set of comparable values that maintains insertion order.

func FromSeqStable

func FromSeqStable[T comparable](seq iter.Seq[T]) *StableSet[T]

FromSeqStable creates a Set from the given iterator.

func FromStable

func FromStable[T comparable](collection []T) *StableSet[T]

FromStable creates a StableSet from the given collection.

func NewStable

func NewStable[T comparable](values ...T) *StableSet[T]

NewStable creates a new StableSet from the given values.

func (StableSet[T]) Contains

func (s StableSet[T]) Contains(value T) bool

Contains checks if the Set contains the given value.

func (*StableSet[T]) Delete

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

Delete removes the given value from the Set.

func (*StableSet[T]) Insert

func (s *StableSet[T]) Insert(value T)

Insert adds the given value to the Set.

func (StableSet[T]) Len

func (s StableSet[T]) Len() int

Len returns the number of elements in the Set.

func (StableSet[T]) String

func (s StableSet[T]) String() string

String returns the string representation of the StableSet.

func (StableSet[T]) ToSlice

func (s StableSet[T]) ToSlice() []T

ToSlice converts the Set to a slice.

func (StableSet[T]) Values

func (s StableSet[T]) Values() iter.Seq[T]

Values returns an iterator over the Set values.

Jump to

Keyboard shortcuts

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