gitee.com/quant1x/gox@v1.21.2/util/arraylist/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 arraylist
     6  
     7  import (
     8  	"gitee.com/quant1x/gox/util/internal"
     9  )
    10  
    11  func assertIteratorImplementation() {
    12  	var _ internal.ReverseIteratorWithIndex = (*Iterator)(nil)
    13  }
    14  
    15  // Iterator holding the iterator's state
    16  type Iterator struct {
    17  	list  *List
    18  	index int
    19  }
    20  
    21  // Iterator returns a stateful iterator whose values can be fetched by an index.
    22  func (list *List) Iterator() Iterator {
    23  	return Iterator{list: list, index: -1}
    24  }
    25  
    26  // Next moves the iterator to the next element and returns true if there was a next element in the container.
    27  // If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
    28  // If Next() was called for the first time, then it will point the iterator to the first element if it exists.
    29  // Modifies the state of the iterator.
    30  func (iterator *Iterator) Next() bool {
    31  	if iterator.index < iterator.list.size {
    32  		iterator.index++
    33  	}
    34  	return iterator.list.withinRange(iterator.index)
    35  }
    36  
    37  // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
    38  // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
    39  // Modifies the state of the iterator.
    40  func (iterator *Iterator) Prev() bool {
    41  	if iterator.index >= 0 {
    42  		iterator.index--
    43  	}
    44  	return iterator.list.withinRange(iterator.index)
    45  }
    46  
    47  // Value returns the current element's value.
    48  // Does not modify the state of the iterator.
    49  func (iterator *Iterator) Value() interface{} {
    50  	return iterator.list.elements[iterator.index]
    51  }
    52  
    53  // Index returns the current element's index.
    54  // Does not modify the state of the iterator.
    55  func (iterator *Iterator) Index() int {
    56  	return iterator.index
    57  }
    58  
    59  // Begin resets the iterator to its initial state (one-before-first)
    60  // Call Next() to fetch the first element if any.
    61  func (iterator *Iterator) Begin() {
    62  	iterator.index = -1
    63  }
    64  
    65  // End moves the iterator past the last element (one-past-the-end).
    66  // Call Prev() to fetch the last element if any.
    67  func (iterator *Iterator) End() {
    68  	iterator.index = iterator.list.size
    69  }
    70  
    71  // First moves the iterator to the first element and returns true if there was a first element in the container.
    72  // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
    73  // Modifies the state of the iterator.
    74  func (iterator *Iterator) First() bool {
    75  	iterator.Begin()
    76  	return iterator.Next()
    77  }
    78  
    79  // Last moves the iterator to the last element and returns true if there was a last element in the container.
    80  // If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
    81  // Modifies the state of the iterator.
    82  func (iterator *Iterator) Last() bool {
    83  	iterator.End()
    84  	return iterator.Prev()
    85  }