treerack/store.go

92 lines
1.4 KiB
Go
Raw Normal View History

2017-07-15 21:49:08 +02:00
package treerack
2017-06-25 17:51:08 +02:00
2017-06-26 02:20:23 +02:00
type storedItem struct {
2017-06-25 17:51:08 +02:00
name string
node *Node
}
2017-06-26 02:20:23 +02:00
type storeEntry struct {
match []*storedItem
2017-06-25 17:51:08 +02:00
noMatch []string
}
2017-06-26 02:20:23 +02:00
type store struct {
tokens []*storeEntry
2017-06-25 17:51:08 +02:00
}
2017-06-26 02:20:23 +02:00
func (c *store) get(offset int, name string) (*Node, bool, bool) {
2017-06-25 17:51:08 +02:00
if len(c.tokens) <= offset {
return nil, false, false
}
tc := c.tokens[offset]
if tc == nil {
return nil, false, false
}
for _, i := range tc.noMatch {
if i == name {
return nil, false, true
}
}
for _, i := range tc.match {
if i.name == name {
return i.node, true, true
}
}
return nil, false, false
}
2017-06-26 02:20:23 +02:00
func (c *store) set(offset int, name string, n *Node) {
var tc *storeEntry
2017-06-26 01:21:46 +02:00
if len(c.tokens) > offset {
tc = c.tokens[offset]
} else {
2017-06-25 17:51:08 +02:00
if cap(c.tokens) > offset {
c.tokens = c.tokens[:offset+1]
} else {
c.tokens = c.tokens[:cap(c.tokens)]
for len(c.tokens) <= offset {
c.tokens = append(c.tokens, nil)
}
}
2017-06-26 02:20:23 +02:00
tc = &storeEntry{}
2017-06-25 17:51:08 +02:00
c.tokens[offset] = tc
}
if n == nil {
for _, i := range tc.match {
if i.name == name {
return
}
}
for _, i := range tc.noMatch {
if i == name {
return
}
}
tc.noMatch = append(tc.noMatch, name)
return
}
for _, i := range tc.match {
if i.name == name {
if n.tokenLength() > i.node.tokenLength() {
i.node = n
}
return
}
}
2017-06-26 02:20:23 +02:00
tc.match = append(tc.match, &storedItem{
2017-06-25 17:51:08 +02:00
name: name,
node: n,
})
}