forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
102 lines (91 loc) · 1.69 KB
/
order.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
package parquet
import (
"bytes"
"github.com/parquet-go/parquet-go/internal/unsafecast"
)
func orderOfBool(data []bool) int {
switch len(data) {
case 0, 1:
return 0
default:
k := 0
i := 0
if data[0] { // true => false: descending
k = -1
i = streakOfTrue(data)
if i == len(data) {
k = +1
} else {
i += streakOfFalse(data[i:])
}
} else { // false => true: ascending
k = +1
i = streakOfFalse(data)
i += streakOfTrue(data[i:])
}
if i != len(data) {
k = 0
}
return k
}
}
func streakOfTrue(data []bool) int {
if i := bytes.IndexByte(unsafecast.Slice[byte](data), 0); i >= 0 {
return i
}
return len(data)
}
func streakOfFalse(data []bool) int {
if i := bytes.IndexByte(unsafecast.Slice[byte](data), 1); i >= 0 {
return i
}
return len(data)
}
func orderOfBytes(data [][]byte) int {
switch len(data) {
case 0, 1:
return 0
}
data = skipBytesStreak(data)
if len(data) < 2 {
return 1
}
ordering := bytes.Compare(data[0], data[1])
switch {
case ordering < 0:
if bytesAreInAscendingOrder(data[1:]) {
return +1
}
case ordering > 0:
if bytesAreInDescendingOrder(data[1:]) {
return -1
}
}
return 0
}
func skipBytesStreak(data [][]byte) [][]byte {
for i := 1; i < len(data); i++ {
if !bytes.Equal(data[i], data[0]) {
return data[i-1:]
}
}
return data[len(data)-1:]
}
func bytesAreInAscendingOrder(data [][]byte) bool {
for i := len(data) - 1; i > 0; i-- {
k := bytes.Compare(data[i-1], data[i])
if k > 0 {
return false
}
}
return true
}
func bytesAreInDescendingOrder(data [][]byte) bool {
for i := len(data) - 1; i > 0; i-- {
k := bytes.Compare(data[i-1], data[i])
if k < 0 {
return false
}
}
return true
}