-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriple.go
171 lines (143 loc) · 3.62 KB
/
triple.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package triples // Enkelvoud maken!!!
import "errors"
const (
// NotFound int = -1
Wildcard int = -1 // Kiezen we niet best overal voor int64? Dit loopt slecht af op een 32-bit systeem.
)
// Store is a data structure to compactly store triples. Store implements fast
// search.
type Store struct {
ptrs0 []uint
ptrs1 []uint
vals1 []int
vals2 []int
}
// Triple ...
type Triple struct {
val0, val1, val2 int
}
// New ...
func New() Store {
return Store{}
}
// From constructs a store from a list of triples.
func From(ts []Triple) (Store, error) {
if len(ts) == 0 {
return Store{}, errors.New("trie: number of triples is zero")
}
n := ts[len(ts)-1].val0 + 2
ptrs0 := make([]uint, 0, n)
ptrs1 := make([]uint, 0, len(ts)+1)
vals1 := make([]int, 0, len(ts))
vals2 := make([]int, 0, len(ts))
var ptr0, ptr1 uint
v0, v1 := -1, -1 // Zonder deze sentinels kunnen de values uint/uint64 zijn! Ik kan als sentinel ook math.MaxUint nemen!!!
for _, t := range ts {
if t.val0 != v0 {
v0, v1 = t.val0, t.val1
ptrs0 = append(ptrs0, ptr0)
ptrs1 = append(ptrs1, ptr1)
vals1 = append(vals1, v1)
ptr0++
} else if t.val1 != v1 {
v1 = t.val1
ptrs1 = append(ptrs1, ptr1)
vals1 = append(vals1, v1)
ptr0++
}
vals2 = append(vals2, t.val2)
ptr1++
}
// sentinels
ptrs0 = append(ptrs0, ptr0)
ptrs1 = append(ptrs1, ptr1)
return Store{ptrs0: ptrs0, ptrs1: ptrs1, vals1: vals1, vals2: vals2}, nil
}
// Triples ...
func (s *Store) Triples() []Triple {
ts := make([]Triple, 0, len(s.vals2))
var lo1, lo2 uint
for i := 1; i < len(s.ptrs0); i++ {
hi1 := s.ptrs0[i]
for j := lo1; j < hi1 && j < uint(len(s.vals1)) && j+1 < uint(len(s.ptrs1)); j++ { // Zijn die extra tests sneller???
v1 := s.vals1[j]
hi2 := s.ptrs1[j+1]
for k := lo2; k < hi2 && k < uint(len(s.vals2)); k++ {
ts = append(ts, Triple{val0: i - 1, val1: v1, val2: s.vals2[k]})
}
lo2 = hi2
}
lo1 = hi1
}
return ts
}
// Build ...
func (s *Store) Build() {
}
// Select returns an iterator over a list of triples
// that fullfil the given triple selection pattern.
// func (s *Store) Select(tp Triple) Iter {
// i := tp.val0
// if i < 0 || len(s.ptrs0) <= i+1 {
// return Iter{invalid: true}
// }
// var ok bool
// j := s.ptrs0[i]
// if tp.val1 != Wildcard {
// vals1 := s.vals1[s.ptrs0[i]:s.ptrs0[i+1]]
// if j, ok = find(vals1, tp.val1); !ok {
// return Iter{invalid: true}
// }
// }
// first := Iter(s.ptrs0.iterAt(i), s.ptrs1.iterAt(j))
// if len(s.idxs) <= j {
// return Iter{invalid: true}
// }
// return Iter{i, first, second}
// }
// find ...
func find(arr []int, search int) (uint, bool) {
var lo, hi uint = 0, uint(len(arr)) // Test doen om lineair te scannen indien lengte kleiner dan threshold!
for lo < hi {
m := lo + (hi-lo)>>1
if arr[m] < search {
lo = m + 1
} else {
hi = m
}
}
return lo, true // We moeten nog NotFound toevoegen als bool!
}
// Iter ...
type Iter struct {
store *Store
p0, p1 int // iteration state
v0, p0Lo, p0Hi, p1Lo, p1Hi int // config iterator
}
// func (t *Triples) Iter() Iter {
// return Iter{
// t: t,
// p0: p0l,
// p1: p1l,
// v0: v0,
// // p0Lo: p0l,
// p0Hi: p0h,
// // p1Lo: p1l,
// p1Hi: p1h,
// }
// }
// func (it *Iter) Triples() Triple {
// hi1 := t.ptrs[i+1]
// for j := range t.idxs[it.p0Lo:it.p0Hi] {
// v1 := t.idxs[lo1+j].val
// hi2 := t.idxs[lo1+j+1].ptr
// for _, v2 := range t.vals[lo2:hi2] {
// tps = append(tps, Triple{val0: i, val1: v1, val2: v2})
// }
// lo2 = hi2
// }
// return Triple{val0: it.v0, val1: v1, val2: v2}
// }
func (it *Iter) HasNext() bool {
return true
}