| 1 |
package trie |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
type ( |
| 8 |
Trie struct { |
| 9 |
root *node |
| 10 |
size int |
| 11 |
} |
| 12 |
node struct { |
| 13 |
key interface{} |
| 14 |
value interface{} |
| 15 |
next [256]*node |
| 16 |
} |
| 17 |
iterator struct { |
| 18 |
step int |
| 19 |
node *node |
| 20 |
prev *iterator |
| 21 |
} |
| 22 |
) |
| 23 |
|
| 24 |
func toBytes(obj interface{}) []byte { |
| 25 |
switch o := obj.(type) { |
| 26 |
case []byte: |
| 27 |
return o |
| 28 |
case string: |
| 29 |
return []byte(o) |
| 30 |
} |
| 31 |
return []byte(fmt.Sprint(obj)) |
| 32 |
} |
| 33 |
|
| 34 |
func New() *Trie { |
| 35 |
return &Trie{nil,0} |
| 36 |
} |
| 37 |
func (this *Trie) Do(handler func(interface{},interface{})bool) { |
| 38 |
if this.size > 0 { |
| 39 |
this.root.do(handler) |
| 40 |
} |
| 41 |
} |
| 42 |
func (this *Trie) Get(key interface{}) interface{} { |
| 43 |
if this.size == 0 { |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
bs := toBytes(key) |
| 48 |
cur := this.root |
| 49 |
for i := 0; i < len(bs); i++ { |
| 50 |
if cur.next[bs[i]] != nil { |
| 51 |
cur = cur.next[bs[i]] |
| 52 |
} else { |
| 53 |
return nil |
| 54 |
} |
| 55 |
} |
| 56 |
return cur.value |
| 57 |
} |
| 58 |
func (this *Trie) Has(key interface{}) bool { |
| 59 |
return this.Get(key) != nil |
| 60 |
} |
| 61 |
func (this *Trie) Init() { |
| 62 |
this.root = nil |
| 63 |
this.size = 0 |
| 64 |
} |
| 65 |
func (this *Trie) Insert(key interface{}, value interface{}) { |
| 66 |
if this.size == 0 { |
| 67 |
this.root = newNode() |
| 68 |
} |
| 69 |
|
| 70 |
bs := toBytes(key) |
| 71 |
cur := this.root |
| 72 |
for i := 0; i < len(bs); i++ { |
| 73 |
if cur.next[bs[i]] != nil { |
| 74 |
cur = cur.next[bs[i]] |
| 75 |
} else { |
| 76 |
cur.next[bs[i]] = newNode() |
| 77 |
cur = cur.next[bs[i]] |
| 78 |
} |
| 79 |
} |
| 80 |
if cur.key == nil { |
| 81 |
this.size++ |
| 82 |
} |
| 83 |
cur.key = key |
| 84 |
cur.value = value |
| 85 |
} |
| 86 |
func (this *Trie) Len() int { |
| 87 |
return this.size |
| 88 |
} |
| 89 |
func (this *Trie) Remove(key interface{}) interface{} { |
| 90 |
if this.size == 0 { |
| 91 |
return nil |
| 92 |
} |
| 93 |
bs := toBytes(key) |
| 94 |
cur := this.root |
| 95 |
|
| 96 |
for i := 0; i < len(bs); i++ { |
| 97 |
if cur.next[bs[i]] != nil { |
| 98 |
cur = cur.next[bs[i]] |
| 99 |
} else { |
| 100 |
return nil |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
val := cur.value |
| 107 |
|
| 108 |
if cur.value != nil { |
| 109 |
this.size-- |
| 110 |
cur.value = nil |
| 111 |
cur.key = nil |
| 112 |
} |
| 113 |
return val |
| 114 |
} |
| 115 |
func (this *Trie) String() string { |
| 116 |
str := "{" |
| 117 |
i := 0 |
| 118 |
this.Do(func(k, v interface{}) bool { |
| 119 |
if i > 0 { |
| 120 |
str += ", " |
| 121 |
} |
| 122 |
str += fmt.Sprint(k, ":", v) |
| 123 |
i++ |
| 124 |
return true |
| 125 |
}) |
| 126 |
str += "}" |
| 127 |
return str |
| 128 |
} |
| 129 |
|
| 130 |
func newNode() *node { |
| 131 |
var next [256]*node |
| 132 |
return &node{nil,nil,next} |
| 133 |
} |
| 134 |
func (this *node) do(handler func(interface{}, interface{}) bool) bool { |
| 135 |
for i := 0; i < 256; i++ { |
| 136 |
if this.next[i] != nil { |
| 137 |
if this.next[i].key != nil { |
| 138 |
if !handler(this.next[i].key, this.next[i].value) { |
| 139 |
return false |
| 140 |
} |
| 141 |
} |
| 142 |
if !this.next[i].do(handler) { |
| 143 |
return false |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
return true |
| 148 |
} |