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
  5package arraylist
  6
  7import "github.com/emirpasic/gods/containers"
  8
  9// Assert Iterator implementation
 10var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
 11
 12// Iterator holding the iterator's state
 13type Iterator struct {
 14	list  *List
 15	index int
 16}
 17
 18// Iterator returns a stateful iterator whose values can be fetched by an index.
 19func (list *List) Iterator() Iterator {
 20	return Iterator{list: list, index: -1}
 21}
 22
 23// Next moves the iterator to the next element and returns true if there was a next element in the container.
 24// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
 25// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
 26// Modifies the state of the iterator.
 27func (iterator *Iterator) Next() bool {
 28	if iterator.index < iterator.list.size {
 29		iterator.index++
 30	}
 31	return iterator.list.withinRange(iterator.index)
 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 index and value can be retrieved by Index() and Value().
 36// Modifies the state of the iterator.
 37func (iterator *Iterator) Prev() bool {
 38	if iterator.index >= 0 {
 39		iterator.index--
 40	}
 41	return iterator.list.withinRange(iterator.index)
 42}
 43
 44// Value returns the current element's value.
 45// Does not modify the state of the iterator.
 46func (iterator *Iterator) Value() interface{} {
 47	return iterator.list.elements[iterator.index]
 48}
 49
 50// Index returns the current element's index.
 51// Does not modify the state of the iterator.
 52func (iterator *Iterator) Index() int {
 53	return iterator.index
 54}
 55
 56// Begin resets the iterator to its initial state (one-before-first)
 57// Call Next() to fetch the first element if any.
 58func (iterator *Iterator) Begin() {
 59	iterator.index = -1
 60}
 61
 62// End moves the iterator past the last element (one-past-the-end).
 63// Call Prev() to fetch the last element if any.
 64func (iterator *Iterator) End() {
 65	iterator.index = iterator.list.size
 66}
 67
 68// First moves the iterator to the first element and returns true if there was a first element in the container.
 69// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
 70// Modifies the state of the iterator.
 71func (iterator *Iterator) First() bool {
 72	iterator.Begin()
 73	return iterator.Next()
 74}
 75
 76// Last moves the iterator to the last element and returns true if there was a last element in the container.
 77// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
 78// Modifies the state of the iterator.
 79func (iterator *Iterator) Last() bool {
 80	iterator.End()
 81	return iterator.Prev()
 82}
 83
 84// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
 85// passed function, and returns true if there was a next element in the container.
 86// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
 87// Modifies the state of the iterator.
 88func (iterator *Iterator) NextTo(f func(index int, value interface{}) bool) bool {
 89	for iterator.Next() {
 90		index, value := iterator.Index(), iterator.Value()
 91		if f(index, value) {
 92			return true
 93		}
 94	}
 95	return false
 96}
 97
 98// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
 99// passed function, and returns true if there was a next element in the container.
100// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
101// Modifies the state of the iterator.
102func (iterator *Iterator) PrevTo(f func(index int, value interface{}) bool) bool {
103	for iterator.Prev() {
104		index, value := iterator.Index(), iterator.Value()
105		if f(index, value) {
106			return true
107		}
108	}
109	return false
110}