[1991be]: src / lib / compiler / LogicalFactory.cc  Maximize  Restore  History

Download this file

114 lines (103 with data), 2.7 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
#include <config.h>
#include <compiler/LogicalFactory.h>
#include <compiler/NodeFactory.h>
#include <model/Model.h>
#include <graph/LogicalNode.h>
#include <graph/Node.h>
#include <graph/ScalarLogicalNode.h>
#include <graph/VectorLogicalNode.h>
#include <graph/ArrayLogicalNode.h>
#include <graph/VSLogicalNode.h>
#include <graph/LinkNode.h>
#include <util/dim.h>
#include <stdexcept>
#include <string>
using std::pair;
using std::map;
using std::vector;
using std::invalid_argument;
using std::runtime_error;
using std::string;
namespace jags {
bool lt(LogicalPair const &arg1, LogicalPair const &arg2)
{
if (arg1.first == arg2.first) {
//Same function: check parameters
return lt(arg1.second, arg2.second);
}
else {
//Diferent functions: sort by address (arbitrary)
return FUNC(arg1.first) < FUNC(arg2.first);
}
}
LogicalNode* LogicalFactory::newNode(FunctionPtr const &func,
vector<Node const *> const &parents)
{
LogicalNode *node = 0;
if (SCALAR(func)) {
unsigned int arglength = 1;
for (unsigned int i = 0; i < parents.size(); ++i) {
if (parents[i]->length() > 1) {
if (arglength == 1) {
arglength = parents[i]->length();
}
else if (parents[i]->length() != arglength) {
arglength = 0;
break;
}
}
}
if (arglength == 0) {
throw runtime_error(string("Incompatible argument lengths for ") +
func.name());
}
else if (arglength == 1) {
node = new ScalarLogicalNode(SCALAR(func), parents);
}
else {
node = new VSLogicalNode(SCALAR(func), parents);
}
}
else if (LINK(func)) {
bool ok = true;
for (unsigned int i = 0; i < parents.size(); ++i) {
if (parents[i]->length() != 1) {
ok = false;
break;
}
}
if (ok) {
node = new LinkNode(LINK(func), parents);
}
else {
throw runtime_error("Invalid vector argument to " + func.name());
}
}
else if (VECTOR(func))
node = new VectorLogicalNode(VECTOR(func), parents);
else if (ARRAY(func))
node = new ArrayLogicalNode(ARRAY(func), parents);
else
throw invalid_argument("Invalid function in getNode");
return node;
}
Node* LogicalFactory::getNode(FunctionPtr const &func,
vector<Node const *> const &parents,
Model &model)
{
if (isNULL(func)) {
throw invalid_argument("NULL function passed to getLogicalNode");
}
LogicalPair lpair(func, parents);
map<LogicalPair, Node*, fuzzy_less<LogicalPair> >::iterator i = _logicalmap.find(lpair);
if (i != _logicalmap.end()) {
return i->second;
}
else {
LogicalNode *lnode = newNode(func, parents);
_logicalmap[lpair] = lnode;
model.addNode(lnode);
return lnode;
}
}
} //namespace jags

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks