0.2.0
Loading...
Searching...
No Matches
Sort.hpp
Go to the documentation of this file.
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
13
14#pragma once
15
16#include <vector>
17#include <algorithm>
18#include <numeric>
19#include "util/Eigen.hpp"
20
21namespace NAV
22{
23
29template<typename T, typename Compare>
30std::vector<size_t> sort_permutation(const std::vector<T>& vec, Compare compare)
31{
32 std::vector<size_t> p(vec.size());
33 std::iota(p.begin(), p.end(), 0);
34 std::sort(p.begin(), p.end(),
35 [&](size_t i, size_t j) { return compare(vec[i], vec[j]); });
36 return p;
37}
38
44template<typename Derived, typename Compare>
45std::vector<size_t> sort_permutation(const Eigen::MatrixBase<Derived>& vec, Compare compare)
46{
47 std::vector<size_t> p(static_cast<size_t>(vec.rows()));
48 std::iota(p.begin(), p.end(), 0);
49 std::sort(p.begin(), p.end(),
50 [&](size_t i, size_t j) { return compare(vec(static_cast<Eigen::Index>(i)), vec(static_cast<Eigen::Index>(j))); });
51 return p;
52}
53
59template<typename T>
60std::vector<T> apply_permutation(const std::vector<T>& vec, const std::vector<size_t>& p)
61{
62 std::vector<T> sorted_vec(vec.size());
63 std::transform(p.begin(), p.end(), sorted_vec.begin(),
64 [&](size_t i) { return vec[i]; });
65 return sorted_vec;
66}
67
73template<typename Scalar, int Size>
74Eigen::Vector<Scalar, Size> apply_permutation(const Eigen::Vector<Scalar, Size>& vec, const std::vector<size_t>& p)
75{
76 Eigen::Vector<Scalar, Size> sorted = vec;
77 std::transform(p.begin(), p.end(), sorted.begin(),
78 [&](size_t i) { return vec(static_cast<Eigen::Index>(i)); });
79 return sorted;
80}
81
87template<typename Derived>
88typename Derived::PlainObject apply_permutation_rowwise(const Eigen::MatrixBase<Derived>& m, const std::vector<size_t>& p)
89{
90 typename Derived::PlainObject sorted = m;
91 std::transform(p.begin(), p.end(), sorted.rowwise().begin(),
92 [&](size_t i) { return m.row(static_cast<Eigen::Index>(i)); });
93 return sorted;
94}
95
101template<typename Derived>
102typename Derived::PlainObject apply_permutation_colwise(const Eigen::MatrixBase<Derived>& m, const std::vector<size_t>& p)
103{
104 typename Derived::PlainObject sorted = m;
105 std::transform(p.begin(), p.end(), sorted.colwise().begin(),
106 [&](size_t i) { return m.col(static_cast<Eigen::Index>(i)); });
107 return sorted;
108}
109
113template<typename T>
114void apply_permutation_in_place(std::vector<T>& vec, const std::vector<size_t>& p)
115{
116 std::vector<bool> done(p.size());
117 for (size_t i = 0; i < p.size(); ++i)
118 {
119 if (done[i])
120 {
121 continue;
122 }
123 done[i] = true;
124 size_t prev_j = i;
125 size_t j = p[i];
126 while (i != j)
127 {
128 std::swap(vec[prev_j], vec[j]);
129 done[j] = true;
130 prev_j = j;
131 j = p[j];
132 }
133 }
134}
135
140template<typename Scalar, int Size>
141void apply_permutation_in_place(Eigen::Vector<Scalar, Size>& vec, const std::vector<size_t>& p)
142{
143 std::vector<bool> done(p.size());
144 for (size_t i = 0; i < p.size(); ++i)
145 {
146 if (done[i])
147 {
148 continue;
149 }
150 done[i] = true;
151 size_t prev_j = i;
152 size_t j = p[i];
153 while (i != j)
154 {
155 std::swap(vec(static_cast<Eigen::Index>(prev_j)), vec(static_cast<Eigen::Index>(j)));
156 done[j] = true;
157 prev_j = j;
158 j = p[j];
159 }
160 }
161}
162
167template<typename Scalar, int Rows, int Cols>
168void apply_permutation_rowwise_in_place(Eigen::Matrix<Scalar, Rows, Cols>& m, const std::vector<size_t>& p)
169{
170 std::vector<bool> done(p.size());
171 for (size_t i = 0; i < p.size(); ++i)
172 {
173 if (done[i])
174 {
175 continue;
176 }
177 done[i] = true;
178 size_t prev_j = i;
179 size_t j = p[i];
180 while (i != j)
181 {
182 m.row(static_cast<Eigen::Index>(prev_j)).swap(m.row(static_cast<Eigen::Index>(j)));
183 done[j] = true;
184 prev_j = j;
185 j = p[j];
186 }
187 }
188}
189
194template<typename Scalar, int Rows, int Cols>
195void apply_permutation_colwise_in_place(Eigen::Matrix<Scalar, Rows, Cols>& m, const std::vector<size_t>& p)
196{
197 std::vector<bool> done(p.size());
198 for (size_t i = 0; i < p.size(); ++i)
199 {
200 if (done[i])
201 {
202 continue;
203 }
204 done[i] = true;
205 size_t prev_j = i;
206 size_t j = p[i];
207 while (i != j)
208 {
209 m.col(static_cast<Eigen::Index>(prev_j)).swap(m.col(static_cast<Eigen::Index>(j)));
210 done[j] = true;
211 prev_j = j;
212 j = p[j];
213 }
214 }
215}
216
217} // namespace NAV
Vector space operations.
void apply_permutation_rowwise_in_place(Eigen::Matrix< Scalar, Rows, Cols > &m, const std::vector< size_t > &p)
Sort a matrix row-wise with a permutation.
Definition Sort.hpp:168
std::vector< T > apply_permutation(const std::vector< T > &vec, const std::vector< size_t > &p)
Sort a std::vector with a permutation.
Definition Sort.hpp:60
Derived::PlainObject apply_permutation_rowwise(const Eigen::MatrixBase< Derived > &m, const std::vector< size_t > &p)
Sort a matrix row-wise with a permutation.
Definition Sort.hpp:88
std::vector< size_t > sort_permutation(const std::vector< T > &vec, Compare compare)
Gives the permutation to sort the std::vector.
Definition Sort.hpp:30
Derived::PlainObject apply_permutation_colwise(const Eigen::MatrixBase< Derived > &m, const std::vector< size_t > &p)
Sort a matrix col-wise with a permutation.
Definition Sort.hpp:102
void apply_permutation_in_place(std::vector< T > &vec, const std::vector< size_t > &p)
Sort a std::vector with a permutation.
Definition Sort.hpp:114
void apply_permutation_colwise_in_place(Eigen::Matrix< Scalar, Rows, Cols > &m, const std::vector< size_t > &p)
Sort a matrix col-wise with a permutation.
Definition Sort.hpp:195