Improved documentation

This commit is contained in:
Eduard Urbach 2025-01-28 13:43:38 +01:00
parent 764b7f5c29
commit b8dab29c54
Signed by: eduard
GPG key ID: 49226B848C78F6C8
6 changed files with 103 additions and 71 deletions

View file

@ -1,45 +1,4 @@
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
}

38
src/data/Finalize.go Normal file
View file

@ -0,0 +1,38 @@
package data
import (
"bytes"
"sort"
)
// 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
}

6
src/data/Insert.go Normal file
View file

@ -0,0 +1,6 @@
package data
// Insert registers a slice of bytes for the given label.
func (data Data) Insert(label string, raw []byte) {
data[label] = raw
}