Permalink
Browse files

fix: pointers/types

  • Loading branch information...
1 parent 3d817be commit 05f56ffe0c1b6d0490e4294c26ae5b7c436d2e1a egon committed Apr 16, 2012
@@ -2,13 +2,13 @@ package trie
import "spexs"
-func output(out Patterns, patterns map[Char]Pattern) {
+func output(out Patterns, patterns map[Char]*Pattern) {
for _, node := range patterns {
out <- node
}
}
-func simpleExtend(node Pattern, ref Reference, patterns map[Char]Pattern) {
+func simpleExtend(node *Pattern, ref *Reference, patterns map[Char]*Pattern) {
for idx, mpos := range node.Pos.Iter() {
plen := byte(len(ref.Pats[idx].Pat))
@@ -26,17 +26,17 @@ func simpleExtend(node Pattern, ref Reference, patterns map[Char]Pattern) {
pat, exists := patterns[char]
if !exists {
- pat = NewNode(char, node)
+ pat = NewPattern(char, node)
patterns[char] = pat
}
pat.Pos.Add(idx, next)
}
}
}
-func SimpleExtender(node Pattern, ref Reference) Patterns {
+func SimpleExtender(node *Pattern, ref *Reference) Patterns {
result := NewPatterns()
- patterns := make(map[Char]Pattern)
+ patterns := make(map[Char]*Pattern)
simpleExtend(node, ref, patterns)
@@ -45,9 +45,9 @@ func SimpleExtender(node Pattern, ref Reference) Patterns {
return result
}
-func groupCombine(node Pattern, ref Reference, patterns map[Char]Pattern, star bool) {
+func groupCombine(node *Pattern, ref *Reference, patterns map[Char]*Pattern, star bool) {
for _, g := range ref.Groups {
- pat := NewNode(g.Id, node)
+ pat := NewPattern(g.Id, node)
pat.IsGroup = true
pat.IsStar = star
patterns[g.Id] = pat
@@ -59,9 +59,9 @@ func groupCombine(node Pattern, ref Reference, patterns map[Char]Pattern, star b
}
}
-func GroupExtender(node Pattern, ref Reference) Patterns {
+func GroupExtender(node *Pattern, ref *Reference) Patterns {
result := NewPatterns()
- patterns := make(map[Char]Pattern)
+ patterns := make(map[Char]*Pattern)
simpleExtend(node, ref, patterns)
groupCombine(node, ref, patterns, false)
@@ -71,7 +71,7 @@ func GroupExtender(node Pattern, ref Reference) Patterns {
return result
}
-func trieStarExtend(node Pattern, ref Reference, stars map[Char]Pattern) {
+func trieStarExtend(node *Pattern, ref *Reference, stars map[Char]*Pattern) {
for idx, mpos := range node.Pos.Iter() {
plen := byte(len(ref.Pats[idx].Pat))
if mpos == 0 {
@@ -89,7 +89,7 @@ func trieStarExtend(node Pattern, ref Reference, stars map[Char]Pattern) {
for valid {
pat, exists := stars[char]
if !exists {
- pat = NewNode(char, node)
+ pat = NewPattern(char, node)
pat.IsStar = true
stars[char] = pat
}
@@ -99,10 +99,10 @@ func trieStarExtend(node Pattern, ref Reference, stars map[Char]Pattern) {
}
}
-func StarExtender(node Pattern, ref Reference) Patterns {
+func StarExtender(node *Pattern, ref *Reference) Patterns {
result := NewPatterns()
- patterns := make(map[Char]Pattern)
- stars := make(map[Char]Pattern)
+ patterns := make(map[Char]*Pattern)
+ stars := make(map[Char]*Pattern)
simpleExtend(node, ref, patterns)
trieStarExtend(node, ref, stars)
@@ -113,10 +113,10 @@ func StarExtender(node Pattern, ref Reference) Patterns {
return result
}
-func GroupStarExtender(node Pattern, ref Reference) Patterns {
+func GroupStarExtender(node *Pattern, ref *Reference) Patterns {
result := NewPatterns()
- patterns := make(map[Char]Pattern)
- stars := make(map[Char]Pattern)
+ patterns := make(map[Char]*Pattern)
+ stars := make(map[Char]*Pattern)
simpleExtend(node, ref, patterns)
groupCombine(node, ref, patterns, false)
@@ -1,11 +1,11 @@
package trie
type FullSet struct {
- Ref Reference
+ Ref *Reference
Count int
}
-func NewFullSet(ref Reference) *FullSet {
+func NewFullSet(ref *Reference) *FullSet {
f := &FullSet{ref, 0}
f.Count = len(ref.Pats)
return f
@@ -2,12 +2,4 @@ package trie
// this specialises spexs.go implementation
-type Pattern *struct {
- *TrieNode
-}
-
-type Reference *struct {
- *UnicodeReference
-}
-
type Char rune
View
@@ -18,7 +18,7 @@ func NewFifoPool() *FifoPool {
return p
}
-func (p *FifoPool) Take() (Pattern, bool) {
+func (p *FifoPool) Take() (*Pattern, bool) {
<-p.token
if p.list.Len() == 0 {
p.token <- 1
@@ -27,10 +27,10 @@ func (p *FifoPool) Take() (Pattern, bool) {
tmp := p.list.Front()
p.list.Remove(tmp)
p.token <- 1
- return tmp.Value.(Pattern), true
+ return tmp.Value.(*Pattern), true
}
-func (p *FifoPool) Put(pat Pattern) {
+func (p *FifoPool) Put(pat *Pattern) {
<-p.token
p.list.PushBack(pat)
p.token <- 1
@@ -42,15 +42,15 @@ func (p *FifoPool) Len() int {
type PriorityPool struct {
token chan int
- items []Pattern
+ items []*Pattern
Fitness FitnessFunc
limit int
}
func NewPriorityPool(fitness FitnessFunc, limit int) *PriorityPool {
p := &PriorityPool{}
p.token = make(chan int, 1)
- p.items = make([]Pattern, 0)
+ p.items = make([]*Pattern, 0)
p.limit = limit
p.Fitness = fitness
p.token <- 1
@@ -63,18 +63,18 @@ func (p *PriorityPool) IsEmpty() bool {
return len(p.items) == 0
}
-func (p *PriorityPool) Take() (Pattern, bool) {
+func (p *PriorityPool) Take() (*Pattern, bool) {
<-p.token
if p.IsEmpty() {
p.token <- 1
return nil, false
}
v := heap.Pop(p)
p.token <- 1
- return v.(Pattern), true
+ return v.(*Pattern), true
}
-func (p *PriorityPool) Put(pat Pattern) {
+func (p *PriorityPool) Put(pat *Pattern) {
<-p.token
heap.Push(p, pat)
if p.Len() > p.limit {
@@ -100,7 +100,7 @@ func (p *PriorityPool) Less(i, j int) bool {
// heap.Interface
func (p *PriorityPool) Push(x interface{}) {
- p.items = append(p.items, x.(Pattern))
+ p.items = append(p.items, x.(*Pattern))
}
func (p *PriorityPool) Pop() interface{} {
@@ -2,18 +2,6 @@ package trie
import "testing"
-type string_pattern struct {
- v string
-}
-
-func newString(v string) *string_pattern {
- return &string_pattern{v}
-}
-
-func (p *string_pattern) String() string {
- return p.v
-}
-
func testTake(t *testing.T, p Pooler, expected string, expectedOk bool) {
val, ok := p.Take()
if ok != expectedOk {
@@ -27,14 +15,14 @@ func testTake(t *testing.T, p Pooler, expected string, expectedOk bool) {
func TestFifoPool(t *testing.T) {
p := NewFifoPool()
- p.Put(newString("alpha"))
- p.Put(newString("beta"))
+ p.Put(pat("alpha"))
+ p.Put(pat("beta"))
testTake(t, p, "alpha", true)
testTake(t, p, "beta", true)
- p.Put(newString("gamma"))
- p.Put(newString("delta"))
+ p.Put(pat("gamma"))
+ p.Put(pat("delta"))
testTake(t, p, "gamma", true)
testTake(t, p, "delta", true)
@@ -43,21 +31,21 @@ func TestFifoPool(t *testing.T) {
}
func TestPriorityPool(t *testing.T) {
- lenFitness := func(p Pattern) float32 {
- return float32(len(p.String()))
+ lenFitness := func(p *Pattern) float64 {
+ return float64(len(p.String()))
}
p := NewPriorityPool(lenFitness, 100)
- p.Put(newString("bc"))
- p.Put(newString("defg"))
- p.Put(newString("a"))
- p.Put(newString("def"))
+ p.Put(pat("bc"))
+ p.Put(pat("defg"))
+ p.Put(pat("a"))
+ p.Put(pat("def"))
testTake(t, p, "a", true)
testTake(t, p, "bc", true)
- p.Put(newString("x"))
- p.Put(newString("defgh"))
+ p.Put(pat("x"))
+ p.Put(pat("defgh"))
testTake(t, p, "x", true)
testTake(t, p, "def", true)
@@ -21,23 +21,23 @@ type ReferencePattern struct {
Group int // validation or reference group
}
-type UnicodeReference struct {
+type Reference struct {
Pats []ReferencePattern
Alphabet []Char
Groups map[Char]Group
Groupings []int
}
-func NewUnicodeReference(size int) *UnicodeReference {
- ref := &UnicodeReference{}
+func NewReference(size int) *Reference {
+ ref := &Reference{}
ref.Pats = make([]ReferencePattern, 0, size)
ref.Alphabet = make([]Char, 0, 8)
ref.Groups = make(map[Char]Group)
ref.Groupings = make([]int, 2) // fix use multiple
return ref
}
-func (ref *UnicodeReference) Next(idx int, pos byte) (Char, byte, bool) {
+func (ref *Reference) Next(idx int, pos byte) (Char, byte, bool) {
if int(pos) >= len(ref.Pats[idx].Pat) {
return 0, 0, false
}
@@ -46,7 +46,7 @@ func (ref *UnicodeReference) Next(idx int, pos byte) (Char, byte, bool) {
return Char(rune), byte(pos + byte(width)), true
}
-func (ref *UnicodeReference) ReplaceGroups(pat string) string {
+func (ref *Reference) ReplaceGroups(pat string) string {
buf := bytes.NewBufferString("")
for _, c := range pat {
if grp, exists := ref.Groups[Char(c)]; exists {
@@ -58,11 +58,11 @@ func (ref *UnicodeReference) ReplaceGroups(pat string) string {
return string(buf.Bytes())
}
-func (ref *UnicodeReference) AddGroup(group Group) {
+func (ref *Reference) AddGroup(group Group) {
ref.Groups[group.Id] = group
}
-func (ref *UnicodeReference) AddPattern(pat ReferencePattern) {
+func (ref *Reference) AddPattern(pat ReferencePattern) {
ref.Pats = append(ref.Pats, pat)
ref.Groupings[pat.Group] += 1
}
@@ -19,39 +19,38 @@ func pattern(data string) ReferencePattern {
b := bytes.NewBufferString(data)
p.Pat = b.Bytes()
p.Count = utf8.RuneCount(p.Pat)
+ p.Group = 0
return p
}
-func createTestUnicodeReference() *UnicodeReference {
- u := &UnicodeReference{}
+func createTestReference() *Reference {
+ u := NewReference(10)
u.Alphabet = chars("ACGT")
- u.Groups = make([]Group, 6)
- u.Groups[0] = *NewGroup("[AC]", '1', chars("AC"))
- u.Groups[1] = *NewGroup("[AG]", '2', chars("AG"))
- u.Groups[2] = *NewGroup("[AT]", '3', chars("AT"))
- u.Groups[3] = *NewGroup("[CG]", '4', chars("CG"))
- u.Groups[4] = *NewGroup("[CT]", '5', chars("CT"))
- u.Groups[5] = *NewGroup("[GT]", '6', chars("GT"))
+ u.AddGroup(*NewGroup("[AC]", '1', chars("AC")))
+ u.AddGroup(*NewGroup("[AG]", '2', chars("AG")))
+ u.AddGroup(*NewGroup("[AT]", '3', chars("AT")))
+ u.AddGroup(*NewGroup("[CG]", '4', chars("CG")))
+ u.AddGroup(*NewGroup("[CT]", '5', chars("CT")))
+ u.AddGroup(*NewGroup("[GT]", '6', chars("GT")))
- u.Pats = make([]ReferencePattern, 4)
- u.Pats[0] = pattern("ACGTACGG")
- u.Pats[1] = pattern("CAGTCCG")
- u.Pats[2] = pattern("ACGGCTA")
- u.Pats[3] = pattern("GGTCAACTG")
+ u.AddPattern(pattern("ACGTACGG"))
+ u.AddPattern(pattern("CAGTCCG"))
+ u.AddPattern(pattern("ACGGCTA"))
+ u.AddPattern(pattern("GGTCAACTG"))
return u
}
-func TestUnicodeReferenceNext(t *testing.T) {
- u := createTestUnicodeReference()
+func TestReferenceNext(t *testing.T) {
+ u := createTestReference()
testStr := func(idx int, str string) {
- p := PosEncode(idx, 0)
var x Char
var ok bool
+ idx, pos := idx, byte(0)
for _, c := range str {
- x, p, ok = u.Next(p)
+ x, pos, ok = u.Next(idx, pos)
if !ok {
t.Errorf("string '%s' ended too early", str)
}
Oops, something went wrong.

0 comments on commit 05f56ff

Please sign in to comment.