gitee.com/quant1x/gox@v1.21.2/util/internal/comparator.go (about) 1 // Copyright (c) 2015, Emir Pasic. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package internal 6 7 import "time" 8 9 // Comparator will make type assertion (see IntComparator for example), 10 // which will panic if a or b are not of the asserted type. 11 // 12 // Should return a number: 13 // 14 // negative , if a < b 15 // zero , if a == b 16 // positive , if a > b 17 type Comparator func(a, b interface{}) int 18 19 // StringComparator provides a fast comparison on strings 20 func StringComparator(a, b interface{}) int { 21 s1 := a.(string) 22 s2 := b.(string) 23 min := len(s2) 24 if len(s1) < len(s2) { 25 min = len(s1) 26 } 27 diff := 0 28 for i := 0; i < min && diff == 0; i++ { 29 diff = int(s1[i]) - int(s2[i]) 30 } 31 if diff == 0 { 32 diff = len(s1) - len(s2) 33 } 34 if diff < 0 { 35 return -1 36 } 37 if diff > 0 { 38 return 1 39 } 40 return 0 41 } 42 43 // IntComparator provides a basic comparison on int 44 func IntComparator(a, b interface{}) int { 45 aAsserted := a.(int) 46 bAsserted := b.(int) 47 switch { 48 case aAsserted > bAsserted: 49 return 1 50 case aAsserted < bAsserted: 51 return -1 52 default: 53 return 0 54 } 55 } 56 57 // Int8Comparator provides a basic comparison on int8 58 func Int8Comparator(a, b interface{}) int { 59 aAsserted := a.(int8) 60 bAsserted := b.(int8) 61 switch { 62 case aAsserted > bAsserted: 63 return 1 64 case aAsserted < bAsserted: 65 return -1 66 default: 67 return 0 68 } 69 } 70 71 // Int16Comparator provides a basic comparison on int16 72 func Int16Comparator(a, b interface{}) int { 73 aAsserted := a.(int16) 74 bAsserted := b.(int16) 75 switch { 76 case aAsserted > bAsserted: 77 return 1 78 case aAsserted < bAsserted: 79 return -1 80 default: 81 return 0 82 } 83 } 84 85 // Int32Comparator provides a basic comparison on int32 86 func Int32Comparator(a, b interface{}) int { 87 aAsserted := a.(int32) 88 bAsserted := b.(int32) 89 switch { 90 case aAsserted > bAsserted: 91 return 1 92 case aAsserted < bAsserted: 93 return -1 94 default: 95 return 0 96 } 97 } 98 99 // Int64Comparator provides a basic comparison on int64 100 func Int64Comparator(a, b interface{}) int { 101 aAsserted := a.(int64) 102 bAsserted := b.(int64) 103 switch { 104 case aAsserted > bAsserted: 105 return 1 106 case aAsserted < bAsserted: 107 return -1 108 default: 109 return 0 110 } 111 } 112 113 // UIntComparator provides a basic comparison on uint 114 func UIntComparator(a, b interface{}) int { 115 aAsserted := a.(uint) 116 bAsserted := b.(uint) 117 switch { 118 case aAsserted > bAsserted: 119 return 1 120 case aAsserted < bAsserted: 121 return -1 122 default: 123 return 0 124 } 125 } 126 127 // UInt8Comparator provides a basic comparison on uint8 128 func UInt8Comparator(a, b interface{}) int { 129 aAsserted := a.(uint8) 130 bAsserted := b.(uint8) 131 switch { 132 case aAsserted > bAsserted: 133 return 1 134 case aAsserted < bAsserted: 135 return -1 136 default: 137 return 0 138 } 139 } 140 141 // UInt16Comparator provides a basic comparison on uint16 142 func UInt16Comparator(a, b interface{}) int { 143 aAsserted := a.(uint16) 144 bAsserted := b.(uint16) 145 switch { 146 case aAsserted > bAsserted: 147 return 1 148 case aAsserted < bAsserted: 149 return -1 150 default: 151 return 0 152 } 153 } 154 155 // UInt32Comparator provides a basic comparison on uint32 156 func UInt32Comparator(a, b interface{}) int { 157 aAsserted := a.(uint32) 158 bAsserted := b.(uint32) 159 switch { 160 case aAsserted > bAsserted: 161 return 1 162 case aAsserted < bAsserted: 163 return -1 164 default: 165 return 0 166 } 167 } 168 169 // UInt64Comparator provides a basic comparison on uint64 170 func UInt64Comparator(a, b interface{}) int { 171 aAsserted := a.(uint64) 172 bAsserted := b.(uint64) 173 switch { 174 case aAsserted > bAsserted: 175 return 1 176 case aAsserted < bAsserted: 177 return -1 178 default: 179 return 0 180 } 181 } 182 183 // Float32Comparator provides a basic comparison on float32 184 func Float32Comparator(a, b interface{}) int { 185 aAsserted := a.(float32) 186 bAsserted := b.(float32) 187 switch { 188 case aAsserted > bAsserted: 189 return 1 190 case aAsserted < bAsserted: 191 return -1 192 default: 193 return 0 194 } 195 } 196 197 // Float64Comparator provides a basic comparison on float64 198 func Float64Comparator(a, b interface{}) int { 199 aAsserted := a.(float64) 200 bAsserted := b.(float64) 201 switch { 202 case aAsserted > bAsserted: 203 return 1 204 case aAsserted < bAsserted: 205 return -1 206 default: 207 return 0 208 } 209 } 210 211 // ByteComparator provides a basic comparison on byte 212 func ByteComparator(a, b interface{}) int { 213 aAsserted := a.(byte) 214 bAsserted := b.(byte) 215 switch { 216 case aAsserted > bAsserted: 217 return 1 218 case aAsserted < bAsserted: 219 return -1 220 default: 221 return 0 222 } 223 } 224 225 // RuneComparator provides a basic comparison on rune 226 func RuneComparator(a, b interface{}) int { 227 aAsserted := a.(rune) 228 bAsserted := b.(rune) 229 switch { 230 case aAsserted > bAsserted: 231 return 1 232 case aAsserted < bAsserted: 233 return -1 234 default: 235 return 0 236 } 237 } 238 239 // TimeComparator provides a basic comparison on time.Time 240 func TimeComparator(a, b interface{}) int { 241 aAsserted := a.(time.Time) 242 bAsserted := b.(time.Time) 243 244 switch { 245 case aAsserted.After(bAsserted): 246 return 1 247 case aAsserted.Before(bAsserted): 248 return -1 249 default: 250 return 0 251 } 252 }