Skip to content

Commit

Permalink
added iterators to interval tree class
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Feb 3, 2024
1 parent b2ad976 commit 62e76e4
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/classes/tree/interval_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ template <typename T> class interval_tree {
return i1.high >= i2.low && i1.low <= i2.high;
}

class Iterator;

/**
* @brief pointer that points to begin
*
* @return Iterator
*/
Iterator begin() {
std::vector<std::pair<T, T>> ino = this->inorder();
return Iterator(0, ino);
}

/**
* @brief pointer that points to end
*
* @return Iterator
*/
Iterator end() {
std::vector<std::pair<T, T>> ino = this->inorder();
return Iterator(ino.size(), ino);
}

/**
*@brief inorder function.
*@returns vector<pair<T,T>>, the elements inorder.
Expand Down Expand Up @@ -338,4 +360,75 @@ template <typename T> class interval_tree {
}
};

/**
* @brief Iterator class
*/
template <typename T> class interval_tree<T>::Iterator {
private:
std::vector<std::pair<T, T>> elements;
int64_t index;

public:
/**
* @brief Construct a new Iterator object
*
* @param els vector<T> - the elements in inorder fashion
*/
explicit Iterator(const int64_t &index,
std::vector<std::pair<T, T>> &els) noexcept
: index(index), elements(els) {}

/**
* @brief = operator for Iterator type
*
* @param index the current index
* @return Iterator&
*/
Iterator &operator=(int64_t index) {
this->index = index;
return *(this);
}

/**
* @brief operator ++ for type Iterator
*
* @return Iterator&
*/
Iterator &operator++() {
if (this->index < elements.size()) {
this->index++;
}
return *(this);
}

/**
* @brief operator ++ for type Iterator
*
* @return Iterator
*/
Iterator operator++(int) {
Iterator it = *this;
++*(this);
return it;
}

/**
* @brief operator != for type Iterator
*
* @param it const Iterator
* @return true if index == it.index
* @return false otherwise
*/
bool operator!=(const Iterator &it) { return index != it.index; }

/**
* @brief operator * for type Iterator
*
* @return T the value of the node
*/
std::pair<T, T> operator*() {
return {elements[index].first, elements[index].second};
}
};

#endif
10 changes: 10 additions & 0 deletions tests/tree/interval_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ TEST_CASE("testing remove") {
REQUIRE(i.search({'w', 'w'}) == false);
REQUIRE(i.search({'c', 'd'}));
REQUIRE(i.search({'a', 'd'}) == true);
}

TEST_CASE("testing iterators") {
interval_tree<int> i({{1, 3}, {5, 6}, {2, 4}, {9, 10}});
std::vector<std::pair<int, int>> els = i.inorder();
std::vector<std::pair<int, int>> check;
for (auto it = i.begin(); it != i.end(); it++) {
check.push_back(*(it));
}
REQUIRE(check == els);
}

0 comments on commit 62e76e4

Please sign in to comment.