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