Debian Codegraph @

lists/doublylinkedlist/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 doublylinkedlist
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 element *element[T]
17 }
18
19 // Iterator returns a stateful iterator whose values can be fetched by an index.
20 func (list *List[T]) Iterator() Iterator[T] {
21 return Iterator[T]{list: list, index: -1, element: nil}
22 }
23
24 // Next moves the iterator to the next element and returns true if there was a next element in the container.
25 // If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
26 // If Next() was called for the first time, then it will point the iterator to the first element if it exists.
27 // Modifies the state of the iterator.
28 func (iterator *Iterator[T]) Next() bool {
29 if iterator.index < iterator.list.size {
30 iterator.index++
31 }
32 if !iterator.list.withinRange(iterator.index) {
33 iterator.element = nil
34 return false
35 }
36 if iterator.index != 0 {
37 iterator.element = iterator.element.next
38 } else {
39 iterator.element = iterator.list.first
40 }
41 return true
42 }
43
44 // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
45 // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
46 // Modifies the state of the iterator.
47 func (iterator *Iterator[T]) Prev() bool {
48 if iterator.index >= 0 {
49 iterator.index--
50 }
51 if !iterator.list.withinRange(iterator.index) {
52 iterator.element = nil
53 return false
54 }
55 if iterator.index == iterator.list.size-1 {
56 iterator.element = iterator.list.last
57 } else {
58 iterator.element = iterator.element.prev
59 }
60 return iterator.list.withinRange(iterator.index)
61 }
62
63 // Value returns the current element's value.
64 // Does not modify the state of the iterator.
65 func (iterator *Iterator[T]) Value() T {
66 return iterator.element.value
67 }
68
69 // Index returns the current element's index.
70 // Does not modify the state of the iterator.
71 func (iterator *Iterator[T]) Index() int {
72 return iterator.index
73 }
74
75 // Begin resets the iterator to its initial state (one-before-first)
76 // Call Next() to fetch the first element if any.
77 func (iterator *Iterator[T]) Begin() {
78 iterator.index = -1
79 iterator.element = nil
80 }
81
82 // End moves the iterator past the last element (one-past-the-end).
83 // Call Prev() to fetch the last element if any.
84 func (iterator *Iterator[T]) End() {
85 iterator.index = iterator.list.size
86 iterator.element = iterator.list.last
87 }
88
89 // First moves the iterator to the first element and returns true if there was a first element in the container.
90 // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
91 // Modifies the state of the iterator.
92 func (iterator *Iterator[T]) First() bool {
93 iterator.Begin()
94 return iterator.Next()
95 }
96
97 // Last moves the iterator to the last element and returns true if there was a last element in the container.
98 // If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
99 // Modifies the state of the iterator.
100 func (iterator *Iterator[T]) Last() bool {
101 iterator.End()
102 return iterator.Prev()
103 }
104
105 // NextTo moves the iterator to the next element from current position that satisfies the condition given by the
106 // passed function, and returns true if there was a next element in the container.
107 // If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
108 // Modifies the state of the iterator.
109 func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
110 for iterator.Next() {
111 index, value := iterator.Index(), iterator.Value()
112 if f(index, value) {
113 return true
114 }
115 }
116 return false
117 }
118
119 // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
120 // passed function, and returns true if there was a next element in the container.
121 // If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
122 // Modifies the state of the iterator.
123 func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
124 for iterator.Prev() {
125 index, value := iterator.Index(), iterator.Value()
126 if f(index, value) {
127 return true
128 }
129 }
130 return false
131 }