comparers

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const AbsEpsilonRatio float64 = 1.0 / 19_000_000_000_000_000_000.0

Variables

This section is empty.

Functions

func RegisterDefaultComparer

func RegisterDefaultComparer[T any](comparer IComparer[T])

Types

type IComparer

type IComparer[T any] interface {
	// CompareTyped compares value from params.
	//
	// If x is less than y, returns -1.
	//
	// If x is equals to y, returns 0.s
	//
	// If x is greater than y, returns 1.
	CompareTyped(x, y T) int

	// CompareAny accept any params.
	//
	// If both x and y are nil, return 0.
	//
	// If x is nil and y is not nil, return -1.
	//
	// If x is not nil and y is nil, return 1.
	//
	// The rest, implement in your own way, since type any means you can pass everything here,
	// and you should handle them carefully
	CompareAny(x, y any) int
}

IComparer use methods Compare* to compare value of 2 input values.

If left is less than right, returns -1.

If left is equals to right, returns 0.

If left is greater than right, returns 1.

var BigFloatComparer IComparer[*big.Float] = NewBigFloatComparer()

BigFloatComparer implements IComparer[*big.Float], this compares the real value of 2 *big.Float struct.

If x before y, returns -1. If x after y, returns 1. Otherwise, returns 0.

_______________

Wrapped as interface or pointer are all accepted by compare using `CompareAny` method.

> Eg 2: CompareAny(*big.Float vs *(big.Float)), CompareAny(*big.Float, *any(*big.Float)).

Panic if the final value detected is not big.Float struct.

var BigIntComparer IComparer[*big.Int] = NewBigIntComparer()

BigIntComparer implements IComparer[*big.Int], this compares the real value of 2 *big.Int struct.

If x before y, returns -1. If x after y, returns 1. Otherwise, returns 0.

_______________

Wrapped as interface or pointer are all accepted by compare using `CompareAny` method.

> Eg 2: CompareAny(*big.Int vs *(big.Int)), CompareAny(*big.Int, *any(*big.Int)).

Panic if the final value detected is not big.Int struct.

var BoolComparer IComparer[bool] = NewBoolComparer()

BoolComparer implements IComparer[bool], this compares the real value of 2 boolean with contract: false < true.

Wrapped as interface or pointer are all accepted by compare using `CompareAny` method.

> Eg 2: CompareAny(*bool vs bool), CompareAny(bool, *any(*bool)).

Panic if the final value detected is not a boolean.

var NumericComparer IComparer[any] = NewNumericComparer()

NumericComparer implements IComparer[any], this compares the real value of 2 input numeric, no matter it is int, float, complex or even types which defined on top of numeric like time.Duration (int64)

> Eg 1: CompareTyped(int8(1), float64(1.1)) will results -1.

_______________

Wrapped as interface or pointer are all accepted by compare using `CompareAny` method.

> Eg 2: CompareAny(*int8(1), float64(1.1)), CompareAny(int8(1), *any(*float64(1.1))) all results -1.

Panic if the final value detected is not numeric.

var StringComparer IComparer[string] = NewStringComparer()

StringComparer implements IComparer[string], this compares the real value of 2 strings.

Wrapped as interface or pointer are all accepted by compare using `CompareAny` method.

> Eg 2: CompareAny(*string vs string), CompareAny(string, *any(*string)).

Panic if the final value detected is not a string.

var TimeComparer IComparer[time.Time] = NewTimeComparer()

TimeComparer implements IComparer[time.Time], this compares the real value of 2 time.Time struct.

If x before y, returns -1. If x after y, returns 1. Otherwise, returns 0.

_______________

Wrapped as interface or pointer are all accepted by compare using `CompareAny` method.

> Eg 2: CompareAny(*Time vs Time), CompareAny(Time, *any(*Time)).

Panic if the final value detected is not time.Time struct.

func ConvertFromComparerIntoDefaultComparer

func ConvertFromComparerIntoDefaultComparer[T any](comparer IComparer[T]) IComparer[any]

ConvertFromComparerIntoDefaultComparer converts any comparer into a default comparer IComparer[any] that both CompareTyped and CompareAny receive any params for convenient purpose, still doing type check when calling CompareTyped as usual.

func GetDefaultComparer

func GetDefaultComparer[T any]() IComparer[any]

func NewBigFloatComparer

func NewBigFloatComparer() IComparer[*big.Float]

func NewBigIntComparer

func NewBigIntComparer() IComparer[*big.Int]

func NewBoolComparer

func NewBoolComparer() IComparer[bool]

func NewNumericComparer

func NewNumericComparer() IComparer[any]

func NewStringComparer

func NewStringComparer() IComparer[string]

func NewTimeComparer

func NewTimeComparer() IComparer[time.Time]

func TryGetDefaultComparer

func TryGetDefaultComparer[T any]() (comparer IComparer[any], ok bool)

func TryGetDefaultComparerFromValue

func TryGetDefaultComparerFromValue(sampleValue any) (comparer IComparer[any], ok bool)

Jump to

Keyboard shortcuts

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