tree.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // tree project tree.go
  2. // a relativey simple recursive intrusive tree with an arbitrary amount of children on each level.
  3. package tree
  4. import "fmt"
  5. /* Add a Node to the Struct you want to use these functions with,
  6. and write an initializer maker func(...interface{}) Noder for use with
  7. the New* functions. Everything else goes by itself.
  8. */
  9. type Node struct {
  10. Parent_ Noder
  11. Child_ Noder
  12. Before_ Noder
  13. After_ Noder
  14. }
  15. type Noder interface {
  16. Child() Noder
  17. Parent() Noder
  18. Before() Noder
  19. After() Noder
  20. SetChild(Noder) Noder
  21. SetParent(Noder) Noder
  22. SetBefore(Noder) Noder
  23. SetAfter(Noder) Noder
  24. }
  25. func (me *Node) Child() Noder {
  26. return me.Child_
  27. }
  28. func (me *Node) Parent() Noder {
  29. return me.Parent_
  30. }
  31. func (me *Node) After() Noder {
  32. return me.After_
  33. }
  34. func (me *Node) Before() Noder {
  35. return me.Before_
  36. }
  37. func (me *Node) SetChild(val Noder) Noder {
  38. me.Child_ = val
  39. return me.Child_
  40. }
  41. func (me *Node) SetParent(val Noder) Noder {
  42. me.Parent_ = val
  43. return me.Parent_
  44. }
  45. func (me *Node) SetAfter(val Noder) Noder {
  46. me.After_ = val
  47. return me.After_
  48. }
  49. func (me *Node) SetBefore(val Noder) Noder {
  50. me.Before_ = val
  51. return me.Before_
  52. }
  53. func NewNoder(parent Noder, maker func(...interface{}) Noder, args ...interface{}) Noder {
  54. child := maker(args...)
  55. child.SetParent(parent)
  56. return child
  57. }
  58. func LastSibling(me Noder) Noder {
  59. var res Noder = me
  60. for res != nil && res.After() != nil {
  61. res = res.After()
  62. }
  63. return res
  64. }
  65. func LastChild(me Noder) Noder {
  66. return LastSibling(me.Child())
  67. }
  68. /* Detaches, I.E removes this node and all it's children from the parent tree. */
  69. func Remove(me Noder) Noder {
  70. parent := me.Parent()
  71. before := me.Before()
  72. after := me.After()
  73. if before != nil {
  74. before.SetAfter(after)
  75. }
  76. if after != nil {
  77. after.SetBefore(before)
  78. }
  79. if parent != nil {
  80. /* Special case if me is the first child of it's parent. */
  81. if me == parent.Child() {
  82. parent.SetChild(after)
  83. }
  84. }
  85. me.SetParent(nil)
  86. return me
  87. }
  88. func InsertSibling(me, sibling Noder) Noder {
  89. after := me.After()
  90. me.SetAfter(sibling)
  91. sibling.SetBefore(me)
  92. sibling.SetAfter(after)
  93. if after != nil {
  94. after.SetBefore(sibling)
  95. }
  96. sibling.SetParent(me.Parent())
  97. return sibling
  98. }
  99. func AppendSibling(me, sibling Noder) Noder {
  100. return InsertSibling(LastSibling(me), sibling)
  101. }
  102. func AppendChild(me, child Noder) Noder {
  103. child.SetParent(me)
  104. if me.Child() == nil {
  105. me.SetChild(child)
  106. } else {
  107. AppendSibling(me.Child(), child)
  108. }
  109. return child
  110. }
  111. func NewSibling(me Noder, maker func(...interface{}) Noder, args ...interface{}) Noder {
  112. node := NewNoder(me.Parent(), maker, args...)
  113. return AppendSibling(me, node)
  114. }
  115. func NewChild(me Noder, maker func(...interface{}) Noder, args ...interface{}) Noder {
  116. node := NewNoder(me, maker, args...)
  117. return AppendChild(me, node)
  118. }
  119. func Walk(me Noder, walker func(me Noder) Noder) Noder {
  120. if found := walker(me); found != nil {
  121. return found
  122. }
  123. if me.Child() != nil {
  124. if found := Walk(me.Child(), walker); found != nil {
  125. return found
  126. }
  127. }
  128. if me.After() != nil {
  129. if found := Walk(me.After(), walker); found != nil {
  130. return found
  131. }
  132. }
  133. return nil
  134. }
  135. func Display(me Noder) {
  136. Walk(me, func(node Noder) Noder {
  137. fmt.Printf("Tree: %v\n", node)
  138. return nil
  139. })
  140. }
  141. /*
  142. interface Walker {
  143. Walk(walker func(me *Node) *Node) *Node
  144. }
  145. func WalkWalker(walker Walker) {
  146. }
  147. */