Skip to content

Commit

Permalink
Added function getMultilineString in StringBijection. Refs #16228
Browse files Browse the repository at this point in the history
  • Loading branch information
palvarezlopez committed Feb 27, 2025
1 parent d7cf9d2 commit 760a044
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/utils/common/StringBijection.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* checkDuplicates is set to false in the constructor or the insert function or if
* the addAlias function is used.
*/

template< class T >
class StringBijection {

Expand All @@ -46,6 +45,7 @@ class StringBijection {
#pragma warning(push)
#pragma warning(disable:4510 4512 4610) // no default constructor and no assignment operator; conflicts with initializer
#endif
/// @brief bijection entry
struct Entry {
const char* str;
const T key;
Expand All @@ -54,18 +54,18 @@ class StringBijection {
#pragma warning(pop)
#endif


/// @brief default constructor
StringBijection() {}


/// @brief parameter constructor
StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates = true) {
int i = 0;
do {
insert(entries[i].str, entries[i].key, checkDuplicates);
} while (entries[i++].key != terminatorKey);
}


/// @brief insert string and their associated key
void insert(const std::string str, const T key, bool checkDuplicates = true) {
if (checkDuplicates) {
if (has(key)) {
Expand All @@ -80,18 +80,18 @@ class StringBijection {
myT2String[key] = str;
}


/// @brief add alias to the given key
void addAlias(const std::string str, const T key) {
myString2T[str] = key;
}


/// @brief remove string
void remove(const std::string str, const T key) {
myString2T.erase(str);
myT2String.erase(key);
}


/// @brief get key
T get(const std::string& str) const {
if (hasString(str)) {
return myString2T.find(str)->second;
Expand All @@ -100,7 +100,7 @@ class StringBijection {
}
}


/// @brief get string
const std::string& getString(const T key) const {
if (has(key)) {
return myT2String.find(key)->second;
Expand All @@ -110,22 +110,22 @@ class StringBijection {
}
}


/// @brief check if the given string exist
bool hasString(const std::string& str) const {
return myString2T.count(str) != 0;
}


/// @brief check if the given key exist
bool has(const T key) const {
return myT2String.count(key) != 0;
}


/// @brief get number of key-attributes
int size() const {
return (int)myString2T.size();
}


/// @brief get all strings
std::vector<std::string> getStrings() const {
std::vector<std::string> result;
for (auto item : myT2String) {
Expand All @@ -134,7 +134,7 @@ class StringBijection {
return result;
}


/// @brief get all keys
std::vector<T> getValues() const {
std::vector<T> result;
for (auto item : myT2String) {
Expand All @@ -143,17 +143,30 @@ class StringBijection {
return result;
}


/// @brief add the given list of keys
void addKeysInto(std::vector<T>& list) const {
typename std::map<T, std::string>::const_iterator it; // learn something new every day
for (it = myT2String.begin(); it != myT2String.end(); it++) {
list.push_back(it->first);
}
}

/// @brief get multiline string (all strings concatenated and separated by '\n')
std::string getMultilineString() const {
std::string result;
if (myT2String.size() > 0) {
for (auto item : myT2String) {
result.append(item.second + "\n");
}
result.pop_back();
}
return result;
}

private:
/// @brief map with the keys vinculated with strings
std::map<std::string, T> myString2T;
std::map<T, std::string> myT2String;

/// @brief map with the strings vinculated with keys
std::map<T, std::string> myT2String;
};

0 comments on commit 760a044

Please sign in to comment.