From: Clifford Wolf Date: Sat, 16 Aug 2014 22:56:47 +0000 (+0200) Subject: Added stackmap<> container X-Git-Tag: yosys-0.4~240 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9bacc0b54c0901dfd34c2230d3295720653c0a7a;p=yosys.git Added stackmap<> container --- diff --git a/kernel/utils.h b/kernel/utils.h index 4226e270e..7f10619bc 100644 --- a/kernel/utils.h +++ b/kernel/utils.h @@ -17,8 +17,114 @@ * */ -#ifndef TOPOSORT_H -#define TOPOSORT_H +// This file contains various c++ utility routines and helper classes that +// do not depend on any other components of yosys (except stuff like log_*). + +#include "kernel/yosys.h" + +#ifndef UTILS_H +#define UTILS_H + +// ------------------------------------------------ +// A map-like container, but you can save and restore the state +// ------------------------------------------------ + +template> +struct stackmap +{ +private: + std::vector> backup_state; + std::map current_state; + static T empty_tuple; + +public: + stackmap() { } + stackmap(const std::map &other) : current_state(other) { } + + template + void operator=(const Other &other) + { + for (auto &it : current_state) + if (!backup_state.empty() && backup_state.back().count(it.first) == 0) + backup_state.back()[it.first] = new T(it.second); + current_state.clear(); + + for (auto &it : other) + set(it.first, it.second); + } + + bool has(const Key &k) + { + return current_state.count(k) != 0; + } + + void set(const Key &k, const T &v) + { + if (!backup_state.empty() && backup_state.back().count(k) == 0) + backup_state.back()[k] = current_state.count(k) ? new T(current_state.at(k)) : nullptr; + current_state[k] = v; + } + + void unset(const Key &k) + { + if (!backup_state.empty() && backup_state.back().count(k) == 0) + backup_state.back()[k] = current_state.count(k) ? new T(current_state.at(k)) : nullptr; + current_state.erase(k); + } + + const T &get(const Key &k) + { + if (current_state.count(k) == 0) + return empty_tuple; + return current_state.at(k); + } + + void reset(const Key &k) + { + for (int i = SIZE(backup_state)-1; i >= 0; i--) + if (backup_state[i].count(k) != 0) { + if (backup_state[i].at(k) == nullptr) + current_state.erase(k); + else + current_state[k] = *backup_state[i].at(k); + return; + } + current_state.erase(k); + } + + const std::map &stdmap() + { + return current_state; + } + + void save() + { + backup_state.resize(backup_state.size()+1); + } + + void restore() + { + log_assert(!backup_state.empty()); + for (auto &it : backup_state.back()) + if (it.second != nullptr) { + current_state[it.first] = *it.second; + delete it.second; + } else + current_state.erase(it.first); + backup_state.pop_back(); + } + + ~stackmap() + { + while (!backup_state.empty()) + restore(); + } +}; + + +// ------------------------------------------------ +// A simple class for topological sorting +// ------------------------------------------------ template struct TopoSort diff --git a/kernel/yosys.h b/kernel/yosys.h index e12069b4c..79c90628c 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -42,6 +42,7 @@ #include #include #include +#include #include #include