github.com/alexandrestein/gods@v1.0.1/utils/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 utils 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 // negative , if a < b 14 // zero , if a == b 15 // positive , if a > b 16 type Comparator func(a, b interface{}) int 17 18 // ComparatorType permits to know what is the type of the comparator 19 type ComparatorType int 20 21 // ComparatorType 22 const ( 23 StringComparatorType ComparatorType = iota 24 IntComparatorType 25 Int8ComparatorType 26 Int16ComparatorType 27 Int32ComparatorType 28 Int64ComparatorType 29 UIntComparatorType 30 UInt8ComparatorType 31 UInt16ComparatorType 32 UInt32ComparatorType 33 UInt64ComparatorType 34 Float32ComparatorType 35 Float64ComparatorType 36 ByteComparatorType 37 RuneComparatorType 38 TimeComparatorType 39 ) 40 41 // StringComparator provides a fast comparison on strings 42 func StringComparator(a, b interface{}) int { 43 s1 := a.(string) 44 s2 := b.(string) 45 min := len(s2) 46 if len(s1) < len(s2) { 47 min = len(s1) 48 } 49 diff := 0 50 for i := 0; i < min && diff == 0; i++ { 51 diff = int(s1[i]) - int(s2[i]) 52 } 53 if diff == 0 { 54 diff = len(s1) - len(s2) 55 } 56 if diff < 0 { 57 return -1 58 } 59 if diff > 0 { 60 return 1 61 } 62 return 0 63 } 64 65 // IntComparator provides a basic comparison on int 66 func IntComparator(a, b interface{}) int { 67 aAsserted := a.(int) 68 bAsserted := b.(int) 69 switch { 70 case aAsserted > bAsserted: 71 return 1 72 case aAsserted < bAsserted: 73 return -1 74 default: 75 return 0 76 } 77 } 78 79 // Int8Comparator provides a basic comparison on int8 80 func Int8Comparator(a, b interface{}) int { 81 aAsserted := a.(int8) 82 bAsserted := b.(int8) 83 switch { 84 case aAsserted > bAsserted: 85 return 1 86 case aAsserted < bAsserted: 87 return -1 88 default: 89 return 0 90 } 91 } 92 93 // Int16Comparator provides a basic comparison on int16 94 func Int16Comparator(a, b interface{}) int { 95 aAsserted := a.(int16) 96 bAsserted := b.(int16) 97 switch { 98 case aAsserted > bAsserted: 99 return 1 100 case aAsserted < bAsserted: 101 return -1 102 default: 103 return 0 104 } 105 } 106 107 // Int32Comparator provides a basic comparison on int32 108 func Int32Comparator(a, b interface{}) int { 109 aAsserted := a.(int32) 110 bAsserted := b.(int32) 111 switch { 112 case aAsserted > bAsserted: 113 return 1 114 case aAsserted < bAsserted: 115 return -1 116 default: 117 return 0 118 } 119 } 120 121 // Int64Comparator provides a basic comparison on int64 122 func Int64Comparator(a, b interface{}) int { 123 aAsserted := a.(int64) 124 bAsserted := b.(int64) 125 switch { 126 case aAsserted > bAsserted: 127 return 1 128 case aAsserted < bAsserted: 129 return -1 130 default: 131 return 0 132 } 133 } 134 135 // UIntComparator provides a basic comparison on uint 136 func UIntComparator(a, b interface{}) int { 137 aAsserted := a.(uint) 138 bAsserted := b.(uint) 139 switch { 140 case aAsserted > bAsserted: 141 return 1 142 case aAsserted < bAsserted: 143 return -1 144 default: 145 return 0 146 } 147 } 148 149 // UInt8Comparator provides a basic comparison on uint8 150 func UInt8Comparator(a, b interface{}) int { 151 aAsserted := a.(uint8) 152 bAsserted := b.(uint8) 153 switch { 154 case aAsserted > bAsserted: 155 return 1 156 case aAsserted < bAsserted: 157 return -1 158 default: 159 return 0 160 } 161 } 162 163 // UInt16Comparator provides a basic comparison on uint16 164 func UInt16Comparator(a, b interface{}) int { 165 aAsserted := a.(uint16) 166 bAsserted := b.(uint16) 167 switch { 168 case aAsserted > bAsserted: 169 return 1 170 case aAsserted < bAsserted: 171 return -1 172 default: 173 return 0 174 } 175 } 176 177 // UInt32Comparator provides a basic comparison on uint32 178 func UInt32Comparator(a, b interface{}) int { 179 aAsserted := a.(uint32) 180 bAsserted := b.(uint32) 181 switch { 182 case aAsserted > bAsserted: 183 return 1 184 case aAsserted < bAsserted: 185 return -1 186 default: 187 return 0 188 } 189 } 190 191 // UInt64Comparator provides a basic comparison on uint64 192 func UInt64Comparator(a, b interface{}) int { 193 aAsserted := a.(uint64) 194 bAsserted := b.(uint64) 195 switch { 196 case aAsserted > bAsserted: 197 return 1 198 case aAsserted < bAsserted: 199 return -1 200 default: 201 return 0 202 } 203 } 204 205 // Float32Comparator provides a basic comparison on float32 206 func Float32Comparator(a, b interface{}) int { 207 aAsserted := a.(float32) 208 bAsserted := b.(float32) 209 switch { 210 case aAsserted > bAsserted: 211 return 1 212 case aAsserted < bAsserted: 213 return -1 214 default: 215 return 0 216 } 217 } 218 219 // Float64Comparator provides a basic comparison on float64 220 func Float64Comparator(a, b interface{}) int { 221 aAsserted := a.(float64) 222 bAsserted := b.(float64) 223 switch { 224 case aAsserted > bAsserted: 225 return 1 226 case aAsserted < bAsserted: 227 return -1 228 default: 229 return 0 230 } 231 } 232 233 // ByteComparator provides a basic comparison on byte 234 func ByteComparator(a, b interface{}) int { 235 aAsserted := a.(byte) 236 bAsserted := b.(byte) 237 switch { 238 case aAsserted > bAsserted: 239 return 1 240 case aAsserted < bAsserted: 241 return -1 242 default: 243 return 0 244 } 245 } 246 247 // RuneComparator provides a basic comparison on rune 248 func RuneComparator(a, b interface{}) int { 249 aAsserted := a.(rune) 250 bAsserted := b.(rune) 251 switch { 252 case aAsserted > bAsserted: 253 return 1 254 case aAsserted < bAsserted: 255 return -1 256 default: 257 return 0 258 } 259 } 260 261 // TimeComparator provides a basic comparison on time.Time 262 func TimeComparator(a, b interface{}) int { 263 aAsserted := a.(time.Time) 264 bAsserted := b.(time.Time) 265 266 switch { 267 case aAsserted.After(bAsserted): 268 return 1 269 case aAsserted.Before(bAsserted): 270 return -1 271 default: 272 return 0 273 } 274 }