forked from parsyl/parquet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repetition.go
121 lines (104 loc) · 2.07 KB
/
repetition.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
package fields
type RepetitionType int
const (
Unseen RepetitionType = -1
Required RepetitionType = 0
Optional RepetitionType = 1
Repeated RepetitionType = 2
)
type RepetitionTypes []RepetitionType
// Def returns the repetition type for the definition level
func (r RepetitionTypes) Def(def int) RepetitionType {
var out RepetitionType
var count int
for _, rt := range r {
if rt == Optional || rt == Repeated {
count++
}
if count == def {
out = rt
}
}
return out
}
// MaxDef returns the largest definition level
func (r RepetitionTypes) MaxDef() uint8 {
var out uint8
for _, rt := range r {
if rt == Optional || rt == Repeated {
out++
}
}
return out
}
// MaxDef returns the largest repetition level
func (r RepetitionTypes) MaxRep() uint8 {
var out uint8
for _, rt := range r {
if rt == Repeated {
out++
}
}
return out
}
// Repeated figures out if there is a repeated field
func (r RepetitionTypes) Repeated() bool {
for _, rt := range r {
if rt == Repeated {
return true
}
}
return false
}
// Optional figures out if there is an optional field
func (r RepetitionTypes) Optional() bool {
for _, rt := range r {
if rt == Optional {
return true
}
}
return false
}
// Required figures out if there are no optional or repeated fields
func (r RepetitionTypes) Required() bool {
for _, rt := range r {
if rt != Required {
return false
}
}
return true
}
// NRepeated figures out if the sub-field at position i
// is repeated.
func (r RepetitionTypes) NRepeated(i int) bool {
var count int
for _, rt := range r {
if rt == Repeated {
count++
}
if count == i {
return true
}
}
return false
}
func reverse(in []field) []field {
flds := append(in[:0:0], in...)
for i := len(flds)/2 - 1; i >= 0; i-- {
opp := len(flds) - 1 - i
flds[i], flds[opp] = flds[opp], flds[i]
}
return flds
}
type rts []RepetitionType
func (r rts) add(i int, rts []RepetitionType) rts {
if len(r) < i+1 {
r = append(r, make([]RepetitionType, len(r)-i+1)...)
}
for _, rt := range rts {
if rt > r[i] {
r[i] = rt
}
}
return r
}