-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers_test.go
135 lines (114 loc) · 2.61 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package store4_test
import (
"fmt"
"sort"
)
// quadSlice implements sort.Interface for []*Quad
type quadSlice []*Quad
func (q quadSlice) Len() int { return len(q) }
func (q quadSlice) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
func (q quadSlice) Less(i, j int) bool {
qi, qj := q[i], q[j]
// Graph.
gi, gj := qi.G, qj.G
if gi < gj {
return true
}
if gi > gj {
return false
}
// Subject.
si, sj := qi.S, qj.S
if si < sj {
return true
}
if si > sj {
return false
}
// Predicate.
pi, pj := qi.P, qj.P
if pi < pj {
return true
}
if pi > pj {
return false
}
// Object.
oi, oj := qi.O, qj.O
return fmt.Sprint(oi) < fmt.Sprint(oj)
}
// SortQuads sorts a slice of quads,
// by graph then subject then predicate then object.
func SortQuads(slice []*Quad) {
sort.Sort(quadSlice(slice))
}
// tripleSlice implements sort.Interface for []*Triple
// ordering by fields SPO.
type tripleSlice []*Triple
func (t tripleSlice) Len() int { return len(t) }
func (t tripleSlice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t tripleSlice) Less(i, j int) bool {
ti, tj := t[i], t[j]
// Subject.
si, sj := ti.S, tj.S
if si < sj {
return true
}
if si > sj {
return false
}
// Predicate.
pi, pj := ti.P, tj.P
if pi < pj {
return true
}
if pi > pj {
return false
}
// Object.
oi, oj := ti.O, tj.O
return fmt.Sprint(oi) < fmt.Sprint(oj)
}
// SortTriples sorts a slice of subject-predicate-object triples,
// by subject then predicate then object.
func SortTriples(slice []*Triple) {
sort.Sort(tripleSlice(slice))
}
// tupleSlice implements sort.Interface for []*Tuple
// ordering by fields PO.
type tupleSlice []*Tuple
func (t tupleSlice) Len() int { return len(t) }
func (t tupleSlice) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t tupleSlice) Less(i, j int) bool {
ti, tj := t[i], t[j]
// Predicate.
si, sj := ti.P, tj.P
if si < sj {
return true
}
if si > sj {
return false
}
// Object.
oi, oj := ti.O, tj.O
return fmt.Sprint(oi) < fmt.Sprint(oj)
}
// SortTuples sorts a slice of predicate-object tuples,
// by predicate then object.
func SortTuples(slice []*Tuple) {
sort.Sort(tupleSlice(slice))
}
// objectSlice implements sort.Interface for []interface{}
// ordering by fields by fmt.Sprintf value.
type objectSlice []interface{}
func (o objectSlice) Len() int { return len(o) }
func (o objectSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o objectSlice) Less(i, j int) bool {
// Object.
return fmt.Sprint(o[i]) < fmt.Sprint(o[j])
}
// sortObjects sorts a slice of object values,
// by fmt.Sprintf value.
func SortObjects(slice []interface{}) {
sort.Sort(objectSlice(slice))
}