From: Michael Andreen Date: Sun, 8 May 2005 10:20:37 +0000 (+0000) Subject: references instead of copies X-Git-Url: https://ruin.nu/git/?p=popboot.git;a=commitdiff_plain;h=e26fd7b3195576b25ffe51253a391b53f412a650 references instead of copies --- diff --git a/action.cpp b/action.cpp index 427440b..626eb37 100644 --- a/action.cpp +++ b/action.cpp @@ -14,11 +14,11 @@ Action::Action(const Action& action){ _effects = action._effects; } -literals Action::effects() const{ +const literals& Action::effects() const{ return _effects; } -literals Action::preconditions() const{ +const literals& Action::preconditions() const{ return _preconditions; } @@ -26,3 +26,7 @@ int Action::execute() const{ cout << "Executing: " << _executable << endl; return 0; } + +const string& Action::executable() const{ + return _executable; +} diff --git a/action.h b/action.h index 35119c3..9b3c927 100644 --- a/action.h +++ b/action.h @@ -11,8 +11,9 @@ class Action { Action(std::string executable, literals preconditions, literals effects); Action(const Action& action); Action(){}; - literals effects() const; - literals preconditions() const; + const literals& effects() const; + const literals& preconditions() const; + const std::string& executable() const; int execute() const; protected: diff --git a/main.cpp b/main.cpp index 2affab5..4463ff2 100644 --- a/main.cpp +++ b/main.cpp @@ -39,6 +39,7 @@ int main(int argc, char** argv){ getline(file,effects); getline(file,precond); cout << exec << ":" << effects << ":" << precond << endl; + if (effects == "") continue; actions.push_back(Action(exec, stringToVector(precond), stringToVector(effects))); } Planner p(actions, stringToVector(argv[2]), stringToVector(argv[3])); diff --git a/node.cpp b/node.cpp index dbb099c..1fb2b2e 100644 --- a/node.cpp +++ b/node.cpp @@ -7,7 +7,7 @@ Node::Node(Action action){ _preconditions = _action.preconditions(); } -Action Node::action(){ +const Action& Node::action() const{ return _action; } @@ -17,8 +17,8 @@ void Node::addChild(Node* node){ } -void Node::execute(literals effects){ - for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ +void Node::execute(const literals& effects){ + for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ _preconditions.erase(find(_preconditions.begin(),_preconditions.end(), *effect)); } if (_preconditions.size() != 0) diff --git a/node.h b/node.h index 0d9cae6..7bcf3a8 100644 --- a/node.h +++ b/node.h @@ -9,8 +9,8 @@ class Node { public: Node(Action action); void addChild(Node* node); - Action action(); - void execute(literals effects); + const Action& action() const; + void execute(const literals& effects); protected: Action _action; diff --git a/planner.cpp b/planner.cpp index a15028e..cf78015 100644 --- a/planner.cpp +++ b/planner.cpp @@ -11,8 +11,8 @@ Planner::Planner(std::vector actions, literals init, literals goal){ Node* finish = new Node(Action("finish",goal,literals())); for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ - literals effects = action->effects(); - for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ + const literals& effects = action->effects(); + for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ cerr << "Adding effect: " << *effect << endl; _actions[*effect] = *action; } @@ -22,13 +22,15 @@ Planner::Planner(std::vector actions, literals init, literals goal){ void Planner::makePlan(Node* node){ - literals preconds = node->action().preconditions(); + cerr << "Fetching preconditions for action: " << node->action().executable() << ".. "; + const literals& preconds = node->action().preconditions(); + cerr << "done" << endl; if (preconds.size() == 0){ cerr << "Found no preconds" << endl; _start->addChild(node); }else{ - for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ + for (literals::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ cerr << "Looking for: " << *precond << endl; hash_map::iterator addedNode = _addedNodes.find(*precond); if(addedNode != _addedNodes.end()){ @@ -51,9 +53,9 @@ void Planner::makePlan(Node* node){ } void Planner::addNode(Node* node){ - literals effects = node->action().effects(); + const literals& effects = node->action().effects(); - for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ + for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ cout << "Adding node for effect: " << *effect << endl; _addedNodes[*effect] = node; } diff --git a/src.pro b/src.pro index d0c6da8..e8c02b9 100644 --- a/src.pro +++ b/src.pro @@ -7,6 +7,7 @@ CONFIG -= qt CONFIG += debug INCLUDEPATH += . TARGET = planner +#LIBS += -lpthread # Input HEADERS += action.h node.h planner.h