rune_trie.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package trie
  2. func (trie *RuneTrie) find(contents []rune, accu []rune) (string, interface{}) {
  3. if trie.value != nil {
  4. return string(accu), trie.value
  5. }
  6. if len(contents) == 0 {
  7. return "", nil
  8. }
  9. k := contents[0]
  10. tt, ok := trie.children[k]
  11. if !ok {
  12. return "", nil
  13. }
  14. return tt.find(contents[1:], append(accu, k))
  15. }
  16. func (trie *RuneTrie) Find(content string, accu string) (string, interface{}) {
  17. rcontent := []rune(content)
  18. if len(rcontent) == 0 {
  19. return "", nil
  20. }
  21. k, v := trie.find(rcontent, []rune(accu))
  22. if v == nil {
  23. return trie.Find(string(rcontent[1:]), accu)
  24. }
  25. return k, v
  26. }
  27. /*
  28. * the belowing codes come from "https://github.com/dghubble/trie"
  29. */
  30. type RuneTrie struct {
  31. value interface{}
  32. children map[rune]*RuneTrie
  33. }
  34. func NewRuneTrie() *RuneTrie {
  35. return &RuneTrie{
  36. children: make(map[rune]*RuneTrie),
  37. }
  38. }
  39. func (trie *RuneTrie) Get(key string) interface{} {
  40. node := trie
  41. for _, r := range key {
  42. node = node.children[r]
  43. if node == nil {
  44. return nil
  45. }
  46. }
  47. return node.value
  48. }
  49. func (trie *RuneTrie) Put(key string, value interface{}) bool {
  50. node := trie
  51. for _, r := range key {
  52. child := node.children[r]
  53. if child == nil {
  54. child = NewRuneTrie()
  55. node.children[r] = child
  56. }
  57. node = child
  58. }
  59. isNewVal := node.value == nil
  60. node.value = value
  61. return isNewVal
  62. }
  63. func (trie *RuneTrie) Delete(key string) bool {
  64. path := make([]nodeRune, len(key))
  65. node := trie
  66. for i, r := range key {
  67. path[i] = nodeRune{r: r, node: node}
  68. node = node.children[r]
  69. if node == nil {
  70. return false
  71. }
  72. }
  73. node.value = nil
  74. if node.isLeaf() {
  75. for i := len(key) - 1; i >= 0; i-- {
  76. parent := path[i].node
  77. r := path[i].r
  78. delete(parent.children, r)
  79. if parent.value != nil || !parent.isLeaf() {
  80. break
  81. }
  82. }
  83. }
  84. return true
  85. }
  86. func (trie *RuneTrie) Walk(walker WalkFunc) error {
  87. return trie.walk("", walker)
  88. }
  89. type nodeRune struct {
  90. node *RuneTrie
  91. r rune
  92. }
  93. func (trie *RuneTrie) walk(key string, walker WalkFunc) error {
  94. if trie.value != nil {
  95. walker(key, trie.value)
  96. }
  97. for r, child := range trie.children {
  98. err := child.walk(key+string(r), walker)
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. return nil
  104. }
  105. func (trie *RuneTrie) isLeaf() bool {
  106. return len(trie.children) == 0
  107. }
  108. type WalkFunc func(key string, value interface{}) error