SUMO - Simulation of Urban MObility

StringBijection.h

Go to the documentation of this file.
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-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef StringBijection_h
00020 #define StringBijection_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <iostream>
00033 #include <map>
00034 #include <vector>
00035 #include <string>
00036 #include <utils/common/UtilExceptions.h>
00037 
00038 // ===========================================================================
00039 // class definitions
00040 // ===========================================================================
00045 template< class T  >
00046 class StringBijection {
00047 
00048 public:
00049 
00050     struct Entry {
00051         const char* str;
00052         const T key;
00053     };
00054 
00055 
00056     StringBijection() {}
00057 
00058 
00059     StringBijection(Entry entries[], T terminatorKey) {
00060         int i = 0;
00061         do {
00062             insert(entries[i].str, entries[i].key);
00063         } while (entries[i++].key != terminatorKey);
00064     }
00065 
00066 
00067     void insert(const std::string str, const T key) {
00068         myString2T[str] = key;
00069         myT2String[key] = str;
00070     }
00071 
00072 
00073     T get(const std::string& str) {
00074         if (hasString(str)) {
00075             return myString2T[str];
00076         } else {
00077             throw InvalidArgument("String '" + str + "' not found.");
00078         }
00079     }
00080 
00081 
00082     const std::string& getString(const T key) {
00083         if (has(key)) {
00084             return myT2String[key];
00085         } else {
00086             // cannot use toString(key) because that might create an infinite loop
00087             throw InvalidArgument("Key not found.");
00088         }
00089     }
00090 
00091 
00092     bool hasString(const std::string& str) {
00093         return myString2T.count(str) != 0;
00094     }
00095 
00096 
00097     bool has(const T key) {
00098         return myT2String.count(key) != 0;
00099     }
00100 
00101 
00102     size_t size() const {
00103         return myString2T.size();
00104     }
00105 
00106 
00107     std::vector<std::string> getStrings() const {
00108         std::vector<std::string> result;
00109         typename std::map<std::string, T>::const_iterator it; // learn something new every day
00110         for (it = myString2T.begin(); it!=myString2T.end(); it++) {
00111             result.push_back(it->first);
00112         }
00113         return result;
00114     }
00115 
00116 
00117 private:
00118     std::map<std::string, T> myString2T;
00119     std::map<T, std::string> myT2String;
00120 
00121 };
00122 
00123 #endif
00124 
00125 /****************************************************************************/
00126 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines