Debian Codegraph @

seq/compare.go

raw symbols SCIP
1 package seq
2
3 import (
4 "fmt"
5 "reflect"
6 "sort"
7 )
8
9 // Compare returns 0 if a and b are equal, -1 if a < b, or 1 if a > b.
10 // Panics if a and b are not of the same type, or are of a type not listed here.
11 // * Bools are compared assuming false < true.
12 // * Strings, integer and float values are compared as Go compares them.
13 // * Two nil pointers are equal; one nil pointer is treated as smaller than a non-nil pointer.
14 // * Non-nil pointers are compared by comparing the values they point to.
15 // * Structures are compared by comparing their fields in order.
16 // * Slices are compared by comparing elements sequentially. If the slices are of different
17 // length and all elements are the same up to the shorter length, the shorter slice is treated as
18 // smaller.
19 // * Maps can only be compared if they have string keys, in which case the ordered list of
20 // keys are first compared as string slices, and if they're equal then the values are compared
21 // sequentially in key order.
22 func Compare(a, b interface{}) int {
23 return compareValue(reflect.ValueOf(a), reflect.ValueOf(b))
24 }
25
26 func boolToInt(b bool) int {
27 if b {
28 return 1
29 }
30 return 0
31 }
32
33 func compareValue(ir1, ir2 reflect.Value) int {
34 var zerovalue reflect.Value
35 r1, r2 := reflect.Indirect(ir1), reflect.Indirect(ir2)
36
37 if r1 == zerovalue {
38 if r2 == zerovalue {
39 return 0
40 }
41 return -1
42 }
43 if r2 == zerovalue {
44 return 1
45 }
46
47 switch r1.Kind() {
48 case reflect.Bool:
49 v1, v2 := boolToInt(r1.Bool()), boolToInt(r2.Bool())
50 if v1 < v2 {
51 return -1
52 }
53 if v1 > v2 {
54 return 1
55 }
56 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
57 v1, v2 := r1.Int(), r2.Int()
58 if v1 < v2 {
59 return -1
60 }
61 if v1 > v2 {
62 return 1
63 }
64 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
65 v1, v2 := r1.Uint(), r2.Uint()
66 if v1 < v2 {
67 return -1
68 }
69 if v1 > v2 {
70 return 1
71 }
72 case reflect.Float32, reflect.Float64:
73 v1, v2 := r1.Float(), r2.Float()
74 if v1 < v2 {
75 return -1
76 }
77 if v1 > v2 {
78 return 1
79 }
80 case reflect.Map:
81 return compareMap(r1, r2)
82 case reflect.Struct:
83 return compareStruct(r1, r2)
84 case reflect.Slice:
85 if r1.Type().Elem().Kind() == reflect.Uint8 {
86 // Not using bytes.Compare because that fails on unexported fields:
87 // return bytes.Compare(r1.Interface().([]byte), r2.Interface().([]byte))
88 var s string
89 strtype := reflect.TypeOf(s)
90 v1, v2 := r1.Convert(strtype).String(), r2.Convert(strtype).String()
91 if v1 < v2 {
92 return -1
93 }
94 if v1 > v2 {
95 return 1
96 }
97 }
98 return compareSlice(r1, r2)
99 case reflect.String:
100 v1, v2 := r1.String(), r2.String()
101 if v1 < v2 {
102 return -1
103 }
104 if v1 > v2 {
105 return 1
106 }
107 default:
108 panic(fmt.Sprintf("don't know how to compare values of type %v", r1.Type()))
109 }
110 return 0
111 }
112
113 func compareStruct(r1, r2 reflect.Value) int {
114 if r1.Type() != r2.Type() {
115 panic(fmt.Sprintf("s1 and s2 are not of the same type: %v, %v", r1.Type(), r2.Type()))
116 }
117
118 n := r1.NumField()
119 for i := 0; i < n; i++ {
120 c := compareValue(r1.Field(i), r2.Field(i))
121 if c != 0 {
122 return c
123 }
124 }
125 return 0
126 }
127
128 func compareSlice(r1, r2 reflect.Value) int {
129 maxlen := r1.Len()
130 if r2.Len() < maxlen {
131 maxlen = r2.Len()
132 }
133 for i := 0; i < maxlen; i++ {
134 c := compareValue(r1.Index(i), r2.Index(i))
135 if c != 0 {
136 return c
137 }
138 }
139 if r1.Len() > maxlen {
140 return 1
141 }
142 if r2.Len() > maxlen {
143 return -1
144 }
145 return 0
146 }
147
148 func sortedKeys(r1 reflect.Value) []string {
149 keys := make([]string, 0, r1.Len())
150 for _, k := range r1.MapKeys() {
151 keys = append(keys, k.String())
152 }
153 sort.Strings(keys)
154 return keys
155 }
156
157 func compareMap(r1, r2 reflect.Value) int {
158 if r1.Type().Key().Kind() != reflect.String {
159 panic("can only compare maps with keys of type string")
160 }
161
162 s1, s2 := sortedKeys(r1), sortedKeys(r2)
163 c := compareSlice(reflect.ValueOf(s1), reflect.ValueOf(s2))
164 if c != 0 {
165 return c
166 }
167
168 for _, k := range s1 {
169 vk := reflect.ValueOf(k)
170 c := compareValue(r1.MapIndex(vk), r2.MapIndex(vk))
171 if c != 0 {
172 return c
173 }
174 }
175 return 0
176 }