Remove unnecessary goroutine from iterator, cleaning up more easily.
This commit is contained in:
parent
bbb0a2f580
commit
abf6cd27ab
1 changed files with 15 additions and 47 deletions
|
|
@ -25,10 +25,9 @@ import (
|
||||||
type LlrbIterator struct {
|
type LlrbIterator struct {
|
||||||
graph.BaseIterator
|
graph.BaseIterator
|
||||||
tree *llrb.LLRB
|
tree *llrb.LLRB
|
||||||
values chan llrb.Item
|
|
||||||
another chan bool
|
|
||||||
data string
|
data string
|
||||||
isRunning bool
|
isRunning bool
|
||||||
|
iterLast Int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type Int64 int64
|
type Int64 int64
|
||||||
|
|
@ -37,41 +36,30 @@ func (i Int64) Less(than llrb.Item) bool {
|
||||||
return i < than.(Int64)
|
return i < than.(Int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IterateAll(tree *llrb.LLRB, c chan llrb.Item, another chan bool) {
|
func IterateOne(tree *llrb.LLRB, last Int64) Int64 {
|
||||||
tree.AscendGreaterOrEqual(Int64(-1), func(i llrb.Item) bool {
|
var next Int64
|
||||||
want_more := <-another
|
tree.AscendGreaterOrEqual(last, func(i llrb.Item) bool {
|
||||||
if want_more {
|
if i.(Int64) == last {
|
||||||
c <- i
|
|
||||||
return true
|
return true
|
||||||
|
} else {
|
||||||
|
next = i.(Int64)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
})
|
})
|
||||||
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
|
func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
|
||||||
var it LlrbIterator
|
var it LlrbIterator
|
||||||
graph.BaseIteratorInit(&it.BaseIterator)
|
graph.BaseIteratorInit(&it.BaseIterator)
|
||||||
it.tree = tree
|
it.tree = tree
|
||||||
it.isRunning = false
|
it.iterLast = Int64(-1)
|
||||||
it.values = make(chan llrb.Item)
|
|
||||||
it.another = make(chan bool, 1)
|
|
||||||
it.data = data
|
it.data = data
|
||||||
return &it
|
return &it
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *LlrbIterator) Reset() {
|
func (it *LlrbIterator) Reset() {
|
||||||
if it.another != nil {
|
it.iterLast = Int64(-1)
|
||||||
it.another <- false
|
|
||||||
close(it.another)
|
|
||||||
}
|
|
||||||
it.another = nil
|
|
||||||
if it.values != nil {
|
|
||||||
close(it.values)
|
|
||||||
}
|
|
||||||
it.values = nil
|
|
||||||
it.isRunning = false
|
|
||||||
it.another = make(chan bool)
|
|
||||||
it.values = make(chan llrb.Item)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *LlrbIterator) Clone() graph.Iterator {
|
func (it *LlrbIterator) Clone() graph.Iterator {
|
||||||
|
|
@ -80,35 +68,15 @@ func (it *LlrbIterator) Clone() graph.Iterator {
|
||||||
return new_it
|
return new_it
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *LlrbIterator) Close() {
|
func (it *LlrbIterator) Close() {}
|
||||||
if it.another != nil {
|
|
||||||
it.another <- false
|
|
||||||
close(it.another)
|
|
||||||
}
|
|
||||||
it.another = nil
|
|
||||||
if it.values != nil {
|
|
||||||
close(it.values)
|
|
||||||
}
|
|
||||||
it.values = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (it *LlrbIterator) Next() (graph.TSVal, bool) {
|
func (it *LlrbIterator) Next() (graph.TSVal, bool) {
|
||||||
graph.NextLogIn(it)
|
graph.NextLogIn(it)
|
||||||
// Little hack here..
|
if it.tree.Max() == nil || it.Last == int64(it.tree.Max().(Int64)) {
|
||||||
if !it.isRunning {
|
|
||||||
go IterateAll(it.tree, it.values, it.another)
|
|
||||||
it.isRunning = true
|
|
||||||
}
|
|
||||||
last := int64(0)
|
|
||||||
if it.Last != nil {
|
|
||||||
last = it.Last.(int64)
|
|
||||||
}
|
|
||||||
if it.tree.Max() == nil || last == int64(it.tree.Max().(Int64)) {
|
|
||||||
return graph.NextLogOut(it, nil, false)
|
return graph.NextLogOut(it, nil, false)
|
||||||
}
|
}
|
||||||
it.another <- true
|
it.iterLast = IterateOne(it.tree, it.iterLast)
|
||||||
val := <-it.values
|
it.Last = int64(it.iterLast)
|
||||||
it.Last = int64(val.(Int64))
|
|
||||||
return graph.NextLogOut(it, it.Last, true)
|
return graph.NextLogOut(it, it.Last, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue