sets

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 3 Imported by: 2

README

go-maps

A Go package that contains useful map types.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EqualSet

type EqualSet[T interface {
	Equals(other T) bool
}] struct {
	// contains filtered or unexported fields
}

EqualSet represents a set of elements that implements the Equals method.

Interface: Equals(other T) bool

func NewEqualSet

func NewEqualSet[T interface {
	Equals(other T) bool
}]() *EqualSet[T]

NewEqualSet creates a new empty set.

Returns:

  • *EqualSet[T]: The created set. Never returns nil.

func (*EqualSet[T]) Add

func (s *EqualSet[T]) Add(elem T)

Add adds an element to the set. If the element already exists or the receiver is nil, this function does nothing.

Parameters:

  • elem: The element to add.

func (*EqualSet[T]) AddMany

func (s *EqualSet[T]) AddMany(elems []T)

AddMany is the same as Add, but adds many elements at once. More optimized than calling Add multiple times.

Parameters:

  • elems: The elements to add.

func (*EqualSet[T]) All

func (s *EqualSet[T]) All() iter.Seq[T]

All returns an iterator that iterates over all elements in the set.

Returns:

  • iter.Seq[T]: The iterator. Never returns nil.

func (EqualSet[T]) IsEmpty

func (s EqualSet[T]) IsEmpty() bool

IsEmpty implements the Set interface.

func (*EqualSet[T]) Reset

func (s *EqualSet[T]) Reset()

Reset implements the Set interface.

func (EqualSet[T]) Size

func (s EqualSet[T]) Size() int

Size implements the Set interface.

func (*EqualSet[T]) Union

func (s *EqualSet[T]) Union(other *EqualSet[T]) int

Union adds all elements from another set to the set.

Parameters:

  • other: The other set to add.

Returns:

  • int: The number of elements added.

If the receiver or 'other' is nil, then 0 is returned, always.

type OrderedSet

type OrderedSet[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

OrderedSet is a set that is ordered by the keys.

func NewOrderedSet

func NewOrderedSet[K cmp.Ordered, V any]() *OrderedSet[K, V]

NewOrderedSet creates a new OrderedSet.

Returns:

  • *OrderedSet: A pointer to the newly created OrderedSet. Never returns nil.

func (*OrderedSet[K, V]) Add

func (m *OrderedSet[K, V]) Add(key K, value V)

Add adds a key-value pair to the map. Does nothing if the receiver is nil or the key already exists.

Parameters:

  • key: The key to add.
  • value: The value to add.

func (OrderedSet[K, V]) Contains

func (m OrderedSet[K, V]) Contains(key K) bool

Contains checks if the key exists in the map.

Parameters:

  • key: The key to check.

Returns:

  • bool: True if the key exists in the map. False if the key does not exist.

func (OrderedSet[K, V]) Entry

func (m OrderedSet[K, V]) Entry() iter.Seq2[K, V]

Entry returns an iterator that iterates over the entries in the map according to the order of the keys.

Returns:

  • iter.Seq2[K, V]: The iterator. Never returns nil.

func (*OrderedSet[K, V]) ForceAdd

func (m *OrderedSet[K, V]) ForceAdd(key K, value V)

ForceAdd is the same as Add, except that it will overwrite the value if the key already exists.

Parameters:

  • key: The key to add.
  • value: The value to add.

func (OrderedSet[K, V]) Get

func (m OrderedSet[K, V]) Get(key K) (V, bool)

Get returns the value of the key in the map.

Parameters:

  • key: The key to get.

Returns:

  • V: The value of the key in the map.
  • bool: True if the key exists in the map. False if the key does not exist.

func (OrderedSet[K, V]) IsEmpty

func (m OrderedSet[K, V]) IsEmpty() bool

IsEmpty implements the Set interface.

func (OrderedSet[K, V]) Keys

func (m OrderedSet[K, V]) Keys() []K

Keys returns a copy of the keys in the map.

Returns:

  • []K: The keys in the map.

func (OrderedSet[K, V]) Map

func (m OrderedSet[K, V]) Map() map[K]V

Map returns a copy of the map of the values in the map.

Returns:

  • map[K]V: The map of the values in the map. Nil if there are no keys.

func (*OrderedSet[K, V]) Remove

func (m *OrderedSet[K, V]) Remove(key K)

Remove removes the key from the map. Does nothing if the receiver is nil or the key does not exist.

Parameters:

  • key: The key to remove.

func (*OrderedSet[K, V]) Reset

func (m *OrderedSet[K, V]) Reset()

Reset implements the Set interface.

func (OrderedSet[K, V]) Size

func (m OrderedSet[K, V]) Size() int

Size implements the Set interface.

type SeenSet

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

SeenSet is a map that keeps track of seen values.

func NewSeenSet

func NewSeenSet[T comparable]() *SeenSet[T]

NewSeenSet creates a new SeenSet.

Returns:

  • *SeenSet[T]: The new SeenSet. Never returns nil.

func (SeenSet[T]) Copy added in v0.1.1

func (sm SeenSet[T]) Copy() *SeenSet[T]

Copy returns a shallow copy of the receiver.

Returns:

  • *SeenSet[T]: The copy. Never returns nil.

func (SeenSet[T]) FilterNotSeen

func (sm SeenSet[T]) FilterNotSeen(elems []T) []T

FilterNotSeen is like FilterSeen but returns the elements that are not seen.

Parameters:

  • elems: The elements to filter.

Returns:

  • []T: The elements that are not seen.

func (SeenSet[T]) FilterSeen

func (sm SeenSet[T]) FilterSeen(elems []T) []T

FilterSeen returns the elements that are seen. The order of the elements is preserved and no duplicates are contained.

Parameters:

  • elems: The elements to filter.

Returns:

  • []T: The elements that are seen.

func (SeenSet[T]) Has

func (sm SeenSet[T]) Has(v T) bool

Has checks whether the value is seen.

Parameters:

  • v: The value to check.

Returns:

  • bool: True if the value is seen, false otherwise.

func (SeenSet[T]) IsEmpty

func (sm SeenSet[T]) IsEmpty() bool

IsEmpty implements the Set interface.

func (*SeenSet[T]) Reset

func (sm *SeenSet[T]) Reset()

Reset implements the Set interface.

func (*SeenSet[T]) See

func (sm *SeenSet[T]) See(v T) bool

See sets the value as seen.

Parameters:

  • v: The value to set as seen.

Returns:

  • bool: True if the value was set as seen or the receiver was nil. False otherwise.

func (*SeenSet[T]) SetSeen

func (sm *SeenSet[T]) SetSeen(v T)

SetSeen sets the value as seen. Does nothing if the receiver is nil or the value is already seen.

Parameters:

  • v: The value to set as seen.

func (SeenSet[T]) Size

func (sm SeenSet[T]) Size() int

Size implements the Set interface.

type Set

type Set interface {
	// IsEmpty checks whether the set is empty.
	//
	// Returns:
	//   - bool: True if the set is empty, false otherwise.
	IsEmpty() bool

	// Size returns the number of elements in the set.
	//
	// Returns:
	//   - int: The number of elements in the set. Never returns a negative number.
	Size() int

	// Reset removes all elements from the set.
	Reset()
}

Jump to

Keyboard shortcuts

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