cmps

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 10 Imported by: 1

README

cmps

通过 tag 比较相同结构体不同实例大小

详见 cmps_test.go

package cmps_test

import (
	"testing"

	"github.com/Drelf2018/cmps"
)

type Scores struct {
	Chinese float64
	Math    float64
	English float64
}

type Student struct {
	Name   string
	ID     int64 `cmps:"810"`
	Male   bool  `cmps:"514"`
	Scores `cmps:"1919;fields:Math,Chinese,English"`
}

func testCmpsOrdered() bool {
	// int
	if cmps.Compare(1, 2) != -1 {
		return false
	}
	if cmps.Compare(1, 1) != 0 {
		return false
	}
	if cmps.Compare(2, 1) != 1 {
		return false
	}
	// float
	if cmps.Compare(1.5, 2.5) != -1 {
		return false
	}
	if cmps.Compare(1.5, 1.5) != 0 {
		return false
	}
	if cmps.Compare(2.5, 1.5) != 1 {
		return false
	}
	// bool
	if cmps.Compare(false, true) != -1 {
		return false
	}
	if cmps.Compare(false, false) != 0 {
		return false
	}
	if cmps.Compare(true, true) != 0 {
		return false
	}
	if cmps.Compare(true, false) != 1 {
		return false
	}
	// string
	if cmps.Compare("1", "2") != -1 {
		return false
	}
	if cmps.Compare("1", "1") != 0 {
		return false
	}
	if cmps.Compare("2", "1") != 1 {
		return false
	}
	return true
}

func testStruct() bool {
	s1 := Student{
		Name: "张三1",
		ID:   2,
		Male: false,
		Scores: Scores{
			Chinese: 90,
			Math:    60,
			English: 80,
		},
	}
	s2 := Student{
		Name: "张三2",
		ID:   2,
		Male: false,
		Scores: Scores{
			Chinese: 90,
			Math:    60,
			English: 80,
		},
	}
	// Order: Male ID Math Chinese English
	if cmps.Compare(s1, s2) != 0 {
		return false
	}
	s2.Male = true
	if cmps.Compare(s1, s2) != -1 {
		return false
	}
	s1.Male = true
	if cmps.Compare(s1, s2) != 0 {
		return false
	}
	s2.Male = false
	if cmps.Compare(s1, s2) != 1 {
		return false
	}
	s1.Male = false
	s1.ID = 1
	if cmps.Compare(s1, s2) != -1 {
		return false
	}
	s1.ID = 3
	if cmps.Compare(s1, s2) != 1 {
		return false
	}
	s1.ID = 2
	s1.Math = 50
	if cmps.Compare(s1, s2) != -1 {
		return false
	}
	s1.Math = 70
	if cmps.Compare(s1, s2) != 1 {
		return false
	}
	s1.Math = 60
	s1.Chinese = 80
	if cmps.Compare(s1, s2) != -1 {
		return false
	}
	s1.Chinese = 100
	if cmps.Compare(s1, s2) != 1 {
		return false
	}
	s1.Chinese = 90
	s1.English = 70
	if cmps.Compare(s1, s2) != -1 {
		return false
	}
	s1.English = 90
	if cmps.Compare(s1, s2) != 1 {
		return false
	}
	return true
}

func TestMain(t *testing.T) {
	i := 1
	for ; i > 0; i-- {
		if !testCmpsOrdered() {
			break
		}
		if !testStruct() {
			break
		}
	}
	if i != 0 {
		t.Fail()
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CmpBool added in v1.7.0

func CmpBool(x, y any) int

func Compare

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

func CompareUnsafe added in v1.7.0

func CompareUnsafe(x, y any) int

func Delete added in v1.5.0

func Delete[S ~[]T, T any](x S, target T) S

func Insert

func Insert[S ~[]T, T any](x S, target T) S

func ParseTag added in v1.7.0

func ParseTag(tag string) (m map[string]string)
func Search[S ~[]T, T any](x S, target T) (int, bool)

func SearchFunc added in v1.5.0

func SearchFunc[F any, S ~[]T, T *F](x S, f func(T)) (int, bool)

func Sort added in v1.7.0

func Sort[S ~[]E, E any](x S)

func StdCompare added in v1.7.0

func StdCompare[T constraints.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.

For floating-point types, 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.

Types

type Slice

type Slice[T any] struct {
	// I stands for Items
	I []T
	// contains filtered or unexported fields
}

func NewSlice added in v1.7.0

func NewSlice[T any](items ...T) *Slice[T]

func (*Slice[T]) Delete added in v1.7.0

func (s *Slice[T]) Delete(t T) bool

func (*Slice[T]) Index added in v1.7.0

func (s *Slice[T]) Index(t T) int

func (*Slice[T]) Insert added in v1.7.0

func (s *Slice[T]) Insert(t T)

func (*Slice[T]) MarshalJSON added in v1.7.0

func (s *Slice[T]) MarshalJSON() ([]byte, error)

func (*Slice[T]) Search added in v1.7.0

func (s *Slice[T]) Search(t T) (zero T)

func (*Slice[T]) Sort added in v1.7.0

func (s *Slice[T]) Sort()

type Tag added in v1.7.0

type Tag struct {
	Reflect.Field
	Cmps   float64 `cmps:"1"`
	Order  int
	Fields Tags
}

func (*Tag) Compare added in v1.7.0

func (t *Tag) Compare(x, y any) int

func (Tag) Insert added in v1.7.0

func (Tag) Insert(field Reflect.Field, ok bool, fields []*Tag, array *[]*Tag)

func (Tag) String added in v1.7.0

func (t Tag) String() string

type Tags added in v1.7.0

type Tags []*Tag

func (Tags) Compare added in v1.7.0

func (t Tags) Compare(x, y any) int

Jump to

Keyboard shortcuts

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