-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathType.h
129 lines (96 loc) · 2.9 KB
/
Type.h
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
#ifndef _TYPE_H_
#define _TYPE_H_
#include "TokenStruct.h"
class Type;
typedef std::vector< Type * > TypeVec;
class Type {
public:
std::string name;
TypeVec subtypes;
Type * supertype;
TokenStruct< std::string > constants;
TokenStruct< std::string > objects;
Type()
: supertype( 0 ) {}
Type( const std::string & s )
: name( s ), supertype( 0 ) {}
Type( const Type * t )
: name( t->name ), supertype( 0 ), constants( t->constants ), objects( t->objects ) {}
virtual ~Type() {
}
virtual std::string getName() const {
return name;
}
void insertSubtype( Type * t ) {
subtypes.push_back( t );
t->supertype = this;
}
void copySubtypes( Type * t, const TokenStruct< Type * > & ts ) {
for ( unsigned i = 0; i < t->subtypes.size(); ++i )
subtypes.push_back( ts.get( t->subtypes[i]->name ) );
}
void print( std::ostream & stream ) const {
stream << name;
if ( supertype ) stream << "[" << supertype->name << "]";
stream << "\n";
}
virtual void PDDLPrint( std::ostream & s ) const {
s << "\t" << name;
if ( supertype ) s << " - " << supertype->name;
s << "\n";
}
std::pair< bool, int > parseConstant( const std::string & object ) {
int k = 0;
int i = constants.index( object );
if ( i < 0 ) k += constants.size();
else return std::make_pair( true, -1 - i );
for ( unsigned i = 0; i < subtypes.size(); ++i ) {
std::pair< bool, int > p = subtypes[i]->parseConstant( object );
if ( p.first ) return std::make_pair( true, - k + p.second );
else k += p.second;
}
return std::make_pair( false, k );
}
std::pair< bool, unsigned > parseObject( const std::string & object ) {
unsigned k = 0;
int i = objects.index( object );
if ( i < 0 ) k += objects.size();
else return std::make_pair( true, i );
for ( unsigned i = 0; i < subtypes.size(); ++i ) {
std::pair< bool, unsigned > p = subtypes[i]->parseObject( object );
if ( p.first ) return std::make_pair( true, k + p.second );
else k += p.second;
}
return std::make_pair( false, k );
}
std::pair< std::string, int > object( int index ) {
if ( index < 0 ) {
if ( -index <= (int)constants.size() ) return std::make_pair( constants[-1 - index], 0 );
else index += constants.size();
}
else {
if ( index < (int)objects.size() ) return std::make_pair( objects[index], 0 );
else index -= objects.size();
}
for ( unsigned i = 0; i < subtypes.size(); ++i ) {
std::pair< std::string, int > p = subtypes[i]->object( index );
if ( p.first.size() ) return p;
else index = p.second;
}
return std::make_pair( "", index );
}
unsigned noObjects() {
unsigned total = objects.size() + constants.size();
for ( unsigned i = 0; i < subtypes.size(); ++i )
total += subtypes[i]->noObjects();
return total;
}
virtual Type * copy() {
return new Type( this );
}
};
inline std::ostream & operator<<( std::ostream & stream, const Type * t ) {
t->print( stream );
return stream;
}
#endif