github.com/mymmsc/gox@v1.3.33/util/iterator.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  // IteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
     8  type IteratorWithIndex interface {
     9  	// Next moves the iterator to the next element and returns true if there was a next element in the container.
    10  	// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
    11  	// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
    12  	// Modifies the state of the iterator.
    13  	Next() bool
    14  
    15  	// Value returns the current element's value.
    16  	// Does not modify the state of the iterator.
    17  	Value() interface{}
    18  
    19  	// Index returns the current element's index.
    20  	// Does not modify the state of the iterator.
    21  	Index() int
    22  
    23  	// Begin resets the iterator to its initial state (one-before-first)
    24  	// Call Next() to fetch the first element if any.
    25  	Begin()
    26  
    27  	// First moves the iterator to the first element and returns true if there was a first element in the container.
    28  	// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
    29  	// Modifies the state of the iterator.
    30  	First() bool
    31  }
    32  
    33  // IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
    34  type IteratorWithKey interface {
    35  	// Next moves the iterator to the next element and returns true if there was a next element in the container.
    36  	// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
    37  	// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
    38  	// Modifies the state of the iterator.
    39  	Next() bool
    40  
    41  	// Value returns the current element's value.
    42  	// Does not modify the state of the iterator.
    43  	Value() interface{}
    44  
    45  	// Key returns the current element's key.
    46  	// Does not modify the state of the iterator.
    47  	Key() interface{}
    48  
    49  	// Begin resets the iterator to its initial state (one-before-first)
    50  	// Call Next() to fetch the first element if any.
    51  	Begin()
    52  
    53  	// First moves the iterator to the first element and returns true if there was a first element in the container.
    54  	// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
    55  	// Modifies the state of the iterator.
    56  	First() bool
    57  }
    58  
    59  // ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
    60  //
    61  // Essentially it is the same as IteratorWithIndex, but provides additional:
    62  //
    63  // Prev() function to enable traversal in reverse
    64  //
    65  // Last() function to move the iterator to the last element.
    66  //
    67  // End() function to move the iterator past the last element (one-past-the-end).
    68  type ReverseIteratorWithIndex interface {
    69  	// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
    70  	// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
    71  	// Modifies the state of the iterator.
    72  	Prev() bool
    73  
    74  	// End moves the iterator past the last element (one-past-the-end).
    75  	// Call Prev() to fetch the last element if any.
    76  	End()
    77  
    78  	// Last moves the iterator to the last element and returns true if there was a last element in the container.
    79  	// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
    80  	// Modifies the state of the iterator.
    81  	Last() bool
    82  
    83  	IteratorWithIndex
    84  }
    85  
    86  // ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
    87  //
    88  // Essentially it is the same as IteratorWithKey, but provides additional:
    89  //
    90  // Prev() function to enable traversal in reverse
    91  //
    92  // Last() function to move the iterator to the last element.
    93  type ReverseIteratorWithKey interface {
    94  	// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
    95  	// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
    96  	// Modifies the state of the iterator.
    97  	Prev() bool
    98  
    99  	// End moves the iterator past the last element (one-past-the-end).
   100  	// Call Prev() to fetch the last element if any.
   101  	End()
   102  
   103  	// Last moves the iterator to the last element and returns true if there was a last element in the container.
   104  	// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
   105  	// Modifies the state of the iterator.
   106  	Last() bool
   107  
   108  	IteratorWithKey
   109  }