|
| 1 | +/// file: BiDict.cpp |
| 2 | + |
| 3 | + |
| 4 | +#include "feather/BiDict.h" |
| 5 | + |
| 6 | + |
| 7 | +namespace feather { |
| 8 | + |
| 9 | + |
| 10 | +BiDict::BiDict(const std::vector<std::string>& schema, |
| 11 | + const std::string& name) { |
| 12 | + this->name_ = name; |
| 13 | + this->schema_ = schema; |
| 14 | + |
| 15 | + int32_t col_index = 0; |
| 16 | + for (const std::string& key1 : schema) { |
| 17 | + this->col_schema_[key1] = col_index++; |
| 18 | + for (const std::string& key2 : schema) { |
| 19 | + if (key1 != key2) { |
| 20 | + std::string dict_name = key1 + "2" + key2; |
| 21 | + this->dicts_[dict_name] = {}; |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +int32_t BiDict::Register( |
| 29 | + const std::vector<std::string>& record) { |
| 30 | + int32_t field_num = this->schema_.size(); |
| 31 | + if (field_num != record.size()) { |
| 32 | + throw "The element number of record should be same with \ |
| 33 | + schema field number."; |
| 34 | + } |
| 35 | + for (int32_t index1 = 0; index1 < field_num; ++index1) { |
| 36 | + for (int32_t index2 = 0; index2 < field_num; ++index2) { |
| 37 | + if (index1 == index2) { continue; } |
| 38 | + std::string dict = this->Indexs2DictName(index1, index2); |
| 39 | + /// TODO@202108281125: Check key existence first. |
| 40 | + this->dicts_[dict][record[index1]].emplace_back(record[index2]); |
| 41 | + } |
| 42 | + } |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +std::string BiDict::Indexs2DictName( |
| 48 | + const int32_t index1, const int32_t index2) { |
| 49 | + if (this->schema_.size() == 0 || this->dicts_.size() == 0) { |
| 50 | + throw "No schema or dict has been registered/initialized."; |
| 51 | + } |
| 52 | + return this->schema_[index1] + "2" + this->schema_[index2]; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +std::vector<std::string> BiDict::Map( |
| 57 | + const std::string from, const std::string to, |
| 58 | + const std::string& key) { |
| 59 | + std::vector<std::string> value; |
| 60 | + std::string using_dict = from + "2" + to; |
| 61 | + if (this->dicts_.find(using_dict) == this->dicts_.end()) { |
| 62 | + throw ("No dict can mapping from " + from + " to " + to); |
| 63 | + } else if ( |
| 64 | + this->dicts_[using_dict].find(key) == this->dicts_[using_dict].end() |
| 65 | + ) { |
| 66 | + throw ("In inner dict '" + using_dict + "', no key " + key); |
| 67 | + } else { |
| 68 | + value = this->dicts_[using_dict][key]; |
| 69 | + } |
| 70 | + return value; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +/// TODO@202108281800 |
| 75 | +std::string BiDict::Persistence(const std::string& path) { |
| 76 | + std::string out_path; |
| 77 | + std::vector<std::string> record_vec; |
| 78 | + std::unordered_map<std::string, bool> records; |
| 79 | + if (path.size() == 0) { out_path = ("./" + this->name_ + ".txt"); } |
| 80 | + |
| 81 | + for (auto dict_iter = this->dicts_.begin(); |
| 82 | + dict_iter != this->dicts_.end(); ++dict_iter) { |
| 83 | + for (auto record_iter = dict_iter->second.begin(); |
| 84 | + record_iter != dict_iter->second.end(); ++record_iter) { |
| 85 | + record_vec.clear(); |
| 86 | + std::string dict_name = dict_iter->first; |
| 87 | + std::string key = record_iter->first; |
| 88 | + std::vector<std::string> value = record_iter->second; |
| 89 | + printf("dbg: %s: key=%s, val=%s\n", |
| 90 | + dict_name.c_str(), key.c_str(), value[0].c_str()); |
| 91 | + } |
| 92 | + } |
| 93 | + return out_path; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +} // namespace feather |
0 commit comments