cmp

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cmp provides tools to do equal, less, compare operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(a, b []byte) int

func BytesEqual

func BytesEqual(a, b []byte) bool

BytesEqual reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.

func Compare

func Compare[T Ordered](x, y T) int

Compare returns

-1 if x is less than y,
 0 if x equals y,
+1 if x is greater than y.

func FCompare

func FCompare[T FOrdered](x, y T) int

FCompare is Compare with floating-point types allowed.

Note:

  • A NaN is considered less than any non-NaN
  • A NaN is considered equal to a NaN
  • and -0.0 is equal to 0.0.

func FLess

func FLess[T FOrdered](x, y T) bool

FLess is Less with floating-point types.

Note:

  • A NaN is considered less than any non-NaN
  • -0.0 is not less than (is equal to) 0.0.

func Less

func Less[T Ordered](a, b T) bool

Less reports whether a is less than b.

func Slice

func Slice[E any](a, b []E, cmp func(ctx *Context[[]E], i int) int) int

Compare compares the elements of a and b, using cmp on each pair of elements. The elements are compared sequentially, starting at index 0, until one element is not equal to the other.

The result of comparing the first non-matching elements is returned. If both slices are equal until one of them ends, the shorter slice is considered less than the longer one. The result is 0 if a == b, -1 if a < b, and +1 if a > b.

func SliceEx

func SliceEx[E, T any](a, b []E, arg T, cmp func(arg T, ctx *Context[[]E], i int) int) int

SliceEx is like Slice, but takes an extra arg.

func String

func String(a, b string) int

func StringEqual

func StringEqual(a, b string) bool

StringEqual reports whether a and b are the same length and contain the same bytes.

Types

type Context

type Context[T any] struct {
	A, B T
}

type FOrdered

type FOrdered interface{ Ordered | num.Float }

FOrdered is Ordered plus floating-point types.

Note that floating-point types may contain NaN ("not-a-number") values. An operator such as == or < will always report false when comparing a NaN value with any other value, NaN or not. See the FCompare function for a consistent way to compare NaN values.

type Ordered

type Ordered interface{ num.Integer | ~string }

Ordered is a constraint that permits any ordered type except floating-point types: any type that supports the operators < <= >= >.

If future releases of Go add new ordered types, this constraint will be modified to include them.

Jump to

Keyboard shortcuts

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