github.com/cayleygraph/cayley@v0.7.7/graph/iterator/iterator_test.go (about)

     1  package iterator_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/cayleygraph/cayley/graph"
     8  	. "github.com/cayleygraph/cayley/graph/iterator"
     9  )
    10  
    11  // A testing iterator that returns the given values for Next() and Err().
    12  type testIterator struct {
    13  	graph.Iterator
    14  
    15  	NextVal bool
    16  	ErrVal  error
    17  }
    18  
    19  func newTestIterator(next bool, err error) graph.Iterator {
    20  	return &testIterator{
    21  		Iterator: NewFixed(),
    22  		NextVal:  next,
    23  		ErrVal:   err,
    24  	}
    25  }
    26  
    27  func (it *testIterator) Next(ctx context.Context) bool {
    28  	return it.NextVal
    29  }
    30  
    31  func (it *testIterator) Err() error {
    32  	return it.ErrVal
    33  }
    34  
    35  type Int64Quad int64
    36  
    37  func (v Int64Quad) Key() interface{} { return v }
    38  
    39  func (Int64Quad) IsNode() bool { return false }
    40  
    41  var _ graph.Iterator = &Int64{}
    42  
    43  // An All iterator across a range of int64 values, from `max` to `min`.
    44  type Int64 struct {
    45  	node     bool
    46  	max, min int64
    47  	at       int64
    48  	result   int64
    49  	runstats graph.IteratorStats
    50  }
    51  
    52  // Creates a new Int64 with the given range.
    53  func newInt64(min, max int64, node bool) *Int64 {
    54  	return &Int64{
    55  		node: node,
    56  		min:  min,
    57  		max:  max,
    58  		at:   min,
    59  	}
    60  }
    61  
    62  // Start back at the beginning
    63  func (it *Int64) Reset() {
    64  	it.at = it.min
    65  }
    66  
    67  func (it *Int64) Close() error {
    68  	return nil
    69  }
    70  
    71  func (it *Int64) TagResults(dst map[string]graph.Ref) {}
    72  
    73  func (it *Int64) String() string {
    74  	return fmt.Sprintf("Int64(%d-%d)", it.min, it.max)
    75  }
    76  
    77  // Next() on an Int64 all iterator is a simple incrementing counter.
    78  // Return the next integer, and mark it as the result.
    79  func (it *Int64) Next(ctx context.Context) bool {
    80  	it.runstats.Next += 1
    81  	if it.at == -1 {
    82  		return false
    83  	}
    84  	val := it.at
    85  	it.at = it.at + 1
    86  	if it.at > it.max {
    87  		it.at = -1
    88  	}
    89  	it.result = val
    90  	return true
    91  }
    92  
    93  func (it *Int64) Err() error {
    94  	return nil
    95  }
    96  
    97  func (it *Int64) toValue(v int64) graph.Ref {
    98  	if it.node {
    99  		return Int64Node(v)
   100  	}
   101  	return Int64Quad(v)
   102  }
   103  
   104  func (it *Int64) Result() graph.Ref {
   105  	return it.toValue(it.result)
   106  }
   107  
   108  func (it *Int64) NextPath(ctx context.Context) bool {
   109  	return false
   110  }
   111  
   112  // No sub-iterators.
   113  func (it *Int64) SubIterators() []graph.Iterator {
   114  	return nil
   115  }
   116  
   117  // The number of elements in an Int64 is the size of the range.
   118  // The size is exact.
   119  func (it *Int64) Size() (int64, bool) {
   120  	sz := (it.max - it.min) + 1
   121  	return sz, true
   122  }
   123  
   124  func valToInt64(v graph.Ref) int64 {
   125  	if v, ok := v.(Int64Node); ok {
   126  		return int64(v)
   127  	}
   128  	return int64(v.(Int64Quad))
   129  }
   130  
   131  // Contains() for an Int64 is merely seeing if the passed value is
   132  // within the range, assuming the value is an int64.
   133  func (it *Int64) Contains(ctx context.Context, tsv graph.Ref) bool {
   134  	it.runstats.Contains += 1
   135  	v := valToInt64(tsv)
   136  	if it.min <= v && v <= it.max {
   137  		it.result = v
   138  		return true
   139  	}
   140  	return false
   141  }
   142  
   143  // There's nothing to optimize about this little iterator.
   144  func (it *Int64) Optimize() (graph.Iterator, bool) { return it, false }
   145  
   146  // Stats for an Int64 are simple. Super cheap to do any operation,
   147  // and as big as the range.
   148  func (it *Int64) Stats() graph.IteratorStats {
   149  	s, exact := it.Size()
   150  	return graph.IteratorStats{
   151  		ContainsCost: 1,
   152  		NextCost:     1,
   153  		Size:         s,
   154  		ExactSize:    exact,
   155  		Next:         it.runstats.Next,
   156  		Contains:     it.runstats.Contains,
   157  	}
   158  }