tree.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package search
  2. import "errors"
  3. const (
  4. colon = ':'
  5. slash = '/'
  6. )
  7. var (
  8. ErrDupItem = errors.New("duplicated item")
  9. ErrDupSlash = errors.New("duplicated slash")
  10. ErrEmptyItem = errors.New("empty item")
  11. ErrInvalidState = errors.New("search tree is in an invalid state")
  12. ErrNotFromRoot = errors.New("path should start with /")
  13. NotFound Result
  14. )
  15. type (
  16. innerResult struct {
  17. key string
  18. value string
  19. named bool
  20. found bool
  21. }
  22. node struct {
  23. item interface{}
  24. children [2]map[string]*node
  25. }
  26. Tree struct {
  27. root *node
  28. }
  29. Result struct {
  30. Item interface{}
  31. Params map[string]string
  32. }
  33. )
  34. func NewTree() *Tree {
  35. return &Tree{
  36. root: newNode(nil),
  37. }
  38. }
  39. func (t *Tree) Add(route string, item interface{}) error {
  40. if len(route) == 0 || route[0] != slash {
  41. return ErrNotFromRoot
  42. }
  43. if item == nil {
  44. return ErrEmptyItem
  45. }
  46. return add(t.root, route[1:], item)
  47. }
  48. func (t *Tree) Search(route string) (Result, bool) {
  49. if len(route) == 0 || route[0] != slash {
  50. return NotFound, false
  51. }
  52. var result Result
  53. ok := t.next(t.root, route[1:], &result)
  54. return result, ok
  55. }
  56. func (t *Tree) next(n *node, route string, result *Result) bool {
  57. if len(route) == 0 && n.item != nil {
  58. result.Item = n.item
  59. return true
  60. }
  61. for i := range route {
  62. if route[i] == slash {
  63. token := route[:i]
  64. return n.forEach(func(k string, v *node) bool {
  65. if r := match(k, token); r.found {
  66. if t.next(v, route[i+1:], result) {
  67. if r.named {
  68. addParam(result, r.key, r.value)
  69. }
  70. return true
  71. }
  72. }
  73. return false
  74. })
  75. }
  76. }
  77. return n.forEach(func(k string, v *node) bool {
  78. if r := match(k, route); r.found && v.item != nil {
  79. result.Item = v.item
  80. if r.named {
  81. addParam(result, r.key, r.value)
  82. }
  83. return true
  84. }
  85. return false
  86. })
  87. }
  88. func (nd *node) forEach(fn func(string, *node) bool) bool {
  89. for _, children := range nd.children {
  90. for k, v := range children {
  91. if fn(k, v) {
  92. return true
  93. }
  94. }
  95. }
  96. return false
  97. }
  98. func (nd *node) getChildren(route string) map[string]*node {
  99. if len(route) > 0 && route[0] == colon {
  100. return nd.children[1]
  101. }
  102. return nd.children[0]
  103. }
  104. func add(nd *node, route string, item interface{}) error {
  105. if len(route) == 0 {
  106. if nd.item != nil {
  107. return ErrDupItem
  108. }
  109. nd.item = item
  110. return nil
  111. }
  112. if route[0] == slash {
  113. return ErrDupSlash
  114. }
  115. for i := range route {
  116. if route[i] == slash {
  117. token := route[:i]
  118. children := nd.getChildren(token)
  119. if child, ok := children[token]; ok {
  120. if child != nil {
  121. return add(child, route[i+1:], item)
  122. }
  123. return ErrInvalidState
  124. }
  125. child := newNode(nil)
  126. children[token] = child
  127. return add(child, route[i+1:], item)
  128. }
  129. }
  130. children := nd.getChildren(route)
  131. if child, ok := children[route]; ok {
  132. if child.item != nil {
  133. return ErrDupItem
  134. }
  135. child.item = item
  136. } else {
  137. children[route] = newNode(item)
  138. }
  139. return nil
  140. }
  141. func addParam(result *Result, k, v string) {
  142. if result.Params == nil {
  143. result.Params = make(map[string]string)
  144. }
  145. result.Params[k] = v
  146. }
  147. func match(pat, token string) innerResult {
  148. if pat[0] == colon {
  149. return innerResult{
  150. key: pat[1:],
  151. value: token,
  152. named: true,
  153. found: true,
  154. }
  155. }
  156. return innerResult{
  157. found: pat == token,
  158. }
  159. }
  160. func newNode(item interface{}) *node {
  161. return &node{
  162. item: item,
  163. children: [2]map[string]*node{
  164. make(map[string]*node),
  165. make(map[string]*node),
  166. },
  167. }
  168. }