LCOV - code coverage report
Current view: top level - src/utils/common - StringBijection.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 61.0 % 41 25
Test Date: 2026-06-15 15:46:12 Functions: 87.6 % 177 155

            Line data    Source code
       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              : /****************************************************************************/
      14              : /// @file    StringBijection.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @author  Jakob Erdmann
      18              : /// @date    Mar 2011
      19              : ///
      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>
      27              : #include <utils/common/UtilExceptions.h>
      28              : 
      29              : // ===========================================================================
      30              : // class definitions
      31              : // ===========================================================================
      32              : /**
      33              :  * Template container for maintaining a bidirectional map between strings and something else
      34              :  * It is not always a bijection since it allows for duplicate entries on both sides if either
      35              :  * checkDuplicates is set to false in the constructor or the insert function or if
      36              :  * the addAlias function is used.
      37              :  */
      38              : template< class T  >
      39              : class StringBijection {
      40              : 
      41              : public:
      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
      47              :     /// @brief bijection entry
      48              :     struct Entry {
      49              :         const std::string str;
      50              :         const T key;
      51              :     };
      52              : #ifdef _MSC_VER
      53              : #pragma warning(pop)
      54              : #endif
      55              : 
      56              :     /// @brief default constructor
      57       415616 :     StringBijection() {}
      58              : 
      59              :     /// @brief parameter constructor
      60      2640628 :     StringBijection(Entry entries[], T terminatorKey, bool checkDuplicates = true) {
      61              :         int i = 0;
      62              :         do {
      63     42710024 :             insert(entries[i].str, entries[i].key, checkDuplicates);
      64     21355012 :         } while (entries[i++].key != terminatorKey);
      65      2640628 :     }
      66              : 
      67              :     /// @brief insert string and their associated key
      68     71477873 :     void insert(const std::string str, const T key, bool checkDuplicates = true) {
      69     71477873 :         if (checkDuplicates) {
      70     59814869 :             if (has(key)) {
      71              :                 // cannot use toString(key) because that might create an infinite loop
      72            0 :                 throw InvalidArgument("Duplicate key.");
      73              :             }
      74              :             if (hasString(str)) {
      75            0 :                 throw InvalidArgument("Duplicate string '" + str + "'.");
      76              :             }
      77              :         }
      78     71477873 :         myString2T[str] = key;
      79     71477873 :         myT2String[key] = str;
      80     71477873 :     }
      81              : 
      82              :     /// @brief add alias to the given key
      83              :     void addAlias(const std::string str, const T key) {
      84      7167635 :         myString2T[str] = key;
      85              :     }
      86              : 
      87              :     /// @brief remove string
      88              :     void remove(const std::string str, const T key) {
      89              :         myString2T.erase(str);
      90              :         myT2String.erase(key);
      91              :     }
      92              : 
      93              :     /// @brief get key
      94     11381172 :     T get(const std::string& str) const {
      95              :         if (hasString(str)) {
      96     11381172 :             return myString2T.find(str)->second;
      97              :         } else {
      98            0 :             throw InvalidArgument("String '" + str + "' not found.");
      99              :         }
     100              :     }
     101              : 
     102              :     /// @brief get string
     103    719342436 :     const std::string& getString(const T key) const {
     104              :         if (has(key)) {
     105    719342436 :             return myT2String.find(key)->second;
     106              :         } else {
     107              :             // cannot use toString(key) because that might create an infinite loop
     108            0 :             throw InvalidArgument("Key not found.");
     109              :         }
     110              :     }
     111              : 
     112              :     /// @brief check if the given string exist
     113              :     bool hasString(const std::string& str) const {
     114            0 :         return myString2T.count(str) != 0;
     115              :     }
     116              : 
     117              :     /// @brief check if the given key exist
     118              :     bool has(const T key) const {
     119              :         return myT2String.count(key) != 0;
     120              :     }
     121              : 
     122              :     /// @brief get number of key-attributes
     123              :     int size() const {
     124          317 :         return (int)myString2T.size();
     125              :     }
     126              : 
     127              :     /// @brief get all strings
     128        29470 :     std::vector<std::string> getStrings() const {
     129              :         std::vector<std::string> result;
     130       774545 :         for (auto item : myT2String) {
     131       745075 :             result.push_back(item.second);
     132              :         }
     133        29470 :         return result;
     134            0 :     }
     135              : 
     136              :     /// @brief get all keys
     137            7 :     std::vector<T> getValues() const {
     138              :         std::vector<T> result;
     139           98 :         for (auto item : myT2String) {
     140           91 :             result.push_back(item.first);
     141              :         }
     142            7 :         return result;
     143            0 :     }
     144              : 
     145              :     /// @brief add the given list of keys
     146            0 :     void addKeysInto(std::vector<T>& list) const {
     147              :         typename std::map<T, std::string>::const_iterator it; // learn something new every day
     148            0 :         for (it = myT2String.begin(); it != myT2String.end(); it++) {
     149            0 :             list.push_back(it->first);
     150              :         }
     151            0 :     }
     152              : 
     153              :     /// @brief get multiline string (all strings concatenated and separated by '\n')
     154              :     /// @note this will be removed after unifying all FXFileDialog
     155            0 :     std::string getMultilineString() const {
     156              :         std::string result;
     157            0 :         if (myT2String.size() > 0) {
     158            0 :             for (auto item : myT2String) {
     159            0 :                 result.append(item.second + "\n");
     160              :             }
     161              :             result.pop_back();
     162              :         }
     163            0 :         return result;
     164              :     }
     165              : 
     166              : private:
     167              :     /// @brief map with the keys vinculated with strings
     168              :     std::map<std::string, T> myString2T;
     169              : 
     170              :     /// @brief map with the strings vinculated with keys
     171              :     std::map<T, std::string> myT2String;
     172              : };
        

Generated by: LCOV version 2.0-1