SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // Bijective Container between string and something else 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This file is part of SUMO. 00014 // SUMO is free software: you can redistribute it and/or modify 00015 // it under the terms of the GNU General Public License as published by 00016 // the Free Software Foundation, either version 3 of the License, or 00017 // (at your option) any later version. 00018 // 00019 /****************************************************************************/ 00020 #ifndef StringBijection_h 00021 #define StringBijection_h 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #include <iostream> 00034 #include <map> 00035 #include <vector> 00036 #include <string> 00037 #include <utils/common/UtilExceptions.h> 00038 00039 // =========================================================================== 00040 // class definitions 00041 // =========================================================================== 00046 template< class T > 00047 class StringBijection { 00048 00049 public: 00050 00051 #ifdef _MSC_VER 00052 #pragma warning(push) 00053 #pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer 00054 #endif 00055 struct Entry { 00056 const char* str; 00057 const T key; 00058 }; 00059 #ifdef _MSC_VER 00060 #pragma warning(pop) 00061 #endif 00062 00063 00064 StringBijection() {} 00065 00066 00067 StringBijection(Entry entries[], T terminatorKey) { 00068 int i = 0; 00069 do { 00070 insert(entries[i].str, entries[i].key); 00071 } while (entries[i++].key != terminatorKey); 00072 } 00073 00074 00075 void insert(const std::string str, const T key) { 00076 myString2T[str] = key; 00077 myT2String[key] = str; 00078 } 00079 00080 00081 T get(const std::string& str) { 00082 if (hasString(str)) { 00083 return myString2T[str]; 00084 } else { 00085 throw InvalidArgument("String '" + str + "' not found."); 00086 } 00087 } 00088 00089 00090 const std::string& getString(const T key) { 00091 if (has(key)) { 00092 return myT2String[key]; 00093 } else { 00094 // cannot use toString(key) because that might create an infinite loop 00095 throw InvalidArgument("Key not found."); 00096 } 00097 } 00098 00099 00100 bool hasString(const std::string& str) { 00101 return myString2T.count(str) != 0; 00102 } 00103 00104 00105 bool has(const T key) { 00106 return myT2String.count(key) != 0; 00107 } 00108 00109 00110 size_t size() const { 00111 return myString2T.size(); 00112 } 00113 00114 00115 std::vector<std::string> getStrings() const { 00116 std::vector<std::string> result; 00117 typename std::map<T, std::string>::const_iterator it; // learn something new every day 00118 for (it = myT2String.begin(); it != myT2String.end(); it++) { 00119 result.push_back(it->second); 00120 } 00121 return result; 00122 } 00123 00124 00125 private: 00126 std::map<std::string, T> myString2T; 00127 std::map<T, std::string> myT2String; 00128 00129 }; 00130 00131 #endif 00132 00133 /****************************************************************************/ 00134