-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathSubSuperIterators.h
More file actions
214 lines (188 loc) · 7 KB
/
SubSuperIterators.h
File metadata and controls
214 lines (188 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifndef SUB_SUPER_ITERATORS
#define SUB_SUPER_ITERATORS
#include "clstepcore/ExpDict.h"
#include "clstepcore/ExpDict.h"
#include <queue>
#include <assert.h>
/** abstract base class for recursive breadth-first input iterators of EntityDescriptor/EntityDescLinkNode
* NOTE: due to pure virtual functions being necessary for initialization, derived class constructor must call reset(t)
*/
class recursiveEntDescripIterator {
protected:
const EntityDescriptor * startEntity;
typedef struct {
const EntityDescriptor * ed;
unsigned int depth; ///< for debugging; records how many lists had to be traversed to find the current node
} queue_pair;
std::deque< queue_pair > q;
unsigned int position; ///< primarily used in comparisons between iterators
///add contents of a linked list to q
void addLinkedList( const queue_pair qp ) {
EntityDescLinkNode * a = listHead( qp.ed );
queue_pair tmp;
tmp.depth = qp.depth + 1;
while( a != 0 ) {
tmp.ed = nodeContent( a );
q.push_back( tmp );
a = ( EntityDescLinkNode * ) a->NextNode( );
}
}
virtual EntityDescLinkNode * listHead( const EntityDescriptor * t ) const = 0; ///< returns the head of something inheriting SingleLinkList
virtual EntityDescriptor * nodeContent( const EntityDescLinkNode * n ) const = 0; ///< returns the content of a SingleLinkNode
public:
recursiveEntDescripIterator( const EntityDescriptor * t = 0 ): startEntity( t ), position( 0 ) {
//NOTE due to pure virtual functions, derived class constructor *must* call reset(t)
}
~recursiveEntDescripIterator( ) {
}
void reset( const EntityDescriptor * t = 0 ) {
position = 0;
q.clear( );
if( t ) {
startEntity = t;
}
if( startEntity ) {
queue_pair p;
p.depth = 0;
p.ed = startEntity;
addLinkedList( p );
}
}
const EntityDescriptor * next( ) {
if( q.empty( ) ) {
return ( EntityDescriptor * ) 0;
} else {
position++;
queue_pair qp = q.front( );
q.pop_front( );
addLinkedList( qp );
return qp.ed;
}
}
const EntityDescriptor * current( ) const {
if( q.empty( ) ) {
return ( EntityDescriptor * ) 0;
}
return( q.front( ).ed );
}
bool hasNext( ) const {
return( ( ( q.size( ) > 1 ) && ( q[1].ed != 0 ) ) //there is another EntityDescriptor in q
|| ( nodeContent( listHead( q[0].ed ) ) != 0 ) ); //or, the only one in the queue has a non-empty list
}
bool empty( ) const {
return q.empty( );
}
unsigned int pos( ) const {
return position;
}
unsigned int depth( ) const {
return q[0].depth;
}
const EntityDescriptor * operator *( ) const {
return current( );
}
const EntityDescriptor * operator ->( ) const {
return current( );
}
/// two iterators are not considered equal unless the startEntity pointers match and the positions match
bool operator ==( const recursiveEntDescripIterator & b ) const {
return( ( startEntity == b.startEntity ) && ( position == b.position ) );
}
bool operator !=( const recursiveEntDescripIterator & b ) const {
return( ( startEntity != b.startEntity ) || ( position != b.position ) );
}
/// for inequality operators, return a Logical; LUnknown means that the startEntity pointers do not match
Logical operator >( const recursiveEntDescripIterator & b ) const {
if( startEntity != b.startEntity ) {
return LUnknown;
}
if( position > b.position ) {
return LTrue;
} else {
return LFalse;
}
}
Logical operator <( const recursiveEntDescripIterator & b ) const {
if( startEntity != b.startEntity ) {
return LUnknown;
}
if( position < b.position ) {
return LTrue;
} else {
return LFalse;
}
}
Logical operator >=( const recursiveEntDescripIterator & b ) const {
if( startEntity != b.startEntity ) {
return LUnknown;
}
if( position >= b.position ) {
return LTrue;
} else {
return LFalse;
}
}
Logical operator <=( const recursiveEntDescripIterator & b ) const {
if( startEntity != b.startEntity ) {
return LUnknown;
}
if( position <= b.position ) {
return LTrue;
} else {
return LFalse;
}
}
const EntityDescriptor * operator ++( ) {
return next( );
}
const EntityDescriptor * operator ++( int ) {
const EntityDescriptor * c = current( );
next( );
return c;
}
};
/** Recursive breadth-first input iterator for supertypes
* \sa subtypesIterator
*/
class supertypesIterator : public recursiveEntDescripIterator {
protected:
EntityDescLinkNode * listHead( const EntityDescriptor * t ) const { ///< returns the head of an EntityDescriptorList
if( !t ) {
return 0;
}
return ( EntityDescLinkNode * ) t->Supertypes().GetHead();
}
EntityDescriptor * nodeContent( const EntityDescLinkNode * n ) const { ///< returns the content of a EntityDescLinkNode
if( !n ) {
return 0;
}
return n->EntityDesc();
}
public:
supertypesIterator( const EntityDescriptor * t = 0 ): recursiveEntDescripIterator( t ) {
reset( t );
}
};
/** Recursive breadth-first input iterator for subtypes
* \sa supertypesIterator
*/
class subtypesIterator: public recursiveEntDescripIterator {
protected:
EntityDescLinkNode * listHead( const EntityDescriptor * t ) const { ///< returns the head of an EntityDescriptorList
if( !t ) {
return 0;
}
return ( EntityDescLinkNode * ) t->Subtypes().GetHead();
}
EntityDescriptor * nodeContent( const EntityDescLinkNode * n ) const { ///< returns the content of a EntityDescLinkNode
if( !n ) {
return 0;
}
return n->EntityDesc();
}
public:
subtypesIterator( const EntityDescriptor * t = 0 ): recursiveEntDescripIterator( t ) {
reset( t );
}
};
#endif //SUB_SUPER_ITERATORS