-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtypes.go
423 lines (384 loc) · 12.4 KB
/
types.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package ceftools
import (
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
type Attribute struct {
Name string
Values []string
}
type Header struct {
Name string
Value string
}
const (
Transposed = 1 << iota
)
const (
MajorVersion = 0
MinorVersion = 1
MagicCEB = 0x09424543
MagicCEF = 0x09464543
)
type Cef struct {
Rows int
Columns int
Headers []Header
Flags int
RowAttributes []Attribute
ColumnAttributes []Attribute
Matrix []float32
}
func (cef *Cef) Get(row int, col int) float32 {
return cef.Matrix[col+row*cef.Columns]
}
func (cef *Cef) Set(row int, col int, val float32) {
cef.Matrix[col+row*cef.Columns] = val
}
func (cef *Cef) GetRow(row int) []float32 {
return cef.Matrix[row*cef.Columns : (row+1)*cef.Columns]
}
func (cef *Cef) GetMatrix() *Matrix {
temp := new(Matrix)
temp.Rows = cef.Rows
temp.Columns = cef.Columns
temp.Matrix = cef.Matrix
return temp
}
// Support the Permutable2D interface
func (cef *Cef) SwapRows(i, j int) {
// Swap entries in all the row attributes
for ix := 0; ix < len(cef.RowAttributes); ix++ {
temp := cef.RowAttributes[ix].Values[i]
cef.RowAttributes[ix].Values[i] = cef.RowAttributes[ix].Values[j]
cef.RowAttributes[ix].Values[j] = temp
}
// Swap rows in the main matrix
for ix := 0; ix < cef.Columns; ix++ {
temp := cef.Get(i, ix)
cef.Set(i, ix, cef.Get(j, ix))
cef.Set(j, ix, temp)
}
}
// Support the Permutable2D interface
func (cef *Cef) SwapColumns(i, j int) {
// Swap entries in all the column attributes
for ix := 0; ix < len(cef.ColumnAttributes); ix++ {
temp := cef.ColumnAttributes[ix].Values[i]
cef.ColumnAttributes[ix].Values[i] = cef.ColumnAttributes[ix].Values[j]
cef.ColumnAttributes[ix].Values[j] = temp
}
return
// Swap columns in the main matrix
for ix := 0; ix < cef.Rows; ix++ {
temp := cef.Get(ix, i)
cef.Set(ix, i, cef.Get(ix, j))
cef.Set(ix, j, temp)
}
}
type stringRec struct {
value string
index int
}
type indexedStrings []stringRec
func (a indexedStrings) Len() int { return len(a) }
func (a indexedStrings) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a indexedStrings) Less(i, j int) bool { return a[i].value < a[j].value }
func (cef Cef) SortByRowAttribute(attr string, reverse bool) (*Cef, error) {
// Find the indexes
var index []string
for i := 0; i < len(cef.RowAttributes); i++ {
if cef.RowAttributes[i].Name == attr {
index = cef.RowAttributes[i].Values
}
}
if index == nil {
return nil, errors.New("Attribute not found when attempting to sort: " + attr)
}
// Collect the values to be sorted
recs := make([]stringRec, len(index))
for i := 0; i < len(index); i++ {
recs[i] = stringRec{index[i], i}
}
// Sort them
sort.Sort(indexedStrings(recs))
// Make the resulting Cef
result := new(Cef)
result.Columns = cef.Columns
result.Rows = cef.Rows
result.Headers = cef.Headers
result.Flags = cef.Flags
result.Matrix = make([]float32, 0)
result.ColumnAttributes = cef.ColumnAttributes
result.RowAttributes = make([]Attribute, len(cef.RowAttributes))
for i := 0; i < len(cef.RowAttributes); i++ {
result.RowAttributes[i].Name = cef.RowAttributes[i].Name
result.RowAttributes[i].Values = make([]string, len(result.RowAttributes[i].Values))
}
if reverse {
for i := cef.Rows - 1; i >= 0; i-- {
from := recs[i].index
result.Matrix = append(result.Matrix, cef.GetRow(from)...)
for j := 0; j < len(cef.RowAttributes); j++ {
result.RowAttributes[j].Values = append(result.RowAttributes[j].Values, cef.RowAttributes[j].Values[from])
}
}
} else {
for i := 0; i < cef.Rows; i++ {
from := recs[i].index
result.Matrix = append(result.Matrix, cef.GetRow(from)...)
for j := 0; j < len(cef.RowAttributes); j++ {
result.RowAttributes[j].Values = append(result.RowAttributes[j].Values, cef.RowAttributes[j].Values[from])
}
}
}
return result, nil
}
type numberRec struct {
value float32
index int
}
type indexedNumbers []numberRec
func (a indexedNumbers) Len() int { return len(a) }
func (a indexedNumbers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a indexedNumbers) Less(i, j int) bool { return a[i].value < a[j].value }
func (cef Cef) SortNumerical(by string, reverse bool) (*Cef, error) {
recs := make([]numberRec, cef.Rows)
if by[0] == '#' {
index, err := strconv.Atoi(by[1:])
if err != nil {
return nil, err
}
if index > cef.Columns {
return nil, errors.New("Column index out of range when attempting to sort: " + by[1:])
}
// Make the list of values
for i := 0; i < cef.Rows; i++ {
recs[i] = numberRec{cef.Get(index-1, i), i}
}
} else {
temp := strings.Split(by, "=")
if len(temp) == 2 {
// Find the column attribute
colAttr := -1
for i := 0; i < len(cef.ColumnAttributes); i++ {
if cef.ColumnAttributes[i].Name == temp[0] {
colAttr = i
break
}
}
if colAttr == -1 {
return nil, errors.New("Column attribute not found when attempting to sort: " + temp[0])
}
// Find the column that matches the value
col := -1
for i := 0; i < cef.Columns; i++ {
if cef.ColumnAttributes[colAttr].Values[i] == temp[1] {
col = i
break
}
}
if col == -1 {
return nil, errors.New("Column attribute value not found when attempting to sort: " + temp[1])
}
// Make the list of values
for i := 0; i < cef.Rows; i++ {
recs[i] = numberRec{cef.Get(col, i), i}
}
} else {
// Find the indexes
var index []string
for i := 0; i < len(cef.RowAttributes); i++ {
if cef.RowAttributes[i].Name == by {
index = cef.RowAttributes[i].Values
}
}
if index == nil {
return nil, errors.New("Attribute not found when attempting to sort: " + by)
}
// Collect the values to be sorted
for i := 0; i < len(index); i++ {
value, err := strconv.ParseFloat(index[i], 32)
if err != nil {
return nil, err
}
recs[i] = numberRec{float32(value), i}
}
}
}
// Sort them
sort.Sort(indexedNumbers(recs))
// Make the resulting Cef
result := new(Cef)
result.Columns = cef.Columns
result.Rows = cef.Rows
result.Headers = cef.Headers
result.Flags = cef.Flags
result.Matrix = make([]float32, 0)
result.ColumnAttributes = cef.ColumnAttributes
result.RowAttributes = make([]Attribute, len(cef.RowAttributes))
for i := 0; i < len(cef.RowAttributes); i++ {
result.RowAttributes[i].Name = cef.RowAttributes[i].Name
result.RowAttributes[i].Values = make([]string, len(result.RowAttributes[i].Values))
}
if reverse {
for i := cef.Rows - 1; i >= 0; i-- {
from := recs[i].index
result.Matrix = append(result.Matrix, cef.GetRow(from)...)
for j := 0; j < len(cef.RowAttributes); j++ {
result.RowAttributes[j].Values = append(result.RowAttributes[j].Values, cef.RowAttributes[j].Values[from])
}
}
} else {
for i := 0; i < cef.Rows; i++ {
from := recs[i].index
result.Matrix = append(result.Matrix, cef.GetRow(from)...)
for j := 0; j < len(cef.RowAttributes); j++ {
result.RowAttributes[j].Values = append(result.RowAttributes[j].Values, cef.RowAttributes[j].Values[from])
}
}
}
return result, nil
}
// Join performs a database-style join of two Cef instances, by
// lining up rows that have the same value for the given attributes.
// The 'mode' parameter determines the type of join performed: left join (mode "left"),
// right join (mode "right") or inner join (mode "inner")
func (left *Cef) Join(right *Cef, leftAttr string, rightAttr string) (*Cef, error) {
// Find the indexes
var leftIndex []string
for i := 0; i < len(left.RowAttributes); i++ {
if left.RowAttributes[i].Name == leftAttr {
leftIndex = left.RowAttributes[i].Values
}
}
var rightIndex []string
for i := 0; i < len(right.RowAttributes); i++ {
if right.RowAttributes[i].Name == rightAttr {
rightIndex = right.RowAttributes[i].Values
}
}
if rightIndex == nil || leftIndex == nil {
return nil, errors.New("Index not found when attempting to join " + leftAttr + " " + rightAttr)
}
leftKeys := map[string]int{}
// Hash the keys of the left table, pointing to the corresponding row index
for i := 0; i < len(leftIndex); i++ {
if leftKeys[leftIndex[i]] == 0 { // Don't add keys twice, to ensure the join will prefer earlier rows
leftKeys[leftIndex[i]] = i + 1 // Store as index + 1, so that we can distinguish the zero value
}
}
// Prepare the result
result := new(Cef)
result.Columns = left.Columns + right.Columns
result.Headers = left.Headers
result.Flags = left.Flags
result.Matrix = make([]float32, 0)
result.ColumnAttributes = make([]Attribute, len(left.ColumnAttributes)+len(right.ColumnAttributes))
// Make empty column attributes
for i := 0; i < len(left.ColumnAttributes); i++ {
result.ColumnAttributes[i].Name = left.ColumnAttributes[i].Name
result.ColumnAttributes[i].Values = make([]string, 0)
}
for i := 0; i < len(right.ColumnAttributes); i++ {
result.ColumnAttributes[i+len(left.ColumnAttributes)].Name = right.ColumnAttributes[i].Name
result.ColumnAttributes[i+len(left.ColumnAttributes)].Values = make([]string, 0)
}
// Make empty row attributes
result.RowAttributes = make([]Attribute, len(left.RowAttributes)+len(right.RowAttributes))
for i := 0; i < len(left.RowAttributes); i++ {
result.RowAttributes[i].Name = left.RowAttributes[i].Name
result.RowAttributes[i].Values = make([]string, 0)
}
for i := 0; i < len(right.RowAttributes); i++ {
result.RowAttributes[i+len(left.RowAttributes)].Name = right.RowAttributes[i].Name
result.RowAttributes[i+len(left.RowAttributes)].Values = make([]string, 0)
}
// Join the column attributes
for j := 0; j < len(left.ColumnAttributes); j++ {
result.ColumnAttributes[j].Values = append(result.ColumnAttributes[j].Values, left.ColumnAttributes[j].Values...)
result.ColumnAttributes[j].Values = append(result.ColumnAttributes[j].Values, make([]string, right.Columns)...)
}
for j := 0; j < len(right.ColumnAttributes); j++ {
result.ColumnAttributes[j+len(left.ColumnAttributes)].Values = append(result.ColumnAttributes[j+len(left.ColumnAttributes)].Values, make([]string, left.Columns)...)
result.ColumnAttributes[j+len(left.ColumnAttributes)].Values = append(result.ColumnAttributes[j+len(left.ColumnAttributes)].Values, right.ColumnAttributes[j].Values...)
}
// For each row of the right table, look it up in the hash
Rows := 0
for i := 0; i < len(rightIndex); i++ {
ix := leftKeys[rightIndex[i]]
if ix != 0 {
// We have a match; append one row to the result
Rows++
leftKeys[rightIndex[i]] = 0 // Delete the key to prevent future matches (skip if doing right join?)
result.Matrix = append(result.Matrix, left.GetRow(ix-1)...)
result.Matrix = append(result.Matrix, right.GetRow(i)...)
// Append to the row attributes
for j := 0; j < len(left.RowAttributes); j++ {
result.RowAttributes[j].Values = append(result.RowAttributes[j].Values, left.RowAttributes[j].Values[ix-1])
}
for j := 0; j < len(right.RowAttributes); j++ {
result.RowAttributes[j+len(left.RowAttributes)].Values = append(result.RowAttributes[j+len(left.RowAttributes)].Values, right.RowAttributes[j].Values[i])
}
} else {
fmt.Fprintf(os.Stderr, "Dropped %v", rightIndex[i])
}
}
result.Rows = Rows
// Merge duplicate column attributes
temp := make([]Attribute, 0)
for i := 0; i < len(result.ColumnAttributes); i++ {
// Check if this attribute has already been appended
found := false
for j := 0; j < i; j++ {
if result.ColumnAttributes[i].Name == result.ColumnAttributes[j].Name {
found = true
// Merge values
for k := 0; k < len(result.ColumnAttributes[j].Values); k++ {
if result.ColumnAttributes[j].Values[k] == "" {
result.ColumnAttributes[j].Values[k] = result.ColumnAttributes[i].Values[k]
}
}
break
}
}
if !found {
temp = append(temp, result.ColumnAttributes[i])
}
}
result.ColumnAttributes = temp
// Drop duplicate row attributes
temp = make([]Attribute, 0)
for i := 0; i < len(result.RowAttributes); i++ {
// Check if this attribute has already been appended
found := false
for j := 0; j < i; j++ {
if result.RowAttributes[i].Name == result.RowAttributes[j].Name {
found = true
break
}
}
if !found {
temp = append(temp, result.RowAttributes[i])
}
}
result.RowAttributes = temp
// Merge headers
for _, hdr := range right.Headers {
var found bool
for _, existing := range left.Headers {
if existing.Name == hdr.Name && existing.Value == hdr.Value {
found = true
break
}
}
if !found {
result.Headers = append(result.Headers, hdr)
}
}
return result, nil
}