mirror of
https://github.com/emirpasic/gods
synced 2024-11-18 09:25:50 +00:00
93 lines
3.3 KiB
Go
93 lines
3.3 KiB
Go
|
/*
|
||
|
Copyright (c) 2015, Emir Pasic
|
||
|
All rights reserved.
|
||
|
|
||
|
Redistribution and use in source and binary forms, with or without
|
||
|
modification, are permitted provided that the following conditions are met:
|
||
|
|
||
|
* Redistributions of source code must retain the above copyright notice, this
|
||
|
list of conditions and the following disclaimer.
|
||
|
|
||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||
|
this list of conditions and the following disclaimer in the documentation
|
||
|
and/or other materials provided with the distribution.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
package singlylinkedlist
|
||
|
|
||
|
import "github.com/emirpasic/gods/containers"
|
||
|
|
||
|
func assertIteratorImplementation() {
|
||
|
var _ containers.IteratorWithIndex = (*Iterator)(nil)
|
||
|
}
|
||
|
|
||
|
// Iterator holding the iterator's state
|
||
|
type Iterator struct {
|
||
|
list *List
|
||
|
index int
|
||
|
element *element
|
||
|
}
|
||
|
|
||
|
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
||
|
func (list *List) Iterator() Iterator {
|
||
|
return Iterator{list: list, index: -1, element: nil}
|
||
|
}
|
||
|
|
||
|
// Next moves the iterator to the next element and returns true if there was a next element in the container.
|
||
|
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
|
||
|
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
|
||
|
// Modifies the state of the iterator.
|
||
|
func (iterator *Iterator) Next() bool {
|
||
|
if iterator.index < iterator.list.size {
|
||
|
iterator.index++
|
||
|
}
|
||
|
if !iterator.list.withinRange(iterator.index) {
|
||
|
iterator.element = nil
|
||
|
return false
|
||
|
}
|
||
|
if iterator.index == 0 {
|
||
|
iterator.element = iterator.list.first
|
||
|
} else {
|
||
|
iterator.element = iterator.element.next
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// Value returns the current element's value.
|
||
|
// Does not modify the state of the iterator.
|
||
|
func (iterator *Iterator) Value() interface{} {
|
||
|
return iterator.element.value
|
||
|
}
|
||
|
|
||
|
// Index returns the current element's index.
|
||
|
// Does not modify the state of the iterator.
|
||
|
func (iterator *Iterator) Index() int {
|
||
|
return iterator.index
|
||
|
}
|
||
|
|
||
|
// Begin resets the iterator to its initial state (one-before-first)
|
||
|
// Call Next() to fetch the first element if any.
|
||
|
func (iterator *Iterator) Begin() {
|
||
|
iterator.index = -1
|
||
|
iterator.element = nil
|
||
|
}
|
||
|
|
||
|
// First moves the iterator to the first element and returns true if there was a first element in the container.
|
||
|
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
|
||
|
// Modifies the state of the iterator.
|
||
|
func (iterator *Iterator) First() bool {
|
||
|
iterator.Begin()
|
||
|
return iterator.Next()
|
||
|
}
|