diff --git a/libs/libnamemanager/src/base/module_name_map.cpp b/libs/libnamemanager/src/base/module_name_map.cpp new file mode 100644 index 000000000..bcb1c23c2 --- /dev/null +++ b/libs/libnamemanager/src/base/module_name_map.cpp @@ -0,0 +1,50 @@ +/****************************************************************************** + * Memember functions for data structure ModuleNameMap + ******************************************************************************/ +/* Headers from vtrutil library */ +#include "module_name_map.h" + +#include + +#include "command_exit_codes.h" +#include "vtr_assert.h" +#include "vtr_log.h" +#include "vtr_time.h" + +/* begin namespace openfpga */ +namespace openfpga { + +/************************************************** + * Public Accessors + *************************************************/ +std::string ModuleNameMap::name(const std::string& tag) const { + auto result = tag2names_.find(tag); + if (result == tag2names_.end()) { + VTR_LOG_ERROR("The given built-in name '%s' does not exist!\n", tag.c_str()); + return std::string(); + } + return result->second; +} + +int ModuleNameMap::set_tag_to_name_pair(const std::string& tag, const std::string& name) { + /* tagA <--x--> nameA + * | + * +----> nameB + * tagB <--x--> nameB + * Scenarios to be considered: + * - Remove the double links between tagA and nameA + * - nameB should NOT be mapped to any other tags! + */ + auto result = name2tags_.find(name); + if (result != name2tags_.end() && result->second != tag) { + VTR_LOG_ERROR("The customized name '%s' has already been mapped to a built-in name '%s'! Fail to bind it to a new built-in name '%s'\n", name.c_str(), result->second.c_str(), tag.c_str()); + return CMD_EXEC_FATAL_ERROR; + } + /* Create double link */ + name2tags_[name] = tag; + tag2names_[tag] = name; + /* Clean up */ + name2tags_.erase(name); +} + +} /* end namespace openfpga */ diff --git a/libs/libnamemanager/src/base/module_name_map.h b/libs/libnamemanager/src/base/module_name_map.h new file mode 100644 index 000000000..d2c921b81 --- /dev/null +++ b/libs/libnamemanager/src/base/module_name_map.h @@ -0,0 +1,34 @@ +#ifndef MODULE_NAME_MAP_H +#define MODULE_NAME_MAP_H + +/******************************************************************** + * Include header files required by the data structure definition + *******************************************************************/ +#include +#include + +/* Begin namespace openfpga */ +namespace openfpga { + +/** + * @brief Module name map is a data structure to show mapping between a tag (built-in name) and customized names (may be given by users) + */ +class ModuleNameMap { + public: /* Public accessors */ + /** @brief Get customized name with a given tag */ + std::string name(const std::string& tag) const; + + public: /* Public mutators */ + /** @brief Create the one-on-one mapping between an built-in name and a customized name. Return 0 for success, return 1 for fail */ + int set_tag_to_name_pair(const std::string& tag, const std::string& name); + private: /* Internal Data */ + /* built-in name -> customized_name + * Create a double link to check any customized name is mapped to more than 1 built-in name! + */ + std::map tag2names_; + std::map name2tags_; +}; + +} /* End namespace openfpga*/ + +#endif