gitee.com/quant1x/gox@v1.21.2/util/treemap/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 treemap
     6  
     7  import (
     8  	"gitee.com/quant1x/gox/util/internal"
     9  	rbt "gitee.com/quant1x/gox/util/redblacktree"
    10  )
    11  
    12  func assertIteratorImplementation() {
    13  	var _ internal.ReverseIteratorWithKey = (*Iterator)(nil)
    14  }
    15  
    16  // Iterator holding the iterator's state
    17  type Iterator struct {
    18  	iterator rbt.Iterator
    19  }
    20  
    21  // Iterator returns a stateful iterator whose elements are key/value pairs.
    22  func (m *Map) Iterator() Iterator {
    23  	return Iterator{iterator: m.tree.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 key and value can be retrieved by Key() 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 key and value can be retrieved by Key() 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  // Key returns the current element's key.
    48  // Does not modify the state of the iterator.
    49  func (iterator *Iterator) Key() interface{} {
    50  	return iterator.iterator.Key()
    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 key and value can be retrieved by Key() 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 key and value can be retrieved by Key() and Value().
    74  // Modifies the state of the iterator.
    75  func (iterator *Iterator) Last() bool {
    76  	return iterator.iterator.Last()
    77  }