LCOV - code coverage report
Current view: top level - src/utils/common - SequentialStringBijection.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 81.8 % 22 18
Test Date: 2026-06-15 15:46:12 Functions: 100.0 % 5 5

            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    SequentialStringBijection.h
      15              : /// @author  Jakob Erdmann
      16              : /// @date    June 2024
      17              : ///
      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>
      26              : #include <utils/common/UtilExceptions.h>
      27              : 
      28              : // ===========================================================================
      29              : // class definitions
      30              : // ===========================================================================
      31              : /**
      32              :  * Template container for maintaining a bidirectional map between strings and something else
      33              :  * It is not always a bijection since it allows for duplicate entries on both sides if either
      34              :  * checkDuplicates is set to false in the constructor or the insert function or if
      35              :  * the addAlias function is used.
      36              :  */
      37              : 
      38              : class SequentialStringBijection {
      39              : 
      40              : public:
      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              : 
      55              :     SequentialStringBijection() {}
      56              : 
      57              : 
      58       109428 :     SequentialStringBijection(Entry entries[], int terminatorKey, bool checkDuplicates = true) {
      59              :         int i = 0;
      60       109428 :         myT2String.resize(terminatorKey + 1);
      61              :         do {
      62     69541494 :             insert(entries[i].str, entries[i].key, checkDuplicates);
      63     69541494 :         } while (entries[i++].key != terminatorKey);
      64       109428 :     }
      65              : 
      66              : 
      67     69541494 :     void insert(const std::string str, int key, bool checkDuplicates = true) {
      68     69541494 :         if (checkDuplicates) {
      69     69541494 :             if (has(key)) {
      70              :                 // cannot use toString(key) because that might create an infinite loop
      71            0 :                 throw InvalidArgument("Duplicate key.");
      72              :             }
      73              :             if (hasString(str)) {
      74            0 :                 throw InvalidArgument("Duplicate string '" + str + "'.");
      75              :             }
      76              :         }
      77     69541494 :         myString2T[str] = key;
      78     69541494 :         myT2String[key] = str;
      79     69541494 :     }
      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         6556 :     int get(const std::string& str) const {
      94              :         if (hasString(str)) {
      95         6556 :             return myString2T.find(str)->second;
      96              :         } else {
      97            0 :             throw InvalidArgument("String '" + str + "' not found.");
      98              :         }
      99              :     }
     100              : 
     101              : 
     102    153497609 :     const std::string& getString(int key) const {
     103    153497609 :         if ((int)myT2String.size() > key) {
     104    153497609 :             return myT2String[key];
     105              :         } else {
     106              :             // cannot use toString(key) because that might create an infinite loop
     107            0 :             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     69541494 :     bool has(int key) const {
     118     69541494 :         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              : 
     149              : private:
     150              :     std::map<std::string, int> myString2T;
     151              :     std::vector<std::string> myT2String;
     152              : 
     153              : };
        

Generated by: LCOV version 2.0-1