vectorOperations.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_VECTOROPERATIONS_HPP
2 #define JULIAN_VECTOROPERATIONS_HPP
3 
4 #include <numeric>
5 #include <vector>
6 #include <algorithm>
7 #include <iostream>
8 
9 namespace julian {
10 
26  template<typename T>
27  std::vector<T> combineVectors(std::vector<T> v1, std::vector<T> v2) {
28  std::vector<T> result;
29 
30  int i = 0;
31  int j = 0;
32  int n1 = v1.size();
33  int n2 = v2.size();
34 
35  do {
36  if ( v1.at(i) < v2.at(j) ) {
37  result.push_back(v1.at(i));
38  i++;
39  } else if ( v1.at(i) > v2.at(j) ) {
40  result.push_back(v2.at(j));
41  j++;
42  } else if ( v1.at(i) == v2.at(j) ) {
43  result.push_back(v2.at(j));
44  i++;
45  j++;
46  }
47 
48  if (i == n1) {
49  std::copy(v2.begin() + j, v2.end(), std::back_inserter(result));
50  break;
51  } else if (j == n2) {
52  std::copy(v1.begin() + i, v1.end(), std::back_inserter(result));
53  break;
54  }
55  } while (true);
56 
57  return result;
58  }
59 
66  template< class T >
67  std::ostream& operator<<(std::ostream& s, std::vector<T>& v) {
68  int n = v.size();
69 
70  for(int i = 0; i < n; i++)
71  s << v.at(i) << std::endl;
72  s << std::endl;
73  return s;
74  }
75 
85  template <typename T,typename S>
86  void sort_vectors(std::vector<T>& benchmark, std::vector<S>& sorted) {
87  // preparing permutation
88  std::vector<std::size_t> p(benchmark.size());
89  std::iota(p.begin(), p.end(), 0);
90  std::sort(p.begin(), p.end(),[&](std::size_t i, std::size_t j){ return benchmark[i] < benchmark[j]; });
91 
92  std::vector<bool> done(benchmark.size());
93  for (std::size_t i = 0; i < benchmark.size(); ++i) {
94  if (done[i]) {
95  continue;
96  }
97  done[i] = true;
98  std::size_t prev_j = i;
99  std::size_t j = p[i];
100  while (i != j) {
101  std::swap(benchmark[prev_j], benchmark[j]);
102  std::swap(sorted[prev_j], sorted[j]);
103  done[j] = true;
104  prev_j = j;
105  j = p[j];
106  }
107  }
108  }
109 
110 } // namespace julian
111 
112 
113 #endif /* VECTOROPERATIONS_HPP */
Definition: cadHoliday.cpp:3
std::vector< T > combineVectors(std::vector< T > v1, std::vector< T > v2)
Combines vector v1 and v2 without duplicates.
Definition: vectorOperations.hpp:27
void sort_vectors(std::vector< T > &benchmark, std::vector< S > &sorted)
Sorting one vector basic on contents of another.
Definition: vectorOperations.hpp:86