This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
65 lines (52 loc) · 1.38 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package linkmap
import "github.com/djordje200179/extendedlibrary/misc"
// Iterator is a struct for iterating over the map.
type Iterator[K, V any] struct {
wrapper *Wrapper[K, V]
curr *Node[K, V]
}
// Valid returns true if the iterator points to a valid element.
func (it *Iterator[K, V]) Valid() bool {
return it.curr != nil
}
// Move moves the iterator to the next element.
func (it *Iterator[K, V]) Move() {
if it.curr == nil {
return
}
it.curr = it.curr.next
}
// Get returns the current entry as a Pair.
func (it *Iterator[K, V]) Get() misc.Pair[K, V] {
return misc.Pair[K, V]{
First: it.Key(),
Second: it.Value(),
}
}
// Key returns the current key.
func (it *Iterator[K, V]) Key() K {
return it.curr.key
}
// Value returns the value of the current entry.
func (it *Iterator[K, V]) Value() V {
return it.curr.Value
}
// ValueRef returns a reference to the value of the current entry.
func (it *Iterator[K, V]) ValueRef() *V {
return &it.curr.Value
}
// SetValue sets the value of the current entry.
func (it *Iterator[K, V]) SetValue(value V) {
it.curr.Value = value
}
// Remove removes the current entry from the map.
// The iterator will be moved to the next element.
func (it *Iterator[K, V]) Remove() {
next := it.curr.next
it.wrapper.m.Remove(it.curr.key)
it.curr = next
}
// Node returns the current node.
func (it *Iterator[K, V]) Node() *Node[K, V] {
return it.curr
}