treerack/store.go

84 lines
1.2 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 storeEntry struct {
2017-07-15 23:00:43 +02:00
match *idSet
noMatch *idSet
nodes []*Node
2017-06-25 17:51:08 +02:00
}
2017-06-26 02:20:23 +02:00
type store struct {
2017-07-15 23:00:43 +02:00
entries []*storeEntry
2017-06-25 17:51:08 +02:00
}
2017-07-15 23:00:43 +02:00
func (c *store) get(offset int, id int) (*Node, bool, bool) {
if len(c.entries) <= offset {
2017-06-25 17:51:08 +02:00
return nil, false, false
}
2017-07-15 23:00:43 +02:00
tc := c.entries[offset]
2017-06-25 17:51:08 +02:00
if tc == nil {
return nil, false, false
}
2017-07-15 23:00:43 +02:00
if tc.noMatch.has(id) {
return nil, false, true
2017-06-25 17:51:08 +02:00
}
2017-07-15 23:00:43 +02:00
if !tc.match.has(id) {
return nil, false, false
}
for _, n := range tc.nodes {
if n.id == id {
return n, true, true
2017-06-25 17:51:08 +02:00
}
}
return nil, false, false
}
2017-07-15 23:00:43 +02:00
func (c *store) set(offset int, id int, n *Node) {
2017-06-26 02:20:23 +02:00
var tc *storeEntry
2017-07-15 23:00:43 +02:00
if len(c.entries) > offset {
tc = c.entries[offset]
2017-06-26 01:21:46 +02:00
} else {
2017-07-15 23:00:43 +02:00
if cap(c.entries) > offset {
c.entries = c.entries[:offset+1]
2017-06-25 17:51:08 +02:00
} else {
2017-07-15 23:00:43 +02:00
c.entries = c.entries[:cap(c.entries)]
for len(c.entries) <= offset {
c.entries = append(c.entries, nil)
2017-06-25 17:51:08 +02:00
}
}
2017-07-15 23:00:43 +02:00
tc = &storeEntry{
match: &idSet{},
noMatch: &idSet{},
}
c.entries[offset] = tc
2017-06-25 17:51:08 +02:00
}
if n == nil {
2017-07-15 23:00:43 +02:00
if tc.match.has(id) {
return
2017-06-25 17:51:08 +02:00
}
2017-07-15 23:00:43 +02:00
tc.noMatch.set(id)
2017-06-25 17:51:08 +02:00
return
}
2017-07-15 23:00:43 +02:00
tc.match.set(id)
for i, ni := range tc.nodes {
if ni.id == id {
if n.tokenLength() > ni.tokenLength() {
tc.nodes[i] = n
2017-06-25 17:51:08 +02:00
}
return
}
}
2017-07-15 23:00:43 +02:00
tc.nodes = append(tc.nodes, n)
2017-06-25 17:51:08 +02:00
}