mirror of
https://github.com/emirpasic/gods
synced 2024-11-06 15:20:25 +00:00
105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package doublylinkedlist
|
|
|
|
import "github.com/emirpasic/gods/containers"
|
|
|
|
func assertIteratorImplementation() {
|
|
var _ containers.ReverseIteratorWithIndex = (*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.element.next
|
|
} else {
|
|
iterator.element = iterator.list.first
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
|
|
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator) Prev() bool {
|
|
if iterator.index >= 0 {
|
|
iterator.index--
|
|
}
|
|
if !iterator.list.withinRange(iterator.index) {
|
|
iterator.element = nil
|
|
return false
|
|
}
|
|
if iterator.index == iterator.list.size-1 {
|
|
iterator.element = iterator.list.last
|
|
} else {
|
|
iterator.element = iterator.element.prev
|
|
}
|
|
return iterator.list.withinRange(iterator.index)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// End moves the iterator past the last element (one-past-the-end).
|
|
// Call Prev() to fetch the last element if any.
|
|
func (iterator *Iterator) End() {
|
|
iterator.index = iterator.list.size
|
|
iterator.element = iterator.list.last
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
// Last moves the iterator to the last element and returns true if there was a last element in the container.
|
|
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator) Last() bool {
|
|
iterator.End()
|
|
return iterator.Prev()
|
|
}
|