Commit b198b960 authored by Quentin Khan's avatar Quentin Khan

Extract FNodeIterator position and direction from template definition

This generates less classes and allows some overloading that did not
work for const iterators.
parent c230172c
......@@ -13,7 +13,7 @@ template<class Node>
class FInOrderNodeIterator : public FNodeIterator<FInOrderNodeIterator<Node>, Node> {
using base_t = FNodeIterator<FInOrderNodeIterator<Node>, Node>;
using Direction = typename base_t::Direction;
using Direction = scalfmm::FNodeIteratorDirection;
friend scalfmm::tests::test_InOrderNodeIterator;
friend base_t;
......
......@@ -6,9 +6,13 @@
#include "Utils/FConstFuncs.hpp"
namespace scalfmm {
namespace tests {
struct test_NodeIterator;
}
namespace tests {
struct test_NodeIterator;
}
enum class FNodeIteratorPosition {begin, end, reverse_end};
enum class FNodeIteratorDirection {forwards, backwards};
}
template<class Derived, class Node>
......@@ -27,10 +31,8 @@ public:
using iterator_category = std::bidirectional_iterator_tag;
constexpr static std::size_t child_count = node_t::child_count;
enum class IteratorPosition {begin, end, reverse_end};
protected:
enum class Direction {forwards, backwards};
using state_container = std::vector<char>;
using index_t = typename state_container::value_type;
......@@ -57,13 +59,13 @@ public:
*
* \warning This constructor must be completed by the derived class.
* It does not set #_cur nor #_cur_state to the right value. */
FNodeIterator(node_t* root, IteratorPosition p = IteratorPosition::begin) :
FNodeIterator(node_t* root, scalfmm::FNodeIteratorPosition p = scalfmm::FNodeIteratorPosition::begin) :
_root(root),
_is_end(p == IteratorPosition::end),
_is_rend(p == IteratorPosition::reverse_end)
_is_end(p == scalfmm::FNodeIteratorPosition::end),
_is_rend(p == scalfmm::FNodeIteratorPosition::reverse_end)
{
if(!_is_end && ! _is_rend) {
static_cast<Derived*>(this)->template move_to_boundary<Direction::forwards>();
static_cast<Derived*>(this)->template move_to_boundary<scalfmm::FNodeIteratorDirection::forwards>();
}
}
......@@ -174,7 +176,7 @@ protected:
// Methods used in all subclasses
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
void goto_root() {
this->_is_rend = false;
this->_is_end = false;
......@@ -184,21 +186,21 @@ protected:
}
/** Returns the end flag corresponding to the direction */
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
bool is_end() {
return dir == Direction::forwards ? this->_is_end : this->_is_rend;
return dir == scalfmm::FNodeIteratorDirection::forwards ? this->_is_end : this->_is_rend;
}
/** Returns the end flag corresponding to the oposite direction */
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
bool is_other_end() {
return dir == Direction::forwards ? this->_is_rend : this->_is_end;
return dir == scalfmm::FNodeIteratorDirection::forwards ? this->_is_rend : this->_is_end;
}
/** Sets the end flag according to the direction */
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
void set_end() {
if(dir == Direction::forwards) {
if(dir == scalfmm::FNodeIteratorDirection::forwards) {
this->_is_end = true;
} else {
this->_is_rend = true;
......@@ -206,16 +208,16 @@ protected:
}
/** Returns the first index to insert in #_cur_state */
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
index_t first_idx() const {
return dir == Direction::forwards ? -1 : (index_t)Node::child_count;
return dir == scalfmm::FNodeIteratorDirection::forwards ? -1 : (index_t)Node::child_count;
}
/** Indicates whether the last child of a node has been visited */
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
bool at_last_child() const {
if(dir == Direction::forwards) { // Compile time optimisation will remove unused branch
if(dir == scalfmm::FNodeIteratorDirection::forwards) { // Compile time optimisation will remove unused branch
return Node::child_count-1 == this->_cur_state.back();
} else {
return 0 == this->_cur_state.back();
......@@ -235,9 +237,9 @@ protected:
*
* \warning Undefined behaviour when the cursor points to a leaf.
*/
template<Direction dir>
template<scalfmm::FNodeIteratorDirection dir>
void move_down() {
if(dir == Direction::forwards) { // Compile time optimisation will remove unused branch
if(dir == scalfmm::FNodeIteratorDirection::forwards) { // Compile time optimisation will remove unused branch
++(this->_cur_state.back());
} else {
--(this->_cur_state.back());
......
......@@ -3,6 +3,8 @@
#include <iterator>
#include "FNodeIterator.hpp"
template <class Node, template <class> class NodeIterator>
class FNodeIteratorBox {
using node_t = Node;
......@@ -26,15 +28,15 @@ public:
}
iterator end() {
return iterator(_root, iterator::IteratorPosition::end);
return iterator(_root, scalfmm::FNodeIteratorPosition::end);
}
const_iterator begin() const {
return const_iterator(_root);
return const_iterator(_root, scalfmm::FNodeIteratorPosition::begin);
}
const_iterator end() const {
return const_iterator(_root, iterator::IteratorPosition::end);
return const_iterator(_root, scalfmm::FNodeIteratorPosition::end);
}
const_iterator cbegin() const {
......
......@@ -13,7 +13,10 @@ namespace scalfmm {
}
}
NAMESPACE_START {
namespace scalfmm {
namespace details {
namespace adaptive_iterator {
enum class NodeIteratorOrder {preorder, postorder};
......@@ -24,7 +27,7 @@ NAMESPACE_START {
friend scalfmm::tests::test_PreOrderNodeIterator;
using base_t = FNodeIterator<__OrderNodeIterator<Node, it_type>, Node>;
using Direction = typename base_t::Direction;
using Direction = FNodeIteratorDirection;
public:
......@@ -160,7 +163,7 @@ NAMESPACE_START {
////////////////////////////////////////////////////////////
};
} NAMESPACE_END
} } }
#undef NAMESPACE_START
......
......@@ -31,14 +31,14 @@ namespace scalfmm {
using base_t::operator++;
using base_t::operator--;
template<base_t::Direction> void move_to_boundary() {}
template<scalfmm::FNodeIteratorDirection> void move_to_boundary() {}
test_iterator& operator++(){++pp; return *this;}
test_iterator& operator--(){++mm; return *this;}
};
tree_t* tree = nullptr;
node_t* root = nullptr;
box_t box {{0,0,0}, 100};
box_t box {100, {0,0,0}};
public:
......
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