@@ -25,12 +25,26 @@ void MultiDList<T, HookPtr>::Iterator::goForward() noexcept {
2525 }
2626 // Move iterator forward
2727 ++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+ }
3448 }
3549 }
3650}
@@ -71,6 +85,25 @@ void MultiDList<T, HookPtr>::Iterator::initToValidRBeginFrom(
7185 : mlist_.lists_ [index_]->rbegin ();
7286}
7387
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+
74107template <typename T, DListHook<T> T::*HookPtr>
75108typename MultiDList<T, HookPtr>::Iterator&
76109MultiDList<T, HookPtr>::Iterator::operator ++() noexcept {
@@ -97,7 +130,16 @@ typename MultiDList<T, HookPtr>::Iterator MultiDList<T, HookPtr>::rbegin(
97130 if (listIdx >= lists_.size ()) {
98131 throw std::invalid_argument (" Invalid list index for MultiDList iterator." );
99132 }
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 );
101143}
102144
103145template <typename T, DListHook<T> T::*HookPtr>
0 commit comments