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