namespace CVC4 {
NodeDfsIterator::NodeDfsIterator(TNode n,
- bool postorder,
+ VisitOrder order,
std::function<bool(TNode)> skipIf)
: d_stack{n},
d_visited(),
- d_postorder(postorder),
+ d_order(order),
d_current(TNode()),
d_skipIf(skipIf)
{
}
-NodeDfsIterator::NodeDfsIterator(bool postorder)
+NodeDfsIterator::NodeDfsIterator(VisitOrder order)
: d_stack(),
d_visited(),
- d_postorder(postorder),
+ d_order(order),
d_current(TNode()),
d_skipIf([](TNode) { return false; })
{
//
// Users should not compare iterators for traversals of different nodes, or
// traversals with different skipIfs.
- Assert(d_postorder == other.d_postorder);
+ Assert(d_order == other.d_order);
return d_stack == other.d_stack && d_current == other.d_current;
}
{
d_stack.push_back(back[i]);
}
- if (!d_postorder)
+ if (d_order == VisitOrder::PREORDER)
{
return;
}
}
- else if (!d_postorder || visitEntry->second)
+ else if (d_order == VisitOrder::PREORDER || visitEntry->second)
{
// if we're previsiting or we've already post-visited this node: skip it
d_stack.pop_back();
}
}
-NodeDfsIterable::NodeDfsIterable(TNode n)
- : d_node(n), d_postorder(true), d_skipIf([](TNode) { return false; })
-{
-}
-
-NodeDfsIterable& NodeDfsIterable::inPostorder()
-{
- d_postorder = true;
- return *this;
-}
-
-NodeDfsIterable& NodeDfsIterable::inPreorder()
-{
- d_postorder = false;
- return *this;
-}
-
-NodeDfsIterable& NodeDfsIterable::skipIf(
- std::function<bool(TNode)> skipCondition)
+NodeDfsIterable::NodeDfsIterable(TNode n,
+ VisitOrder order,
+ std::function<bool(TNode)> skipIf)
+ : d_node(n), d_order(order), d_skipIf(skipIf)
{
- d_skipIf = skipCondition;
- return *this;
}
NodeDfsIterator NodeDfsIterable::begin() const
{
- return NodeDfsIterator(d_node, d_postorder, d_skipIf);
+ return NodeDfsIterator(d_node, d_order, d_skipIf);
}
NodeDfsIterator NodeDfsIterable::end() const
{
- return NodeDfsIterator(d_postorder);
+ return NodeDfsIterator(d_order);
}
} // namespace CVC4
namespace CVC4 {
-// Iterator for traversing a node in post-order
+/**
+ * Enum that represents an order in which nodes are visited.
+ */
+enum class VisitOrder
+{
+ PREORDER,
+ POSTORDER
+};
+
+// Iterator for traversing a node in pre-/post-order
// It does DAG-traversal, so indentical sub-nodes will be visited once only.
class NodeDfsIterator
{
using difference_type = std::ptrdiff_t;
// Construct a traversal iterator beginning at `n`
- NodeDfsIterator(TNode n, bool postorder, std::function<bool(TNode)> skipIf);
+ NodeDfsIterator(TNode n, VisitOrder order, std::function<bool(TNode)> skipIf);
// Construct an end-of-traversal iterator
- NodeDfsIterator(bool postorder);
+ NodeDfsIterator(VisitOrder order);
// Move/copy construction and assignment. Destructor.
NodeDfsIterator(NodeDfsIterator&&) = default;
// Set to `true` if we've also already post-visited it.
std::unordered_map<TNode, bool, TNodeHashFunction> d_visited;
- // Whether this is a post-order iterator (the alternative is pre-order)
- bool d_postorder;
-
- // Whether this iterator has been initialized (advanced to its first
- // visit)
- bool d_initialized;
+ // The visit order that this iterator is using
+ VisitOrder d_order;
// Current referent node. A valid node to visit if non-null.
// Null after construction (but before first access) and at the end.
std::function<bool(TNode)> d_skipIf;
};
-// Node wrapper that is iterable in DAG post-order
+// Node wrapper that is iterable in DAG pre-/post-order
class NodeDfsIterable
{
public:
- NodeDfsIterable(TNode n);
-
- // Modifying the traversal order
- // Modify this iterable to be in post-order (default)
- NodeDfsIterable& inPostorder();
- // Modify this iterable to be in pre-order
- NodeDfsIterable& inPreorder();
-
- // Skip a node (and its descendants) if true.
- NodeDfsIterable& skipIf(std::function<bool(TNode)> skipCondition);
+ /**
+ * Creates a new node wrapper that can be used to iterate over the children
+ * of a node in pre-/post-order.
+ *
+ * @param n The node the iterate
+ * @param order The order in which the children are visited.
+ * @param skipIf Function that determines whether a given node and its
+ * descendants should be skipped or not.
+ */
+ NodeDfsIterable(
+ TNode n,
+ VisitOrder order = VisitOrder::POSTORDER,
+ std::function<bool(TNode)> skipIf = [](TNode) { return false; });
// Move/copy construction and assignment. Destructor.
NodeDfsIterable(NodeDfsIterable&&) = default;
private:
TNode d_node;
- bool d_postorder;
+ VisitOrder d_order;
std::function<bool(TNode)> d_skipIf;
};
Node eb = d_nodeManager->mkConst(false);
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
- auto traversal = NodeDfsIterable(cnd).inPostorder();
+ auto traversal = NodeDfsIterable(cnd, VisitOrder::POSTORDER);
NodeDfsIterator i = traversal.begin();
NodeDfsIterator end = traversal.end();
TS_ASSERT_EQUALS(*i, tb);
Node eb = d_nodeManager->mkConst(false);
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
- auto traversal = NodeDfsIterable(cnd).inPostorder();
+ auto traversal = NodeDfsIterable(cnd, VisitOrder::POSTORDER);
NodeDfsIterator i = traversal.begin();
NodeDfsIterator end = traversal.end();
TS_ASSERT_EQUALS(*(i++), tb);
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
size_t count = 0;
- for (auto i : NodeDfsIterable(cnd).inPostorder())
+ for (auto i : NodeDfsIterable(cnd, VisitOrder::POSTORDER))
{
++count;
}
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
size_t count = 0;
- for (auto i : NodeDfsIterable(cnd).inPostorder())
+ for (auto i : NodeDfsIterable(cnd, VisitOrder::POSTORDER))
{
if (i.isConst())
{
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
Node top = d_nodeManager->mkNode(XOR, cnd, cnd);
- auto traversal = NodeDfsIterable(top).inPostorder();
+ auto traversal = NodeDfsIterable(top, VisitOrder::POSTORDER);
size_t count = std::count_if(traversal.begin(),
traversal.end(),
Node top = d_nodeManager->mkNode(XOR, cnd, cnd);
std::vector<TNode> expected = {tb, eb, cnd, top};
- auto traversal = NodeDfsIterable(top).inPostorder();
+ auto traversal = NodeDfsIterable(top, VisitOrder::POSTORDER);
std::vector<TNode> actual;
std::copy(traversal.begin(), traversal.end(), std::back_inserter(actual));
Node top = d_nodeManager->mkNode(XOR, cnd, cnd);
std::vector<TNode> expected = {top};
- auto traversal = NodeDfsIterable(top).inPostorder().skipIf(
- [&cnd](TNode n) { return n == cnd; });
+ auto traversal = NodeDfsIterable(
+ top, VisitOrder::POSTORDER, [&cnd](TNode n) { return n == cnd; });
std::vector<TNode> actual;
std::copy(traversal.begin(), traversal.end(), std::back_inserter(actual));
Node eb = d_nodeManager->mkConst(false);
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
- auto traversal = NodeDfsIterable(cnd).inPreorder();
+ auto traversal = NodeDfsIterable(cnd, VisitOrder::PREORDER);
NodeDfsIterator i = traversal.begin();
NodeDfsIterator end = traversal.end();
TS_ASSERT_EQUALS(*i, cnd);
Node eb = d_nodeManager->mkConst(false);
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
- auto traversal = NodeDfsIterable(cnd).inPreorder();
+ auto traversal = NodeDfsIterable(cnd, VisitOrder::PREORDER);
NodeDfsIterator i = traversal.begin();
NodeDfsIterator end = traversal.end();
TS_ASSERT_EQUALS(*(i++), cnd);
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
size_t count = 0;
- for (auto i : NodeDfsIterable(cnd).inPreorder())
+ for (auto i : NodeDfsIterable(cnd, VisitOrder::PREORDER))
{
++count;
}
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
size_t count = 0;
- for (auto i : NodeDfsIterable(cnd).inPreorder())
+ for (auto i : NodeDfsIterable(cnd, VisitOrder::PREORDER))
{
if (i.isConst())
{
Node cnd = d_nodeManager->mkNode(XOR, tb, eb);
Node top = d_nodeManager->mkNode(XOR, cnd, cnd);
- auto traversal = NodeDfsIterable(top).inPreorder();
+ auto traversal = NodeDfsIterable(top, VisitOrder::PREORDER);
size_t count = std::count_if(traversal.begin(),
traversal.end(),
Node top = d_nodeManager->mkNode(XOR, cnd, cnd);
std::vector<TNode> expected = {top, cnd, tb, eb};
- auto traversal = NodeDfsIterable(top).inPreorder();
+ auto traversal = NodeDfsIterable(top, VisitOrder::PREORDER);
std::vector<TNode> actual;
std::copy(traversal.begin(), traversal.end(), std::back_inserter(actual));
Node top = d_nodeManager->mkNode(XOR, cnd, cnd);
std::vector<TNode> expected = {top, cnd, eb};
- auto traversal = NodeDfsIterable(top).inPreorder().skipIf(
- [&tb](TNode n) { return n == tb; });
+ auto traversal = NodeDfsIterable(
+ top, VisitOrder::PREORDER, [&tb](TNode n) { return n == tb; });
std::vector<TNode> actual;
std::copy(traversal.begin(), traversal.end(), std::back_inserter(actual));