SUMO - Simulation of Urban MObility
StringBijection.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2011-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Bidirectional map between string and something else
20 /****************************************************************************/
21 #ifndef StringBijection_h
22 #define StringBijection_h
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <iostream>
35 #include <map>
36 #include <vector>
37 #include <string>
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
50 template< class T >
52 
53 public:
54 
55 #ifdef _MSC_VER
56 #pragma warning(push)
57 #pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer
58 #endif
59  struct Entry {
60  const char* str;
61  const T key;
62  };
63 #ifdef _MSC_VER
64 #pragma warning(pop)
65 #endif
66 
67 
69 
70 
71  StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates = true) {
72  int i = 0;
73  do {
74  insert(entries[i].str, entries[i].key, checkDuplicates);
75  } while (entries[i++].key != terminatorKey);
76  }
77 
78 
79  void insert(const std::string str, const T key, bool checkDuplicates = true) {
80  if (checkDuplicates) {
81  if (has(key)) {
82  // cannot use toString(key) because that might create an infinite loop
83  throw InvalidArgument("Duplicate key.");
84  }
85  if (hasString(str)) {
86  throw InvalidArgument("Duplicate string '" + str + "'.");
87  }
88  }
89  myString2T[str] = key;
90  myT2String[key] = str;
91  }
92 
93 
94  void addAlias(const std::string str, const T key) {
95  myString2T[str] = key;
96  }
97 
98 
99  void remove(const std::string str, const T key) {
100  myString2T.erase(str);
101  myT2String.erase(key);
102  }
103 
104 
105  T get(const std::string& str) const {
106  if (hasString(str)) {
107  return myString2T.find(str)->second;
108  } else {
109  throw InvalidArgument("String '" + str + "' not found.");
110  }
111  }
112 
113 
114  const std::string& getString(const T key) const {
115  if (has(key)) {
116  return myT2String.find(key)->second;
117  } else {
118  // cannot use toString(key) because that might create an infinite loop
119  throw InvalidArgument("Key not found.");
120  }
121  }
122 
123 
124  bool hasString(const std::string& str) const {
125  return myString2T.count(str) != 0;
126  }
127 
128 
129  bool has(const T key) const {
130  return myT2String.count(key) != 0;
131  }
132 
133 
134  int size() const {
135  return (int)myString2T.size();
136  }
137 
138 
139  std::vector<std::string> getStrings() const {
140  std::vector<std::string> result;
141  typename std::map<T, std::string>::const_iterator it; // learn something new every day
142  for (it = myT2String.begin(); it != myT2String.end(); it++) {
143  result.push_back(it->second);
144  }
145  return result;
146  }
147 
148 
149  void addKeysInto(std::vector<T>& list) const {
150  typename std::map<T, std::string>::const_iterator it; // learn something new every day
151  for (it = myT2String.begin(); it != myT2String.end(); it++) {
152  list.push_back(it->first);
153  }
154  }
155 
156 
157 private:
158  std::map<std::string, T> myString2T;
159  std::map<T, std::string> myT2String;
160 
161 };
162 
163 #endif
164 
165 /****************************************************************************/
166 
const std::string & getString(const T key) const
bool has(const T key) const
std::vector< std::string > getStrings() const
int size() const
void insert(const std::string str, const T key, bool checkDuplicates=true)
std::map< std::string, T > myString2T
std::map< T, std::string > myT2String
void addAlias(const std::string str, const T key)
void addKeysInto(std::vector< T > &list) const
const char * str
const T key
StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates=true)
bool hasString(const std::string &str) const