treerack/store.go

83 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 store struct {
2017-07-17 23:59:26 +02:00
noMatch []*idSet
match [][]int
2017-07-17 21:58:03 +02:00
}
func (s *store) hasNoMatch(offset, id int) bool {
2017-07-17 23:59:26 +02:00
if len(s.noMatch) <= offset || s.noMatch[offset] == nil {
2017-07-17 21:58:03 +02:00
return false
2017-06-25 17:51:08 +02:00
}
2017-07-17 23:59:26 +02:00
return s.noMatch[offset].has(id)
2017-07-17 21:58:03 +02:00
}
func (s *store) getMatch(offset, id int) (int, bool, bool) {
2017-07-17 23:59:26 +02:00
if s.hasNoMatch(offset, id) {
2017-07-17 04:23:29 +02:00
return 0, false, true
2017-06-25 17:51:08 +02:00
}
2017-07-17 23:59:26 +02:00
if len(s.match) <= offset {
2017-07-17 04:23:29 +02:00
return 0, false, false
2017-07-15 23:00:43 +02:00
}
2017-07-17 23:59:26 +02:00
var (
found bool
length int
)
for i := 0; i < len(s.match[offset]); i++ {
if s.match[offset][i] != id {
continue
}
found = true
if s.match[offset][i + 1] > length {
length = s.match[offset][i + 1]
2017-06-25 17:51:08 +02:00
}
}
2017-07-17 23:59:26 +02:00
return length, found, found
2017-06-25 17:51:08 +02:00
}
2017-07-17 21:58:03 +02:00
func (s *store) ensureOffset(offset int) {
2017-07-17 23:59:26 +02:00
if len(s.match) > offset {
2017-06-25 17:51:08 +02:00
return
}
2017-07-17 23:59:26 +02:00
if cap(s.match) > offset {
s.match = s.match[:offset+1]
2017-07-17 21:58:03 +02:00
return
2017-07-16 23:15:42 +02:00
}
2017-07-17 23:59:26 +02:00
s.match = s.match[:cap(s.match)]
for i := len(s.match); i <= offset; i++ {
s.match = append(s.match, nil)
2017-07-16 23:15:42 +02:00
}
}
2017-07-17 23:59:26 +02:00
func (s *store) setMatch(offset, id, to int) {
2017-07-17 21:58:03 +02:00
s.ensureOffset(offset)
2017-07-17 23:59:26 +02:00
s.match[offset] = append(s.match[offset], id, to)
2017-07-16 23:15:42 +02:00
}
2017-07-17 23:59:26 +02:00
func (s *store) setNoMatch(offset, id int) {
if len(s.noMatch) <= offset {
if cap(s.noMatch) > offset {
s.noMatch = s.noMatch[:offset + 1]
} else {
s.noMatch = s.noMatch[:cap(s.noMatch)]
for i := len(s.noMatch); i <= offset; i++ {
s.noMatch = append(s.noMatch, nil)
2017-07-17 21:58:03 +02:00
}
2017-07-16 23:15:42 +02:00
}
}
2017-07-17 23:59:26 +02:00
if s.noMatch[offset] == nil {
s.noMatch[offset] = &idSet{}
2017-07-16 23:15:42 +02:00
}
2017-07-17 23:59:26 +02:00
s.noMatch[offset].set(id)
2017-07-16 23:15:42 +02:00
}