tree.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. for _, children := range n.children {
  65. for k, v := range children {
  66. if r := match(k, token); r.found {
  67. if t.next(v, route[i+1:], result) {
  68. if r.named {
  69. addParam(result, r.key, r.value)
  70. }
  71. return true
  72. }
  73. }
  74. }
  75. }
  76. return false
  77. }
  78. }
  79. for _, children := range n.children {
  80. for k, v := range children {
  81. if r := match(k, route); r.found && v.item != nil {
  82. result.Item = v.item
  83. if r.named {
  84. addParam(result, r.key, r.value)
  85. }
  86. return true
  87. }
  88. }
  89. }
  90. return false
  91. }
  92. func (nd *node) getChildren(route string) map[string]*node {
  93. if len(route) > 0 && route[0] == colon {
  94. return nd.children[1]
  95. } else {
  96. return nd.children[0]
  97. }
  98. }
  99. func add(nd *node, route string, item interface{}) error {
  100. if len(route) == 0 {
  101. if nd.item != nil {
  102. return ErrDupItem
  103. }
  104. nd.item = item
  105. return nil
  106. }
  107. if route[0] == slash {
  108. return ErrDupSlash
  109. }
  110. for i := range route {
  111. if route[i] == slash {
  112. token := route[:i]
  113. children := nd.getChildren(token)
  114. if child, ok := children[token]; ok {
  115. if child != nil {
  116. return add(child, route[i+1:], item)
  117. } else {
  118. return ErrInvalidState
  119. }
  120. } else {
  121. child := newNode(nil)
  122. children[token] = child
  123. return add(child, route[i+1:], item)
  124. }
  125. }
  126. }
  127. children := nd.getChildren(route)
  128. if child, ok := children[route]; ok {
  129. if child.item != nil {
  130. return ErrDupItem
  131. }
  132. child.item = item
  133. } else {
  134. children[route] = newNode(item)
  135. }
  136. return nil
  137. }
  138. func addParam(result *Result, k, v string) {
  139. if result.Params == nil {
  140. result.Params = make(map[string]string)
  141. }
  142. result.Params[k] = v
  143. }
  144. func match(pat, token string) innerResult {
  145. if pat[0] == colon {
  146. return innerResult{
  147. key: pat[1:],
  148. value: token,
  149. named: true,
  150. found: true,
  151. }
  152. }
  153. return innerResult{
  154. found: pat == token,
  155. }
  156. }
  157. func newNode(item interface{}) *node {
  158. return &node{
  159. item: item,
  160. children: [2]map[string]*node{
  161. make(map[string]*node),
  162. make(map[string]*node),
  163. },
  164. }
  165. }