|
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 struct Entry { 00052 const char* str; 00053 const T key; 00054 }; 00055 00056 00057 StringBijection() {} 00058 00059 00060 StringBijection(Entry entries[], T terminatorKey) { 00061 int i = 0; 00062 do { 00063 insert(entries[i].str, entries[i].key); 00064 } while (entries[i++].key != terminatorKey); 00065 } 00066 00067 00068 void insert(const std::string str, const T key) { 00069 myString2T[str] = key; 00070 myT2String[key] = str; 00071 } 00072 00073 00074 T get(const std::string& str) { 00075 if (hasString(str)) { 00076 return myString2T[str]; 00077 } else { 00078 throw InvalidArgument("String '" + str + "' not found."); 00079 } 00080 } 00081 00082 00083 const std::string& getString(const T key) { 00084 if (has(key)) { 00085 return myT2String[key]; 00086 } else { 00087 // cannot use toString(key) because that might create an infinite loop 00088 throw InvalidArgument("Key not found."); 00089 } 00090 } 00091 00092 00093 bool hasString(const std::string& str) { 00094 return myString2T.count(str) != 0; 00095 } 00096 00097 00098 bool has(const T key) { 00099 return myT2String.count(key) != 0; 00100 } 00101 00102 00103 size_t size() const { 00104 return myString2T.size(); 00105 } 00106 00107 00108 std::vector<std::string> getStrings() const { 00109 std::vector<std::string> result; 00110 typename std::map<std::string, T>::const_iterator it; // learn something new every day 00111 for (it = myString2T.begin(); it != myString2T.end(); it++) { 00112 result.push_back(it->first); 00113 } 00114 return result; 00115 } 00116 00117 00118 private: 00119 std::map<std::string, T> myString2T; 00120 std::map<T, std::string> myT2String; 00121 00122 }; 00123 00124 #endif 00125 00126 /****************************************************************************/ 00127