Properly stop looping when vec values go past the end.

This commit is contained in:
King_DuckZ 2017-08-02 23:37:25 +01:00
parent 445d7b5e27
commit d9dacb739a
2 changed files with 30 additions and 8 deletions

View file

@ -83,6 +83,7 @@ namespace vwr {
private: private:
V m_current; V m_current;
const sequence_range_type& m_seq; const sequence_range_type& m_seq;
size_type m_active_elem;
}; };
template <typename V, typename OP, typename CMP, size_type... I> template <typename V, typename OP, typename CMP, size_type... I>
@ -118,10 +119,14 @@ namespace vwr {
namespace implem { namespace implem {
template <size_type SEEK, size_type IDX, size_type... I> template <size_type SEEK, size_type IDX, size_type... I>
struct get_at_index; struct get_at_index;
template <size_type SEEK, size_type IDX, size_type I> template <size_type N>
struct get_at_index<SEEK, IDX, I> { struct get_at_index<N, N> {
static_assert(SEEK == IDX, "Index out of range"); static constexpr size_type value = N;
static constexpr size_type value = I; };
template <size_type SEEK, size_type IDX>
struct get_at_index<SEEK, IDX> {
static_assert(SEEK < IDX, "Index out of range");
static constexpr size_type value = 0;
}; };
template <size_type SEEK, size_type IDX, size_type L, size_type... I> template <size_type SEEK, size_type IDX, size_type L, size_type... I>
struct get_at_index<SEEK, IDX, L, I...> { struct get_at_index<SEEK, IDX, L, I...> {
@ -155,7 +160,8 @@ namespace vwr {
template <typename V, typename OP, typename CMP, size_type... I> template <typename V, typename OP, typename CMP, size_type... I>
sequence_range_iterator<V, OP, CMP, I...>::sequence_range_iterator (const V& parCurrent, const sequence_range_type& parSeq) : sequence_range_iterator<V, OP, CMP, I...>::sequence_range_iterator (const V& parCurrent, const sequence_range_type& parSeq) :
m_current(parCurrent), m_current(parCurrent),
m_seq(parSeq) m_seq(parSeq),
m_active_elem(implem::get_at_index<0, 0, I...>::value)
{ {
} }
@ -174,6 +180,7 @@ namespace vwr {
m_current[index] = m_seq.from()[index]; m_current[index] = m_seq.from()[index];
index = lst[count++]; index = lst[count++];
m_current[index] = advance_op(m_current[index], m_seq.step()[index]); m_current[index] = advance_op(m_current[index], m_seq.step()[index]);
m_active_elem = index;
} while (count < sizeof...(I) and not cmp_op(m_current[index], m_seq.upper()[index])); } while (count < sizeof...(I) and not cmp_op(m_current[index], m_seq.upper()[index]));
} }
@ -182,14 +189,17 @@ namespace vwr {
template <typename V, typename OP, typename CMP, size_type... I> 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 { bool sequence_range_iterator<V, OP, CMP, I...>::operator!= (const sequence_range_iterator& parOther) const {
assert(&m_seq.from() == &parOther.m_seq.from() and &m_seq.upper() == &parOther.m_seq.upper()); return not this->operator==(parOther);
return m_current != parOther.m_current;
} }
template <typename V, typename OP, typename CMP, size_type... I> 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 { bool sequence_range_iterator<V, OP, CMP, I...>::operator== (const sequence_range_iterator& parOther) const {
assert(&m_seq.from() == &parOther.m_seq.from() and &m_seq.upper() == &parOther.m_seq.upper()); assert(&m_seq.from() == &parOther.m_seq.from() and &m_seq.upper() == &parOther.m_seq.upper());
return m_current == parOther.m_current; const CMP& cmp_op = *static_cast<const CMP*>(this);
return m_current == parOther.m_current or (
implem::get_at_index<sizeof...(I) - 1, 0, I...>::value == m_active_elem and
not cmp_op(m_current[m_active_elem], m_seq.upper()[m_active_elem])
);
} }
template <typename V, typename OP, typename CMP, size_type... I> template <typename V, typename OP, typename CMP, size_type... I>

View file

@ -82,4 +82,16 @@ TEST(vwr, vector_iterator) {
EXPECT_EQ(expected[z] + 1, results[results.size() - 1 - z]); EXPECT_EQ(expected[z] + 1, results[results.size() - 1 - z]);
} }
} }
{
auto seq = increasing_sequence_range<ivec3>(from, to, ivec3(3));
std::vector<ivec3> results;
for (auto& itm : seq) {
results.push_back(itm);
}
ASSERT_EQ(2, results.size());
EXPECT_EQ(ivec3(100, 200, 300), results[0]);
EXPECT_EQ(ivec3(100, 200, 303), results[1]);
}
} }