Debian Codegraph @

maps/treemap/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 treemap
6
7 import (
8 "github.com/emirpasic/gods/v2/containers"
9 rbt "github.com/emirpasic/gods/v2/trees/redblacktree"
10 )
11
12 // Assert Iterator implementation
13 var _ containers.ReverseIteratorWithKey[string, int] = (*Iterator[string, int])(nil)
14
15 // Iterator holding the iterator's state
16 type Iterator[K comparable, V any] struct {
17 iterator *rbt.Iterator[K, V]
18 }
19
20 // Iterator returns a stateful iterator whose elements are key/value pairs.
21 func (m *Map[K, V]) Iterator() *Iterator[K, V] {
22 return &Iterator[K, V]{iterator: m.tree.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 key and value can be retrieved by Key() 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[K, V]) 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 key and value can be retrieved by Key() and Value().
35 // Modifies the state of the iterator.
36 func (iterator *Iterator[K, V]) 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[K, V]) Value() V {
43 return iterator.iterator.Value()
44 }
45
46 // Key returns the current element's key.
47 // Does not modify the state of the iterator.
48 func (iterator *Iterator[K, V]) Key() K {
49 return iterator.iterator.Key()
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[K, V]) 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[K, V]) 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 key and value can be retrieved by Key() and Value().
66 // Modifies the state of the iterator
67 func (iterator *Iterator[K, V]) 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 key and value can be retrieved by Key() and Value().
73 // Modifies the state of the iterator.
74 func (iterator *Iterator[K, V]) 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 key and value can be retrieved by Key() and Value().
81 // Modifies the state of the iterator.
82 func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
83 for iterator.Next() {
84 key, value := iterator.Key(), iterator.Value()
85 if f(key, value) {
86 return true
87 }
88 }
89 return false
90 }
91
92 // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
93 // passed function, and returns true if there was a next element in the container.
94 // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value().
95 // Modifies the state of the iterator.
96 func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
97 for iterator.Prev() {
98 key, value := iterator.Key(), iterator.Value()
99 if f(key, value) {
100 return true
101 }
102 }
103 return false
104 }