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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Bidirectional map between string and something else
21 /****************************************************************************/
22 #pragma once
23 #include <iostream>
24 #include <map>
25 #include <vector>
26 #include <string>
28 
29 // ===========================================================================
30 // class definitions
31 // ===========================================================================
39 template< class T >
41 
42 public:
43 
44 #ifdef _MSC_VER
45 #pragma warning(push)
46 #pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer
47 #endif
48  struct Entry {
49  const char* str;
50  const T key;
51  };
52 #ifdef _MSC_VER
53 #pragma warning(pop)
54 #endif
55 
56 
58 
59 
60  StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates = true) {
61  int i = 0;
62  do {
63  insert(entries[i].str, entries[i].key, checkDuplicates);
64  } while (entries[i++].key != terminatorKey);
65  }
66 
67 
68  void insert(const std::string str, const T key, bool checkDuplicates = true) {
69  if (checkDuplicates) {
70  if (has(key)) {
71  // cannot use toString(key) because that might create an infinite loop
72  throw InvalidArgument("Duplicate key.");
73  }
74  if (hasString(str)) {
75  throw InvalidArgument("Duplicate string '" + str + "'.");
76  }
77  }
78  myString2T[str] = key;
79  myT2String[key] = str;
80  }
81 
82 
83  void addAlias(const std::string str, const T key) {
84  myString2T[str] = key;
85  }
86 
87 
88  void remove(const std::string str, const T key) {
89  myString2T.erase(str);
90  myT2String.erase(key);
91  }
92 
93 
94  T get(const std::string& str) const {
95  if (hasString(str)) {
96  return myString2T.find(str)->second;
97  } else {
98  throw InvalidArgument("String '" + str + "' not found.");
99  }
100  }
101 
102 
103  const std::string& getString(const T key) const {
104  if (has(key)) {
105  return myT2String.find(key)->second;
106  } else {
107  // cannot use toString(key) because that might create an infinite loop
108  throw InvalidArgument("Key not found.");
109  }
110  }
111 
112 
113  bool hasString(const std::string& str) const {
114  return myString2T.count(str) != 0;
115  }
116 
117 
118  bool has(const T key) const {
119  return myT2String.count(key) != 0;
120  }
121 
122 
123  int size() const {
124  return (int)myString2T.size();
125  }
126 
127 
128  std::vector<std::string> getStrings() const {
129  std::vector<std::string> result;
130  typename std::map<T, std::string>::const_iterator it; // learn something new every day
131  for (it = myT2String.begin(); it != myT2String.end(); it++) {
132  result.push_back(it->second);
133  }
134  return result;
135  }
136 
137 
138  void addKeysInto(std::vector<T>& list) const {
139  typename std::map<T, std::string>::const_iterator it; // learn something new every day
140  for (it = myT2String.begin(); it != myT2String.end(); it++) {
141  list.push_back(it->first);
142  }
143  }
144 
145 
146 private:
147  std::map<std::string, T> myString2T;
148  std::map<T, std::string> myT2String;
149 
150 };
void remove(const std::string str, const T key)
bool has(const T key) const
bool hasString(const std::string &str) const
StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates=true)
int size() const
const std::string & getString(const T key) const
std::map< std::string, T > myString2T
void addAlias(const std::string str, const T key)
T get(const std::string &str) const
void addKeysInto(std::vector< T > &list) const
std::map< T, std::string > myT2String
std::vector< std::string > getStrings() const
void insert(const std::string str, const T key, bool checkDuplicates=true)
const T key
const char * str