From 0e96412fe4073516a2990489adaee2612667655f Mon Sep 17 00:00:00 2001 From: Fabian Reister <fabian.reister@kit.edu> Date: Thu, 4 Mar 2021 10:17:38 +0100 Subject: [PATCH] removing nested range iterator --- SimoxUtility/iterator.h | 1 - SimoxUtility/iterator/nested_range_iterator.h | 109 ------------------ SimoxUtility/tests/iterator/CMakeLists.txt | 2 - .../tests/iterator/NestedRangeIterator.cpp | 97 ---------------- 4 files changed, 209 deletions(-) delete mode 100644 SimoxUtility/iterator/nested_range_iterator.h delete mode 100644 SimoxUtility/tests/iterator/NestedRangeIterator.cpp diff --git a/SimoxUtility/iterator.h b/SimoxUtility/iterator.h index 5c19f4aca..14e76f038 100644 --- a/SimoxUtility/iterator.h +++ b/SimoxUtility/iterator.h @@ -2,5 +2,4 @@ // This file is generated! -#include "iterator/nested_range_iterator.h" #include "iterator/xy_index_range_iterator.h" diff --git a/SimoxUtility/iterator/nested_range_iterator.h b/SimoxUtility/iterator/nested_range_iterator.h deleted file mode 100644 index 2f13688e0..000000000 --- a/SimoxUtility/iterator/nested_range_iterator.h +++ /dev/null @@ -1,109 +0,0 @@ -#include <exception> -#include <iterator> -#include <vector> - -namespace simox::iterator { - -// FIXME: there is a bug. The last element will not be visited. - - -/** The NestedRangeIterator class -* -* The purpose of this class is to provide an iterator over a nested vector e.g. vector<vector<...>> . -* This iterator can then be passed to all std algorithms. -*/ -template <typename ElementType> -class NestedRangeIterator - : public std::iterator<std::input_iterator_tag, ElementType> { -public: - using InnerRangeType = std::vector<ElementType>; - using NestedRangeType = std::vector<InnerRangeType>; - - typedef typename InnerRangeType::iterator RangeIterator; - typedef typename NestedRangeType::iterator OuterRangeIterator; - - NestedRangeIterator() = delete; - - NestedRangeIterator(NestedRangeType &nested_range) - : NestedRangeIterator(nested_range, nested_range.begin(), - nested_range.begin()->begin()) {} - - NestedRangeIterator(NestedRangeType &nested_range, - OuterRangeIterator it_outer, RangeIterator it_inner) - : nested_range(nested_range), it_outer(it_outer), it_inner(it_inner) {} - - NestedRangeIterator &operator++() { - - if(endReached()){ - return *this; - } - - // first increment inner iterator if possible - // never set to end as it is invalid - if (it_inner != it_outer->end()) { - ++it_inner; - } - - if ((it_inner != it_outer->end())) { - - assert(not endReached()); - return *this; - } - - // we reached end of inner loop - // increment outer iterator - if (it_outer != nested_range.end()) { - ++it_outer; - - if (it_outer != nested_range.end()) { - // now set inner loop iterator to the beginning of that range - it_inner = it_outer->begin(); - } else { - std::cout << "End reached" << std::endl; - } - - return *this; - } - - return *this; - } - - ElementType &operator*() { return *it_inner; } - - const ElementType &operator*() const { return *it_inner; } - - bool operator==(const NestedRangeIterator &other) const { - return (it_inner == other.it_inner) and (it_outer == other.it_outer); - } - - bool operator!=(const NestedRangeIterator &other) const { - return not operator==(other); - } - - NestedRangeIterator begin() { return NestedRangeIterator(nested_range); } - - //! the end is defined as: outer iterator valid, inner invalid - NestedRangeIterator end() { - - const auto &valid_outer = nested_range.end() - 1; - - return NestedRangeIterator(nested_range, valid_outer, valid_outer->end()-1); - } - - bool endReached(){ - const auto &valid_outer = nested_range.end() - 1; - const auto& end_inner = valid_outer->end(); - - return (valid_outer == it_outer) and (end_inner == it_inner); - - // return NestedRangeIterator(nested_range, valid_outer, valid_outer->end()); - } - -private: - NestedRangeType &nested_range; - - OuterRangeIterator it_outer; - RangeIterator it_inner; -}; - -} // namespace simox::iterator \ No newline at end of file diff --git a/SimoxUtility/tests/iterator/CMakeLists.txt b/SimoxUtility/tests/iterator/CMakeLists.txt index a0c9487f6..0d14d2ef6 100644 --- a/SimoxUtility/tests/iterator/CMakeLists.txt +++ b/SimoxUtility/tests/iterator/CMakeLists.txt @@ -1,4 +1,2 @@ -ADD_SU_TEST( NestedRangeIterator ) - ADD_SU_TEST( XYIndexRangeIterator ) \ No newline at end of file diff --git a/SimoxUtility/tests/iterator/NestedRangeIterator.cpp b/SimoxUtility/tests/iterator/NestedRangeIterator.cpp deleted file mode 100644 index 8309be0a6..000000000 --- a/SimoxUtility/tests/iterator/NestedRangeIterator.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * This file is part of ArmarX. - * - * ArmarX is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * ArmarX is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - * @package - * @author Fabian Reister ( fabian dot reister at kit dot edu ) - * @date 2021 - * @copyright http://www.gnu.org/licenses/gpl-2.0.txt - * GNU General Public License - */ - -#include <algorithm> -#include <boost/test/unit_test_suite.hpp> -#include <numeric> -#define BOOST_TEST_MODULE SimoxUtility/iterator/XYIndexRangeIterator - -#include <boost/test/included/unit_test.hpp> - -#include <vector> -#include <iostream> - -#include <SimoxUtility/iterator.h> - - -BOOST_AUTO_TEST_SUITE(XYIndexRangeIterator) - - -BOOST_AUTO_TEST_CASE(test_loop) -{ - // dimensions - constexpr int N = 10; - constexpr int M = 20; - - // create 2D vector of size N*M - const auto v = std::vector<std::vector<int>>(N, std::vector<int>(M, 1)); - - const auto range_it = simox::iterator::XYIndexRangeIterator(v); - - // max counters for test checks - int x_max = 0; - int y_max = 0; - int cnt = 0; - - for(const auto [x,y] : range_it){ - x_max = std::max(x, x_max); - y_max = std::max(x, y_max); - - cnt++; - } - - BOOST_CHECK_EQUAL(x_max, N-1); - BOOST_CHECK_EQUAL(y_max, M-1); - BOOST_CHECK_EQUAL(cnt, N*M); -} - - -BOOST_AUTO_TEST_CASE(test_foreach) -{ - // dimensions - constexpr int N = 10; - constexpr int M = 20; - - // create 2D vector of size N*M - const auto v = std::vector<std::vector<int>>(N, std::vector<int>(M, 1)); - - const auto range_it = simox::iterator::XYIndexRangeIterator(v); - - int x_max = 0; - int y_max = 0; - int cnt = 0; - - std::for_each(range_it.begin(), range_it.end(), [&](const auto& pnt){ - const auto [x,y] = pnt; - - x_max = std::max(x, x_max); - y_max = std::max(x, y_max); - - cnt++; - }); - - BOOST_CHECK_EQUAL(x_max, N-1); - BOOST_CHECK_EQUAL(y_max, M-1); - BOOST_CHECK_EQUAL(cnt, N*M); -} - -BOOST_AUTO_TEST_SUITE_END() -- GitLab