-
Notifications
You must be signed in to change notification settings - Fork 5
/
predicates.cc
116 lines (94 loc) · 3.57 KB
/
predicates.cc
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
/*
* Copyright 2003-2005 Carnegie Mellon University and Rutgers University
* Copyright 2007 Håkan Younes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "predicates.h"
/* ====================================================================== */
/* Predicate */
/* Output operator for predicates. */
std::ostream& operator<<(std::ostream& os, const Predicate& p) {
os << PredicateTable::names_[p.index_];
return os;
}
/* ====================================================================== */
/* PredicateTable */
/* Predicate names. */
std::vector<std::string> PredicateTable::names_;
/* Predicate parameters. */
std::vector<TypeList> PredicateTable::parameters_;
/* Static predicates. */
PredicateSet PredicateTable::static_predicates_;
/* Adds a parameter with the given type to the given predicate. */
void PredicateTable::add_parameter(const Predicate& predicate,
const Type& type) {
parameters_[predicate.index_].push_back(type);
}
/* Returns the name of the given predicate. */
const std::string& PredicateTable::name(const Predicate& predicate) {
return names_[predicate.index_];
}
/* Returns the parameter types of the given predicate. */
const TypeList& PredicateTable::parameters(const Predicate& predicate) {
return parameters_[predicate.index_];
}
/* Makes the given predicate dynamic. */
void PredicateTable::make_dynamic(const Predicate& predicate) {
static_predicates_.erase(predicate);
}
/* Tests if the given predicate is static. */
bool PredicateTable::static_predicate(const Predicate& predicate) {
return static_predicates_.find(predicate) != static_predicates_.end();
}
/* Adds a predicate with the given name to this table and returns
the predicate. */
const Predicate& PredicateTable::add_predicate(const std::string& name) {
std::pair<std::map<std::string, Predicate>::const_iterator, bool> pi =
predicates_.insert(std::make_pair(name, Predicate(names_.size())));
const Predicate& predicate = (*pi.first).second;
names_.push_back(name);
parameters_.push_back(TypeList());
static_predicates_.insert(predicate);
return predicate;
}
/* Returns a pointer to the predicate with the given name, or 0 if
no predicate with the given name exists. */
const Predicate*
PredicateTable::find_predicate(const std::string& name) const {
std::map<std::string, Predicate>::const_iterator pi = predicates_.find(name);
if (pi != predicates_.end()) {
return &(*pi).second;
} else {
return 0;
}
}
/* Output operator for predicate tables. */
std::ostream& operator<<(std::ostream& os, const PredicateTable& t) {
for (std::map<std::string, Predicate>::const_iterator pi =
t.predicates_.begin();
pi != t.predicates_.end(); pi++) {
const Predicate& p = (*pi).second;
os << std::endl << " (" << p;
const TypeList& types = PredicateTable::parameters(p);
for (TypeList::const_iterator ti = types.begin();
ti != types.end(); ti++) {
os << " ?v - " << *ti;
}
os << ")";
if (PredicateTable::static_predicate(p)) {
os << " <static>";
}
}
return os;
}