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 Vector.hpp | ||
10 | /// @brief Vector Utility functions | ||
11 | /// @author T. Topp (topp@ins.uni-stuttgart.de) | ||
12 | /// @date 2022-03-07 | ||
13 | |||
14 | #pragma once | ||
15 | |||
16 | #include <vector> | ||
17 | #include <span> | ||
18 | |||
19 | namespace NAV | ||
20 | { | ||
21 | |||
22 | /// @brief Moves an element within a vector to a new position | ||
23 | /// @param[in, out] v Vector with the elements | ||
24 | /// @param[in] sourceIdx Old index which will be moved | ||
25 | /// @param[in] targetIdx New index which is the target | ||
26 | template<typename T> | ||
27 | 438 | void move(std::vector<T>& v, size_t sourceIdx, size_t targetIdx) | |
28 | { | ||
29 |
1/2✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
|
438 | if (sourceIdx > targetIdx) |
30 | { | ||
31 |
1/2✓ Branch 8 taken 219 times.
✗ Branch 9 not taken.
|
438 | std::rotate(v.rend() - static_cast<int64_t>(sourceIdx) - 1, |
32 | v.rend() - static_cast<int64_t>(sourceIdx), v.rend() - static_cast<int64_t>(targetIdx)); | ||
33 | } | ||
34 | else | ||
35 | { | ||
36 | ✗ | std::rotate(v.begin() + static_cast<int64_t>(sourceIdx), | |
37 | ✗ | v.begin() + static_cast<int64_t>(sourceIdx) + 1, v.begin() + static_cast<int64_t>(targetIdx) + 1); | |
38 | } | ||
39 | 438 | } | |
40 | |||
41 | /// @brief Returns a container filled with the given range | ||
42 | /// @param start Inclusive start value of the range | ||
43 | /// @param stepSize Step size of the range | ||
44 | /// @param end Exclusive end value of the range | ||
45 | template<typename Scalar> | ||
46 | std::vector<Scalar> genRangeVector(Scalar start, Scalar stepSize, Scalar end) | ||
47 | { | ||
48 | std::vector<Scalar> container; | ||
49 | container.reserve(static_cast<size_t>(std::ceil((end - start) / stepSize))); | ||
50 | |||
51 | while (start < end) | ||
52 | { | ||
53 | container.push_back(start); | ||
54 | start += stepSize; | ||
55 | } | ||
56 | |||
57 | return container; | ||
58 | }; | ||
59 | |||
60 | /// @brief Comparison operator for span and vector | ||
61 | /// @param lhs Left-hand side | ||
62 | /// @param rhs Right-hand side | ||
63 | /// @return True if all elements are equal | ||
64 | template<typename T> | ||
65 | 2 | [[nodiscard]] bool operator==(const std::vector<T>& lhs, std::span<const T> rhs) | |
66 | { | ||
67 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (lhs.size() != rhs.size()) { return false; } |
68 | ✗ | for (size_t i = 0; i < lhs.size(); i++) | |
69 | { | ||
70 | ✗ | if (lhs[i] != rhs[i]) { return false; } | |
71 | } | ||
72 | ✗ | return true; | |
73 | } | ||
74 | |||
75 | /// @brief Comparison operator for span and vector | ||
76 | /// @param lhs Left-hand side | ||
77 | /// @param rhs Right-hand side | ||
78 | /// @return True if all elements are equal | ||
79 | template<typename T> | ||
80 | 2 | [[nodiscard]] bool operator==(std::span<const T> lhs, const std::vector<T>& rhs) | |
81 | { | ||
82 | 2 | return rhs == lhs; | |
83 | } | ||
84 | |||
85 | } // namespace NAV | ||
86 |