Replace boost::optional with std::optional

This commit is contained in:
JacksonCampolattaro 2023-07-09 17:49:12 +02:00
parent 02e10bf85a
commit 03cda70191
3 changed files with 34 additions and 31 deletions

View File

@ -113,7 +113,7 @@ public:
/*! /*!
* \brief Optional index of a node in the tree. * \brief Optional index of a node in the tree.
*/ */
typedef boost::optional<Node_index> Maybe_node_index; typedef std::optional<Node_index> Maybe_node_index;
// todo: maybe this could be private? // todo: maybe this could be private?
typedef Properties::Property_container<Node_index> Node_property_container; typedef Properties::Property_container<Node_index> Node_property_container;
@ -405,23 +405,23 @@ public:
// Skip if this neighbor is a direct sibling (it's guaranteed to be the same depth) // Skip if this neighbor is a direct sibling (it's guaranteed to be the same depth)
// TODO: This check might be redundant, if it doesn't affect performance maybe I could remove it // TODO: This check might be redundant, if it doesn't affect performance maybe I could remove it
if (parent(neighbor.get()) == parent(node)) if (parent(*neighbor) == parent(node))
continue; continue;
// If it's already been split, skip it // If it's already been split, skip it
if (!is_leaf(neighbor.get())) if (!is_leaf(neighbor))
continue; continue;
// Check if the neighbor breaks our grading rule // Check if the neighbor breaks our grading rule
// TODO: could the rule be parametrized? // TODO: could the rule be parametrized?
if ((depth(node) - depth(neighbor.get())) > 1) { if ((depth(node) - depth(*neighbor)) > 1) {
// Split the neighbor // Split the neighbor
split(neighbor.get()); split(*neighbor);
// Add newly created children to the queue // Add newly created children to the queue
for (int i = 0; i < Degree::value; ++i) { for (int i = 0; i < Degree::value; ++i) {
leaf_nodes.push(child(neighbor.get(), i)); leaf_nodes.push(child(*neighbor, i));
} }
} }
} }
@ -478,6 +478,7 @@ public:
Node_index_range traverse_indices(Traversal traversal) const { Node_index_range traverse_indices(Traversal traversal) const {
Node_index first = traversal.first_index(); Node_index first = traversal.first_index();
std::cout << first << std::endl;
auto next = [=](const Self& tree, Node_index index) -> Maybe_node_index { auto next = [=](const Self& tree, Node_index index) -> Maybe_node_index {
return traversal.next_index(index); return traversal.next_index(index);
@ -696,6 +697,9 @@ public:
} }
std::size_t depth(Node_index n) const { std::size_t depth(Node_index n) const {
// std::cerr << n
// << " " << m_node_depths.size()
// << std::endl;
return m_node_depths[n]; return m_node_depths[n];
} }
@ -724,12 +728,12 @@ public:
*/ */
Node_index parent(Node_index node) const { Node_index parent(Node_index node) const {
CGAL_precondition (!is_root(node)); CGAL_precondition (!is_root(node));
return m_node_parents[node].get(); return *m_node_parents[node];
} }
Node_index child(Node_index node, std::size_t i) const { Node_index child(Node_index node, std::size_t i) const {
CGAL_precondition (!is_leaf(node)); CGAL_precondition (!is_leaf(node));
return m_node_children[node].get() + i; return *m_node_children[node] + i;
} }
Node_index descendant(Node_index node, std::size_t i) { return child(node, i); } Node_index descendant(Node_index node, std::size_t i) { return child(node, i); }
@ -768,9 +772,9 @@ public:
auto up = Maybe_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)) return {next_sibling(*up)};
up = is_root(up.get()) ? Maybe_node_index{} : Maybe_node_index{parent(up.get())}; up = is_root(*up) ? Maybe_node_index{} : Maybe_node_index{parent(*up)};
} }
return {}; return {};
@ -824,10 +828,10 @@ public:
m_node_children[n] = m_node_properties.emplace_group(Degree::value); m_node_children[n] = m_node_properties.emplace_group(Degree::value);
for (std::size_t i = 0; i < Degree::value; i++) { for (std::size_t i = 0; i < Degree::value; i++) {
Node_index c = m_node_children[n].get() + i; Node_index c = *m_node_children[n] + i;
// Make sure the node isn't one of its own children // Make sure the node isn't one of its own children
CGAL_assertion(n != m_node_children[n].get() + i); CGAL_assertion(n != *m_node_children[n] + i);
Local_coordinates local_coordinates{i}; Local_coordinates local_coordinates{i};
for (int i = 0; i < Dimension::value; i++) for (int i = 0; i < Dimension::value; i++)
@ -985,11 +989,11 @@ public:
if (!adjacent_node_of_parent) return {}; if (!adjacent_node_of_parent) return {};
// If the parent's adjacent node has no children, then it's this node's adjacent node // If the parent's adjacent node has no children, then it's this node's adjacent node
if (is_leaf(adjacent_node_of_parent.get())) if (is_leaf(*adjacent_node_of_parent))
return adjacent_node_of_parent; return adjacent_node_of_parent;
// Return the nearest node of the parent by subtracting the offset instead of adding // Return the nearest node of the parent by subtracting the offset instead of adding
return {child(adjacent_node_of_parent.get(), local_coordinates(n).to_ulong() - offset)}; return {child(*adjacent_node_of_parent, local_coordinates(n).to_ulong() - offset)};
} }
/*! /*!

View File

@ -14,6 +14,8 @@
#include <CGAL/license/Orthtree.h> #include <CGAL/license/Orthtree.h>
#include <optional>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/iterator/iterator_facade.hpp> #include <boost/iterator/iterator_facade.hpp>
@ -118,7 +120,7 @@ public:
* *
* \todo * \todo
*/ */
typedef std::function<boost::optional<std::size_t>(const Tree&, std::size_t)> Traversal_function; typedef std::function<std::optional<std::size_t>(const Tree&, std::size_t)> Traversal_function;
typedef typename Tree::Node_index Node_index; typedef typename Tree::Node_index Node_index;
@ -160,18 +162,18 @@ private:
void increment() { void increment() {
// invoking increment on the sentinel is undefined behavior // invoking increment on the sentinel is undefined behavior
m_index = m_next(*m_tree, m_index.get()); m_index = m_next(*m_tree, *m_index);
} }
Node_index dereference() const { Node_index dereference() const {
return m_index.get(); return *m_index;
} }
private: private:
Traversal_function m_next; Traversal_function m_next;
boost::optional<std::size_t> m_index = {}; std::optional<std::size_t> m_index;
const Tree* m_tree = nullptr; const Tree* m_tree = nullptr;
}; };

View File

@ -61,10 +61,10 @@ public:
const Node* next(const Node* n) const { const Node* next(const Node* n) const {
if (n == nullptr || !next_index(m_orthtree.index(*n))) return nullptr; if (n == nullptr || !next_index(m_orthtree.index(*n))) return nullptr;
return &m_orthtree[next_index(m_orthtree.index(*n)).get()]; return &m_orthtree[*next_index(m_orthtree.index(*n))];
} }
boost::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const { std::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const {
if (m_orthtree.is_leaf(n)) { if (m_orthtree.is_leaf(n)) {
@ -76,10 +76,7 @@ public:
return next; return next;
} else { } else {
// todo: this shouldn't be necessary, I'd prefer to directly get m_orthtree[n].m_children_index
return m_orthtree.child(n, 0); return m_orthtree.child(n, 0);
} }
} }
@ -110,7 +107,7 @@ public:
} }
typename Tree::Node_index first_index() const { typename Tree::Node_index first_index() const {
return m_orthtree.index(first()).get(); return *m_orthtree.index(first());
} }
const Node* next(const Node* n) const { const Node* next(const Node* n) const {
@ -123,7 +120,7 @@ public:
return next; return next;
} }
boost::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const { std::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const {
return m_orthtree.index(next(&m_orthtree[n])); return m_orthtree.index(next(&m_orthtree[n]));
} }
}; };
@ -166,13 +163,13 @@ public:
return next; return next;
} }
boost::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const { std::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const {
if (m_orthtree.next_sibling(n)) if (m_orthtree.next_sibling(n))
return m_orthtree.deepest_first_child(m_orthtree.next_sibling(n).get()); return m_orthtree.deepest_first_child(*m_orthtree.next_sibling(n));
if (m_orthtree.next_sibling_up(n)) if (m_orthtree.next_sibling_up(n))
return m_orthtree.deepest_first_child(m_orthtree.next_sibling_up(n).get()); return m_orthtree.deepest_first_child(*m_orthtree.next_sibling_up(n));
return {}; return {};
} }
@ -210,7 +207,7 @@ public:
typename Tree::Node_index first_index() const { typename Tree::Node_index first_index() const {
// assumes the tree has at least one child at m_depth // assumes the tree has at least one child at m_depth
return m_orthtree.first_child_at_depth(m_orthtree.root(), m_depth).get(); return *m_orthtree.first_child_at_depth(m_orthtree.root(), m_depth);
} }
template <typename Node> template <typename Node>
@ -231,7 +228,7 @@ public:
return next; return next;
} }
boost::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const { std::optional<typename Tree::Node_index> next_index(typename Tree::Node_index n) const {
auto next = m_orthtree.next_sibling(n); auto next = m_orthtree.next_sibling(n);
@ -243,7 +240,7 @@ public:
if (!m_orthtree.next_sibling_up(up)) if (!m_orthtree.next_sibling_up(up))
return {}; return {};
up = m_orthtree.next_sibling_up(up).get(); up = *m_orthtree.next_sibling_up(up);
next = m_orthtree.first_child_at_depth(up, m_depth); next = m_orthtree.first_child_at_depth(up, m_depth);
} while (!next); } while (!next);