Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
StringBijection.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2011-2026 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 <config.h>
24#include <map>
25#include <vector>
26#include <string>
28
29// ===========================================================================
30// class definitions
31// ===========================================================================
38template< class T >
40
41public:
42
43#ifdef _MSC_VER
44#pragma warning(push)
45#pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer
46#endif
48 struct Entry {
49 const std::string str;
50 const T key;
51 };
52#ifdef _MSC_VER
53#pragma warning(pop)
54#endif
55
58
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
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
83 void addAlias(const std::string str, const T key) {
84 myString2T[str] = key;
85 }
86
88 void remove(const std::string str, const T key) {
89 myString2T.erase(str);
90 myT2String.erase(key);
91 }
92
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
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
113 bool hasString(const std::string& str) const {
114 return myString2T.count(str) != 0;
115 }
116
118 bool has(const T key) const {
119 return myT2String.count(key) != 0;
120 }
121
123 int size() const {
124 return (int)myString2T.size();
125 }
126
128 std::vector<std::string> getStrings() const {
129 std::vector<std::string> result;
130 for (auto item : myT2String) {
131 result.push_back(item.second);
132 }
133 return result;
134 }
135
137 std::vector<T> getValues() const {
138 std::vector<T> result;
139 for (auto item : myT2String) {
140 result.push_back(item.first);
141 }
142 return result;
143 }
144
146 void addKeysInto(std::vector<T>& list) const {
147 typename std::map<T, std::string>::const_iterator it; // learn something new every day
148 for (it = myT2String.begin(); it != myT2String.end(); it++) {
149 list.push_back(it->first);
150 }
151 }
152
155 std::string getMultilineString() const {
156 std::string result;
157 if (myT2String.size() > 0) {
158 for (auto item : myT2String) {
159 result.append(item.second + "\n");
160 }
161 result.pop_back();
162 }
163 return result;
164 }
165
166private:
168 std::map<std::string, T> myString2T;
169
171 std::map<T, std::string> myT2String;
172};
const std::string & getString(const T key) const
get string
void remove(const std::string str, const T key)
remove string
std::vector< std::string > getStrings() const
get all strings
bool has(const T key) const
check if the given key exist
std::vector< T > getValues() const
get all keys
bool hasString(const std::string &str) const
check if the given string exist
StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates=true)
parameter constructor
StringBijection()
default constructor
std::string getMultilineString() const
get multiline string (all strings concatenated and separated by ' ')
int size() const
get number of key-attributes
std::map< std::string, T > myString2T
map with the keys vinculated with strings
void addAlias(const std::string str, const T key)
add alias to the given key
T get(const std::string &str) const
get key
void addKeysInto(std::vector< T > &list) const
add the given list of keys
std::map< T, std::string > myT2String
map with the strings vinculated with keys
void insert(const std::string str, const T key, bool checkDuplicates=true)
insert string and their associated key
bijection entry
const T key
const std::string str