Crevice  0.1
 All Classes Functions Variables Pages
Term.cc
1 /*
2  * File: Term.cc
3  *
4  * Copyright 2012 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "Problem.h"
20 #include "TermVariable.h"
21 #include <iostream>
22 #include <set>
23 #include <string>
24 #include <typeinfo>
25 
30 Term::Term(Problem* problem) {
31  problem_ = problem;
32  termIndex_ = INVALID_INDEX;
33 }
34 
39 }
40 
49  return this;
50 }
51 
57 double Term::evaluate(const DoubleVector* variableValues) {
58  double ret;
59  TermValueSet* termValues;
60  termValues = new TermValueSet;
61  ret = this->evaluate(variableValues, termValues);
62  delete termValues;
63  return ret;
64 }
65 
73  const DoubleVector* variableValues,
74  TermValueSet* termValues) {
75  TermValue termValue;
76  termValue.index = this->termIndex_;
77  TermValueSet::iterator position = termValues->find(termValue);
78  if (position == termValues->end()) {
79  termValue.value = this->eval(variableValues, termValues);
80  termValues->insert(termValue);
81  return termValue.value;
82  } else {
83  return (*position).value;
84  }
85 }
86 
93 double Term::eval(
94  const DoubleVector* variableValues,
95  TermValueSet* termValues) {
96  return 0.;
97 }
98 
104  return problem_;
105 }
106 
112 bool Term::less(const Term* term) const {
113  return typeid (*this).before(typeid (*term));
114 }
115 
120 const int Term::getTermIndex() {
121  return termIndex_;
122 }
123 
128 void Term::setTermIndex(const int termIndex) {
129  termIndex_ = termIndex;
130 }
131 
136 std::string Term::toString() const {
137  return "emptyTerm";
138 };
139 
146 std::ostream& operator<<(std::ostream& outs, const Term* term) {
147  outs << term->toString();
148  return outs;
149 }