Debian Codegraph @

lists/arraylist/iterator.go

raw symbols SCIP
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 arraylist
6
7 import "github.com/emirpasic/gods/v2/containers"
8
9 // Assert Iterator implementation
10 var _ containers.ReverseIteratorWithIndex[int] = (*Iterator[int])(nil)
11
12 // Iterator holding the iterator's state
13 type Iterator[T comparable] struct {
14 list *List[T]
15 index int
16 }
17
18 // Iterator returns a stateful iterator whose values can be fetched by an index.
19 func (list *List[T]) Iterator() *Iterator[T] {
20 return &Iterator[T]{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.
27 func (iterator *Iterator[T]) 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.
37 func (iterator *Iterator[T]) 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.
46 func (iterator *Iterator[T]) Value() T {
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.
52 func (iterator *Iterator[T]) 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.
58 func (iterator *Iterator[T]) 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.
64 func (iterator *Iterator[T]) 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.
71 func (iterator *Iterator[T]) 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.
79 func (iterator *Iterator[T]) 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.
88 func (iterator *Iterator[T]) NextTo(f func(index int, value T) 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.
102 func (iterator *Iterator[T]) PrevTo(f func(index int, value T) 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 }