Add a "Maybe" type hiding the boost::optional implementation detail

This commit is contained in:
JacksonCampolattaro 2023-04-25 16:07:00 +02:00
parent f2467dea77
commit 9a52cf7026
2 changed files with 18 additions and 12 deletions

View File

@ -110,6 +110,11 @@ public:
*/ */
typedef std::size_t Node_index; typedef std::size_t Node_index;
/*!
* \brief Optional index of a node in the tree.
*/
typedef boost::optional<Node_index> Maybe_node_index;
/*! /*!
* \brief The Sub-tree / Orthant type. * \brief The Sub-tree / Orthant type.
*/ */
@ -443,7 +448,7 @@ public:
return std::distance(m_nodes.data(), &node); return std::distance(m_nodes.data(), &node);
} }
boost::optional<Node_index> index(const Node* node) const { Maybe_node_index index(const Node* node) const {
if (node == nullptr) return {}; if (node == nullptr) return {};
return index(*node); return index(*node);
} }
@ -480,7 +485,7 @@ public:
auto first = traversal.first_index(); auto first = traversal.first_index();
auto next = [=](const Self& tree, Node_index index) -> boost::optional<Node_index> { auto next = [=](const Self& tree, Node_index index) -> Maybe_node_index {
return traversal.next_index(index); return traversal.next_index(index);
}; };
@ -493,7 +498,7 @@ public:
Node_index first = traversal.first_index(); Node_index first = traversal.first_index();
auto next = [=](const Self& tree, Node_index index) -> boost::optional<Node_index> { auto next = [=](const Self& tree, Node_index index) -> Maybe_node_index {
return traversal.next_index(index); return traversal.next_index(index);
}; };
@ -719,7 +724,7 @@ public:
return m_nodes[node].m_children_index.get() + i; return m_nodes[node].m_children_index.get() + i;
} }
const boost::optional<Node_index> next_sibling(Node_index n) const { const Maybe_node_index next_sibling(Node_index n) const {
// Root node has no siblings // Root node has no siblings
if (is_root(n)) return {}; if (is_root(n)) return {};
@ -735,17 +740,17 @@ public:
return child(parent(n), local_coords + 1); return child(parent(n), local_coords + 1);
} }
const boost::optional<Node_index> next_sibling_up(Node_index n) const { const Maybe_node_index next_sibling_up(Node_index n) const {
// the root node has no next sibling up // the root node has no next sibling up
if (n == 0) return {}; if (n == 0) return {};
auto up = boost::optional<Node_index>{parent(n)}; auto up = Maybe_node_index{parent(n)};
while (up) { while (up) {
if (next_sibling(up.get())) return {next_sibling(up.get())}; if (next_sibling(up.get())) return {next_sibling(up.get())};
up = is_root(up.get()) ? boost::optional<Node_index>{} : boost::optional<Node_index>{parent(up.get())}; up = is_root(up.get()) ? Maybe_node_index{} : Maybe_node_index{parent(up.get())};
} }
return {}; return {};
@ -760,7 +765,7 @@ public:
return first; return first;
} }
boost::optional<Node_index> first_child_at_depth(Node_index n, std::size_t d) const { Maybe_node_index first_child_at_depth(Node_index n, std::size_t d) const {
std::queue<Node_index> todo; std::queue<Node_index> todo;
todo.push(n); todo.push(n);
@ -916,7 +921,7 @@ public:
\return the index of the adjacent node if it exists, nothing otherwise. \return the index of the adjacent node if it exists, nothing otherwise.
*/ */
boost::optional<Node_index> adjacent_node(Node_index n, typename Node::Local_coordinates direction) const { Maybe_node_index adjacent_node(Node_index n, typename Node::Local_coordinates direction) const {
// Direction: LEFT RIGHT DOWN UP BACK FRONT // Direction: LEFT RIGHT DOWN UP BACK FRONT
// direction: 000 001 010 011 100 101 // direction: 000 001 010 011 100 101
@ -962,7 +967,7 @@ public:
/*! /*!
\brief equivalent to `adjacent_node()`, with an adjacency direction rather than a bitset. \brief equivalent to `adjacent_node()`, with an adjacency direction rather than a bitset.
*/ */
boost::optional<Node_index> adjacent_node(Node_index n, typename Node::Adjacency adjacency) const { Maybe_node_index adjacent_node(Node_index n, typename Node::Adjacency adjacency) const {
return adjacent_node(n, std::bitset<Dimension::value>(static_cast<int>(adjacency))); return adjacent_node(n, std::bitset<Dimension::value>(static_cast<int>(adjacency)));
} }

View File

@ -49,6 +49,7 @@ public:
typedef typename Enclosing::Dimension Dimension; ///< Dimension type. typedef typename Enclosing::Dimension Dimension; ///< Dimension type.
typedef typename Enclosing::Degree Degree; ///< Degree type. typedef typename Enclosing::Degree Degree; ///< Degree type.
typedef typename Enclosing::Node_index Node_index; ///< Index type. typedef typename Enclosing::Node_index Node_index; ///< Index type.
typedef typename Enclosing::Maybe_node_index Maybe_node_index; ///< Index type.
/*! /*!
\brief Self typedef for convenience. \brief Self typedef for convenience.
@ -98,8 +99,8 @@ private:
std::uint8_t m_depth = 0; std::uint8_t m_depth = 0;
Global_coordinates m_global_coordinates{}; Global_coordinates m_global_coordinates{};
boost::optional<Node_index> m_parent_index{}; Maybe_node_index m_parent_index{};
boost::optional<Node_index> m_children_index{}; Maybe_node_index m_children_index{};
// Only the Orthtree class has access to the non-default // Only the Orthtree class has access to the non-default
// constructor, mutators, etc. // constructor, mutators, etc.