-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtomSet.cpp
More file actions
255 lines (205 loc) · 6.47 KB
/
AtomSet.cpp
File metadata and controls
255 lines (205 loc) · 6.47 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
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
/***************************************************************************
* Copyright (C) 2014 by Mushthofa *
* Mushthofa.Mushthofa@Ugent.be *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*
* AtomSet.cpp
*
* Created on: Feb 2014
* Author: mush
*/
/**
* @file AtomSet.cpp
* @author Roman Schindlauer, Mushthofa
*
* @brief AtomSet class
*
*
*/
#include "AtomSet.h"
#include <algorithm>
AtomSet::const_iterator
AtomSet::begin() const
{
return const_iterator(atoms.begin());
}
AtomSet::const_iterator
AtomSet::end() const
{
return const_iterator(atoms.end());
}
void AtomSet::clear()
{
atoms.clear();
}
bool AtomSet::empty() const
{
return atoms.empty();
}
size_t AtomSet::size() const
{
return atoms.size();
}
void AtomSet::insert(AtomPtr& ap)
{
atoms.insert(ap);
}
void AtomSet::insert(const AtomSet& add)
{
atoms.insert(add.atoms.begin(), add.atoms.end());
}
void AtomSet::insert(const AtomSet::const_iterator& it)
{
atoms.insert(it.getPtr());
}
AtomSet AtomSet::difference(AtomSet& as) const
{
AtomSet res;
for (atomset_t::const_iterator a = atoms.begin();
a != atoms.end();
a++)
{
if (as.atoms.find(*a) == as.atoms.end())
res.atoms.insert(*a);
}
return res;
}
void AtomSet::intersectWith(const AtomSet& as)
{
atomset_t result;
std::insert_iterator<atomset_t> insert_it (result, result.begin());
std::set_intersection(as.atoms.begin(), as.atoms.end(),
atoms.begin(),atoms.end(), insert_it);
atoms = result;
}
struct atomMatches : public std::binary_function<AtomPtr, Atom, bool>
{
bool operator()(const AtomPtr g, const Atom& a) const
{
return (*g == a);
}
};
bool AtomSet::isConsistent() const
{
atomset_t::iterator cur = atoms.begin();
atomset_t::const_iterator last = atoms.end();
while (cur != last)
{
Atom a(**cur);
a.negate();
if (std::find_if(++cur, last, bind2nd(atomMatches(), a)) != last)
return false;
}
return true;
}
int AtomSet::operator< (const AtomSet& atomset2) const
{
if (this->size() < atomset2.size())
return true;
if (this->size() > atomset2.size())
return false;
std::pair<AtomSet::const_iterator, AtomSet::const_iterator> result;
result = std::mismatch(this->begin(), this->end(), atomset2.begin());
if (result.first == this->end())
return false;
return *(result.first) < *(result.second);
}
/*
struct predicateMatches : public std::binary_function<AtomPtr, std::string, bool>
{
bool operator()(const AtomPtr g, const std::string& pred) const
{
return (g->getPredicate() == Term(pred, Term::SYMBOL));
}
};
*/
/*
void AtomSet::remove(const std::string& pred)
{
atomset_t::iterator cur = atoms.begin();
atomset_t::const_iterator last = atoms.end();
while ((cur = std::find_if(cur, last, bind2nd(predicateMatches(), pred))) != last)
{
atomset_t::iterator tmp = cur++;
atoms.erase(tmp);
}
}
void AtomSet::remove(const std::vector<std::string>& preds)
{
for (std::vector<std::string>::const_iterator predit = preds.begin();
predit != preds.end();
++predit)
{
remove(*predit);
}
}
void AtomSet::keep(const std::vector<std::string>& preds)
{
atomset_t::iterator cur = atoms.begin();
atomset_t::const_iterator last = atoms.end();
while (cur != last)
{
if ((std::find_if(preds.begin(), preds.end(),
bind1st(predicateMatches(), *cur))) == preds.end())
{
atomset_t::iterator tmp = cur++;
atoms.erase(tmp);
}
else
{
cur++;
}
}
}
*/
std::vector<AtomPtr> AtomSet::asVector() const
{
std::vector<AtomPtr> result;
atomset_t::iterator it;
for(it = atoms.begin(); it!=atoms.end(); ++it)
result.push_back(*it);
return result;
}
/*
std::set<struct PredicateSign> AtomSet::getPredicates() const
{
std::set<struct PredicateSign> preds;
atomset_t::const_iterator a_it;
for (a_it = atoms.begin(); a_it!=atoms.end(); ++a_it)
{
struct PredicateSign pred;
pred.name = (*a_it)->getPredicateName();
pred.arity = (*a_it)->getArity();
preds.insert(pred);
}
return preds;
}
*/
std::ostream& operator<<(std::ostream& os, const AtomSet& as)
{
unsigned i = 1;
os<<"{";
for(AtomSet::const_iterator as_it = as.begin(); as_it!=as.end(); ++as_it)
{
os<<*as_it;
if(i++<as.size())
os<<", ";
}
os<<"}";
return os;
}