summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-07-09 17:09:49 +1000
committerCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-07-09 17:09:49 +1000
commit5cdc743d491a04ee0446bd757cedb4c7b6bc585f (patch)
treef9096fd6a5b8c8c9ec33719158a3d7ffdf3b25df
parent8040a5fd511912cb2141e46e5ca9eb1ab766133e (diff)
Cleaning up a bit of code around the place.
- Especially making logging a bit nicer.
-rw-r--r--impl/EquationSystem.hpp3
-rw-r--r--impl/Expression.hpp4
-rw-r--r--impl/FixpointAlgorithm.hpp4
-rw-r--r--impl/ImprovementOperator.hpp19
-rw-r--r--impl/Log.hpp71
-rw-r--r--impl/main.cpp35
6 files changed, 77 insertions, 59 deletions
diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp
index c7e5f4c..f5f87c3 100644
--- a/impl/EquationSystem.hpp
+++ b/impl/EquationSystem.hpp
@@ -28,8 +28,7 @@ struct EquationSystem {
++it) {
delete *it;
}
- if (_expr_to_var)
- delete _expr_to_var;
+ delete _expr_to_var;
}
MaxExpression<Domain>& maxExpression(const std::vector<Expression<Domain>*>& arguments) {
diff --git a/impl/Expression.hpp b/impl/Expression.hpp
index 508d7ad..01921b6 100644
--- a/impl/Expression.hpp
+++ b/impl/Expression.hpp
@@ -122,9 +122,7 @@ struct OperatorExpression : public Expression<Domain> {
template<typename Domain>
struct MaxExpression : public OperatorExpression<Domain> {
MaxExpression(const unsigned int& id, const Maximum<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
- : OperatorExpression<Domain>(op, arguments), _id(id) {
- log::debug << "Op id" << id;
- }
+ : OperatorExpression<Domain>(op, arguments), _id(id) { }
virtual Domain eval(const VariableAssignment<Domain>& rho, const MaxStrategy<Domain>& strat) const {
return this->_arguments[strat.get(*this)]->eval(rho, strat);
diff --git a/impl/FixpointAlgorithm.hpp b/impl/FixpointAlgorithm.hpp
index 8511c43..bb08e72 100644
--- a/impl/FixpointAlgorithm.hpp
+++ b/impl/FixpointAlgorithm.hpp
@@ -33,11 +33,11 @@ struct NaiveFixpointAlgorithm : public FixpointAlgorithm<Domain> {
VariableAssignment<Domain>* rho = NULL;
VariableAssignment<Domain>* result = system.assignment(infinity<Domain>());
do {
- if (rho) delete rho;
+ delete rho;
rho = result;
result = _system.eval(*rho, strat);
} while (!_system.equalAssignments(*rho, *result));
- if (rho) delete rho;
+ delete rho;
for (unsigned int i = 0, length = system.variableCount(); i < length; ++i) {
Variable<Domain> var = system.variable(i);
diff --git a/impl/ImprovementOperator.hpp b/impl/ImprovementOperator.hpp
index 716768d..d99ccbc 100644
--- a/impl/ImprovementOperator.hpp
+++ b/impl/ImprovementOperator.hpp
@@ -39,7 +39,7 @@ struct NaiveImprovementOperator : public ImprovementOperator<Domain> {
strat.set(expr, index);
}
}
- //log::strategy << strat;
+ //log::strategy << strat << std::endl;
return changed;
}
};
@@ -169,7 +169,6 @@ struct SmartImprovementOperator : public ImprovementOperator<Domain> {
};
void invalidateSubexpressions(IdSet<MaxExpression<Domain>>& set, const Expression<Domain>* expr) const {
- if (!expr) return;
const MaxExpression<Domain>* maxExpr = dynamic_cast<const MaxExpression<Domain>*>(expr);
if (maxExpr != NULL) {
if (!set.contains(*maxExpr)) {
@@ -178,8 +177,7 @@ struct SmartImprovementOperator : public ImprovementOperator<Domain> {
end = maxExpr->arguments().end();
it != end;
++it) {
- if (*it)
- invalidateSubexpressions(set, *it);
+ invalidateSubexpressions(set, *it);
}
}
}
@@ -226,17 +224,20 @@ struct SmartImprovementOperator : public ImprovementOperator<Domain> {
stableSet = invalidSet.inverse();
}
- log::strategy << "stable: " << stableSet;
- log::strategy << "infl: " << _influence;
+ log::strategy << "stable: " << stableSet << std::endl;
+ log::strategy << "infl: " << _influence << std::endl;
DynamicMaxStrategy strat(_system, stratOut, _influence, rho, stableSet, changedSet);
- log::strategy << "invalid: " << invalidSet;
+ log::strategy << "invalid: " << invalidSet << std::endl;
for (auto it = invalidSet.begin(),
end = invalidSet.end();
it != end;
++it) {
- log::strategy << _system.maxExpression(*it);
- strat.get(_system.maxExpression(*it));
+ unsigned int subExpression = strat.get(_system.maxExpression(*it));
+ log::strategy << _system.maxExpression(*it)
+ << " -> "
+ << subExpression << std::endl;
}
+ log::strategy << std::endl;
IdSet<MaxExpression<Domain>> seen;
for (auto it = changedSet.begin(),
end = changedSet.end();
diff --git a/impl/Log.hpp b/impl/Log.hpp
index 1e340c7..0f12fec 100644
--- a/impl/Log.hpp
+++ b/impl/Log.hpp
@@ -1,50 +1,63 @@
#ifndef LOG_HPP
#define LOG_HPP
+// could not be hackier, but C++ is annoying
+#define protected public
+#include <streambuf>
+#undef protected
+
#include <string>
-#include <ostream>
+#include <iostream>
#include <map>
namespace log {
- struct Logger {
- Logger(std::ostream& stream, const std::string& name)
- : _stream(stream),
- _name(name),
- _enabled(false) {}
+ struct LoggerBuffer : public std::streambuf {
+ LoggerBuffer(std::streambuf* buffer, const std::string& name)
+ : _buffer(buffer),
+ _prefix(name + " :- "),
+ _prefix_next(true) { }
+
+ int_type overflow(int_type c=EOF) {
+ if (_prefix_next) {
+ _buffer->sputn(_prefix.c_str(), _prefix.length());
+ _prefix_next = false;
+ }
+ if (c == '\n') {
+ _prefix_next = true;
+ }
+ return _buffer->overflow(c);
+ }
+ private:
+ std::streambuf* _buffer;
+ std::string _prefix;
+ bool _prefix_next;
+ };
+
+ struct Logger : public std::ostream {
+ Logger(std::streambuf* buffer, const std::string& name)
+ : std::ostream(NULL),
+ _buffer(buffer, name) { }
bool enabled() const {
- return _enabled;
+ return rdbuf() != NULL;
}
bool enabled(bool v) {
- bool ret = _enabled;
- _enabled = v;
+ bool ret = enabled();
+ rdbuf(v ? &_buffer : NULL);
return ret;
}
- template<typename T>
- friend Logger& operator<<(Logger&, const T&);
-
- private:
- std::ostream& _stream;
- std::string _name;
- bool _enabled;
+ private:
+ LoggerBuffer _buffer;
};
- template<typename T>
- Logger& operator<<(Logger& logger, const T& obj) {
- if (logger._enabled) {
- logger._stream << logger._name << " :- " << obj << std::endl;
- }
- return logger;
- }
-
- Logger info(std::cout, "info");
- Logger trace(std::cerr, "trace");
- Logger strategy(std::cerr, "strategy");
- Logger fixpoint(std::cerr, "fixpoint");
- Logger debug(std::cerr, "debug");
+ Logger info(std::cout.rdbuf(), "info");
+ Logger trace(std::cerr.rdbuf(), "trace");
+ Logger strategy(std::cerr.rdbuf(), "strategy");
+ Logger fixpoint(std::cerr.rdbuf(), "fixpoint");
+ Logger debug(std::cerr.rdbuf(), "debug");
}
diff --git a/impl/main.cpp b/impl/main.cpp
index 664fb6f..9e34071 100644
--- a/impl/main.cpp
+++ b/impl/main.cpp
@@ -1,7 +1,7 @@
+#include "Log.hpp"
#include <iostream>
#include <vector>
#include <sstream>
-#include "Log.hpp"
#include "Complete.hpp"
#include "Expression.hpp"
#include "Operator.hpp"
@@ -98,21 +98,27 @@ void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem<T>& system) {
typedef Complete<int> ZBar;
int main (int argc, char* argv[]) {
if (argc <= 3) {
- cout << "Usage: " << argv[0] << " filename [fixpoint strategy log]" << endl
+ cout << "Usage: " << argv[0] << " filename fixpoint strategy [log]" << endl
<< " fixpoint: naive | smart" << endl
<< " strategy: naive | repeat | smart" << endl
- << " log: [section[,section[,section[...]]]]" << endl;
+ << " log: section[,section[,section[...]]]" << endl;
exit(1);
}
- for (int i = 4; i < argc; ++i) {
- if (!strcmp(argv[i], "trace")) {
- log::trace.enabled(true);
- } else if (!strcmp(argv[i], "strategy")) {
- log::strategy.enabled(true);
- } else if (!strcmp(argv[i], "debug")) {
- log::debug.enabled(true);
- }
+ map<string,log::Logger*> loggers;
+ loggers["info"] = &log::info;
+ loggers["trace"] = &log::trace;
+ loggers["strategy"] = &log::strategy;
+ loggers["fixpoint"] = &log::fixpoint;
+ loggers["debug"] = &log::debug;
+
+ if (argc > 3) {
+ char* arg = argv[4];
+ char* str = strtok(arg, ",");
+ do {
+ if (loggers[str])
+ loggers[str]->enabled(true);
+ } while ((str = strtok(NULL, ",")) != NULL);
}
auto input = antlr3FileStreamNew((pANTLR3_UINT8)argv[1], ANTLR3_ENC_8BIT);
@@ -156,7 +162,7 @@ int main (int argc, char* argv[]) {
exit(1);
}
- log::debug << system;
+ log::debug << system << std::endl;
system.indexMaxExpressions(); // make reverse-lookup O(1) instead of O(n)
StableVariableAssignment<ZBar> result(system.variableCount(), infinity<ZBar>());
ConcreteMaxStrategy<ZBar> strategy(system);
@@ -169,11 +175,11 @@ int main (int argc, char* argv[]) {
s1.clear();
algorithm->maxFixpoint(system, strategy, result, s2, s1);
- log::debug << result;
+ log::debug << result << std::endl;
s2.clear();
changed = improvement->improve(system, strategy, result, s1, s2);
- log::debug << (changed ? "true" : "false");
+ log::debug << "Changed: " << (changed ? "true" : "false") << std::endl;
} while(changed);
cout << endl;
@@ -194,3 +200,4 @@ int main (int argc, char* argv[]) {
return 0;
}
+