Simplified file structure
This commit is contained in:
parent
cacee7260a
commit
a466281307
219 changed files with 453 additions and 457 deletions
45
src/data/Data.go
Normal file
45
src/data/Data.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Data saves slices of bytes referenced by labels.
|
||||
type Data map[string][]byte
|
||||
|
||||
// Finalize returns the final raw data slice and a map of labels with their respective indices.
|
||||
// It will try to reuse existing data whenever possible.
|
||||
func (data Data) Finalize() ([]byte, map[string]int32) {
|
||||
var (
|
||||
final []byte
|
||||
keys = make([]string, 0, len(data))
|
||||
positions = make(map[string]int32, len(data))
|
||||
)
|
||||
|
||||
for key := range data {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.SliceStable(keys, func(i, j int) bool {
|
||||
return len(data[keys[i]]) > len(data[keys[j]])
|
||||
})
|
||||
|
||||
for _, key := range keys {
|
||||
raw := data[key]
|
||||
position := bytes.Index(final, raw)
|
||||
|
||||
if position != -1 {
|
||||
positions[key] = int32(position)
|
||||
} else {
|
||||
positions[key] = int32(len(final))
|
||||
final = append(final, raw...)
|
||||
}
|
||||
}
|
||||
|
||||
return final, positions
|
||||
}
|
||||
|
||||
func (data Data) Insert(label string, raw []byte) {
|
||||
data[label] = raw
|
||||
}
|
28
src/data/Data_test.go
Normal file
28
src/data/Data_test.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package data_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/data"
|
||||
"git.akyoto.dev/go/assert"
|
||||
)
|
||||
|
||||
func TestInterning(t *testing.T) {
|
||||
d := data.Data{}
|
||||
d.Insert("label1", []byte("Hello"))
|
||||
d.Insert("label2", []byte("ello"))
|
||||
raw, positions := d.Finalize()
|
||||
assert.DeepEqual(t, raw, []byte("Hello"))
|
||||
assert.Equal(t, positions["label1"], 0)
|
||||
assert.Equal(t, positions["label2"], 1)
|
||||
}
|
||||
|
||||
func TestInterningReverse(t *testing.T) {
|
||||
d := data.Data{}
|
||||
d.Insert("label1", []byte("ello"))
|
||||
d.Insert("label2", []byte("Hello"))
|
||||
raw, positions := d.Finalize()
|
||||
assert.DeepEqual(t, raw, []byte("Hello"))
|
||||
assert.Equal(t, positions["label1"], 1)
|
||||
assert.Equal(t, positions["label2"], 0)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue