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