File indexing completed on 2025-08-06 08:14:12
0001 #ifndef noiDict__h
0002 #define noiDict__h
0003
0004
0005
0006 struct noiWordIn {
0007 string val;
0008 noiWordIn (const char* _) { ostringstream inp; inp << _; val = inp.str(); };
0009 noiWordIn (string _) { ostringstream inp; inp << _; val = inp.str(); };
0010 noiWordIn (int _) { ostringstream inp; inp << _; val = inp.str(); };
0011 noiWordIn (double _) { ostringstream inp; inp << _; val = inp.str(); };
0012 };
0013 struct noiWordOut {
0014 string val;
0015 noiWordOut(string _) : val{_} {};
0016 operator string () { return val; };
0017
0018
0019 operator double () { istringstream iss (val); double rval; iss >> rval; return rval; };
0020 operator const char* () { return val.c_str(); };
0021 };
0022 struct noiDict {
0023
0024 vector<string> data {};
0025 noiDict (vector<noiWordIn> words) {
0026 for (auto W : words) data.push_back(W.val);
0027 };
0028 noiDict() {};
0029 bool operator[](string key) { return find(data.begin(),data.end(),key) != data.end(); };
0030
0031 noiWordOut operator()(string key, int offset=1) {
0032
0033 auto iter = find(data.begin(),data.end(),key);
0034 if (iter == data.end()) {
0035 throw runtime_error(Form("Failed to find key \"%s\" in noiDict",key.c_str()));
0036 return {""};
0037 }
0038 int i_iter = iter-data.begin();
0039 if (i_iter+offset >= data.size()) {
0040 throw runtime_error(Form("Failed to find value for key \"%s\" offset by %i in noiDict",key.c_str(),offset));
0041 return {""};
0042 }
0043 return {*(iter+offset)};
0044 }
0045 noiDict& operator += (noiDict& _) {
0046 data.insert(data.begin(),_.data.begin(),_.data.end());
0047 return *this;
0048 }
0049 };
0050 ostream& operator<< (ostream& os, const noiDict& opt) {
0051 for (auto& val : opt.data) cout << val << " "; cout << endl;
0052 return os;
0053 };
0054 #endif