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