-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparator.go
More file actions
126 lines (105 loc) · 2.61 KB
/
Copy pathcomparator.go
File metadata and controls
126 lines (105 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015 mint.zhao.chiu@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package container
import (
"reflect"
)
// compareFunc的结果值:
// 小于0: 第一个参数小于第二个参数
// 等于0: 第一个参数等于第二个参数
// 大于1: 第一个参数大于第二个参数
type CompareFunction func(interface{}, interface{}) int8
func ValidElements(elements ...interface{}) bool {
for _, e := range elements {
if !reflect.ValueOf(e).IsValid() {
return false
}
}
return true
}
func Float64CompareFunctionASC(e1, e2 interface{}) int8 {
if !ValidElements(e1, e2) {
return 0
}
k1 := e1.(float64)
k2 := e2.(float64)
if k1 < k2 {
return -1
} else if k1 > k2 {
return 1
} else {
return 0
}
}
func Float64CompareFunctionDESC(e1, e2 interface{}) int8 {
return -Float64CompareFunctionASC(e1, e2)
}
func Uint64CompareFunctionASC(e1, e2 interface{}) int8 {
if !ValidElements(e1, e2) {
return 0
}
k1 := e1.(uint64)
k2 := e2.(uint64)
return Float64CompareFunctionASC(float64(k1), float64(k2))
}
func Uint64CompareFunctionDESC(e1, e2 interface{}) int8 {
return -Uint64CompareFunctionASC(e1, e2)
}
func Int64CompareFunctionASC(e1, e2 interface{}) int8 {
if !ValidElements(e1, e2) {
return 0
}
k1 := e1.(int64)
k2 := e2.(int64)
return Float64CompareFunctionASC(float64(k1), float64(k2))
}
func Int64CompareFunctionDESC(e1, e2 interface{}) int8 {
return -Int64CompareFunctionASC(e1, e2)
}
func IntCompareFunctionASC(e1, e2 interface{}) int8 {
if !ValidElements(e1, e2) {
return 0
}
k1 := e1.(int)
k2 := e2.(int)
return Float64CompareFunctionASC(float64(k1), float64(k2))
}
func IntCompareFunctionDESC(e1, e2 interface{}) int8 {
return -IntCompareFunctionASC(e1, e2)
}
func StringCompareFunction(e1, e2 interface{}) int8 {
if !ValidElements(e1, e2) {
return 0
}
s1 := e1.(string)
s2 := e2.(string)
min := len(s2)
if len(s1) < len(s2) {
min = len(s1)
}
diff := 0
for i := 0; i < min && diff == 0; i++ {
diff = int(s1[i]) - int(s2[i])
}
if diff == 0 {
diff = len(s1) - len(s2)
}
if diff < 0 {
return -1
}
if diff > 0 {
return 1
}
return 0
}