-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.hpp
122 lines (103 loc) · 3.11 KB
/
Utilities.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//============================================================================
// Name : Utilities.hpp
// Author : lindorfer
// Version :
// Copyright : Your copyright notice
// Description : Simplistic C++ Utilities to make life easier
//============================================================================
#include <bits/stdc++.h>
using namespace std;
//-----Range-----
//-----The data-type of the step is used for the data-type of the vector-----
template <typename start_type, typename stop_type, typename step_type>
vector<step_type> range(start_type start, stop_type stop, step_type step){
if (step == step_type(0)){
throw std::invalid_argument("step for range must be non-zero");
}
if(typeid(step) != typeid(start)){
}
vector<step_type> result;
step_type i = start;
while ((step > 0) ? (i < stop) : (i > stop)){
result.push_back(i);
i += step;
}
return result;
}
template <typename T>
vector<T> range(T start, T stop){
return range(start, stop, T(1));
}
template <typename T>
vector<T> range(T stop){
return range(T(0), stop, T(1));
}
//-----std::vector cout-----
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
out << "{";
size_t last = v.size() - 1;
for(size_t i = 0; i < v.size(); ++i) {
out << v[i];
if (i != last)
out << ", ";
}
out << "}";
return out;
}
//-----std::tuple cout-----
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), string>::type
stringval(const std::tuple<Tp...> & t)
{
std::stringstream buffer;
buffer << "}";
return buffer.str();
}
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), string>::type
stringval(const std::tuple<Tp...> & t)
{
std::stringstream buffer;
size_t len = sizeof...(Tp);
if(I==0)
buffer << "{";
buffer << std::get<I>(t);
if(I < len - 1)
buffer << ", ";
buffer << stringval<I + 1, Tp...>(t);
return buffer.str();
}
template<typename... Tp> ostream& operator <<(ostream& out, const std::tuple<Tp...> & t)
{
out << stringval(t);
return out;
}
//-----Unordered_Map-----
//-----Output unordered_map to ofstream-----
template<typename K, typename T>
ostream& operator<< (ostream& out, unordered_map<K, T>& umap) {
out << "{";
for(auto it = umap.begin(); it != umap.end(); it++){
if(next(it) == umap.end()){
out << "{" << it -> first << ", " << it -> second << "}";
}
else{
out << "{" << it -> first << ", " << it -> second << "}, ";
}
}
out << "}";
return out;
}
//-----Move Elements within a Container (e.g. Boost or Std Vector)-----
template <typename vec> void move_element(vec& v, size_t oldIndex, size_t newIndex){
if (oldIndex > newIndex)
std::rotate(v.rend() - oldIndex - 1, v.rend() - oldIndex, v.rend() - newIndex);
else
std::rotate(v.begin() + oldIndex, v.begin() + oldIndex + 1, v.begin() + newIndex + 1);
}
//-----Example: Using a Boost-Vector and moving Element 1 to position 4-----
// Vektor<int> v;
// vec_append(v, { 3, 4, 5, 6, 7, 8, 9 });
// move_element(v, 1, 4);
// cout << v << endl;