| 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 linkedlistqueue |
| 6 | |
| 7 | import "github.com/emirpasic/gods/v2/containers" |
| 8 | |
| 9 | // Assert Iterator implementation |
| 10 | var _ containers.IteratorWithIndex[int] = (*Iterator[int])(nil) |
| 11 | |
| 12 | // Iterator returns a stateful iterator whose values can be fetched by an index. |
| 13 | type Iterator[T comparable] struct { |
| 14 | queue *Queue[T] |
| 15 | index int |
| 16 | } |
| 17 | |
| 18 | // Iterator returns a stateful iterator whose values can be fetched by an index. |
| 19 | func (queue *Queue[T]) Iterator() *Iterator[T] { |
| 20 | return &Iterator[T]{queue: queue, 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. |
| 27 | func (iterator *Iterator[T]) Next() bool { |
| 28 | if iterator.index < iterator.queue.Size() { |
| 29 | iterator.index++ |
| 30 | } |
| 31 | return iterator.queue.withinRange(iterator.index) |
| 32 | } |
| 33 | |
| 34 | // Value returns the current element's value. |
| 35 | // Does not modify the state of the iterator. |
| 36 | func (iterator *Iterator[T]) Value() T { |
| 37 | value, _ := iterator.queue.list.Get(iterator.index) |
| 38 | return value |
| 39 | } |
| 40 | |
| 41 | // Index returns the current element's index. |
| 42 | // Does not modify the state of the iterator. |
| 43 | func (iterator *Iterator[T]) Index() int { |
| 44 | return iterator.index |
| 45 | } |
| 46 | |
| 47 | // Begin resets the iterator to its initial state (one-before-first) |
| 48 | // Call Next() to fetch the first element if any. |
| 49 | func (iterator *Iterator[T]) Begin() { |
| 50 | iterator.index = -1 |
| 51 | } |
| 52 | |
| 53 | // First moves the iterator to the first element and returns true if there was a first element in the container. |
| 54 | // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). |
| 55 | // Modifies the state of the iterator. |
| 56 | func (iterator *Iterator[T]) First() bool { |
| 57 | iterator.Begin() |
| 58 | return iterator.Next() |
| 59 | } |
| 60 | |
| 61 | // NextTo moves the iterator to the next element from current position that satisfies the condition given by the |
| 62 | // passed function, and returns true if there was a next element in the container. |
| 63 | // If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 64 | // Modifies the state of the iterator. |
| 65 | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool { |
| 66 | for iterator.Next() { |
| 67 | index, value := iterator.Index(), iterator.Value() |
| 68 | if f(index, value) { |
| 69 | return true |
| 70 | } |
| 71 | } |
| 72 | return false |
| 73 | } |