C
Chris Thomasson
Chris Thomasson said:[...]Chris Thomasson said:[...]Chris Thomasson said:Chris Thomasson wrote:
[...]
Yes: the task rewrites expressions trees. There is a very similar C++
program here:
http://www.codecodex.com/wiki/index.php?title=Derivative
[...]
Alls I have to do is replace 'Var, Int, Plus and Times' with the VERY
simple cache outlined above and I know it will cut the number of
new/delete calls by a large margin. That about all I can do in C++. Is
that fair?
Also, I would need to create a special virtual function or id in the base
class which allows be to replace delete calls. Sketch:
___________________________________________________________________________
class Base {
public:
virtual ~Base() {};
virtual void CacheDelete() = 0;
virtual const Base *clone() = 0;
virtual const Base *d(const std::string &v) const = 0;
virtual std:stream &print(std:stream &o) const = 0;
};
#define INT_DEPTH() 10000
struct Int : public Base {
int n;
Int* m_next;
Int(int m) : n(m) {}
~Int() {}
void Ctor(int m) {
n = m;
}
static Int* g_head;
static int g_depth;
void CacheDelete() {
CachePush(this);
}
static Int* CachePop(int const m) {
Int* _this = g_head;
if (! _this) {
_this = new Int(m);
} else {
g_head = _this->m_next;
--g_depth;
_this->Ctor(m);
}
return _this;
}
static void CachePush(Int* const _this) {
if (g_depth < INT_DEPTH()) {
_this->m_next = g_head;
g_head = _this;
++g_depth;
} else {
delete _this;
}
}
const Base *clone() { return CachePop(n); }
const Base *d(const std::string &v) const { return CachePop(0); }
std:stream &print(std:stream &o) const { return o << n; }
};
Int* Int::g_head = NULL;
int Int::g_depth = 0;
___________________________________________________________________________
This is only the Int class, but you get my drift... I would replace delete
calls on the Base with Base->DELETE(). This would be simpler than an id.