sets

package
v0.0.0-...-15c7053 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package sets provides set types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bit

type Bit struct {
	// contains filtered or unexported fields
}

Bit is a bitmap with O(1) lookup, insertion, and deletion. The zero value is an empty set.

func CollectBit

func CollectBit(seq iter.Seq[uint]) *Bit

Collect returns a new set that contains the elements of the given iterator.

func NewBit

func NewBit(elem ...uint) *Bit

New returns a new set that contains the arguments passed to it.

func (*Bit) Add

func (s *Bit) Add(elem ...uint)

Add adds the arguments to the set.

func (*Bit) AddSeq

func (s *Bit) AddSeq(seq iter.Seq[uint])

AddSeq adds the values from seq to the set.

func (*Bit) All

func (s *Bit) All() iter.Seq[uint]

All returns an iterator of the elements of s. Elements are in ascending order.

func (*Bit) Clear

func (s *Bit) Clear()

Clear removes all elements from the set, but retains the space allocated for the set.

func (*Bit) Clone

func (s *Bit) Clone() *Bit

Clone returns a new set that contains the same elements as s.

func (*Bit) Delete

func (s *Bit) Delete(x uint)

Delete removes x from the set if present.

func (*Bit) Equal

func (s *Bit) Equal(s2 *Bit) bool

Equal reports whether s and s2 contain the same elements.

func (*Bit) Format

func (s *Bit) Format(f fmt.State, verb rune)

Format implements fmt.Formatter by formatting its elements according to the printer state and verb surrounded by braces.

func (*Bit) Has

func (s *Bit) Has(x uint) bool

Has reports whether the set contains x.

func (*Bit) Len

func (s *Bit) Len() int

Len returns the number of elements in the set.

func (*Bit) Max

func (s *Bit) Max() (_ uint, nonEmpty bool)

Max returns the largest value in the set.

func (*Bit) Min

func (s *Bit) Min() (_ uint, nonEmpty bool)

Min returns the smallest value in the set.

func (*Bit) Reversed

func (s *Bit) Reversed() iter.Seq[uint]

Reversed returns an iterator of the elements of s in descending order.

type Set

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

Set is an unordered set with O(1) lookup, insertion, and deletion. The zero value is an empty set.

func Collect

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

Collect returns a new set that contains the elements of the given iterator.

func New

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

New returns a new set that contains the arguments passed to it.

func (Set[T]) Add

func (s Set[T]) Add(elem ...T)

Add adds the arguments to the set.

func (Set[T]) AddSeq

func (s Set[T]) AddSeq(seq iter.Seq[T])

AddSeq adds the values from seq to the set.

func (Set[T]) All

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

All returns an iterator of the elements of s.

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all elements from the set, but retains the space allocated for the set.

func (Set[T]) Clone

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

Clone returns a new set that contains the same elements as s.

func (Set[T]) Delete

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

Delete removes x from the set if present.

func (Set[T]) Format

func (s Set[T]) Format(f fmt.State, verb rune)

Format implements fmt.Formatter by formatting its elements according to the printer state and verb surrounded by braces.

Example
package main

import (
	"fmt"

	"zb.256lights.llc/pkg/sets"
)

func main() {
	s := sets.New(3.14159)
	fmt.Printf("%.2f\n", s)
}
Output:

{3.14}

func (Set[T]) Has

func (s Set[T]) Has(x T) bool

Has reports whether the set contains x.

func (Set[T]) Len

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

Len returns the number of elements in the set.

type Sorted

type Sorted[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Sorted is a sorted list of unique items. The zero value is an empty set. nil is treated like an empty set, but any attempts to add to it will panic.

func CollectSorted

func CollectSorted[T cmp.Ordered](seq iter.Seq[T]) *Sorted[T]

Collect returns a new set that contains the elements of the given iterator. Equivalent to calling Sorted.AddSeq on a zero set.

func NewSorted

func NewSorted[T cmp.Ordered](elem ...T) *Sorted[T]

NewSorted returns a new set with the given elements. Equivalent to calling Sorted.Add on a zero set.

func (*Sorted[T]) Add

func (s *Sorted[T]) Add(elem ...T)

Add adds the arguments to the set.

func (*Sorted[T]) AddSeq

func (s *Sorted[T]) AddSeq(seq iter.Seq[T])

AddSeq adds the values from seq to the set.

func (*Sorted[T]) AddSet

func (s *Sorted[T]) AddSet(other *Sorted[T])

AddSet adds the elements in other to s.

func (*Sorted[T]) All

func (s *Sorted[T]) All() iter.Seq2[int, T]

All returns an iterator of the indices and elements of s.

func (*Sorted[T]) At

func (s *Sorted[T]) At(i int) T

At returns the i'th element in ascending order of the set.

func (*Sorted[T]) Clear

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

Clear removes all elements from the set, but retains the space allocated for the set.

func (*Sorted[T]) Clone

func (s *Sorted[T]) Clone() *Sorted[T]

Clone returns a new set that contains the same elements as s.

func (*Sorted[T]) Delete

func (s *Sorted[T]) Delete(x T)

Delete removes x from the set if present.

func (*Sorted[T]) Format

func (s *Sorted[T]) Format(f fmt.State, verb rune)

Format implements fmt.Formatter by formatting its elements according to the printer state and verb surrounded by braces.

Example
package main

import (
	"fmt"

	"zb.256lights.llc/pkg/sets"
)

func main() {
	s := sets.NewSorted(3.14159, -1.234)
	fmt.Printf("%.2f\n", s)
}
Output:

{-1.23 3.14}

func (*Sorted[T]) Grow

func (s *Sorted[T]) Grow(n int)

Grow ensures that the set can add n more unique elements without allocating.

func (*Sorted[T]) Has

func (s *Sorted[T]) Has(x T) bool

Has reports whether the set contains x.

func (*Sorted[T]) Len

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

Len returns the number of elements in the set.

func (*Sorted[T]) Values

func (s *Sorted[T]) Values() iter.Seq[T]

Values returns an iterator of the elements of s.

Jump to

Keyboard shortcuts

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