@@ -25,12 +25,26 @@ void MultiDList<T, HookPtr>::Iterator::goForward() noexcept {
25
25
}
26
26
// Move iterator forward
27
27
++currIter_;
28
- // If we land at the rend of this list, move to the previous list.
29
- while (index_ != kInvalidIndex &&
30
- currIter_ == mlist_.lists_ [index_]->rend ()) {
31
- --index_;
32
- if (index_ != kInvalidIndex ) {
33
- currIter_ = mlist_.lists_ [index_]->rbegin ();
28
+
29
+ if (currIter_.getDirection () == DListIterator::Direction::FROM_HEAD) {
30
+ // If we land at the rend of this list, move to the previous list.
31
+ while (index_ != kInvalidIndex && index_ != mlist_.lists_ .size () &&
32
+ currIter_ == mlist_.lists_ [index_]->end ()) {
33
+ ++index_;
34
+ if (index_ != kInvalidIndex && index_ != mlist_.lists_ .size ()) {
35
+ currIter_ = mlist_.lists_ [index_]->begin ();
36
+ } else {
37
+ return ;
38
+ }
39
+ }
40
+ } else {
41
+ // If we land at the rend of this list, move to the previous list.
42
+ while (index_ != kInvalidIndex &&
43
+ currIter_ == mlist_.lists_ [index_]->rend ()) {
44
+ --index_;
45
+ if (index_ != kInvalidIndex ) {
46
+ currIter_ = mlist_.lists_ [index_]->rbegin ();
47
+ }
34
48
}
35
49
}
36
50
}
@@ -71,6 +85,25 @@ void MultiDList<T, HookPtr>::Iterator::initToValidRBeginFrom(
71
85
: mlist_.lists_ [index_]->rbegin ();
72
86
}
73
87
88
+ template <typename T, DListHook<T> T::*HookPtr>
89
+ void MultiDList<T, HookPtr>::Iterator::initToValidBeginFrom(
90
+ size_t listIdx) noexcept {
91
+ // Find the first non-empty list.
92
+ index_ = listIdx;
93
+ while (index_ != mlist_.lists_ .size () &&
94
+ mlist_.lists_ [index_]->size () == 0 ) {
95
+ ++index_;
96
+ }
97
+ if (index_ == mlist_.lists_ .size ()) {
98
+ // we reached the end - we should get set to
99
+ // invalid index
100
+ index_ = std::numeric_limits<size_t >::max ();
101
+ }
102
+ currIter_ = index_ == std::numeric_limits<size_t >::max ()
103
+ ? mlist_.lists_ [0 ]->begin ()
104
+ : mlist_.lists_ [index_]->begin ();
105
+ }
106
+
74
107
template <typename T, DListHook<T> T::*HookPtr>
75
108
typename MultiDList<T, HookPtr>::Iterator&
76
109
MultiDList<T, HookPtr>::Iterator::operator ++() noexcept {
@@ -97,7 +130,16 @@ typename MultiDList<T, HookPtr>::Iterator MultiDList<T, HookPtr>::rbegin(
97
130
if (listIdx >= lists_.size ()) {
98
131
throw std::invalid_argument (" Invalid list index for MultiDList iterator." );
99
132
}
100
- return MultiDList<T, HookPtr>::Iterator (*this , listIdx);
133
+ return MultiDList<T, HookPtr>::Iterator (*this , listIdx, false );
134
+ }
135
+
136
+ template <typename T, DListHook<T> T::*HookPtr>
137
+ typename MultiDList<T, HookPtr>::Iterator MultiDList<T, HookPtr>::begin(
138
+ size_t listIdx) const {
139
+ if (listIdx >= lists_.size ()) {
140
+ throw std::invalid_argument (" Invalid list index for MultiDList iterator." );
141
+ }
142
+ return MultiDList<T, HookPtr>::Iterator (*this , listIdx, true );
101
143
}
102
144
103
145
template <typename T, DListHook<T> T::*HookPtr>
0 commit comments