@@ -6,23 +6,23 @@ package linkedhashmap
6
6
7
7
import (
8
8
"github.com/emirpasic/gods/v2/containers"
9
- "github.com/emirpasic/gods/v2/lists/doublylinkedlist"
10
9
)
11
10
12
11
// Assert Iterator implementation
13
12
var _ containers.ReverseIteratorWithKey [string , int ] = (* Iterator [string , int ])(nil )
14
13
15
14
// Iterator holding the iterator's state
16
15
type Iterator [K comparable , V any ] struct {
17
- iterator doublylinkedlist.Iterator [K ]
18
- table map [K ]V
16
+ m * Map [K , V ]
17
+ index int
18
+ element * element [K , V ]
19
19
}
20
20
21
21
// Iterator returns a stateful iterator whose elements are key/value pairs.
22
22
func (m * Map [K , V ]) Iterator () * Iterator [K , V ] {
23
23
return & Iterator [K , V ]{
24
- iterator : m . ordering . Iterator () ,
25
- table : m . table ,
24
+ m : m ,
25
+ index : - 1 ,
26
26
}
27
27
}
28
28
@@ -31,53 +31,84 @@ func (m *Map[K, V]) Iterator() *Iterator[K, V] {
31
31
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
32
32
// Modifies the state of the iterator.
33
33
func (iterator * Iterator [K , V ]) Next () bool {
34
- return iterator .iterator .Next ()
34
+ if iterator .index < iterator .m .Size () {
35
+ iterator .index ++
36
+ }
37
+ if ! iterator .m .withinRange (iterator .index ) {
38
+ iterator .element = nil
39
+ return false
40
+ }
41
+ if iterator .index != 0 {
42
+ iterator .element = iterator .element .next
43
+ } else {
44
+ iterator .element = iterator .m .first
45
+ }
46
+ return true
35
47
}
36
48
37
49
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
38
50
// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
39
51
// Modifies the state of the iterator.
40
52
func (iterator * Iterator [K , V ]) Prev () bool {
41
- return iterator .iterator .Prev ()
53
+ if iterator .index >= 0 {
54
+ iterator .index --
55
+ }
56
+ if ! iterator .m .withinRange (iterator .index ) {
57
+ iterator .element = nil
58
+ return false
59
+ }
60
+ if iterator .index == iterator .m .Size ()- 1 {
61
+ iterator .element = iterator .m .last
62
+ } else {
63
+ iterator .element = iterator .element .prev
64
+ }
65
+ return iterator .m .withinRange (iterator .index )
42
66
}
43
67
44
68
// Value returns the current element's value.
45
69
// Does not modify the state of the iterator.
46
70
func (iterator * Iterator [K , V ]) Value () V {
47
- key := iterator .iterator .Value ()
48
- return iterator .table [key ]
71
+ if iterator .element != nil {
72
+ return iterator .element .value
73
+ }
74
+ var v V
75
+ return v
49
76
}
50
77
51
78
// Key returns the current element's key.
52
79
// Does not modify the state of the iterator.
53
80
func (iterator * Iterator [K , V ]) Key () K {
54
- return iterator .iterator . Value ()
81
+ return iterator .element . key
55
82
}
56
83
57
84
// Begin resets the iterator to its initial state (one-before-first)
58
85
// Call Next() to fetch the first element if any.
59
86
func (iterator * Iterator [K , V ]) Begin () {
60
- iterator .iterator .Begin ()
87
+ iterator .index = - 1
88
+ iterator .element = nil
61
89
}
62
90
63
91
// End moves the iterator past the last element (one-past-the-end).
64
92
// Call Prev() to fetch the last element if any.
65
93
func (iterator * Iterator [K , V ]) End () {
66
- iterator .iterator .End ()
94
+ iterator .index = iterator .m .Size ()
95
+ iterator .element = nil
67
96
}
68
97
69
98
// First moves the iterator to the first element and returns true if there was a first element in the container.
70
99
// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
71
100
// Modifies the state of the iterator
72
101
func (iterator * Iterator [K , V ]) First () bool {
73
- return iterator .iterator .First ()
102
+ iterator .Begin ()
103
+ return iterator .Next ()
74
104
}
75
105
76
106
// Last moves the iterator to the last element and returns true if there was a last element in the container.
77
107
// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
78
108
// Modifies the state of the iterator.
79
109
func (iterator * Iterator [K , V ]) Last () bool {
80
- return iterator .iterator .Last ()
110
+ iterator .End ()
111
+ return iterator .Prev ()
81
112
}
82
113
83
114
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
@@ -107,3 +138,8 @@ func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
107
138
}
108
139
return false
109
140
}
141
+
142
+ // Check that the index is within bounds of the list
143
+ func (m * Map [K , V ]) withinRange (index int ) bool {
144
+ return index >= 0 && index < m .Size ()
145
+ }
0 commit comments