From 0e739c9582e4e6fcf67e909a33aa58f7125a8b08 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 26 Jul 2017 13:52:30 +0100 Subject: [PATCH] Broken implementation of vector_iterator. WiP --- .gitignore | 1 + include/vectorwrapper/vector_iterator.hpp | 126 ++++++++++++++++++++++ test/unit/CMakeLists.txt | 1 + test/unit/test_vector_iterator.cpp | 67 ++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 .gitignore create mode 100644 include/vectorwrapper/vector_iterator.hpp create mode 100644 test/unit/test_vector_iterator.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e92f57 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags diff --git a/include/vectorwrapper/vector_iterator.hpp b/include/vectorwrapper/vector_iterator.hpp new file mode 100644 index 0000000..0b92422 --- /dev/null +++ b/include/vectorwrapper/vector_iterator.hpp @@ -0,0 +1,126 @@ +/* + * Copyright 2015-2017 Michele "King_DuckZ" Santullo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "vectorwrapper.hpp" +#include "sequence_bt.hpp" +#include "size_type.hpp" +#include +#include +#include +#include + +#if defined VWR_OUTER_NAMESPACE +namespace VWR_OUTER_NAMESPACE { +#endif + +namespace vwr { + namespace op { + template + struct inc { + T operator() (T parVal) const { return parVal + Inc; } + }; + + template + struct dec { + T operator() (T parVal) const { return parVal - Inc; } + }; + } //namespace op + + template + class vector_iterator : OP, CMP { + //static_assert(VectorWrapperInfo::dimensions >= sizeof...(I), "Start index out of valid range"); + public: + vector_iterator (const V& parFrom, const V& parUpper); + ~vector_iterator() = default; + + vector_iterator& operator++ (); + const V& operator*() const { return m_current; } + bool operator!= (const vector_iterator& parOther) const; + bool operator== (const vector_iterator& parOther) const; + + private: + V m_current; + V m_from; + V m_upper; + }; + + template + vector_iterator::vector_iterator (const V& parFrom, const V& parUpper) : + m_current(parFrom), + m_from(parFrom), + m_upper(parUpper) + { + } + + template + auto vector_iterator::operator++ () -> vector_iterator& { + static_assert(sizeof...(I) > 0, "At least one index must be given"); + const OP& advance_op = *static_cast(this); + const CMP& cmp_op = *static_cast(this); + + std::array lst {I...}; + size_type index = lst[0]; + m_current[index] = advance_op(m_current[index]); + if (1 < sizeof...(I) and not cmp_op(m_current[index], m_upper[index])) { + size_type count = 1; + do { + m_current[index] = m_from[index]; + index = lst[count++]; + m_current[index] = advance_op(m_current[index]); + } while (count < sizeof...(I) and not cmp_op(m_current[index], m_upper[index])); + } + + return *this; + } + + template + bool vector_iterator::operator!= (const vector_iterator& parOther) const { + return m_current != parOther.m_current; + } + + template + bool vector_iterator::operator== (const vector_iterator& parOther) const { + return m_current == parOther.m_current; + } + + template + inline vector_iterator make_vector_iterator ( + const Vec& parFrom, + const Vec& parUpper, + bt::number_seq + ) { + return vector_iterator, OP, CMP, I...>(parFrom, parUpper); + } + + template + inline vector_iterator make_vector_iterator_end ( + Vec parFrom, + const Vec& parUpper, + bt::number_seq + ) { + OP advance_op; + auto& last = parFrom[sizeof...(I) - 1]; + const auto& upper_last = parUpper[sizeof...(I) - 1]; + last = advance_op(upper_last); + return vector_iterator, OP, CMP, I...>(parFrom, parUpper); + } +} //namespace vwr + +#if defined VWR_OUTER_NAMESPACE +} //namespace VWR_OUTER_NAMESPACE +#endif diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 52abae0..1ed2532 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(${PROJECT_NAME} example.cpp test_get_at.cpp test_operators.cpp + test_vector_iterator.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/test/unit/test_vector_iterator.cpp b/test/unit/test_vector_iterator.cpp new file mode 100644 index 0000000..c4e818b --- /dev/null +++ b/test/unit/test_vector_iterator.cpp @@ -0,0 +1,67 @@ +/* + * Copyright 2015-2017 Michele "King_DuckZ" Santullo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sample_vectors.hpp" +#include "vectorwrapper/vector_iterator.hpp" +#include +#include +#include +#include + +TEST(vwr, vector_iterator) { + using namespace vwr; + + ivec3 from(100, 200, 300); + ivec3 to(102, 203, 304); + auto it = make_vector_iterator, std::less>(from, to, bt::number_range()); + auto it_end = make_vector_iterator_end, std::less>(from, to, bt::number_range()); + + std::array expected{ + ivec3(100, 200, 300), + ivec3(101, 200, 300), + ivec3(100, 201, 300), + ivec3(101, 201, 300), + ivec3(100, 202, 300), + ivec3(101, 202, 300), + ivec3(100, 200, 301), + ivec3(101, 200, 301), + ivec3(100, 201, 301), + ivec3(101, 201, 301), + ivec3(100, 202, 301), + ivec3(101, 202, 301), + ivec3(100, 200, 302), + ivec3(101, 200, 302), + ivec3(100, 201, 302), + ivec3(101, 201, 302), + ivec3(100, 202, 302), + ivec3(101, 202, 302), + ivec3(100, 200, 303), + ivec3(101, 200, 303), + ivec3(100, 201, 303), + ivec3(101, 201, 303), + ivec3(100, 202, 303), + ivec3(101, 202, 303) + }; + std::vector results; + for (; it != it_end; ++it) { + results.push_back(*it); + } + + EXPECT_EQ(expected.size(), results.size()); + for (std::size_t z = 0; z < expected.size(); ++z) { + EXPECT_EQ(expected[z], results[z]); + } +}