Debian Codegraph @

lists/singlylinkedlist/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 singlylinkedlist
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 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.list.first
38 } else {
39 iterator.element = iterator.element.next
40 }
41 return true
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.element.value
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 iterator.element = nil
61 }
62
63 // First moves the iterator to the first element and returns true if there was a first element in the container.
64 // If First() returns true, then first element's index and value can be retrieved by Index() and Value().
65 // Modifies the state of the iterator.
66 func (iterator *Iterator[T]) First() bool {
67 iterator.Begin()
68 return iterator.Next()
69 }
70
71 // NextTo moves the iterator to the next element from current position that satisfies the condition given by the
72 // passed function, and returns true if there was a next element in the container.
73 // If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
74 // Modifies the state of the iterator.
75 func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
76 for iterator.Next() {
77 index, value := iterator.Index(), iterator.Value()
78 if f(index, value) {
79 return true
80 }
81 }
82 return false
83 }