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