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

@ -77,10 +77,14 @@ type Iterator interface {
// the iteration interface.
//
// To get the full results of iteration, do the following:
// while (!Next()):
// emit result
// while (!NextPath()):
// emit result
//
// for graph.Next(it) {
// val := it.Result()
// ... do things with val.
// for it.NextPath() {
// ... find other paths to iterate
// }
// }
//
// All of them should set iterator.Last to be the last returned value, to
// make results work.
@ -135,22 +139,22 @@ type Iterator interface {
}
type Nexter interface {
// Next() advances the iterator and returns the next valid result. Returns
// (<value>, true) or (nil, false)
Next() (Value, bool)
// Next advances the iterator to the next value, which will then be available through
// the Result method. It returns false if no further advancement is possible.
Next() bool
Iterator
}
// Next is a convenience function that conditionally calls the Next method
// of an Iterator if it is a Nexter. If the Iterator is not a Nexter, Next
// return a nil Value and false.
func Next(it Iterator) (Value, bool) {
// returns false.
func Next(it Iterator) bool {
if n, ok := it.(Nexter); ok {
return n.Next()
}
glog.Errorln("Nexting an un-nextable iterator")
return nil, false
return false
}
// FixedIterator wraps iterators that are modifiable by addition of fixed value sets.
@ -253,7 +257,7 @@ func NextLogIn(it Iterator) {
}
}
func NextLogOut(it Iterator, val Value, ok bool) (Value, bool) {
func NextLogOut(it Iterator, val Value, ok bool) bool {
if glog.V(4) {
if ok {
glog.V(4).Infof("%s %d NEXT IS %d", strings.ToUpper(it.Type().String()), it.UID(), val)
@ -261,5 +265,5 @@ func NextLogOut(it Iterator, val Value, ok bool) (Value, bool) {
glog.V(4).Infof("%s %d NEXT DONE", strings.ToUpper(it.Type().String()), it.UID())
}
}
return val, ok
return ok
}