node.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package stringx
  2. type node struct {
  3. children map[rune]*node
  4. fail *node
  5. depth int
  6. end bool
  7. }
  8. func (n *node) add(word string) {
  9. chars := []rune(word)
  10. if len(chars) == 0 {
  11. return
  12. }
  13. nd := n
  14. var depth int
  15. for i, char := range chars {
  16. if nd.children == nil {
  17. child := new(node)
  18. child.depth = i + 1
  19. nd.children = map[rune]*node{char: child}
  20. nd = child
  21. } else if child, ok := nd.children[char]; ok {
  22. nd = child
  23. depth++
  24. } else {
  25. child := new(node)
  26. child.depth = i + 1
  27. nd.children[char] = child
  28. nd = child
  29. }
  30. }
  31. nd.end = true
  32. }
  33. func (n *node) build() {
  34. var nodes []*node
  35. for _, child := range n.children {
  36. child.fail = n
  37. nodes = append(nodes, child)
  38. }
  39. for len(nodes) > 0 {
  40. nd := nodes[0]
  41. nodes = nodes[1:]
  42. for key, child := range nd.children {
  43. nodes = append(nodes, child)
  44. cur := nd
  45. for cur != nil {
  46. if cur.fail == nil {
  47. child.fail = n
  48. break
  49. }
  50. if fail, ok := cur.fail.children[key]; ok {
  51. child.fail = fail
  52. break
  53. }
  54. cur = cur.fail
  55. }
  56. }
  57. }
  58. }
  59. func (n *node) find(chars []rune) []scope {
  60. var scopes []scope
  61. size := len(chars)
  62. cur := n
  63. for i := 0; i < size; i++ {
  64. child, ok := cur.children[chars[i]]
  65. if ok {
  66. cur = child
  67. } else {
  68. for cur != n {
  69. cur = cur.fail
  70. if child, ok = cur.children[chars[i]]; ok {
  71. cur = child
  72. break
  73. }
  74. }
  75. if child == nil {
  76. continue
  77. }
  78. }
  79. for child != n {
  80. if child.end {
  81. scopes = append(scopes, scope{
  82. start: i + 1 - child.depth,
  83. stop: i + 1,
  84. })
  85. }
  86. child = child.fail
  87. }
  88. }
  89. return scopes
  90. }