2019-11-21 18:38:00 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tuples, eg [(index, bucket, key, value)]
|
|
|
|
type Tuples struct {
|
|
|
|
Values [][]byte
|
|
|
|
SortBy int
|
|
|
|
Length int
|
|
|
|
arity int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTuples(size, arity, sortBy int) *Tuples {
|
|
|
|
return &Tuples{
|
|
|
|
Values: make([][]byte, 0, size*arity),
|
|
|
|
Length: 0,
|
|
|
|
SortBy: sortBy,
|
|
|
|
arity: arity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tuples) Append(values ...[]byte) error {
|
|
|
|
if len(values) != t.arity {
|
|
|
|
return errors.New("got an incorrect number of values")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Length++
|
|
|
|
|
2021-03-29 03:58:45 +00:00
|
|
|
t.Values = append(t.Values, values...)
|
|
|
|
|
2019-11-21 18:38:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tuples) Len() int {
|
|
|
|
return t.Length
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tuples) Less(i, j int) bool {
|
|
|
|
return bytes.Compare(t.Values[i*t.arity+t.SortBy], t.Values[j*t.arity+t.SortBy]) == -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tuples) Swap(i, j int) {
|
|
|
|
for index := 0; index < t.arity; index++ {
|
|
|
|
t.Values[i*t.arity+index], t.Values[j*t.arity+index] = t.Values[j*t.arity+index], t.Values[i*t.arity+index]
|
|
|
|
}
|
|
|
|
}
|