compare

package
v0.297.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEqual

func IsEqual(cmp int) bool

IsEqual reports whether two values are equal based on their comparison result.

func IsGreater

func IsGreater(cmp int) bool

IsGreater reports whether the receiver is greater than another value.

func IsGreaterOrEqual

func IsGreaterOrEqual(cmp int) bool

IsGreaterOrEqual reports whether the receiver is greater than or equal to another value.

func IsLess

func IsLess(cmp int) bool

IsLess reports whether the receiver is less than another value.

func IsLessOrEqual

func IsLessOrEqual(cmp int) bool

IsLessOrEqual reports whether the receiver is less than or equal to another value.

func IsMore

func IsMore(cmp int) bool

IsMore reports whether the receiver is greater than another value.

func IsMoreOrEqual

func IsMoreOrEqual(cmp int) bool

IsMoreOrEqual reports whether the receiver is more than or equal to another value.

func Numbers

func Numbers[T constraints.Number](a, b T) int

Types

type Equable

type Equable[T any] interface {
	IsEqual(oth T) bool
}

type Interface

type Interface[T any] interface {
	// Compare returns:
	//   -1 if receiver is less than the argument,
	//    0 if they're equal, and
	//   +1 if receiver is greater.
	//
	// Think of the result of Compare like a seesaw:
	// The side that’s lower (touching the ground) represents the smaller value.
	// The side that’s up shows the larger value — it’s higher, so it’s greater.
	//
	// Implementors must ensure consistent ordering semantics.
	Compare(T) int
}

Interface defines how comparison can be implemented. An implementation of Interface can be sorted by the routines in this package. The methods refer to elements of the underlying collection by integer index.

Types implementing this interface must provide a Compare method that defines the ordering or equivalence of values. This pattern is useful when working with: 1. Custom user-defined types requiring comparison logic 2. Encapsulated values needing semantic comparisons 3. Comparison-agnostic systems (e.g., sorting algorithms)

Example usage:

type MyNumber int

func (m MyNumber) Compare(other MyNumber) int {
	if m < other {
		return -1
	}
	if other < m {
		return +1
	}
	return 0
}

type ShortInterface

type ShortInterface[T any] interface {
	// Cmp compares x and y and returns:
	//   - -1 if x  < y;
	//   -  0 if x == y;
	//   - +1 if x  > y.
	//
	// x cmp y == x cmp y
	// x cmp (-y) == x
	// (-x) cmp y == y
	// (-x) cmp (-y) == -(x cmp y)
	//
	Cmp(T) int
}

Jump to

Keyboard shortcuts

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