]> git.giacomolli.uk - onechat/blobdiff - tree.go
Start the builds in the order of the messages
[onechat] / tree.go
diff --git a/tree.go b/tree.go
index ee3ff6807dbe8c660b8aba4cec90e6eb095c9867..30a40ae04aa1737ac782e10a62bf8ec5bf3b87dd 100644 (file)
--- a/tree.go
+++ b/tree.go
@@ -1,12 +1,14 @@
 package main
 
 import (
 package main
 
 import (
+       "cmp"
        "context"
        "encoding/json"
        "fmt"
        "math/bits"
        "os"
        "path/filepath"
        "context"
        "encoding/json"
        "fmt"
        "math/bits"
        "os"
        "path/filepath"
+       "slices"
        "time"
        "unicode/utf8"
 )
        "time"
        "unicode/utf8"
 )
@@ -147,6 +149,20 @@ func (t *Tree) Node(level, index int) (Node, bool) {
        return node, built
 }
 
        return node, built
 }
 
+// Nodes returns the built nodes, ordered by level and then by index.
+func (t *Tree) Nodes() []Node {
+       nodes := make([]Node, 0, len(t.nodes))
+       for _, node := range t.nodes {
+               nodes = append(nodes, node)
+       }
+
+       slices.SortFunc(nodes, func(a, b Node) int {
+               return cmp.Or(cmp.Compare(a.Level, b.Level), cmp.Compare(a.Index, b.Index))
+       })
+
+       return nodes
+}
+
 // leafSource returns the text that the leaf of message holds or summarizes.
 func leafSource(message Message) string {
        return message.Kind + ": " + message.Text
 // leafSource returns the text that the leaf of message holds or summarizes.
 func leafSource(message Message) string {
        return message.Kind + ": " + message.Text