diff options
Diffstat (limited to 'src/nodecache.h')
-rw-r--r-- | src/nodecache.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/nodecache.h b/src/nodecache.h new file mode 100644 index 0000000..cc3355e --- /dev/null +++ b/src/nodecache.h @@ -0,0 +1,46 @@ +#ifndef NODECACHE_H_ +#define NODECACHE_H_ + +#include <vector> +#include <string> +#include "node.h" + +/*! + Caches string values per node based on the node.index(). + The node index guaranteed to be unique per node tree since the index is reset + every time a new tree is generated. +*/ +class NodeCache +{ +public: + NodeCache() { } + virtual ~NodeCache() { } + + bool contains(const AbstractNode &node) const { + return !(*this)[node].empty(); + } + + const std::string & operator[](const AbstractNode &node) const { + if (this->cache.size() > node.index()) return this->cache[node.index()]; + else return this->nullvalue; + } + + void insert(const class AbstractNode &node, const std::string & value) { + if (this->cache.size() <= node.index()) this->cache.resize(node.index() + 1); + this->cache[node.index()] = value; + } + + void remove(const class AbstractNode &node) { + if (this->cache.size() > node.index()) this->cache[node.index()] = std::string(); + } + + void clear() { + this->cache.clear(); + } + +private: + std::vector<std::string> cache; + std::string nullvalue; +}; + +#endif |