Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * Iterator supporting Node "self-iteration." 11 : : */ 12 : : 13 : : #include "cvc5_private.h" 14 : : 15 : : #ifndef CVC5__EXPR__NODE_SELF_ITERATOR_H 16 : : #define CVC5__EXPR__NODE_SELF_ITERATOR_H 17 : : 18 : : #include <iterator> 19 : : 20 : : #include "base/check.h" 21 : : #include "expr/node.h" 22 : : 23 : : namespace cvc5::internal { 24 : : namespace expr { 25 : : 26 : : class NodeSelfIterator 27 : : { 28 : : Node d_node; 29 : : Node::const_iterator d_child; 30 : : 31 : : public: 32 : : /* The following types are required by trait std::iterator_traits */ 33 : : 34 : : /** Iterator tag */ 35 : : using iterator_category = std::forward_iterator_tag; 36 : : 37 : : /** The type of the item */ 38 : : using value_type = Node; 39 : : 40 : : /** The pointer type of the item */ 41 : : using pointer = Node*; 42 : : 43 : : /** The reference type of the item */ 44 : : using reference = Node&; 45 : : 46 : : /** The type returned when two iterators are subtracted */ 47 : : using difference_type = std::ptrdiff_t; 48 : : 49 : : /* End of std::iterator_traits required types */ 50 : : 51 : : static NodeSelfIterator self(TNode n); 52 : : static NodeSelfIterator selfEnd(TNode n); 53 : : 54 : : NodeSelfIterator(); 55 : : NodeSelfIterator(Node n); 56 : : NodeSelfIterator(TNode n); 57 : : 58 : : NodeSelfIterator(Node::const_iterator i); 59 : : NodeSelfIterator(TNode::const_iterator i); 60 : : 61 : : Node operator*() const; 62 : : NodeSelfIterator& operator++(); 63 : : NodeSelfIterator operator++(int); 64 : : 65 : : bool operator==(NodeSelfIterator i) const; 66 : : bool operator!=(NodeSelfIterator i) const; 67 : : 68 : : }; /* class NodeSelfIterator */ 69 : : 70 : 303824035 : inline NodeSelfIterator NodeSelfIterator::self(TNode n) 71 : : { 72 [ - + ][ - + ]: 303824035 : Assert(!n.isNull()) << "Self-iteration over null nodes not permitted."; [ - - ] 73 : 303824035 : return NodeSelfIterator(n); 74 : : } 75 : : 76 : 275053979 : inline NodeSelfIterator NodeSelfIterator::selfEnd(TNode n) 77 : : { 78 [ - + ][ - + ]: 275053979 : Assert(!n.isNull()) << "Self-iteration over null nodes not permitted."; [ - - ] 79 : 275053979 : return NodeSelfIterator(n.end()); 80 : : } 81 : : 82 : 56 : inline NodeSelfIterator::NodeSelfIterator() : d_node(), d_child() {} 83 : : 84 : 1 : inline NodeSelfIterator::NodeSelfIterator(Node node) : d_node(node), d_child() 85 : : { 86 [ - + ][ - + ]: 1 : Assert(!node.isNull()) << "Self-iteration over null nodes not permitted."; [ - - ] 87 : 1 : } 88 : : 89 : 303824035 : inline NodeSelfIterator::NodeSelfIterator(TNode node) : d_node(node), d_child() 90 : : { 91 [ - + ][ - + ]: 303824035 : Assert(!node.isNull()) << "Self-iteration over null nodes not permitted."; [ - - ] 92 : 303824035 : } 93 : : 94 : 74500047 : inline NodeSelfIterator::NodeSelfIterator(Node::const_iterator i) 95 : 74500047 : : d_node(), d_child(i) 96 : : { 97 : 74500047 : } 98 : : 99 : 275054035 : inline NodeSelfIterator::NodeSelfIterator(TNode::const_iterator i) 100 : 275054035 : : d_node(), d_child(i) 101 : : { 102 : 275054035 : } 103 : : 104 : 198324459 : inline Node NodeSelfIterator::operator*() const 105 : : { 106 [ + + ]: 198324459 : return d_node.isNull() ? *d_child : d_node; 107 : : } 108 : : 109 : 311717384 : inline NodeSelfIterator& NodeSelfIterator::operator++() 110 : : { 111 [ + + ]: 311717384 : if (d_node.isNull()) 112 : : { 113 : 47851102 : ++d_child; 114 : : } 115 : : else 116 : : { 117 : 263866282 : d_child = d_node.end(); 118 : 263866282 : d_node = Node::null(); 119 : : } 120 : 311717384 : return *this; 121 : : } 122 : : 123 : 2 : inline NodeSelfIterator NodeSelfIterator::operator++(int) 124 : : { 125 : 2 : NodeSelfIterator i = *this; 126 : 2 : ++*this; 127 : 2 : return i; 128 : 0 : } 129 : : 130 : 834854166 : inline bool NodeSelfIterator::operator==(NodeSelfIterator i) const 131 : : { 132 [ + + ][ + + ]: 834854166 : return d_node == i.d_node && d_child == i.d_child; 133 : : } 134 : : 135 : 439532130 : inline bool NodeSelfIterator::operator!=(NodeSelfIterator i) const 136 : : { 137 : 439532130 : return !(*this == i); 138 : : } 139 : : 140 : : } // namespace expr 141 : : } // namespace cvc5::internal 142 : : 143 : : #endif /* CVC5__EXPR__NODE_SELF_ITERATOR_H */