children.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (c) 2014 The go-patricia AUTHORS
  2. //
  3. // Use of this source code is governed by The MIT License
  4. // that can be found in the LICENSE file.
  5. package patricia
  6. import "sort"
  7. type childList interface {
  8. length() int
  9. head() *Trie
  10. add(child *Trie) childList
  11. replace(b byte, child *Trie)
  12. remove(child *Trie)
  13. next(b byte) *Trie
  14. walk(prefix *Prefix, visitor VisitorFunc) error
  15. }
  16. type tries []*Trie
  17. func (t tries) Len() int {
  18. return len(t)
  19. }
  20. func (t tries) Less(i, j int) bool {
  21. strings := sort.StringSlice{string(t[i].prefix), string(t[j].prefix)}
  22. return strings.Less(0, 1)
  23. }
  24. func (t tries) Swap(i, j int) {
  25. t[i], t[j] = t[j], t[i]
  26. }
  27. type sparseChildList struct {
  28. children tries
  29. }
  30. func newSparseChildList(maxChildrenPerSparseNode int) childList {
  31. return &sparseChildList{
  32. children: make(tries, 0, DefaultMaxChildrenPerSparseNode),
  33. }
  34. }
  35. func (list *sparseChildList) length() int {
  36. return len(list.children)
  37. }
  38. func (list *sparseChildList) head() *Trie {
  39. return list.children[0]
  40. }
  41. func (list *sparseChildList) add(child *Trie) childList {
  42. // Search for an empty spot and insert the child if possible.
  43. if len(list.children) != cap(list.children) {
  44. list.children = append(list.children, child)
  45. return list
  46. }
  47. // Otherwise we have to transform to the dense list type.
  48. return newDenseChildList(list, child)
  49. }
  50. func (list *sparseChildList) replace(b byte, child *Trie) {
  51. // Seek the child and replace it.
  52. for i, node := range list.children {
  53. if node.prefix[0] == b {
  54. list.children[i] = child
  55. return
  56. }
  57. }
  58. }
  59. func (list *sparseChildList) remove(child *Trie) {
  60. for i, node := range list.children {
  61. if node.prefix[0] == child.prefix[0] {
  62. list.children = append(list.children[:i], list.children[i+1:]...)
  63. return
  64. }
  65. }
  66. // This is not supposed to be reached.
  67. panic("removing non-existent child")
  68. }
  69. func (list *sparseChildList) next(b byte) *Trie {
  70. for _, child := range list.children {
  71. if child.prefix[0] == b {
  72. return child
  73. }
  74. }
  75. return nil
  76. }
  77. func (list *sparseChildList) walk(prefix *Prefix, visitor VisitorFunc) error {
  78. sort.Sort(list.children)
  79. for _, child := range list.children {
  80. *prefix = append(*prefix, child.prefix...)
  81. if child.item != nil {
  82. err := visitor(*prefix, child.item)
  83. if err != nil {
  84. if err == SkipSubtree {
  85. *prefix = (*prefix)[:len(*prefix)-len(child.prefix)]
  86. continue
  87. }
  88. *prefix = (*prefix)[:len(*prefix)-len(child.prefix)]
  89. return err
  90. }
  91. }
  92. err := child.children.walk(prefix, visitor)
  93. *prefix = (*prefix)[:len(*prefix)-len(child.prefix)]
  94. if err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }
  100. type denseChildList struct {
  101. min int
  102. max int
  103. children []*Trie
  104. }
  105. func newDenseChildList(list *sparseChildList, child *Trie) childList {
  106. var (
  107. min int = 255
  108. max int = 0
  109. )
  110. for _, child := range list.children {
  111. b := int(child.prefix[0])
  112. if b < min {
  113. min = b
  114. }
  115. if b > max {
  116. max = b
  117. }
  118. }
  119. b := int(child.prefix[0])
  120. if b < min {
  121. min = b
  122. }
  123. if b > max {
  124. max = b
  125. }
  126. children := make([]*Trie, max-min+1)
  127. for _, child := range list.children {
  128. children[int(child.prefix[0])-min] = child
  129. }
  130. children[int(child.prefix[0])-min] = child
  131. return &denseChildList{min, max, children}
  132. }
  133. func (list *denseChildList) length() int {
  134. return list.max - list.min + 1
  135. }
  136. func (list *denseChildList) head() *Trie {
  137. return list.children[0]
  138. }
  139. func (list *denseChildList) add(child *Trie) childList {
  140. b := int(child.prefix[0])
  141. switch {
  142. case list.min <= b && b <= list.max:
  143. if list.children[b-list.min] != nil {
  144. panic("dense child list collision detected")
  145. }
  146. list.children[b-list.min] = child
  147. case b < list.min:
  148. children := make([]*Trie, list.max-b+1)
  149. children[0] = child
  150. copy(children[list.min-b:], list.children)
  151. list.children = children
  152. list.min = b
  153. default: // b > list.max
  154. children := make([]*Trie, b-list.min+1)
  155. children[b-list.min] = child
  156. copy(children, list.children)
  157. list.children = children
  158. list.max = b
  159. }
  160. return list
  161. }
  162. func (list *denseChildList) replace(b byte, child *Trie) {
  163. list.children[int(b)-list.min] = nil
  164. list.children[int(child.prefix[0])-list.min] = child
  165. }
  166. func (list *denseChildList) remove(child *Trie) {
  167. i := int(child.prefix[0]) - list.min
  168. if list.children[i] == nil {
  169. // This is not supposed to be reached.
  170. panic("removing non-existent child")
  171. }
  172. list.children[i] = nil
  173. }
  174. func (list *denseChildList) next(b byte) *Trie {
  175. i := int(b)
  176. if i < list.min || list.max < i {
  177. return nil
  178. }
  179. return list.children[i-list.min]
  180. }
  181. func (list *denseChildList) walk(prefix *Prefix, visitor VisitorFunc) error {
  182. for _, child := range list.children {
  183. if child == nil {
  184. continue
  185. }
  186. *prefix = append(*prefix, child.prefix...)
  187. if child.item != nil {
  188. if err := visitor(*prefix, child.item); err != nil {
  189. if err == SkipSubtree {
  190. *prefix = (*prefix)[:len(*prefix)-len(child.prefix)]
  191. continue
  192. }
  193. *prefix = (*prefix)[:len(*prefix)-len(child.prefix)]
  194. return err
  195. }
  196. }
  197. err := child.children.walk(prefix, visitor)
  198. *prefix = (*prefix)[:len(*prefix)-len(child.prefix)]
  199. if err != nil {
  200. return err
  201. }
  202. }
  203. return nil
  204. }