Skip to content

Commit

Permalink
[22_2] make iterator usable with range based for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori authored May 23, 2024
1 parent 2faf0a7 commit 1e93ae7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Kernel/Containers/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,23 @@ template <class T> class iterator_rep : public abstract_struct {
virtual bool busy ()= 0;

/**
* @brief Returns the next element of the iterator.
* @return The next element of the iterator.
* @brief Returns the current element of the iterator, and move the iterator
* forward
* @return The current element of the iterator.
*/
virtual T next ()= 0;

/**
* @brief Returns the current element of the iterator.
* @return The current element of the iterator.
*/
virtual T current ()= 0;

/**
* @brief Increase the iterator
*/
virtual void increase ()= 0;

/**
* @brief Returns the number of elements remaining in the iterator.
* @return The number of elements remaining in the iterator.
Expand All @@ -56,6 +68,15 @@ template <class T> class iterator_rep : public abstract_struct {
*/
template <class T> struct iterator {
ABSTRACT_TEMPLATE (iterator, T);
iterator<T> begin () { return *this; };
std::nullptr_t end () { return nullptr; };
void operator++ () { this->rep->increase (); };
/**
* @brief Returns the current element of the iterator, then move the iterator
* to the next element.
*/
T operator* () { return this->rep->current (); };
bool operator!= (std::nullptr_t) { return this->rep->busy (); }
};
ABSTRACT_TEMPLATE_CODE (iterator, class, T);

Expand Down
32 changes: 32 additions & 0 deletions Kernel/Containers/iterator.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public:
hashset_iterator_rep<T> (hashset<T> h);
bool busy ();
T next ();
T current ();
void increase ();
};

template <class T>
Expand Down Expand Up @@ -78,6 +80,20 @@ hashset_iterator_rep<T>::next () {
return x;
}

template <class T>
T
hashset_iterator_rep<T>::current () {
ASSERT (busy (), "end of iterator");
T x (l->item);
return x;
}

template <class T>
void
hashset_iterator_rep<T>::increase () {
l= l->next;
}

template <class T>
iterator<T>
iterate (hashset<T> h) {
Expand All @@ -97,6 +113,8 @@ public:
hashmap_iterator_rep (hashmap<T, U> h);
bool busy ();
T next ();
T current ();
void increase ();
};

template <class T, class U>
Expand Down Expand Up @@ -129,6 +147,20 @@ hashmap_iterator_rep<T, U>::next () {
return x;
}

template <class T, class U>
T
hashmap_iterator_rep<T, U>::current () {
ASSERT (busy (), "end of iterator");
T x (l->item.key);
return x;
}

template <class T, class U>
void
hashmap_iterator_rep<T, U>::increase () {
l= l->next;
}

template <class T, class U>
iterator<T>
iterate (hashmap<T, U> h) {
Expand Down
44 changes: 44 additions & 0 deletions tests/Kernel/Containers/iterator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@ TEST_CASE ("iterate on hashmap") {
CHECK_EQ (a == string ("123"), true);
}

TEST_CASE ("ranged for on hashmap") {
SUBCASE ("ordinary") {
hashmap<int, int> h;
h (1)= 1;
h (2)= 4;
h (3)= 6;
string a;
for (auto i : iterate (h)) {
a= a * as_string (i);
}
string_eq (a, "123");
};
SUBCASE ("empty") {
hashmap<int, int> h;
string a;
for (auto i : iterate (h)) {
a= a * as_string (i);
}
string_eq (a, "");
};
}

TEST_CASE ("iterate on hashset") {
hashset<int> h;
h->insert (1);
Expand All @@ -68,4 +90,26 @@ TEST_CASE ("iterate on hashset") {
a << as_string (it->next ());
}
CHECK_EQ (a == string ("123"), true);
}

TEST_CASE ("ranged for on hashset") {
SUBCASE ("ordinary") {
hashset<int> h;
h->insert (1);
h->insert (2);
h->insert (3);
string a;
for (auto i : iterate (h)) {
a << as_string (i);
}
string_eq (a, "123");
};
SUBCASE ("empty") {
hashset<int> h;
string a;
for (auto i : iterate (h)) {
a= a * as_string (i);
}
string_eq (a, "");
}
}

0 comments on commit 1e93ae7

Please sign in to comment.