[lib] developing naming manager

This commit is contained in:
tangxifan 2023-09-15 16:02:13 -07:00
parent af67b02cca
commit b65dda90c4
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,50 @@
/******************************************************************************
* Memember functions for data structure ModuleNameMap
******************************************************************************/
/* Headers from vtrutil library */
#include "module_name_map.h"
#include <algorithm>
#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 */

View File

@ -0,0 +1,34 @@
#ifndef MODULE_NAME_MAP_H
#define MODULE_NAME_MAP_H
/********************************************************************
* Include header files required by the data structure definition
*******************************************************************/
#include <string>
#include <map>
/* 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<std::string, std::string> tag2names_;
std::map<std::string, std::string> name2tags_;
};
} /* End namespace openfpga*/
#endif