glob/match/btree.go

142 lines
2.8 KiB
Go
Raw Normal View History

2016-01-08 20:14:31 +03:00
package match
import (
"fmt"
2016-01-09 02:34:41 +03:00
"unicode/utf8"
2016-01-08 20:14:31 +03:00
)
type BTree struct {
2016-01-14 21:32:02 +03:00
Value Matcher
Left Matcher
Right Matcher
ValueLengthRunes int
LeftLengthRunes int
RightLengthRunes int
LengthRunes int
2016-01-08 20:14:31 +03:00
}
2016-01-14 18:29:13 +03:00
func NewBTree(Value, Left, Right Matcher) (tree BTree) {
tree.Value = Value
tree.Left = Left
tree.Right = Right
2016-01-08 20:14:31 +03:00
2016-01-14 18:29:13 +03:00
lenOk := true
2016-01-14 21:32:02 +03:00
if tree.ValueLengthRunes = Value.Len(); tree.ValueLengthRunes == -1 {
2016-01-14 18:29:13 +03:00
lenOk = false
}
if Left != nil {
2016-01-14 21:32:02 +03:00
if tree.LeftLengthRunes = Left.Len(); tree.LeftLengthRunes == -1 {
2016-01-14 18:29:13 +03:00
lenOk = false
}
}
2016-01-09 02:34:41 +03:00
2016-01-14 18:29:13 +03:00
if Right != nil {
2016-01-14 21:32:02 +03:00
if tree.RightLengthRunes = Right.Len(); tree.RightLengthRunes == -1 {
2016-01-14 18:29:13 +03:00
lenOk = false
}
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
if lenOk {
2016-01-14 21:32:02 +03:00
tree.LengthRunes = tree.LeftLengthRunes + tree.ValueLengthRunes + tree.RightLengthRunes
2016-01-14 18:29:13 +03:00
} else {
2016-01-14 21:32:02 +03:00
tree.LengthRunes = -1
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
return tree
}
2016-01-09 02:34:41 +03:00
func (self BTree) Len() int {
2016-01-14 21:32:02 +03:00
return self.LengthRunes
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
// todo?
2016-02-02 22:03:37 +03:00
func (self BTree) Index(s string, segments []int) (int, []int) {
2016-01-12 14:06:59 +03:00
return -1, nil
}
2016-01-08 20:14:31 +03:00
func (self BTree) Match(s string) bool {
2016-01-09 02:34:41 +03:00
inputLen := len(s)
2016-01-14 21:32:02 +03:00
// self.Length, self.RLen and self.LLen are values meaning the length of runes for each part
// here we manipulating byte length for better optimizations
// but these checks still works, cause minLen of 1-rune string is 1 byte.
if self.LengthRunes != -1 && self.LengthRunes > inputLen {
2016-01-09 02:34:41 +03:00
return false
}
2016-01-08 20:14:31 +03:00
2016-01-14 21:32:02 +03:00
// try to cut unnecessary parts
// by knowledge of length of right and left part
2016-01-09 02:34:41 +03:00
var offset, limit int
2016-01-14 21:32:02 +03:00
if self.LeftLengthRunes >= 0 {
offset = self.LeftLengthRunes
2016-01-09 02:34:41 +03:00
}
2016-01-14 21:32:02 +03:00
if self.RightLengthRunes >= 0 {
limit = inputLen - self.RightLengthRunes
2016-01-09 02:34:41 +03:00
} else {
limit = inputLen
}
2016-01-08 20:14:31 +03:00
2016-02-22 18:47:01 +03:00
if offset >= limit {
return false
}
2016-02-05 16:57:42 +03:00
// reusable segments list
// inputLen is the maximum size of output segments values
2016-02-22 23:34:16 +03:00
segments := acquireSegments(inputLen)
defer func() {
releaseSegments(segments)
}()
// segments := make([]int, 0, 4)
2016-02-02 22:34:10 +03:00
2016-01-09 02:34:41 +03:00
for offset < limit {
2016-01-14 21:32:02 +03:00
// search for matching part in substring
2016-02-22 23:34:16 +03:00
index, segments := self.Value.Index(s[offset:limit], segments[:0])
2016-01-08 20:14:31 +03:00
if index == -1 {
return false
}
2016-01-14 21:32:02 +03:00
l := s[:offset+index]
2016-01-09 02:34:41 +03:00
var left bool
if self.Left != nil {
left = self.Left.Match(l)
} else {
left = l == ""
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
if left {
for i := len(segments) - 1; i >= 0; i-- {
length := segments[i]
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
var right bool
var r string
// if there is no string for the right branch
if inputLen <= offset+index+length {
r = ""
} else {
r = s[offset+index+length:]
}
if self.Right != nil {
right = self.Right.Match(r)
} else {
right = r == ""
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
if right {
return true
}
2016-01-08 20:14:31 +03:00
}
}
2016-01-09 02:34:41 +03:00
_, step := utf8.DecodeRuneInString(s[offset+index:])
offset += index + step
2016-01-08 20:14:31 +03:00
}
return false
}
func (self BTree) String() string {
2016-01-13 01:26:48 +03:00
return fmt.Sprintf("<btree:[%s<-%s->%s]>", self.Left, self.Value, self.Right)
2016-01-08 20:14:31 +03:00
}