treerack/store.go

163 lines
2.8 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) getMatch(offset, id int) (int, bool, bool) {
2017-07-30 02:35:51 +02:00
if len(s.noMatch) > offset && s.noMatch[offset] != nil && s.noMatch[offset].has(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 (
2017-07-29 18:40:22 +02:00
found bool
to int
2017-07-17 23:59:26 +02:00
)
2017-07-26 18:06:11 +02:00
for i := 0; i < len(s.match[offset]); i += 2 {
2017-07-17 23:59:26 +02:00
if s.match[offset][i] != id {
continue
}
found = true
2017-07-29 18:40:22 +02:00
if s.match[offset][i+1] > to {
to = s.match[offset][i+1]
2017-06-25 17:51:08 +02:00
}
}
2017-07-29 18:40:22 +02:00
return to, found, found
}
2017-07-30 02:35:51 +02:00
func (s *store) hasMatchTo(offset, id, to int) bool {
if len(s.noMatch) > offset && s.noMatch[offset] != nil && s.noMatch[offset].has(id) {
return false
}
if len(s.match) <= offset {
return false
}
for i := 0; i < len(s.match[offset]); i += 2 {
if s.match[offset][i] != id {
continue
}
if s.match[offset][i+1] == to {
return true
}
2017-07-29 18:40:22 +02:00
}
2017-07-30 02:35:51 +02:00
return false
}
func (s *store) takeMatch(offset, id int, includedBy *idSet) (int, bool) {
2017-07-29 18:40:22 +02:00
if len(s.match) <= offset {
return 0, false
}
var (
found bool
to int
index int
)
for i := 0; i < len(s.match[offset]); i += 2 {
if s.match[offset][i] != id {
continue
}
found = true
if s.match[offset][i+1] > to {
to = s.match[offset][i+1]
index = i
}
}
if found {
s.match[offset][index] = -1
2017-07-30 02:35:51 +02:00
for i := 0; i < len(s.match[offset]); i += 2 {
if includedBy.has(s.match[offset][i]) && s.match[offset][i+1] == to {
s.match[offset][i] = -1
}
}
2017-07-29 18:40:22 +02:00
}
return to, found
2017-06-25 17:51:08 +02:00
}
2017-07-30 02:35:51 +02:00
func (s *store) takeMatchLength(offset, id, to int) {
2017-07-29 20:04:22 +02:00
if len(s.match) <= offset {
2017-07-30 02:35:51 +02:00
return
2017-07-29 20:04:22 +02:00
}
for i := 0; i < len(s.match[offset]); i += 2 {
2017-07-30 02:35:51 +02:00
if s.match[offset][i] == id && s.match[offset][i+1] == to {
2017-07-29 20:04:22 +02:00
s.match[offset][i] = -1
2017-07-30 02:35:51 +02:00
return
2017-07-29 20:04:22 +02:00
}
}
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)]
2017-07-27 02:18:19 +02:00
for i := len(s.match); i <= offset; i++ {
2017-07-17 23:59:26 +02:00
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-30 02:35:51 +02:00
s.ensureOffset(offset)
for i := 0; i < len(s.match[offset]); i += 2 {
if s.match[offset][i] != id || s.match[offset][i+1] != to {
continue
}
2017-07-29 16:25:17 +02:00
return
}
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) {
2017-07-30 02:35:51 +02:00
if len(s.match) > offset {
for i := 0; i < len(s.match[offset]); i += 2 {
if s.match[offset][i] != id {
continue
}
return
}
}
2017-07-17 23:59:26 +02:00
if len(s.noMatch) <= offset {
if cap(s.noMatch) > offset {
s.noMatch = s.noMatch[:offset+1]
2017-07-17 23:59:26 +02:00
} else {
s.noMatch = s.noMatch[:cap(s.noMatch)]
for i := cap(s.noMatch); i <= offset; i++ {
2017-07-17 23:59:26 +02:00
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
}