本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
4.8KB

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