Debian Codegraph @

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