Sadness about data structures

This commit is contained in:
Ian Gulliver
2023-12-30 15:51:29 -07:00
parent 855b15ad29
commit ac2dfbb5d1
3 changed files with 25 additions and 21 deletions

View File

@@ -18,8 +18,11 @@ func main() {
return len(sample)
}))
log.Printf("default=%d", totalLength(heap.NewHeap(), samples))
log.Printf("chat=%d", totalLength(seeds.ChatHeap(), samples))
def := heap.NewHeap()
log.Printf("def=%d [%s]", totalLength(def, samples), def)
chat := seeds.ChatHeap()
log.Printf("chat=%d [%s]", totalLength(chat, samples), chat)
opt := optimize(heap.NewHeap(), samples)
log.Printf("opt=%d [%s]", totalLength(opt, samples), opt)
@@ -47,10 +50,10 @@ func optimize2(baseHeap *heap.Heap, samples [][]byte) *heap.Heap {
ch := make(chan sampleResult, 100)
for i := 0; i < 256; i++ {
i := i
s := byte(i)
go func () {
h := baseHeap.Clone()
h.IncrementSymbol(byte(i))
h.IncrementSymbol(s)
ch <- sampleResult{
heap: h,
score: totalLength(h, samples),

View File

@@ -45,7 +45,7 @@ func (h *Heap) IncrementSymbol(symbol byte) int {
for iterIndex != 0 {
parentIndex := h.parentIndex(iterIndex)
if h.nodes[iterIndex].count <= h.nodes[parentIndex].count {
if h.nodes[iterIndex].count < h.nodes[parentIndex].count || (h.nodes[iterIndex].count == h.nodes[parentIndex].count && h.nodes[iterIndex].symbol > h.nodes[parentIndex].symbol) {
break
}
@@ -69,7 +69,8 @@ func (h Heap) String() string {
nodes = append(nodes, node)
}
slices.SortStableFunc(nodes, func(a, b node) int { return b.count - a.count })
slices.SortStableFunc(nodes, func(a, b node) int { return int(a.symbol) - int(b.symbol) })
slices.SortStableFunc(nodes, func(a, b node) int { return a.count - b.count })
strs := []string{}

View File

@@ -5,21 +5,21 @@ import (
)
var chatHeap = newHeapFromSeed([][]byte{
[]byte(`]\_}`),
[]byte(`[ê%=Z`),
[]byte(`#ÄQ<>`),
[]byte(`&X@+*`),
[]byte(`$~"V;`),
[]byte(`/78q9`),
[]byte("zRE54F(U-6\n"),
[]byte(`NLx:C01D2BJ)K3GP`),
[]byte(`STWH!OYAjM`),
[]byte(`?,'`),
[]byte(`bIv`),
[]byte(`mygwc.pfk`),
[]byte(`isrhlud`),
[]byte(`eotan`),
[]byte(` `),
/* 01 */ []byte("\x07'(,-8?ACDFHJLMNPRSTUWYbcfgjkpxzê"),
/* 02 */ []byte("\n.dvw"),
/* 03 */ []byte("Ihlmor"),
/* 04 */ []byte("nu"),
/* 05 */ []byte("ey"),
/* 06 */ []byte("i"),
/* 07 */ []byte("s"),
/* 08 */ []byte(""),
/* 09 */ []byte(""),
/* 10 */ []byte(""),
/* 11 */ []byte("at"),
/* 12 */ []byte(""),
/* 13 */ []byte(""),
/* 14 */ []byte(""),
/* 15 */ []byte(" "),
})
func ChatHeap() *heap.Heap {