mirror of https://github.com/emirpasic/gods
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.5 KiB
Go
74 lines
2.5 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 linkedlistqueue
|
|
|
|
import "github.com/emirpasic/gods/containers"
|
|
|
|
// Assert Iterator implementation
|
|
var _ containers.IteratorWithIndex = (*Iterator)(nil)
|
|
|
|
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
|
type Iterator struct {
|
|
queue *Queue
|
|
index int
|
|
}
|
|
|
|
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
|
func (queue *Queue) Iterator() Iterator {
|
|
return Iterator{queue: queue, index: -1}
|
|
}
|
|
|
|
// 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.queue.Size() {
|
|
iterator.index++
|
|
}
|
|
return iterator.queue.withinRange(iterator.index)
|
|
}
|
|
|
|
// Value returns the current element's value.
|
|
// Does not modify the state of the iterator.
|
|
func (iterator *Iterator) Value() interface{} {
|
|
value, _ := iterator.queue.list.Get(iterator.index)
|
|
return 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
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
|
|
// passed function, and returns true if there was a next element in the container.
|
|
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator) NextTo(f func(index int, value interface{}) bool) bool {
|
|
for iterator.Next() {
|
|
index, value := iterator.Index(), iterator.Value()
|
|
if f(index, value) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|