Commit b0964cc3 authored by Eric Myhre's avatar Eric Myhre

Add more explicit discussion of indicies to ListIterator.

Per https://github.com/ipld/go-ipld-prime/issues/175 and some discussions today.

Also touched up the docs for Next vs Done a bit while I was in the area;
they're not significantly changed, but there were some dangling antecedents
that didn't read very well, and now it's a bit better.
parent 806e36fb
...@@ -114,13 +114,16 @@ type Node interface { ...@@ -114,13 +114,16 @@ type Node interface {
// before itr.Done becomes true. // before itr.Done becomes true.
MapIterator() MapIterator MapIterator() MapIterator
// ListIterator returns an iterator which yields key-value pairs // ListIterator returns an iterator which traverses the node and yields indicies and list entries.
// traversing the node.
// If the node kind is anything other than a list, nil will be returned. // If the node kind is anything other than a list, nil will be returned.
// //
// The iterator will yield every entry in the list; that is, it // The iterator will yield every entry in the list; that is, it
// can be expected that itr.Next will be called node.Length times // can be expected that itr.Next will be called node.Length times
// before itr.Done becomes true. // before itr.Done becomes true.
//
// List iteration is ordered, and indices yielded during iteration will range from 0 to Node.Length-1.
// (The IPLD Data Model definition of lists only defines that it is an ordered list of elements;
// the definition does not include a concept of sparseness, so the indices are always sequential.)
ListIterator() ListIterator ListIterator() ListIterator
// Length returns the length of a list, or the number of entries in a map, // Length returns the length of a list, or the number of entries in a map,
...@@ -236,8 +239,10 @@ type MapIterator interface { ...@@ -236,8 +239,10 @@ type MapIterator interface {
// An error value can also be returned at any step: in the case of advanced // An error value can also be returned at any step: in the case of advanced
// data structures with incremental loading, it's possible to encounter // data structures with incremental loading, it's possible to encounter
// cancellation or I/O errors at any point in iteration. // cancellation or I/O errors at any point in iteration.
// If an error is returned, the boolean will always be false (so it's // If an error will be returned by the next call to Next,
// correct to check the bool first and short circuit to continuing if true). // then the boolean returned by the Done method will be false
// (meaning it's acceptable to check Done first and move on if it's true,
// since that both means the iterator is complete and that there is no error).
// If an error is returned, the key and value may be nil. // If an error is returned, the key and value may be nil.
Next() (key Node, value Node, err error) Next() (key Node, value Node, err error)
...@@ -256,16 +261,20 @@ type MapIterator interface { ...@@ -256,16 +261,20 @@ type MapIterator interface {
// Sequential calls to Next() will yield index-value pairs; // Sequential calls to Next() will yield index-value pairs;
// Done() describes whether iteration should continue. // Done() describes whether iteration should continue.
// //
// A loop which iterates from 0 to Node.Length is a valid // ListIterator's Next method returns an index for convenience,
// alternative to using a ListIterator. // but this number will always start at 0 and increment by 1 monotonically.
// A loop which iterates from 0 to Node.Length while calling Node.LookupByIndex
// is equivalent to using a ListIterator.
type ListIterator interface { type ListIterator interface {
// Next returns the next index and value. // Next returns the next index and value.
// //
// An error value can also be returned at any step: in the case of advanced // An error value can also be returned at any step: in the case of advanced
// data structures with incremental loading, it's possible to encounter // data structures with incremental loading, it's possible to encounter
// cancellation or I/O errors at any point in iteration. // cancellation or I/O errors at any point in iteration.
// If an error is returned, the boolean will always be false (so it's // If an error will be returned by the next call to Next,
// correct to check the bool first and short circuit to continuing if true). // then the boolean returned by the Done method will be false
// (meaning it's acceptable to check Done first and move on if it's true,
// since that both means the iterator is complete and that there is no error).
// If an error is returned, the key and value may be nil. // If an error is returned, the key and value may be nil.
Next() (idx int64, value Node, err error) Next() (idx int64, value Node, err error)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment