File: trie.go

package info (click to toggle)
golang-github-badgerodon-collections 0.0~git20130729.604e922-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 172 kB
  • sloc: makefile: 5
file content (148 lines) | stat: -rw-r--r-- 2,529 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package trie

import (
	"fmt"
)

type (
	Trie struct {
		root *node
		size int
	}
	node struct {
		key interface{}
		value interface{}
		next [256]*node
	}
	iterator struct {
		step int
		node *node
		prev *iterator
	}
)

func toBytes(obj interface{}) []byte {
	switch o := obj.(type) {
	case []byte:
		return o
	case string:
		return []byte(o)
	}
	return []byte(fmt.Sprint(obj))
}

func New() *Trie {
	return &Trie{nil,0}
}
func (this *Trie) Do(handler func(interface{},interface{})bool) {
	if this.size > 0 {
		this.root.do(handler)
	}
}
func (this *Trie) Get(key interface{}) interface{} {
	if this.size == 0 {
		return nil
	}
	
	bs := toBytes(key)
	cur := this.root
	for i := 0; i < len(bs); i++ {
		if cur.next[bs[i]] != nil {
			cur = cur.next[bs[i]]
		} else {
			return nil
		}
	}
	return cur.value
}
func (this *Trie) Has(key interface{}) bool {
	return this.Get(key) != nil
}
func (this *Trie) Init() {
	this.root = nil
	this.size = 0
}
func (this *Trie) Insert(key interface{}, value interface{}) {
	if this.size == 0 {
		this.root = newNode()
	}
	
	bs := toBytes(key)
	cur := this.root
	for i := 0; i < len(bs); i++ {
		if cur.next[bs[i]] != nil {
			cur = cur.next[bs[i]]
		} else {
			cur.next[bs[i]] = newNode()
			cur = cur.next[bs[i]]
		}
	}	
	if cur.key == nil {
		this.size++
	}
	cur.key = key
	cur.value = value
}
func (this *Trie) Len() int {
	return this.size
}
func (this *Trie) Remove(key interface{}) interface{} {
	if this.size == 0 {
		return nil
	}
	bs := toBytes(key)
	cur := this.root
	
	for i := 0; i < len(bs); i++ {
		if cur.next[bs[i]] != nil {
			cur = cur.next[bs[i]]
		} else {
			return nil
		}
	}
	
	// TODO: cleanup dead nodes
	
	val := cur.value
	
	if cur.value != nil {
		this.size--
		cur.value = nil
		cur.key = nil
	}
	return val
}
func (this *Trie) String() string {
	str := "{"
	i := 0
	this.Do(func(k, v interface{}) bool {
		if i > 0 {
			str += ", "
		}
		str += fmt.Sprint(k, ":", v)
		i++
		return true
	})
	str += "}"
	return str
}

func newNode() *node {
	var next [256]*node
	return &node{nil,nil,next}
}
func (this *node) do(handler func(interface{}, interface{}) bool) bool {
	for i := 0; i < 256; i++ {
		if this.next[i] != nil {
			if this.next[i].key != nil {
				if !handler(this.next[i].key, this.next[i].value) {
					return false
				}
			}
			if !this.next[i].do(handler) {
				return false
			}
		}
	}
	return true
}