Simplify Nexter interface

This change allows a Nexter to be used in the same manner as a scanner
using a for graph.Next(it) {} construction.

It is important that graph.Next(it) and any associated it.Result() calls
operate on the same iterator.
This commit is contained in:
kortschak 2014-08-01 09:15:02 +09:30
parent f8e28e066e
commit b1a70d99aa
31 changed files with 168 additions and 233 deletions

View file

@ -141,35 +141,34 @@ func (it *Or) AddSubIterator(sub graph.Iterator) {
it.itCount++
}
// Returns the Next value from the Or graph.iterator. Because the Or is the
// union of its subiterators, it must produce from all subiterators -- unless
// it's shortcircuiting, in which case, it's the first one that returns anything.
func (it *Or) Next() (graph.Value, bool) {
// Next advances the Or graph.iterator. Because the Or is the union of its
// subiterators, it must produce from all subiterators -- unless it it
// shortcircuiting, in which case, it is the first one that returns anything.
func (it *Or) Next() bool {
graph.NextLogIn(it)
var curr graph.Value
var exists bool
firstTime := false
var first bool
for {
if it.currentIterator == -1 {
it.currentIterator = 0
firstTime = true
first = true
}
curIt := it.internalIterators[it.currentIterator]
curr, exists = graph.Next(curIt)
if !exists {
if it.isShortCircuiting && !firstTime {
return graph.NextLogOut(it, nil, false)
}
it.currentIterator++
if it.currentIterator == it.itCount {
return graph.NextLogOut(it, nil, false)
}
} else {
it.result = curr
return graph.NextLogOut(it, curr, true)
if graph.Next(curIt) {
it.result = curIt.Result()
return graph.NextLogOut(it, it.result, true)
}
if it.isShortCircuiting && !first {
break
}
it.currentIterator++
if it.currentIterator == it.itCount {
break
}
}
panic("unreachable")
return graph.NextLogOut(it, nil, false)
}
func (it *Or) Result() graph.Value {