compare

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package compare provides functions for comparison.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnySliceEqual added in v0.5.0

func AnySliceEqual[S constraints.Slice[T], T any](a, b S) bool

AnySliceEqual is a generic function to test whether the specified slices have the same length and the items with the same index are equal. In particular, a nil slice and a non-nil empty slice are considered unequal.

It uses the function AnyEqual to test the equality of the slice items.

The client can instantiate it to get an EqualFunc.

func AnyValueMapEqual added in v0.12.0

func AnyValueMapEqual[M constraints.Map[K, V], K comparable, V any](
	a M,
	b M,
) bool

AnyValueMapEqual is a generic function to test whether the specified maps have the same key-value pairs. In particular, a nil map and a non-nil empty map are considered unequal.

It uses the equal operator (==) to test the equality of the map keys and the function AnyEqual to test the equality of the map values.

The client can instantiate it to get an EqualFunc.

func Equal

func Equal[T comparable](a, b T) bool

Equal is a generic function to test whether a == b.

The client can instantiate it to get an EqualFunc.

For floating-point numbers, to consider NaN values equal to each other, use function FloatEqual.

func FloatCompare added in v0.13.0

func FloatCompare[T constraints.Float](a, b T) int

FloatCompare is a generic function that returns

-1 if a < b, or a is a NaN and b is not a NaN,
+1 if a > b, or a is not a NaN and b is a NaN,
 0 otherwise (a == b or both a and b are NaN).

It implements a strict weak ordering. See <https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings> for details.

It treats NaN values as less than any others. A NaN is considered equal to a NaN, and -0.0 is equal to 0.0.

The client can instantiate it to get a CompareFunc.

func FloatEqual added in v0.7.2

func FloatEqual[T constraints.Float](a, b T) bool

FloatEqual is a generic function that returns true if a == b or both a and b are NaN.

The client can instantiate it to get an EqualFunc.

To just test whether a == b, use function Equal.

func FloatLess added in v0.5.0

func FloatLess[T constraints.Float](a, b T) bool

FloatLess is a generic function to test whether a < b for floating-point numbers.

It implements a strict weak ordering. See <https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings> for details.

It treats NaN values as less than any others.

The client can instantiate it to get a LessFunc.

func FloatSliceEqual added in v0.7.2

func FloatSliceEqual[S constraints.Slice[T], T constraints.Float](a, b S) bool

FloatSliceEqual is a generic function to test whether the specified slices have the same length and the items with the same index are equal. In particular, a nil slice and a non-nil empty slice are considered unequal.

Two items (floating-point numbers) x and y are considered equal if x == y or both x and y are NaN.

The client can instantiate it to get an EqualFunc.

func FloatSliceEqualWithoutOrder added in v0.7.2

func FloatSliceEqualWithoutOrder[S constraints.Slice[T], T constraints.Float](
	a S,
	b S,
) bool

FloatSliceEqualWithoutOrder is like SliceEqualWithoutOrder, but it considers two items (floating-point numbers) x and y equal if x == y or both x and y are NaN.

The client can instantiate it to get an EqualFunc.

func FloatValueMapEqual added in v0.12.0

func FloatValueMapEqual[M constraints.Map[K, V], K comparable, V constraints.Float](
	a M,
	b M,
) bool

FloatValueMapEqual is a generic function to test whether the specified maps have the same key-value pairs. In particular, a nil map and a non-nil empty map are considered unequal.

Two map keys k1 and k2 are considered equal if k1 == k2. Two map values v1 and v2 are considered equal if v1 == v2 or both v1 and v2 are NaN.

The client can instantiate it to get an EqualFunc.

func MapEqual added in v0.12.0

func MapEqual[M constraints.Map[K, V], K, V comparable](a, b M) bool

MapEqual is a generic function to test whether the specified maps have the same key-value pairs. In particular, a nil map and a non-nil empty map are considered unequal.

It uses the not equal operator (!=) to test the equality of the map keys and values.

The client can instantiate it to get an EqualFunc.

func OrderedCompare added in v0.13.0

func OrderedCompare[T constraints.Ordered](a, b T) int

OrderedCompare is a generic function that returns

-1 if a < b,
+1 if a > b,
 0 otherwise.

The client can instantiate it to get a CompareFunc.

Note that floating-point comparison (the < operator on float32 or float64 values) is not a strict weak ordering when not-a-number (NaN) values are involved. If a strict weak ordering is required (such as sorting), use the function FloatCompare for floating-point numbers.

func OrderedLess added in v0.5.0

func OrderedLess[T constraints.Ordered](a, b T) bool

OrderedLess is a generic function to test whether a < b.

The client can instantiate it to get a LessFunc.

Note that floating-point comparison (the < operator on float32 or float64 values) is not a strict weak ordering when not-a-number (NaN) values are involved. If a strict weak ordering is required (such as sorting), use the function FloatLess for floating-point numbers.

func ReflexiveEqual added in v0.13.0

func ReflexiveEqual[T comparable](a, b T) bool

ReflexiveEqual is a generic function to test whether a equals b. In particular, it considers that all elements that are not equal to themselves (such as not-a-number (NaN) values of floating-point numbers) are equal. Therefore, this equality relation is reflexive.

The client can instantiate it to get an EqualFunc.

To just test whether a == b, use function Equal.

func SliceEqual added in v0.11.0

func SliceEqual[S constraints.Slice[T], T comparable](a, b S) bool

SliceEqual is a generic function to test whether the specified slices have the same length and the items with the same index are equal. In particular, a nil slice and a non-nil empty slice are considered unequal.

It uses the not equal operator (!=) to test the equality of the slice items.

The client can instantiate it to get an EqualFunc.

func SliceEqualWithoutOrder added in v0.11.0

func SliceEqualWithoutOrder[S constraints.Slice[T], T comparable](a, b S) bool

SliceEqualWithoutOrder is a generic function to test whether the specified slices have the same length and items. It compares the items of the slice regardless of their order. For example, the following slices are equal to each other for this function:

[]int{0, 0, 1, 2}, []int{0, 0, 2, 1}, []int{0, 1, 0, 2}, []int{0, 1, 2, 0},
[]int{0, 2, 0, 1}, []int{0, 2, 1, 0}, []int{1, 0, 0, 2}, []int{1, 0, 2, 0},
...

because they all have two "0", one "1", and one "2". In particular, a nil slice and a non-nil empty slice are considered unequal.

It is useful when slices are treated as sets or multisets rather than sequences.

The client can instantiate it to get an EqualFunc.

Types

type CompareFunc added in v0.13.0

type CompareFunc[T any] func(a, b T) int

CompareFunc is a function that returns

-1 if a is less than b,
 0 if a equals b,
+1 if a is greater than b.

To use CompareFunc in cases where a strict weak ordering is required, such as sorting, it must implement a strict weak ordering. For strict weak ordering, see <https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings>.

Note that floating-point comparison (the < operator on float32 or float64 values) is not a strict weak ordering when not-a-number (NaN) values are involved.

func (CompareFunc[T]) Reverse added in v0.13.0

func (cf CompareFunc[T]) Reverse() CompareFunc[T]

Reverse returns a reverse function that returns

-1 if a is greater than b,
 0 if a equals b,
+1 if a is less than b.

Reverse returns nil if this CompareFunc is nil.

func (CompareFunc[T]) ToEqual added in v0.13.0

func (cf CompareFunc[T]) ToEqual() EqualFunc[T]

ToEqual returns an EqualFunc to test whether a == b. The returned function reports true if and only if

compare(a, b) == 0

ToEqual returns nil if this CompareFunc is nil.

func (CompareFunc[T]) ToLess added in v0.13.0

func (cf CompareFunc[T]) ToLess() LessFunc[T]

ToLess returns a LessFunc to test whether a < b. The returned function reports true if and only if

compare(a, b) < 0

ToLess returns nil if this CompareFunc is nil.

type EqualFunc

type EqualFunc[T any] func(a, b T) bool

EqualFunc is a function to test whether a == b.

var AnyEqual EqualFunc[any] = anyEqual

AnyEqual is a prefab EqualFunc performing as follows:

If any input variable is nil (the nil any (i.e., nil interface{})), it returns true if and only if the other input variable is also nil or is the zero value of its type.

Otherwise (two input variables are both non-nil any (i.e., non-nil interface{})), it returns true if and only if the two input variables satisfies the following three conditions:

  1. they have identical dynamic types;
  2. values of their type are comparable;
  3. they have equal dynamic values.

If any input variable is not comparable, it returns false rather than panicking.

Note that for floating-point numbers, NaN values are not equal to each other. To consider NaN values equal to each other, use function FloatEqual.

For more information about identical types, see <https://go.dev/ref/spec#Type_identity>.

For more information about comparable types, see <https://go.dev/ref/spec#Comparison_operators>.

func EqualToSliceEqual added in v0.11.0

func EqualToSliceEqual[S constraints.Slice[T], T any](
	ef EqualFunc[T],
	nilEqualsEmpty bool,
) EqualFunc[S]

EqualToSliceEqual returns a function to test whether two slices of type S (whose underlying type is []T), a and b, have the same length and whether their items with the same index are equal.

It uses ef to test the equality of the slice items. If ef is nil, it uses AnyEqual instead.

nilEqualsEmpty indicates whether to consider a nil slice equal to a non-nil empty slice.

func ValueEqualToMapEqual added in v0.12.0

func ValueEqualToMapEqual[M constraints.Map[K, V], K comparable, V any](
	ef EqualFunc[V],
	nilEqualsEmpty bool,
) EqualFunc[M]

ValueEqualToMapEqual returns a function to test whether two maps of type M (whose underlying type is map[K]V), a and b, have the same key-value pairs.

It uses the equal operator (==) to test the equality of the map keys and ef to test the equality of the map values. If ef is nil, it uses AnyEqual instead.

nilEqualsEmpty indicates whether to consider a nil map equal to a non-nil empty map.

func (EqualFunc[T]) Not

func (ef EqualFunc[T]) Not() EqualFunc[T]

Not returns a negative function to test whether !(a == b).

It returns nil if this EqualFunc is nil.

func (EqualFunc[T]) Reflexive added in v0.13.0

func (ef EqualFunc[T]) Reflexive() EqualFunc[T]

Reflexive returns an EqualFunc that guarantees the reflexivity of the equality relation. The returned function considers that all elements that are not equal to themselves (such as not-a-number (NaN) values of floating-point numbers) are equal.

It returns nil if this EqualFunc is nil.

type LessFunc

type LessFunc[T any] func(a, b T) bool

LessFunc is a function to test whether a < b.

To use LessFunc in cases where a strict weak ordering is required, such as sorting, it must implement a strict weak ordering. For strict weak ordering, see <https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings>.

Note that floating-point comparison (the < operator on float32 or float64 values) is not a strict weak ordering when not-a-number (NaN) values are involved.

func (LessFunc[T]) Not

func (lf LessFunc[T]) Not() LessFunc[T]

Not returns a negative function to test whether !(a < b).

It returns nil if this LessFunc is nil.

func (LessFunc[T]) Reverse

func (lf LessFunc[T]) Reverse() LessFunc[T]

Reverse returns a reverse function to test whether b < a.

It returns nil if this LessFunc is nil.

func (LessFunc[T]) ToCompare added in v0.13.0

func (lf LessFunc[T]) ToCompare() CompareFunc[T]

ToCompare returns a CompareFunc that returns

-1 if less(a, b),
+1 if less(b, a),
 0 otherwise.

ToCompare returns nil if this LessFunc is nil.

func (LessFunc[T]) ToEqual

func (lf LessFunc[T]) ToEqual() EqualFunc[T]

ToEqual returns an EqualFunc to test whether a == b. The returned function reports true if and only if

!(less(a, b) || less(b, a))

ToEqual returns nil if this LessFunc is nil.

Jump to

Keyboard shortcuts

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