github.com/mymmsc/gox@v1.3.33/util/linkedhashset/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 linkedhashset
     6  
     7  import (
     8  	"github.com/mymmsc/gox/util"
     9  	"github.com/mymmsc/gox/util/doublylinkedlist"
    10  )
    11  
    12  func assertIteratorImplementation() {
    13  	var _ util.ReverseIteratorWithIndex = (*Iterator)(nil)
    14  }
    15  
    16  // Iterator holding the iterator's state
    17  type Iterator struct {
    18  	iterator doublylinkedlist.Iterator
    19  }
    20  
    21  // Iterator returns a stateful iterator whose values can be fetched by an index.
    22  func (set *Set) Iterator() Iterator {
    23  	return Iterator{iterator: set.ordering.Iterator()}
    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  	return iterator.iterator.Next()
    32  }
    33  
    34  // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
    35  // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
    36  // Modifies the state of the iterator.
    37  func (iterator *Iterator) Prev() bool {
    38  	return iterator.iterator.Prev()
    39  }
    40  
    41  // Value returns the current element's value.
    42  // Does not modify the state of the iterator.
    43  func (iterator *Iterator) Value() interface{} {
    44  	return iterator.iterator.Value()
    45  }
    46  
    47  // Index returns the current element's index.
    48  // Does not modify the state of the iterator.
    49  func (iterator *Iterator) Index() int {
    50  	return iterator.iterator.Index()
    51  }
    52  
    53  // Begin resets the iterator to its initial state (one-before-first)
    54  // Call Next() to fetch the first element if any.
    55  func (iterator *Iterator) Begin() {
    56  	iterator.iterator.Begin()
    57  }
    58  
    59  // End moves the iterator past the last element (one-past-the-end).
    60  // Call Prev() to fetch the last element if any.
    61  func (iterator *Iterator) End() {
    62  	iterator.iterator.End()
    63  }
    64  
    65  // First moves the iterator to the first element and returns true if there was a first element in the container.
    66  // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
    67  // Modifies the state of the iterator.
    68  func (iterator *Iterator) First() bool {
    69  	return iterator.iterator.First()
    70  }
    71  
    72  // Last moves the iterator to the last element and returns true if there was a last element in the container.
    73  // If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
    74  // Modifies the state of the iterator.
    75  func (iterator *Iterator) Last() bool {
    76  	return iterator.iterator.Last()
    77  }