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 |
9/18✓ 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 18 taken 1244 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 1244 times.
✗ Branch 22 not taken.
✓ Branch 26 taken 1244 times.
✗ Branch 27 not taken.
✓ Branch 35 taken 276 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 276 times.
✗ Branch 39 not taken.
✓ Branch 43 taken 276 times.
✗ Branch 44 not taken.
|
3884 | 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 |
4/8✓ Branch 1 taken 176 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 176 times.
✗ Branch 6 not taken.
✓ Branch 12 taken 176 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 176 times.
✗ Branch 17 not taken.
|
704 | 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 | 35596 | bool operator<(const std::bitset<N>& lhs, const std::bitset<N>& rhs) | |
60 | { | ||
61 | 35596 | for (size_t i = N - 1;; i--) | |
62 | { | ||
63 |
2/2✓ Branch 2 taken 35596 times.
✓ Branch 3 taken 2883132 times.
|
2918728 | if (lhs[i] ^ rhs[i]) { return rhs[i]; } |
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2883132 times.
|
2883132 | if (i == 0) { break; } |
65 | } | ||
66 | ✗ | return false; | |
67 | } | ||
68 | |||
69 | } // namespace NAV | ||
70 |