sortedset

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareValue

func CompareValue(a, b reflect.Value, fields ...string) int
Example (Case1)
for _, t := range []struct {
	a, b T
}{
	{a1, a2}, {a1, b1}, {a1, b2}, {a2, b1},
	{a1, a1}, {b2, b2},
	{a2, a1}, {b1, a1}, {b1, a2}, {b2, b1},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b), "s", "i"))
}
Output:

-1
-1
-1
-1
0
0
1
1
1
1
Example (Case2)
for _, t := range []struct {
	a, b T
}{
	{a1, a2}, {a1, b1}, {a1, b2}, {b1, a2},
	{a1, a1}, {b2, b2},
	{a2, a1}, {b1, a1}, {a2, b1}, {b2, a1},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b), "i", "s"))
}
Output:

-1
-1
-1
-1
0
0
1
1
1
1
Example (Int)
for _, t := range []struct {
	a, b int
}{
	{7, 8}, {7, 7}, {8, 7},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b)))
}
Output:

-1
0
1
Example (IntField)
for _, t := range []struct {
	a, b T
}{
	{a1, a2}, {a1, b2}, {b1, a2}, {b1, b2},
	{a1, b1}, {a2, b2},
	{a2, a1}, {a2, b1}, {b2, a1}, {b2, b1},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b), "i"))
}
Output:

-1
-1
-1
-1
0
0
1
1
1
1
Example (Nil)
var p1, p2 *int
fmt.Println(CompareValue(reflect.ValueOf(p1), reflect.ValueOf(p2)))
fmt.Println(CompareValue(reflect.ValueOf(p1), reflect.ValueOf(10)))
fmt.Println(CompareValue(reflect.ValueOf(10), reflect.ValueOf(p2)))
var i = 3
p1 = &i
fmt.Println(CompareValue(reflect.ValueOf(p1), reflect.ValueOf(p2)))
fmt.Println(CompareValue(reflect.ValueOf(p2), reflect.ValueOf(p1)))
fmt.Println(CompareValue(reflect.ValueOf(p1), reflect.ValueOf(2)))
fmt.Println(CompareValue(reflect.ValueOf(p1), reflect.ValueOf(3)))
fmt.Println(CompareValue(reflect.ValueOf(p1), reflect.ValueOf(4)))
Output:

0
-1
1
1
-1
1
0
-1
Example (NoFields)
defer func() {
	fmt.Println(recover())
}()
fmt.Println(CompareValue(reflect.ValueOf(struct{}{}), reflect.ValueOf(struct{}{})))
Output:

empty fields for struct value
Example (String)
for _, t := range []struct {
	a, b string
}{
	{"a", "b"}, {"a", "ab"}, {"", "a"},
	{"a", "a"}, {"ab", "ab"}, {"", ""},
	{"b", "a"}, {"ab", "a"}, {"a", ""},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b)))
}
Output:

-1
-1
-1
0
0
0
1
1
1
Example (StringField)
for _, t := range []struct {
	a, b T
}{
	{a1, b1}, {a1, b2}, {a2, b1}, {a2, b2},
	{a1, a2}, {b1, b2},
	{b1, a1}, {b1, a2}, {b2, a1}, {b2, a2},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b), "s"))
}
Output:

-1
-1
-1
-1
0
0
1
1
1
1
Example (Uint)
for _, t := range []struct {
	a, b uint
}{
	{7, 8}, {7, 7}, {8, 7},
} {
	fmt.Println(CompareValue(reflect.ValueOf(t.a), reflect.ValueOf(t.b)))
}
Output:

-1
0
1
Example (UnsupportedType)
defer func() {
	fmt.Println(recover())
}()
fmt.Println(CompareValue(reflect.ValueOf(false), reflect.ValueOf(true)))
Output:

unsupported value: bool(false)

func RemoveInt64

func RemoveInt64(s []int64, target int64) []int64
Example
s := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, v := range []int64{5, 3, 8, 0} {
	s = RemoveInt64(s, v)
}
fmt.Println(s)
fmt.Println(RemoveInt64(nil, 3), RemoveInt64([]int64{}, 3), RemoveInt64([]int64{3}, 3))
Output:

[1 2 4 6 7 9]
[] [] []

func RemoveString

func RemoveString(s []string, target string) []string
Example
s := []string{"a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9"}
for _, v := range []string{"a5", "a3", "a8", "a0"} {
	s = RemoveString(s, v)
}
fmt.Println(s)
fmt.Println(
	RemoveString(nil, "3"), RemoveString([]string{}, "3"), RemoveString([]string{"3"}, "3"),
)
Output:

[a1 a2 a4 a6 a7 a9]
[] [] []

func RemoveValue

func RemoveValue(s, target reflect.Value, fields ...string) reflect.Value
Example (Int64)
s := reflect.ValueOf([]int64{1, 2, 3, 4, 5, 6, 7, 8, 9})
for _, v := range []int64{5, 3, 8, 0} {
	s = RemoveValue(s, reflect.ValueOf(v))
}
fmt.Println(s)
fmt.Println(RemoveValue(reflect.ValueOf([]int64(nil)), reflect.ValueOf(3)))
fmt.Println(RemoveValue(reflect.Value{}, reflect.ValueOf(3)))
Output:

[1 2 4 6 7 9]
[]
<invalid reflect.Value>
Example (IntField)
var s = reflect.ValueOf([]testStruct{
	{1, `name1`}, {2, `name2`}, {3, `name3`}, {4, `name4`}, {5, `name5`},
	{6, `name6`}, {7, `name7`}, {8, `name8`}, {9, `name9`},
})
for _, v := range []testStruct{
	{Id: 4}, {Id: 2}, {Id: 4}, {Id: 7}, {Id: 0},
} {
	s = RemoveValue(s, reflect.ValueOf(v), `Id`)
}
fmt.Println(s)
Output:

[{1 name1} {3 name3} {5 name5} {6 name6} {8 name8} {9 name9}]
Example (StringField)
var s = reflect.ValueOf([]testStruct{
	{1, `name1`}, {2, `name2`}, {3, `name3`}, {4, `name4`}, {5, `name5`},
	{6, `name6`}, {7, `name7`}, {8, `name8`}, {9, `name9`},
})
for _, v := range []testStruct{
	{Name: `name4`}, {Name: `name2`}, {Name: `name4`}, {Name: `name7`}, {Name: `name0`},
} {
	s = RemoveValue(s, reflect.ValueOf(v), `Name`)
}
fmt.Println(s)
Output:

[{1 name1} {3 name3} {5 name5} {6 name6} {8 name8} {9 name9}]

func SaveInt64

func SaveInt64(s []int64, target int64) []int64
Example
var s []int64
for _, v := range []int64{3, 6, 2, 8, 9, 1, 5, 2, 4, 6, 7} {
	s = SaveInt64(s, v)
}
fmt.Println(s)
Output:

[1 2 3 4 5 6 7 8 9]

func SaveString

func SaveString(s []string, target string) []string
Example
var s []string
for _, v := range []string{"3", "6", "2", "8", "9", "1", "5", "2", "4", "6", "7"} {
	s = SaveString(s, v)
}
fmt.Println(s)
Output:

[1 2 3 4 5 6 7 8 9]

func SaveValue

func SaveValue(s, target reflect.Value, fields ...string) reflect.Value
Example (Int64)
var s = reflect.ValueOf([]int64{})
for _, v := range []int64{3, 6, 2, 8, 9, 1, 5, 2, 4, 6, 7} {
	s = SaveValue(s, reflect.ValueOf(v))
}
fmt.Println(s)
Output:

[1 2 3 4 5 6 7 8 9]
Example (IntField)
var s = reflect.ValueOf([]testStruct{})
for _, v := range []testStruct{
	{Id: 3, Name: `name3`}, {Id: 6, Name: `name_6`}, {Id: 2, Name: `name_2`},
	{Id: 5, Name: `name5`}, {Id: 4, Name: `name4`}, {Id: 7, Name: `name7`},
	{Id: 2, Name: `name-2`}, {Id: 9, Name: `name9`}, {Id: 1, Name: `name1`},
	{Id: 2, Name: `name2`}, {Id: 8, Name: `name8`}, {Id: 6, Name: `name6`},
} {
	s = SaveValue(s, reflect.ValueOf(v), `Id`)
}
fmt.Println(s)
Output:

[{1 name1} {2 name2} {3 name3} {4 name4} {5 name5} {6 name6} {7 name7} {8 name8} {9 name9}]
Example (InvalidSlice)
var s = reflect.Value{}
for _, v := range []int64{3, 6, 2, 8, 9, 1, 5, 2, 4, 6, 7} {
	s = SaveValue(s, reflect.ValueOf(v))
}
fmt.Println(s)
Output:

[1 2 3 4 5 6 7 8 9]
Example (String)
var s = reflect.ValueOf([]string{})
for _, v := range []string{"aa", "ba", "ab", "cc", "a", "ab", "b"} {
	s = SaveValue(s, reflect.ValueOf(v))
}
fmt.Println(s)
Output:

[a aa ab b ba cc]
Example (StringField)
var s = reflect.ValueOf([]testStruct{})
for _, v := range []testStruct{
	{Id: 3, Name: `name3`}, {Id: 16, Name: `name6`}, {Id: 12, Name: `name2`},
	{Id: 5, Name: `name5`}, {Id: 4, Name: `name4`}, {Id: 7, Name: `name7`},
	{Id: 22, Name: `name2`}, {Id: 9, Name: `name9`}, {Id: 1, Name: `name1`},
	{Id: 2, Name: `name2`}, {Id: 8, Name: `name8`}, {Id: 6, Name: `name6`},
} {
	s = SaveValue(s, reflect.ValueOf(v), `Name`)
}
fmt.Println(s)
Output:

[{1 name1} {2 name2} {3 name3} {4 name4} {5 name5} {6 name6} {7 name7} {8 name8} {9 name9}]

Types

This section is empty.

Jump to

Keyboard shortcuts

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