Commit 9eb06e92 by Maarten L. Hekkelman

fixes in iterators

parent 629e06d6
...@@ -84,11 +84,11 @@ class iterator_impl ...@@ -84,11 +84,11 @@ class iterator_impl
iterator_impl() = default; iterator_impl() = default;
iterator_impl(const iterator_impl &rhs) = default; iterator_impl(const iterator_impl &rhs) = default;
iterator_impl(iterator_impl &&rhs) = default;
template <typename C2, typename... T2s> template <typename C2, typename... T2s>
iterator_impl(const iterator_impl<C2, T2s...> &rhs) iterator_impl(const iterator_impl<C2, T2s...> &rhs)
: m_category(rhs.m_category) : m_current(const_cast<row_handle&>(rhs.m_current))
, m_current(rhs.m_current)
, m_value(rhs.m_value) , m_value(rhs.m_value)
, m_item_ix(rhs.m_item_ix) , m_item_ix(rhs.m_item_ix)
{ {
...@@ -96,8 +96,7 @@ class iterator_impl ...@@ -96,8 +96,7 @@ class iterator_impl
template <typename IRowType> template <typename IRowType>
iterator_impl(iterator_impl<IRowType, Ts...> &rhs) iterator_impl(iterator_impl<IRowType, Ts...> &rhs)
: m_category(rhs.m_category) : m_current(const_cast<row_handle&>(rhs.m_current))
, m_current(const_cast<row_type *>(rhs.m_current))
, m_value(rhs.m_value) , m_value(rhs.m_value)
, m_item_ix(rhs.m_item_ix) , m_item_ix(rhs.m_item_ix)
{ {
...@@ -106,19 +105,17 @@ class iterator_impl ...@@ -106,19 +105,17 @@ class iterator_impl
template <typename IRowType> template <typename IRowType>
iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, N> &cix) iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, N> &cix)
: m_category(rhs.m_category) : m_current(const_cast<row_handle&>(rhs.m_current))
, m_current(rhs.m_current)
, m_item_ix(cix) , m_item_ix(cix)
{ {
m_value = get(std::make_index_sequence<N>()); m_value = get(std::make_index_sequence<N>());
} }
iterator_impl &operator=(const iterator_impl &i) iterator_impl &operator=(iterator_impl i)
{ {
m_category = i.m_category; std::swap(m_current, i.m_current);
m_current = i.m_current; std::swap(m_item_ix, i.m_item_ix);
m_item_ix = i.m_item_ix; std::swap(m_value, i.m_value);
m_value = i.m_value;
return *this; return *this;
} }
...@@ -136,18 +133,18 @@ class iterator_impl ...@@ -136,18 +133,18 @@ class iterator_impl
operator const row_handle() const operator const row_handle() const
{ {
return { *m_category, *m_current }; return m_current;
} }
operator row_handle() operator row_handle()
{ {
return { *m_category, *m_current }; return m_current;
} }
iterator_impl &operator++() iterator_impl &operator++()
{ {
if (m_current != nullptr) if (m_current)
m_current = m_current->m_next; m_current.m_row = m_current.m_row->m_next;
m_value = get(std::make_index_sequence<N>()); m_value = get(std::make_index_sequence<N>());
...@@ -182,17 +179,10 @@ class iterator_impl ...@@ -182,17 +179,10 @@ class iterator_impl
template <size_t... Is> template <size_t... Is>
tuple_type get(std::index_sequence<Is...>) const tuple_type get(std::index_sequence<Is...>) const
{ {
if (m_current != nullptr) return m_current ? tuple_type{ m_current[m_item_ix[Is]].template as<Ts>()... } : tuple_type{};
{
row_handle rh{ *m_category, *m_current };
return tuple_type{ rh[m_item_ix[Is]].template as<Ts>()... };
}
return {};
} }
category_type *m_category = nullptr; row_handle m_current;
row_type *m_current = nullptr;
value_type m_value; value_type m_value;
std::array<uint16_t, N> m_item_ix; std::array<uint16_t, N> m_item_ix;
}; };
...@@ -219,37 +209,34 @@ class iterator_impl<Category> ...@@ -219,37 +209,34 @@ class iterator_impl<Category>
using iterator_category = std::forward_iterator_tag; using iterator_category = std::forward_iterator_tag;
using value_type = row_handle; using value_type = row_handle;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using pointer = row_handle; using pointer = value_type *;
using reference = row_handle; using reference = value_type &;
iterator_impl() = default; iterator_impl() = default;
iterator_impl(const iterator_impl &rhs) = default; iterator_impl(const iterator_impl &rhs) = default;
iterator_impl(iterator_impl &&rhs) = default;
template <typename C2> template <typename C2>
iterator_impl(const iterator_impl<C2> &rhs) iterator_impl(const iterator_impl<C2> &rhs)
: m_category(rhs.m_category) : m_current(const_cast<row_handle &>(rhs.m_current))
, m_current(const_cast<row_type *>(rhs.m_current))
{ {
} }
iterator_impl(Category &cat, row *current) iterator_impl(Category &cat, row *current)
: m_category(const_cast<category_type *>(&cat)) : m_current(cat, *current)
, m_current(current)
{ {
} }
template <typename IRowType> template <typename IRowType>
iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, 0> &) iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, 0> &)
: m_category(rhs.m_category) : m_current(const_cast<row_handle &>(rhs.m_current))
, m_current(rhs.m_current)
{ {
} }
iterator_impl &operator=(const iterator_impl &i) iterator_impl &operator=(iterator_impl i)
{ {
m_category = i.m_category; std::swap(m_current, i.m_current);
m_current = i.m_current;
return *this; return *this;
} }
...@@ -257,7 +244,7 @@ class iterator_impl<Category> ...@@ -257,7 +244,7 @@ class iterator_impl<Category>
reference operator*() reference operator*()
{ {
return { *m_category, *m_current }; return m_current;
} }
pointer operator->() pointer operator->()
...@@ -267,18 +254,18 @@ class iterator_impl<Category> ...@@ -267,18 +254,18 @@ class iterator_impl<Category>
operator const row_handle() const operator const row_handle() const
{ {
return { *m_category, *m_current }; return m_current;
} }
operator row_handle() operator row_handle()
{ {
return { *m_category, *m_current }; return m_current;
} }
iterator_impl &operator++() iterator_impl &operator++()
{ {
if (m_current != nullptr) if (m_current)
m_current = m_current->m_next; m_current.m_row = m_current.m_row->m_next;
return *this; return *this;
} }
...@@ -308,8 +295,7 @@ class iterator_impl<Category> ...@@ -308,8 +295,7 @@ class iterator_impl<Category>
/** @endcond */ /** @endcond */
private: private:
category_type *m_category = nullptr; row_handle m_current;
row_type *m_current = nullptr;
}; };
/** /**
...@@ -342,11 +328,11 @@ class iterator_impl<Category, T> ...@@ -342,11 +328,11 @@ class iterator_impl<Category, T>
iterator_impl() = default; iterator_impl() = default;
iterator_impl(const iterator_impl &rhs) = default; iterator_impl(const iterator_impl &rhs) = default;
iterator_impl(iterator_impl &&rhs) = default;
template <typename C2, typename T2> template <typename C2, typename T2>
iterator_impl(const iterator_impl<C2, T2> &rhs) iterator_impl(const iterator_impl<C2, T2> &rhs)
: m_category(rhs.m_category) : m_current(rhs.m_current)
, m_current(rhs.m_current)
, m_value(rhs.m_value) , m_value(rhs.m_value)
, m_item_ix(rhs.m_item_ix) , m_item_ix(rhs.m_item_ix)
{ {
...@@ -354,29 +340,26 @@ class iterator_impl<Category, T> ...@@ -354,29 +340,26 @@ class iterator_impl<Category, T>
template <typename IRowType> template <typename IRowType>
iterator_impl(iterator_impl<IRowType, T> &rhs) iterator_impl(iterator_impl<IRowType, T> &rhs)
: m_category(rhs.m_category) : m_current(const_cast<row_handle&>(rhs.m_current))
, m_current(const_cast<row_type *>(rhs.m_current))
, m_value(rhs.m_value) , m_value(rhs.m_value)
, m_item_ix(rhs.m_item_ix) , m_item_ix(rhs.m_item_ix)
{ {
m_value = get(m_current); m_value = get();
} }
template <typename IRowType> template <typename IRowType>
iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, 1> &cix) iterator_impl(const iterator_impl<IRowType> &rhs, const std::array<uint16_t, 1> &cix)
: m_category(rhs.m_category) : m_current(const_cast<row_handle&>(rhs.m_current))
, m_current(rhs.m_current)
, m_item_ix(cix[0]) , m_item_ix(cix[0])
{ {
m_value = get(); m_value = get();
} }
iterator_impl &operator=(const iterator_impl &i) iterator_impl &operator=(iterator_impl i)
{ {
m_category = i.m_category; std::swap(m_current, i.m_current);
m_current = i.m_current; std::swap(m_item_ix, i.m_item_ix);
m_item_ix = i.m_item_ix; std::swap(m_value, i.m_value);
m_value = i.m_value;
return *this; return *this;
} }
...@@ -394,18 +377,18 @@ class iterator_impl<Category, T> ...@@ -394,18 +377,18 @@ class iterator_impl<Category, T>
operator const row_handle() const operator const row_handle() const
{ {
return { *m_category, *m_current }; return m_current;
} }
operator row_handle() operator row_handle()
{ {
return { *m_category, *m_current }; return m_current;
} }
iterator_impl &operator++() iterator_impl &operator++()
{ {
if (m_current != nullptr) if (m_current)
m_current = m_current->m_next; m_current.m_row = m_current.m_row->m_next;
m_value = get(); m_value = get();
...@@ -439,17 +422,10 @@ class iterator_impl<Category, T> ...@@ -439,17 +422,10 @@ class iterator_impl<Category, T>
private: private:
value_type get() const value_type get() const
{ {
if (m_current != nullptr) return m_current ? m_current[m_item_ix].template as<value_type>() : value_type{};
{
row_handle rh{ *m_category, *m_current };
return rh[m_item_ix].template as<T>();
}
return {};
} }
category_type *m_category = nullptr; row_handle m_current;
row_type *m_current = nullptr;
value_type m_value; value_type m_value;
uint16_t m_item_ix; uint16_t m_item_ix;
}; };
......
...@@ -208,6 +208,7 @@ class row_handle ...@@ -208,6 +208,7 @@ class row_handle
friend class category; friend class category;
friend class category_index; friend class category_index;
friend class row_initializer; friend class row_initializer;
template <typename, typename...> friend class iterator_impl;
row_handle() = default; row_handle() = default;
......
...@@ -1659,7 +1659,7 @@ category::iterator category::insert_impl(const_iterator pos, row *n) ...@@ -1659,7 +1659,7 @@ category::iterator category::insert_impl(const_iterator pos, row *n)
m_index->insert(*this, n); m_index->insert(*this, n);
// insert at end, most often this is the case // insert at end, most often this is the case
if (pos.m_current == nullptr) if (pos.m_current.m_row == nullptr)
{ {
if (m_head == nullptr) if (m_head == nullptr)
m_tail = m_head = n; m_tail = m_head = n;
...@@ -1670,7 +1670,7 @@ category::iterator category::insert_impl(const_iterator pos, row *n) ...@@ -1670,7 +1670,7 @@ category::iterator category::insert_impl(const_iterator pos, row *n)
{ {
assert(m_head != nullptr); assert(m_head != nullptr);
if (pos.m_current == m_head) if (pos.m_current.m_row == m_head)
m_head = n->m_next = m_head; m_head = n->m_next = m_head;
else else
n = n->m_next = m_head->m_next; n = n->m_next = m_head->m_next;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment