cmp

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteElem added in v0.1.6

func DeleteElem[T cmp.Ordered](slice []T, elem T) []T

DeleteElem deletes an element from a slice and returns the new slice. If the element is not found, the slice is returned unchanged.

Parameters:

  • slice: The slice to delete the element from.
  • elem: The element to delete.

Returns:

  • []T: The new slice with the element deleted.

func Max

func Max[T cmp.Ordered](a, b T) T

Max returns the maximum of a and b.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • T: The maximum value.

func MaxVar

func MaxVar[T cmp.Ordered](elems []T) (T, bool)

MaxVar returns the maximum of the elements in the slice.

Parameters:

  • elems: The elements.

Returns:

  • T: The maximum value.
  • bool: Whether the operation was successful.

func Maxs

func Maxs[T cmp.Ordered](elems []T) []int

Maxs returns the maximums of the elements in the slice.

Parameters:

  • elems: The elements.

Returns:

  • []int: The indices of the maximum values.

func Min

func Min[T cmp.Ordered](a, b T) T

Min returns the minimum of a and b.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • T: The minimum value.

func MinVar

func MinVar[T cmp.Ordered](elems []T) (T, bool)

MinVar returns the minimum of the elements in the slice.

Parameters:

  • elems: The elements.

Returns:

  • T: The minimum value.
  • bool: Whether the operation was successful.

func Mins

func Mins[T cmp.Ordered](elems []T) []int

Mins returns the minimums of the elements in the slice.

Parameters:

  • elems: The elements.

Returns:

  • []int: The indices of the minimum values.

func Uniquefy added in v0.1.11

func Uniquefy[T cmp.Ordered](S []T, prioritizeFirst bool) []T

Uniquefy removes duplicate elements from the slice.

Parameters:

  • S: slice of elements.
  • prioritizeFirst: If true, the first occurrence of an element is kept. If false, the last occurrence of an element is kept.

Returns:

  • []T: slice of elements with duplicates removed.

Behavior:

  • The function preserves the order of the elements in the slice.

Types

type Set added in v0.1.11

type Set[T SetElem] struct {
	// contains filtered or unexported fields
}

Set represents a set of elements.

func NewSet added in v0.1.11

func NewSet[T SetElem]() *Set[T]

NewSet creates a new Set.

Returns:

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

func NewSetFromSlice added in v0.1.11

func NewSetFromSlice[T SetElem](elems []T) *Set[T]

NewSetFromSlice creates a new Set from a slice of elements.

Parameters:

  • elems: The slice of elements.

Returns:

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

func (*Set[T]) Add added in v0.1.11

func (s *Set[T]) Add(elem T) bool

Add adds an element to the set. If the element is already in the set, this method does nothing.

Parameters:

  • elem: The element to add.

Returns:

  • bool: True if the element was added to the set, false otherwise.

if the receiver is nil, this method returns false.

func (Set[T]) All added in v0.1.11

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

All returns an iterator over all elements in the set.

Returns:

  • iter.Seq[T]: An iterator over all elements in the set.

func (Set[T]) Contains added in v0.1.11

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

Contains checks whether the set contains the given element.

Parameters:

  • elem: The element to check for.

Returns:

  • bool: True if the set contains the element, false otherwise.

func (Set[T]) Equals added in v0.1.11

func (s Set[T]) Equals(other *Set[T]) bool

Equals checks whether the set is equal to another set. Two Sets are said to be equal if the intersection of their elements is non-empty.

Parameters:

  • other: The other set to compare with.

Returns:

  • bool: True if the sets are equal, false otherwise.

If 'others' is nil, this method returns false.

func (Set[T]) IsEmpty added in v0.1.11

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

IsEmpty checks whether the set is empty.

Returns:

  • bool: True if the set is empty, false otherwise.

func (Set[T]) Len added in v0.1.11

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

Len returns the number of elements in the set.

Returns:

  • int: The number of elements in the set.

func (Set[T]) Slice added in v0.1.11

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

Slice returns the slice of elements in the set.

Returns:

  • []T: The slice of elements in the set.

func (Set[T]) String added in v0.1.11

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

String implements the fmt.Stringer interface

Format:

{elem1, elem2, ...}

func (*Set[T]) Union added in v0.1.11

func (s *Set[T]) Union(other *Set[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.

type SetElem added in v0.1.11

type SetElem interface {
	cmp.Ordered
	fmt.Stringer
}

SetElem represents an element in a set.

Jump to

Keyboard shortcuts

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