xerus
a general purpose tensor library
sort.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #include <vector>
26 #include <algorithm>
27 
28 #include "check.h"
29 
30 namespace xerus {
31  namespace misc {
32  template<class T, class Comparator>
33  std::vector<size_t> create_sort_permutation(const std::vector<T>& _vec, Comparator _comp) {
34  std::vector<size_t> permutation(_vec.size());
35  std::iota(permutation.begin(), permutation.end(), 0);
36  std::sort(permutation.begin(), permutation.end(), [&](const size_t _i, const size_t _j){ return _comp(_vec[_i], _vec[_j]); });
37  return permutation;
38  }
39 
40  template<class T>
41  void apply_permutation( std::vector<T>& _vec, const std::vector<size_t>& _permutation) {
42  XERUS_REQUIRE(_vec.size() == _permutation.size(), "Vector and permutation size must coincide.");
43  std::vector<T> sorted_vec;
44  sorted_vec.reserve(_permutation.size());
45  for(size_t i = 0; i < _permutation.size(); ++i) {
46  sorted_vec.emplace_back(std::move(_vec[_permutation[i]]));
47  }
48  _vec = std::move(sorted_vec);
49  }
50 
51  template <class KeyType, class DataType, class Comparator>
52  void simultaneous_sort( std::vector<KeyType>& _keyVector, std::vector<DataType>& _dataVector, Comparator _comp) {
53  XERUS_REQUIRE(_keyVector.size() == _dataVector.size(), "Vector sizes must coincide.");
54  const std::vector<size_t> permutation = create_sort_permutation(_keyVector, _comp);
55  apply_permutation(_keyVector, permutation);
56  apply_permutation(_dataVector, permutation);
57  }
58  }
59 }
void simultaneous_sort(std::vector< KeyType > &_keyVector, std::vector< DataType > &_dataVector, Comparator _comp)
Definition: sort.h:52
Header file for CHECK and REQUIRE macros.
The main namespace of xerus.
Definition: basic.h:37
#define XERUS_REQUIRE(condition, message)
Checks whether condition is true. logs an error otherwise via XERUS_LOG(error, message).
Definition: check.h:65
std::vector< size_t > create_sort_permutation(const std::vector< T > &_vec, Comparator _comp)
Definition: sort.h:33
void apply_permutation(std::vector< T > &_vec, const std::vector< size_t > &_permutation)
Definition: sort.h:41