| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | // This file is part of INSTINCT, the INS Toolkit for Integrated | ||
| 2 | // Navigation Concepts and Training by the Institute of Navigation of | ||
| 3 | // the University of Stuttgart, Germany. | ||
| 4 | // | ||
| 5 | // This Source Code Form is subject to the terms of the Mozilla Public | ||
| 6 | // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| 7 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| 8 | |||
| 9 | /// @file STL.hpp | ||
| 10 | /// @brief Algorithms concerning the STL containers | ||
| 11 | /// @author T. Topp (topp@ins.uni-stuttgart.de) | ||
| 12 | /// @date 2024-01-11 | ||
| 13 | |||
| 14 | #pragma once | ||
| 15 | |||
| 16 | #include <fmt/format.h> | ||
| 17 | #include <cstring> | ||
| 18 | #include <string> | ||
| 19 | #include <bitset> | ||
| 20 | #include <functional> | ||
| 21 | |||
| 22 | namespace NAV | ||
| 23 | { | ||
| 24 | |||
| 25 | /// @brief Joins the container to a string | ||
| 26 | /// @param[in] container Container to join | ||
| 27 | /// @param[in] delimiter Delimiter to use to concatenate the elements | ||
| 28 | /// @param[in] elementFormat fmt format string, which gets places inside '{}' to format the elements | ||
| 29 | template<typename T> | ||
| 30 | 876 | std::string joinToString(const T& container, const char* delimiter = ", ", const std::string& elementFormat = "") | |
| 31 | { | ||
| 32 | 876 | std::string text; | |
| 33 | 
        1/2✓ Branch 3 taken 438 times. 
            ✗ Branch 4 not taken. 
           | 
      4760 | std::for_each(container.begin(), container.end(), [&](const auto& element) { | 
| 34 | 
        12/24✓ Branch 1 taken 422 times. 
            ✗ Branch 2 not taken. 
            ✓ Branch 4 taken 422 times. 
            ✗ Branch 5 not taken. 
            ✓ Branch 9 taken 422 times. 
            ✗ Branch 10 not taken. 
            ✓ Branch 12 taken 422 times. 
            ✗ Branch 13 not taken. 
            ✓ Branch 21 taken 1244 times. 
            ✗ Branch 22 not taken. 
            ✓ Branch 24 taken 1244 times. 
            ✗ Branch 25 not taken. 
            ✓ Branch 29 taken 1244 times. 
            ✗ Branch 30 not taken. 
            ✓ Branch 32 taken 1244 times. 
            ✗ Branch 33 not taken. 
            ✓ Branch 41 taken 276 times. 
            ✗ Branch 42 not taken. 
            ✓ Branch 44 taken 276 times. 
            ✗ Branch 45 not taken. 
            ✓ Branch 49 taken 276 times. 
            ✗ Branch 50 not taken. 
            ✓ Branch 52 taken 276 times. 
            ✗ Branch 53 not taken. 
           | 
      1942 | text += fmt::format(fmt::runtime("{" + elementFormat + "}{}"), element, delimiter); | 
| 35 | }); | ||
| 36 | 
        1/2✓ Branch 2 taken 438 times. 
            ✗ Branch 3 not taken. 
           | 
      1752 | return text.substr(0, text.length() - strlen(delimiter)); | 
| 37 | 876 | } | |
| 38 | |||
| 39 | /// @brief Joins the container to a string | ||
| 40 | /// @param[in] container Container to join | ||
| 41 | /// @param[in] formatFunc Function to evaluate for each element to get the string representation | ||
| 42 | /// @param[in] delimiter Delimiter to use to concatenate the elements | ||
| 43 | template<typename T, typename U> | ||
| 44 | 448 | std::string joinToStringCustom(const T& container, U&& formatFunc, const char* delimiter = ", ") | |
| 45 | { | ||
| 46 | 448 | std::string text; | |
| 47 | 
        1/2✓ Branch 3 taken 224 times. 
            ✗ Branch 4 not taken. 
           | 
      1152 | std::for_each(container.begin(), container.end(), [&](const auto& element) { | 
| 48 | 
        6/12✓ Branch 1 taken 176 times. 
            ✗ Branch 2 not taken. 
            ✓ Branch 4 taken 176 times. 
            ✗ Branch 5 not taken. 
            ✓ Branch 7 taken 176 times. 
            ✗ Branch 8 not taken. 
            ✓ Branch 14 taken 176 times. 
            ✗ Branch 15 not taken. 
            ✓ Branch 17 taken 176 times. 
            ✗ Branch 18 not taken. 
            ✓ Branch 20 taken 176 times. 
            ✗ Branch 21 not taken. 
           | 
      352 | text += fmt::format("{}{}", formatFunc(element), delimiter); | 
| 49 | }); | ||
| 50 | 
        1/2✓ Branch 2 taken 224 times. 
            ✗ Branch 3 not taken. 
           | 
      896 | return text.substr(0, text.length() - strlen(delimiter)); | 
| 51 | 448 | } | |
| 52 | |||
| 53 | /// @brief Comparison operator for bitsets | ||
| 54 | /// @tparam N Amount of bits | ||
| 55 | /// @param[in] lhs Left-hand side | ||
| 56 | /// @param[in] rhs Right-hand side | ||
| 57 | /// @return True when lhs < rhs | ||
| 58 | template<std::size_t N> | ||
| 59 | 674178 | bool operator<(const std::bitset<N>& lhs, const std::bitset<N>& rhs) | |
| 60 | { | ||
| 61 | 674178 | for (size_t i = N - 1;; i--) | |
| 62 | { | ||
| 63 | 
        2/2✓ Branch 2 taken 674178 times. 
            ✓ Branch 3 taken 52580286 times. 
           | 
      53254464 | if (lhs[i] ^ rhs[i]) { return rhs[i]; } | 
| 64 | 
        1/2✗ Branch 0 not taken. 
            ✓ Branch 1 taken 52580286 times. 
           | 
      52580286 | if (i == 0) { break; } | 
| 65 | } | ||
| 66 | ✗ | return false; | |
| 67 | } | ||
| 68 | |||
| 69 | } // namespace NAV | ||
| 70 |