Finish implementing the sequence_range feature.
This commit is contained in:
parent
0e739c9582
commit
221f236f40
4 changed files with 181 additions and 130 deletions
176
include/vectorwrapper/sequence_range.hpp
Normal file
176
include/vectorwrapper/sequence_range.hpp
Normal file
|
@ -0,0 +1,176 @@
|
|||
/*
|
||||
* 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 <iterator>
|
||||
#include <ciso646>
|
||||
#include <cassert>
|
||||
#include <array>
|
||||
|
||||
#if defined VWR_OUTER_NAMESPACE
|
||||
namespace VWR_OUTER_NAMESPACE {
|
||||
#endif
|
||||
|
||||
namespace vwr {
|
||||
namespace op {
|
||||
template <typename T, T Inc=1>
|
||||
struct inc {
|
||||
T operator() (T parVal) const { return parVal + Inc; }
|
||||
};
|
||||
|
||||
template <typename T, T Inc=1>
|
||||
struct dec {
|
||||
T operator() (T parVal) const { return parVal - Inc; }
|
||||
};
|
||||
} //namespace op
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
class sequence_range_iterator : OP, CMP {
|
||||
//static_assert(VectorWrapperInfo<V>::dimensions >= sizeof...(I), "Start index out of valid range");
|
||||
public:
|
||||
sequence_range_iterator (const V& parFrom, const V& parUpper);
|
||||
sequence_range_iterator (const V& parCurrent, const V& parFrom, const V& parUpper);
|
||||
~sequence_range_iterator() = default;
|
||||
|
||||
sequence_range_iterator& operator++ ();
|
||||
const V& operator*() const { return m_current; }
|
||||
bool operator!= (const sequence_range_iterator& parOther) const;
|
||||
bool operator== (const sequence_range_iterator& parOther) const;
|
||||
|
||||
private:
|
||||
V m_current;
|
||||
const V& m_from;
|
||||
const V& m_upper;
|
||||
};
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
class sequence_range {
|
||||
public:
|
||||
typedef sequence_range_iterator<V, OP, CMP, I...> const_iterator;
|
||||
|
||||
sequence_range (const V& parFrom, const V& parUpper);
|
||||
const_iterator cbegin() const;
|
||||
const_iterator cend() const;
|
||||
const_iterator begin() const { return cbegin(); }
|
||||
const_iterator end() const { return cend(); }
|
||||
|
||||
private:
|
||||
V m_from;
|
||||
V m_upper;
|
||||
V m_end;
|
||||
};
|
||||
|
||||
namespace implem {
|
||||
template <typename T, typename OP, typename CMP, typename U>
|
||||
struct make_sequence_helper;
|
||||
|
||||
template <typename T, typename OP, typename CMP, size_type... I>
|
||||
struct make_sequence_helper<T, OP, CMP, bt::number_seq<size_type, I...>> {
|
||||
typedef sequence_range<Vec<T>, OP, CMP, I...> type;
|
||||
};
|
||||
|
||||
template <typename V>
|
||||
inline V make_end_vector (V parFrom, const V& parUpper) {
|
||||
auto& last = parFrom[V::dimensions - 1];
|
||||
const auto& upper_last = parUpper[V::dimensions - 1];
|
||||
last = upper_last;
|
||||
return parFrom;
|
||||
}
|
||||
} //namespace implem
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
sequence_range_iterator<V, OP, CMP, I...>::sequence_range_iterator (const V& parFrom, const V& parUpper) :
|
||||
m_current(parFrom),
|
||||
m_from(parFrom),
|
||||
m_upper(parUpper)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
sequence_range_iterator<V, OP, CMP, I...>::sequence_range_iterator (const V& parCurrent, const V& parFrom, const V& parUpper) :
|
||||
m_current(parCurrent),
|
||||
m_from(parFrom),
|
||||
m_upper(parUpper)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
auto sequence_range_iterator<V, OP, CMP, I...>::operator++ () -> sequence_range_iterator& {
|
||||
static_assert(sizeof...(I) > 0, "At least one index must be given");
|
||||
const OP& advance_op = *static_cast<OP*>(this);
|
||||
const CMP& cmp_op = *static_cast<CMP*>(this);
|
||||
|
||||
std::array<size_type, sizeof...(I)> 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 <typename V, typename OP, typename CMP, size_type... I>
|
||||
bool sequence_range_iterator<V, OP, CMP, I...>::operator!= (const sequence_range_iterator& parOther) const {
|
||||
assert(&m_from == &parOther.m_from and &m_upper == &parOther.m_upper);
|
||||
return m_current != parOther.m_current;
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
bool sequence_range_iterator<V, OP, CMP, I...>::operator== (const sequence_range_iterator& parOther) const {
|
||||
assert(&m_from == &parOther.m_from and &m_upper == &parOther.m_upper);
|
||||
return m_current == parOther.m_current;
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
sequence_range<V, OP, CMP, I...>::sequence_range (const V& parFrom, const V& parUpper) :
|
||||
m_from(parFrom),
|
||||
m_upper(parUpper),
|
||||
m_end(implem::make_end_vector<V>(parFrom, parUpper))
|
||||
{
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
auto sequence_range<V, OP, CMP, I...>::cbegin() const -> const_iterator {
|
||||
return sequence_range_iterator<V, OP, CMP, I...>(m_from, m_upper);
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
auto sequence_range<V, OP, CMP, I...>::cend() const -> const_iterator {
|
||||
return sequence_range_iterator<V, OP, CMP, I...>(m_end, m_from, m_upper);
|
||||
}
|
||||
|
||||
template <typename V>
|
||||
using increasing_sequence_range = typename implem::make_sequence_helper<
|
||||
typename V::vector_type,
|
||||
op::inc<typename V::scalar_type>,
|
||||
std::less<typename V::scalar_type>,
|
||||
bt::number_range<size_type, 0, V::dimensions>
|
||||
>::type;
|
||||
} //namespace vwr
|
||||
|
||||
#if defined VWR_OUTER_NAMESPACE
|
||||
} //namespace VWR_OUTER_NAMESPACE
|
||||
#endif
|
|
@ -1,126 +0,0 @@
|
|||
/*
|
||||
* 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 <iterator>
|
||||
#include <ciso646>
|
||||
#include <cassert>
|
||||
#include <array>
|
||||
|
||||
#if defined VWR_OUTER_NAMESPACE
|
||||
namespace VWR_OUTER_NAMESPACE {
|
||||
#endif
|
||||
|
||||
namespace vwr {
|
||||
namespace op {
|
||||
template <typename T, T Inc=1>
|
||||
struct inc {
|
||||
T operator() (T parVal) const { return parVal + Inc; }
|
||||
};
|
||||
|
||||
template <typename T, T Inc=1>
|
||||
struct dec {
|
||||
T operator() (T parVal) const { return parVal - Inc; }
|
||||
};
|
||||
} //namespace op
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
class vector_iterator : OP, CMP {
|
||||
//static_assert(VectorWrapperInfo<V>::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 <typename V, typename OP, typename CMP, size_type... I>
|
||||
vector_iterator<V, OP, CMP, I...>::vector_iterator (const V& parFrom, const V& parUpper) :
|
||||
m_current(parFrom),
|
||||
m_from(parFrom),
|
||||
m_upper(parUpper)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
auto vector_iterator<V, OP, CMP, I...>::operator++ () -> vector_iterator& {
|
||||
static_assert(sizeof...(I) > 0, "At least one index must be given");
|
||||
const OP& advance_op = *static_cast<OP*>(this);
|
||||
const CMP& cmp_op = *static_cast<CMP*>(this);
|
||||
|
||||
std::array<size_type, sizeof...(I)> 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 <typename V, typename OP, typename CMP, size_type... I>
|
||||
bool vector_iterator<V, OP, CMP, I...>::operator!= (const vector_iterator& parOther) const {
|
||||
return m_current != parOther.m_current;
|
||||
}
|
||||
|
||||
template <typename V, typename OP, typename CMP, size_type... I>
|
||||
bool vector_iterator<V, OP, CMP, I...>::operator== (const vector_iterator& parOther) const {
|
||||
return m_current == parOther.m_current;
|
||||
}
|
||||
|
||||
template <typename OP, typename CMP, typename T, size_type... I>
|
||||
inline vector_iterator<T, OP, CMP, I...> make_vector_iterator (
|
||||
const Vec<T>& parFrom,
|
||||
const Vec<T>& parUpper,
|
||||
bt::number_seq<size_type, I...>
|
||||
) {
|
||||
return vector_iterator<Vec<T>, OP, CMP, I...>(parFrom, parUpper);
|
||||
}
|
||||
|
||||
template <typename OP, typename CMP, typename T, size_type... I>
|
||||
inline vector_iterator<T, OP, CMP, I...> make_vector_iterator_end (
|
||||
Vec<T> parFrom,
|
||||
const Vec<T>& parUpper,
|
||||
bt::number_seq<size_type, I...>
|
||||
) {
|
||||
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<Vec<T>, OP, CMP, I...>(parFrom, parUpper);
|
||||
}
|
||||
} //namespace vwr
|
||||
|
||||
#if defined VWR_OUTER_NAMESPACE
|
||||
} //namespace VWR_OUTER_NAMESPACE
|
||||
#endif
|
|
@ -7,7 +7,7 @@ add_executable(${PROJECT_NAME}
|
|||
example.cpp
|
||||
test_get_at.cpp
|
||||
test_operators.cpp
|
||||
test_vector_iterator.cpp
|
||||
test_sequence_range.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
*/
|
||||
|
||||
#include "sample_vectors.hpp"
|
||||
#include "vectorwrapper/vector_iterator.hpp"
|
||||
#include "vectorwrapper/sequence_range.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <functional>
|
||||
#include <array>
|
||||
|
@ -26,8 +26,9 @@ TEST(vwr, vector_iterator) {
|
|||
|
||||
ivec3 from(100, 200, 300);
|
||||
ivec3 to(102, 203, 304);
|
||||
auto it = make_vector_iterator<op::inc<int64_t>, std::less<int64_t>>(from, to, bt::number_range<size_type, 0, 3>());
|
||||
auto it_end = make_vector_iterator_end<op::inc<int64_t>, std::less<int64_t>>(from, to, bt::number_range<size_type, 0, 3>());
|
||||
auto seq = increasing_sequence_range<ivec3>(from, to);
|
||||
auto it = seq.begin();
|
||||
auto it_end = seq.end();
|
||||
|
||||
std::array<ivec3, 24> expected{
|
||||
ivec3(100, 200, 300),
|
Loading…
Reference in a new issue