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