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) return len(sample)
})) }))
log.Printf("default=%d", totalLength(heap.NewHeap(), samples)) def := heap.NewHeap()
log.Printf("chat=%d", totalLength(seeds.ChatHeap(), samples)) 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) opt := optimize(heap.NewHeap(), samples)
log.Printf("opt=%d [%s]", totalLength(opt, samples), opt) 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) ch := make(chan sampleResult, 100)
for i := 0; i < 256; i++ { for i := 0; i < 256; i++ {
i := i s := byte(i)
go func () { go func () {
h := baseHeap.Clone() h := baseHeap.Clone()
h.IncrementSymbol(byte(i)) h.IncrementSymbol(s)
ch <- sampleResult{ ch <- sampleResult{
heap: h, heap: h,
score: totalLength(h, samples), score: totalLength(h, samples),

View File

@@ -45,7 +45,7 @@ func (h *Heap) IncrementSymbol(symbol byte) int {
for iterIndex != 0 { for iterIndex != 0 {
parentIndex := h.parentIndex(iterIndex) 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 break
} }
@@ -69,7 +69,8 @@ func (h Heap) String() string {
nodes = append(nodes, node) 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{} strs := []string{}

View File

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