SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringBijection.h
Go to the documentation of this file.
1 /****************************************************************************/
9 // Bidirectional map between string and something else
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2011-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef StringBijection_h
23 #define StringBijection_h
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <iostream>
36 #include <map>
37 #include <vector>
38 #include <string>
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
51 template< class T >
53 
54 public:
55 
56 #ifdef _MSC_VER
57 #pragma warning(push)
58 #pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer
59 #endif
60  struct Entry {
61  const char* str;
62  const T key;
63  };
64 #ifdef _MSC_VER
65 #pragma warning(pop)
66 #endif
67 
68 
70 
71 
72  StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates = true) {
73  int i = 0;
74  do {
75  insert(entries[i].str, entries[i].key, checkDuplicates);
76  } while (entries[i++].key != terminatorKey);
77  }
78 
79 
80  void insert(const std::string str, const T key, bool checkDuplicates = true) {
81  if (checkDuplicates) {
82  if (has(key)) {
83  // cannot use toString(key) because that might create an infinite loop
84  throw InvalidArgument("Duplicate key.");
85  }
86  if (hasString(str)) {
87  throw InvalidArgument("Duplicate string '" + str + "'.");
88  }
89  }
90  myString2T[str] = key;
91  myT2String[key] = str;
92  }
93 
94 
95  void addAlias(const std::string str, const T key) {
96  myString2T[str] = key;
97  }
98 
99 
100  void remove(const std::string str, const T key) {
101  myString2T.erase(str);
102  myT2String.erase(key);
103  }
104 
105 
106  T get(const std::string& str) const {
107  if (hasString(str)) {
108  return myString2T.find(str)->second;
109  } else {
110  throw InvalidArgument("String '" + str + "' not found.");
111  }
112  }
113 
114 
115  const std::string& getString(const T key) const {
116  if (has(key)) {
117  return myT2String.find(key)->second;
118  } else {
119  // cannot use toString(key) because that might create an infinite loop
120  throw InvalidArgument("Key not found.");
121  }
122  }
123 
124 
125  bool hasString(const std::string& str) const {
126  return myString2T.count(str) != 0;
127  }
128 
129 
130  bool has(const T key) const {
131  return myT2String.count(key) != 0;
132  }
133 
134 
135  size_t size() const {
136  return myString2T.size();
137  }
138 
139 
140  std::vector<std::string> getStrings() const {
141  std::vector<std::string> result;
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  result.push_back(it->second);
145  }
146  return result;
147  }
148 
149 
150  void addKeysInto(std::vector<T>& list) const {
151  typename std::map<T, std::string>::const_iterator it; // learn something new every day
152  for (it = myT2String.begin(); it != myT2String.end(); it++) {
153  list.push_back(it->first);
154  }
155  }
156 
157 
158 private:
159  std::map<std::string, T> myString2T;
160  std::map<T, std::string> myT2String;
161 
162 };
163 
164 #endif
165 
166 /****************************************************************************/
167