| 1 |
// Author: Jim Idle - [email protected] / [email protected] |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package jwz |
| 5 |
|
| 6 |
import "slices" |
| 7 |
|
| 8 |
// ThreadLess specifies the signature of a function that compares two Threadables in some way you define, |
| 9 |
// such as comparing dates in the emails they contain. Note your function should be able to handle Dummy |
| 10 |
// Threadables in some sensible way, such as using the child of the Dummy for the sort parameters. |
| 11 |
// |
| 12 |
// ThreadLess reports whether the Threadable t1 must sort before the Threadable t2. |
| 13 |
// |
| 14 |
// If both ThreadLess(t1, t2) and ThreadLess(t2, t1) are false, then t1 and t2 are considered equal. |
| 15 |
// Sort may place equal elements in any order in the final result. |
| 16 |
// |
| 17 |
// ThreadLess must describe a transitive ordering: |
| 18 |
// |
| 19 |
// - if both ThreadLess(t1, t2) and ThreadLess(t2, t3) are true, then ThreadLess(t1, t3) must be true as well. |
| 20 |
// - if both ThreadLess(t1, t2) and ThreadLess(t2, t3) are false, then ThreadLess(t1, t3) must be false as well. |
| 21 |
type ThreadLess func(t1 Threadable, t2 Threadable) bool |
| 22 |
|
| 23 |
// WalkFunction specifies the signature of a function that can be called by the generic Walk utility function. |
| 24 |
// As well as being passed the current Threadable in the walk, the function will be passed an interface of your |
| 25 |
// choosing, which is passed in to the walk function and then propagated to each call of this function. T |
| 26 |
// |
| 27 |
// The Walk will not interact with the any, just keep it accessible to your walk function, hence you |
| 28 |
// can pass nil if you do not need it. |
| 29 |
// |
| 30 |
// If your searcher function returns true, then the tree walk will end where it is |
| 31 |
type WalkFunction func(t Threadable, u any) (bool, error) |
| 32 |
|
| 33 |
// Count will traverse the supplied Threadable and store the count of Threadables contained within it in the |
| 34 |
// given counter location. |
| 35 |
// |
| 36 |
// Note that any Dummy placeholder nodes are excluded from the count. |
| 37 |
func Count(root Threadable, counter *int) { |
| 38 |
if root == nil { |
| 39 |
return |
| 40 |
} |
| 41 |
|
| 42 |
for node := root; node != nil; node = node.GetNext() { |
| 43 |
|
| 44 |
if c := node.GetChild(); c != nil { |
| 45 |
// Count children of the current one first then |
| 46 |
// |
| 47 |
Count(c, counter) |
| 48 |
} |
| 49 |
|
| 50 |
// Only count this one if it is not a dummy placeholder |
| 51 |
// |
| 52 |
if !node.IsDummy() { |
| 53 |
*counter++ |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Sort will create order from the chaos created by threading a set of emails, that even if given as input |
| 59 |
// in a specific order, will be threaded in whatever order the go data structures happen to spit out - which |
| 60 |
// will usually be a different order each time. |
| 61 |
// |
| 62 |
// Note that Sort will not change the embedded nature of the Threads. As in, while the child of a particular |
| 63 |
// Threadable can, and usually will, be changed, the new child will belong to the set of next Threadables that |
| 64 |
// its original child pointer belonged to. If sorting by date for instance, the set of reply threads/emails |
| 65 |
// will be ordered such that the replies are in date order, which is what you want. However, the set of replies |
| 66 |
// will remain the same, as the song goes. |
| 67 |
// |
| 68 |
// Note that you should use the returned Threadable as the new root of your threads, as your old one is |
| 69 |
// very likely to have been moved halfway down the top level chain that you passed in. That will confuse the |
| 70 |
// BeJesus out of you. I know it did me for a minute. |
| 71 |
// |
| 72 |
// So, all the chains will be sorted by asking your supplied ThreadLess function whether the first Threadable |
| 73 |
// it gives you should sort before the second one it gives you. This makes the sort trivial for you to sort |
| 74 |
// the Threadable set and avoids you having to get your head around following the chain of pointers etc. |
| 75 |
func Sort(threads Threadable, by ThreadLess) Threadable { |
| 76 |
// Guard against stupidity |
| 77 |
// |
| 78 |
if threads == nil { |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
// If this node has no next pointers, then it is the only element in this current chain, so it is |
| 83 |
// sorted by default |
| 84 |
// |
| 85 |
if threads.GetNext() == nil { |
| 86 |
return threads |
| 87 |
} |
| 88 |
|
| 89 |
// Now we sort the chain of current pointers. The easiest way is to convert to a slice then |
| 90 |
// sort the slice. This is because after sorting, the child of the node above should become the |
| 91 |
// first element of the sorted slice |
| 92 |
// |
| 93 |
s := make([]Threadable, 0, 42) |
| 94 |
for current := threads; current != nil; current = current.GetNext() { |
| 95 |
|
| 96 |
// If the current node we are inspecting has a child, then sort that first |
| 97 |
// |
| 98 |
if c := current.GetChild(); c != nil { |
| 99 |
current.SetChild(Sort(c, by)) |
| 100 |
} |
| 101 |
s = append(s, current) |
| 102 |
} |
| 103 |
|
| 104 |
// We can now sort the slice of next pointers at this level. Note that the child of this node |
| 105 |
// will already have been sorted, so if it is used to get the date or something like that |
| 106 |
// - because this node is a Dummy - then it will be OK to use it as it will be in the correct |
| 107 |
// order already |
| 108 |
// |
| 109 |
slices.SortFunc(s, func(a, b Threadable) int { |
| 110 |
if by(a, b) { |
| 111 |
return -1 |
| 112 |
} |
| 113 |
if by(b, a) { |
| 114 |
return 1 |
| 115 |
} |
| 116 |
return 0 |
| 117 |
}) |
| 118 |
|
| 119 |
// And we now rebuild the chain from the slice |
| 120 |
// |
| 121 |
l := len(s) - 1 |
| 122 |
for i := range l { |
| 123 |
s[i].SetNext(s[i+1]) |
| 124 |
} |
| 125 |
|
| 126 |
// Last element in the slice no longer has a current of course |
| 127 |
// |
| 128 |
s[l].SetNext(nil) |
| 129 |
|
| 130 |
// And the new child of the node above is the first of the newly sorted (or at the top level the new root) |
| 131 |
// |
| 132 |
newChild := s[0] |
| 133 |
|
| 134 |
// And return the new child for the node above us |
| 135 |
// |
| 136 |
return newChild |
| 137 |
} |
| 138 |
|
| 139 |
// Walk allows the caller to execute some function against each node in the tree, while avoiding dealing with the |
| 140 |
// internal structure of the Threadable tree. It is easy to get tree walk code wrong, and while this generic walk |
| 141 |
// will probably not do everything that everyone wants - such as cause Leeds United to beat Manchester United - it |
| 142 |
// is likely to work for most cases. |
| 143 |
// |
| 144 |
// Walk will call your WalkFunction for each node in the tree and will supply the node, and an interface value |
| 145 |
// (which can be nil) that you can supply for your own tracking if simple walk is not enough for you. |
| 146 |
// |
| 147 |
// The walker will perform depth first search if asked, by passing parameter isDepth=true |
| 148 |
func Walk(isDepth bool, tree Threadable, f WalkFunction, u any) error { |
| 149 |
// Guard against stupidity |
| 150 |
// |
| 151 |
if tree == nil { |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
// A depth first search means we descend in to the children until there aren't any, then traverse the |
| 156 |
// next pointers. A breadth first search means we traverse all the next pointers then descend in to the children |
| 157 |
// |
| 158 |
if isDepth { |
| 159 |
for current := tree; current != nil; current = current.GetNext() { |
| 160 |
|
| 161 |
// If the current node we are inspecting has a child, then call with that first |
| 162 |
// |
| 163 |
if c := current.GetChild(); c != nil { |
| 164 |
if err := Walk(isDepth, c, f, u); err != nil { |
| 165 |
return err |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Now call the user function on this current node, which of course means we eventually call them all |
| 170 |
// |
| 171 |
stop, err := f(current, u) |
| 172 |
if err != nil || stop { |
| 173 |
return err |
| 174 |
} |
| 175 |
} |
| 176 |
} else { |
| 177 |
|
| 178 |
// This is breadth first, which means we call the function with all the Threadables in the next |
| 179 |
// chain, then we call their children if they have any |
| 180 |
// |
| 181 |
for current := tree; current != nil; current = current.GetNext() { |
| 182 |
|
| 183 |
// Call the function on each next in turn |
| 184 |
// |
| 185 |
stop, err := f(current, u) |
| 186 |
if err != nil || stop { |
| 187 |
return err |
| 188 |
} |
| 189 |
} |
| 190 |
for current := tree; current != nil; current = current.GetNext() { |
| 191 |
// If the current node we are inspecting has a child, then walk it breadth wise |
| 192 |
// |
| 193 |
if c := current.GetChild(); c != nil { |
| 194 |
if err := Walk(isDepth, c, f, u); err != nil { |
| 195 |
return err |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
// We are finished with this bit |
| 201 |
// |
| 202 |
return nil |
| 203 |
} |