Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
go fmt radix.
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Dec 20, 2020
1 parent 780d42e commit 79bea4e
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 129 deletions.
36 changes: 18 additions & 18 deletions internal/radix/node.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package radix

type Node struct {
children [alphabet]*Node
word bool
hasChildren bool
data int32
children [alphabet]*Node
word bool
hasChildren bool
data int32
}

func (n *Node) Word() bool {
return n.word
return n.word
}

func (n *Node) Data() int32 {
return n.data
return n.data
}

func (node *Node) Next(next byte) *Node {
return node.children[next - chOffset]
return node.children[next-chOffset]
}

func (node *Node) traverse(word *[longestWord]byte, end int, callback func(string, int32)) {
if node.word {
callback(string(word[0:end]), node.data)
}
if node.hasChildren {
for i, n := range node.children {
if n != nil {
word[end] = byte(i) + chOffset
n.traverse(word, end + 1, callback)
}
}
}
if node.word {
callback(string(word[0:end]), node.data)
}
if node.hasChildren {
for i, n := range node.children {
if n != nil {
word[end] = byte(i) + chOffset
n.traverse(word, end+1, callback)
}
}
}
}
222 changes: 111 additions & 111 deletions internal/radix/radix.go
Original file line number Diff line number Diff line change
@@ -1,149 +1,149 @@
package radix

const (
alphabet = 26
longestWord = 28
chMax = 1 + byte('z') - byte('a')
chOffset = byte('a')
alphabet = 26
longestWord = 28
chMax = 1 + byte('z') - byte('a')
chOffset = byte('a')
)

type Tree struct {
root *Node
length int
root *Node
length int
}

func New() *Tree {
return NewFromList("")
return NewFromList("")
}

func NewFromList(words string) (tree *Tree) {
var (
scanBuffer [longestWord]byte
nodeCache [longestWord + 1]*Node
)

tree = &Tree{root: &Node{}}

nodeCache[0] = tree.root
i := 0 // Index of current word
previousLength := -2 // Length of previous word
indexed := 0 // How many words were indexed
p := tree.root

for c := 0; c < len(words); c++ {
ch := words[c] - chOffset
if ch < chMax && i < longestWord {
if scanBuffer[i] == ch {
if i < previousLength {
i++
continue
}
} else {
scanBuffer[i] = ch
}

i++

if previousLength != -1 {
previousLength = -1
p = nodeCache[i - 1]
}

q := &Node{}

p.children[ch] = q
nodeCache[i] = q
p.hasChildren = true

p = q
} else if i > 0 {
tree.length++
p.word = true

indexed++
previousLength = i
i = 0
}
}
return
var (
scanBuffer [longestWord]byte
nodeCache [longestWord + 1]*Node
)

tree = &Tree{root: &Node{}}

nodeCache[0] = tree.root
i := 0 // Index of current word
previousLength := -2 // Length of previous word
indexed := 0 // How many words were indexed
p := tree.root

for c := 0; c < len(words); c++ {
ch := words[c] - chOffset
if ch < chMax && i < longestWord {
if scanBuffer[i] == ch {
if i < previousLength {
i++
continue
}
} else {
scanBuffer[i] = ch
}

i++

if previousLength != -1 {
previousLength = -1
p = nodeCache[i-1]
}

q := &Node{}

p.children[ch] = q
nodeCache[i] = q
p.hasChildren = true

p = q
} else if i > 0 {
tree.length++
p.word = true

indexed++
previousLength = i
i = 0
}
}
return
}

func (tree *Tree) Root() *Node {
return tree.root
return tree.root
}

func (tree *Tree) Add(word string, data int32) {
current := tree.root
for i := 0; i < len(word); i++ {
next := current.Next(word[i])
if next == nil {
next = &Node{}
current.children[word[i] - chOffset] = next
current.hasChildren = true
}
current = next;
}
current.data = data
if !current.word {
current.word = true
tree.length++
}
current := tree.root
for i := 0; i < len(word); i++ {
next := current.Next(word[i])
if next == nil {
next = &Node{}
current.children[word[i]-chOffset] = next
current.hasChildren = true
}
current = next
}
current.data = data
if !current.word {
current.word = true
tree.length++
}
}

func (tree *Tree) Remove(word string) {
current := tree.root
for i := 0; i < len(word); i++ {
next := current.Next(word[i])
if next == nil {
break
}
if i == len(word) - 1 {
if next.word {
tree.length--
}
if next.hasChildren {
next.data = 0
next.word = false
} else {
current.children[word[i] - chOffset] = nil
// TODO: update hasChildren
// TODO: update higher nodes
}
break
}
current = next;
}
current := tree.root
for i := 0; i < len(word); i++ {
next := current.Next(word[i])
if next == nil {
break
}
if i == len(word)-1 {
if next.word {
tree.length--
}
if next.hasChildren {
next.data = 0
next.word = false
} else {
current.children[word[i]-chOffset] = nil
// TODO: update hasChildren
// TODO: update higher nodes
}
break
}
current = next
}
}

func (tree *Tree) get(word string) (node *Node) {
current := tree.root
for i := 0; i < len(word); i++ {
current = current.Next(word[i])
if current == nil {
return
}
}
return current
current := tree.root
for i := 0; i < len(word); i++ {
current = current.Next(word[i])
if current == nil {
return
}
}
return current
}

func (tree *Tree) Get(word string) (data int32) {
node := tree.get(word)
if node != nil && node.word {
data = node.data
}
return
node := tree.get(word)
if node != nil && node.word {
data = node.data
}
return
}

func (tree *Tree) Contains(word string) (contains bool) {
node := tree.get(word)
return node != nil && node.word
node := tree.get(word)
return node != nil && node.word
}

// Preorder
func (tree *Tree) Traverse(callback func(string, int32)) {
tree.root.traverse(&[longestWord]byte{}, 0, callback)
tree.root.traverse(&[longestWord]byte{}, 0, callback)
}

func (tree *Tree) Len() (length int) {
return tree.length
return tree.length
}

0 comments on commit 79bea4e

Please sign in to comment.