-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetArguments.cpp
273 lines (250 loc) · 6.62 KB
/
SetArguments.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @file SetArguments.cpp
* @class SetArguments
* @brief Class encompassing a set of arguments
* @author Federico Cerutti <[email protected]>
* @copyright GNU Public License v2
*/
#include "SetArguments.h"
/**
* @brief Simple constructor
*/
SetArguments::SetArguments()
{
this->arguments = map<string, Argument *>();
this->key_assoc = map<int, string>();
}
/**
* @brief Add an argument to the set
* @param[in] arg Pointer to an Argument
*/
void SetArguments::add_Argument(Argument *arg)
{
this->arguments.insert(pair<string, Argument *>(arg->getName(), arg));
this->key_assoc.insert(pair<int, string>(arg->getNumber(), arg->getName()));
}
/**
* @brief Retuns the cardinality of this set
* @retval int
*/
int SetArguments::cardinality() const
{
return (int) this->arguments.size();
}
/**
* @brief This method returns the pointer to the Argument whose name is given as parameter
* @param[in] name The name of the argument
* @retval Argument* The pointer to the Argument object, or NULL if not found
*/
Argument *SetArguments::getArgumentByName(string name)
{
return this->arguments.at(name);
}
/**
* @brief This method returns the pointer to the Argument whose identification number is given as parameter
* @param[in] num The name of the argument
* @retval Argument* The pointer to the Argument object, or NULL if not found
*/
Argument *SetArguments::getArgumentByNumber(int num)
{
return this->arguments.at(this->key_assoc.at(num));
}
/**
* @brief Check wether or not this set is empty
* @retval bool
*/
bool SetArguments::empty()
{
return this->arguments.empty();
}
/**
* @brief Begin of the iterator for this set of arguments
* @retval SetArgumentsIterator An iterator pointing at the first of the elements of this set
*/
SetArgumentsIterator SetArguments::begin() const
{
return SetArgumentsIterator((this->arguments.begin()));
}
/**
* @brief End of the iterator for this set of arguments
* @retval SetArgumentsIterator An iterator pointing at the last of the elements of this set
*/
SetArgumentsIterator SetArguments::end() const
{
return SetArgumentsIterator(this->arguments.end());
}
/**
* @brief Check whether this set is a subset of `other`
* @param[in] other The other set to check
* @retval bool
*/
bool SetArguments::is_subset_equal(SetArguments *other)
{
if (this->empty() && other->empty())
return true;
if (this->empty() && !other->empty())
return true;
if (!this->empty() && other->empty())
return false;
if (this->cardinality() > other->cardinality())
return false;
for (SetArgumentsIterator it = this->begin(); it != this->end(); it++)
{
if (!other->exists((*it)))
return false;
}
return true;
}
/**
* @brief Check whether this set is a proper subset of `other`
* @param[in] other The other set to check
* @retval bool
*/
bool SetArguments::is_subset(SetArguments *other)
{
if ((this->cardinality() < other->cardinality()) &&
this->is_subset_equal(other))
return true;
else
return false;
}
/**
* @brief Setminus
* @param[in] other The other term of the subtraction
* @param[out] res The result of the subtraction
* @retval void
*/
void SetArguments::setminus(SetArguments *other, SetArguments *result)
{
this->clone(result);
for (SetArgumentsIterator it = other->begin(); it != other->end(); it++)
{
if (result->exists((*it)))
result->remove((*it));
}
}
/**
* @brief Check whether or not an argument exists in this set
* @param[in] arg
* @retval bool
*/
bool SetArguments::exists(Argument * arg)
{
return (this->arguments.find(arg->getName()) != this->arguments.end());
}
/**
* @brief Remove an argument from this set
* @param[in] arg
* @retval void
*/
void SetArguments::remove(Argument * arg)
{
this->arguments.erase(arg->getName());
}
/**
* @brief Clone this set
* @param[in] set The set that will be the new clone
* @retval void
*/
void SetArguments::clone(SetArguments * set)
{
for (SetArgumentsIterator it = this->begin(); it != this->end(); it++)
{
set->add_Argument((*it));
}
}
/**
* @brief Intersect this set the `other` set and write the result in `res`
* @param[in] other The other term of the intersection
* @param[out] res The result of the intersection
* @retval void
*/
void SetArguments::intersect(SetArguments *other, SetArguments *res)
{
for (SetArgumentsIterator it = this->begin(); it != this->end(); it++)
{
for (SetArgumentsIterator inner = other->begin(); inner != other->end();
inner++)
{
if (*(*it) == *(*inner))
{
res->add_Argument(*it);
}
}
}
}
/**
* @brief Overloading of the == operator
* @param[in] other The other term of the comparison
* @retval bool
*/
bool SetArguments::operator==(const SetArguments &other) const
{
if (this->cardinality() != other.cardinality())
return false;
for(SetArgumentsIterator it = this->begin(), it2 = other.begin(); it != this->end() and it2 != other.begin(); it++, it2++)
{
if ((*it) != (*it2))
return false;
}
return true;
}
SetArguments::~SetArguments()
{
// TODO Auto-generated destructor stub
}
/**
* @brief Method for printing the SetArguments
*/
ostream& operator<<(ostream& out, const SetArguments& r)
{
out << "{";
SetArgumentsIterator it;
for (it = r.begin(); it != r.end();)
{
out << (*it)->getName();
//out << (*it)->getName() <<"(#"<<(*it)->getNumber()<<")";
if (++it != r.end())
out << " ";
}
out << "}";
return out;
}
/**
* @brief Setunion
* @param[in] other The other term of the union
* @param[out] res The result of the union
* @retval void
*/
void SetArguments::setunion(SetArguments *other, SetArguments *result)
{
this->clone(result);
for (SetArgumentsIterator it = other->begin(); it != other->end(); it++)
{
if (!(result->exists((*it))))
result->add_Argument((*it));
}
}
/**
* @brief Method for adjust a SetArguments considering the index number of a correct SetArguments
* @details Iteration on this SetArguments, look in set_correct, if there is the Argument we put it as arg_correct in this set
if in the correct set there isn't that argument, we create a new one, using as index one that it isn't already used
* @param[in] the SetArguments with the correct Arguments
* @retval A new adjusted SetArguments
*/
SetArguments * SetArguments::adjust_set(SetArguments * set_correct){
SetArguments * new_args = new SetArguments();
int cont=0;
for(SetArgumentsIterator it = this->begin(); it != this->end(); it++){
Argument * arg_correct;
if(set_correct->exists(*it)){
arg_correct = set_correct->getArgumentByName((*it)->getName());
}
else{
arg_correct = new Argument(**it, (*it)->get_af() ,set_correct->cardinality()+cont);
cont++;
}
new_args->add_Argument(arg_correct);
}
return new_args;
}