Util.h
Go to the documentation of this file.
1 // \author Darko Veberic
2 // \date 2014
3 
4 #ifndef _Util_h_
5 #define _Util_h_
6 
7 #include <iostream>
8 #include <vector>
9 #include <string>
10 #include <sstream>
11 
12 
13 template<typename T>
14 struct Separator {
15  Separator(const std::vector<T>& v, const char* const sep)
16  : fVec(v), fSep(sep) { }
17  const std::vector<T>& fVec;
18  const char* const fSep;
19 };
20 
21 
22 template<typename T>
23 inline
25 Separate(const std::vector<T>& v, const char* const sep)
26 {
27  return Separator<T>(v, sep);
28 }
29 
30 
31 template<typename T>
32 struct Separator2D {
33  Separator2D(const std::vector<T>& v, const char* const sep1, const char* const sep2)
34  : fVec(v), fSep1(sep1), fSep2(sep2) { }
35  const std::vector<T>& fVec;
36  const char* const fSep1;
37  const char* const fSep2;
38 };
39 
40 
41 template<typename T>
42 inline
44 Separate(const std::vector<T>& v, const char* const sep1, const char* const sep2)
45 {
46  return Separator2D<T>(v, sep1, sep2);
47 }
48 
49 
50 namespace std {
51 
52 template<typename T>
53 inline
54 ostream&
55 operator<<(ostream& os, const Separator<T>& s)
56 {
57  copy(s.fVec.begin(), s.fVec.end(), ostream_iterator<T>(os, s.fSep));
58  return os;
59 }
60 
61 
62 template<typename T>
63 inline
64 ostream&
65 operator<<(ostream& os, const Separator2D<T>& s)
66 {
67  for (const auto it : s.fVec)
68  os << Separate(it, s.fSep1) << s.fSep2;
69  return os;
70 }
71 
72 }
73 
74 
75 inline
76 std::vector<double>
77 ParseListRange(const std::string& str)
78 {
79  const bool isRange = (str.find(':') != std::string::npos);
80  const char sep = (isRange ? ':' : ',');
81  std::istringstream iss(str);
82  std::string n;
83  std::vector<double> ret;
84  while (std::getline(iss, n, sep))
85  ret.push_back(stod(n));
86  if (isRange) {
87  const double a = ret.at(0);
88  const double b = ret.at(1);
89  const unsigned int n = ret.at(2);
90  ret.clear();
91  for (unsigned int i = 0; i <= n; ++i)
92  ret.push_back(a + (b-a)*i/n);
93  }
94  return ret;
95 }
96 
97 
98 inline
99 void
100 Update(std::vector<double>& init, const std::vector<double>& res)
101 {
102  // overwrite whole init with beginning of res
103  for (unsigned int i = 0, n = init.size(); i < n; ++i)
104  init[i] = res.at(i);
105 }
106 
107 
108 #endif
Separator(const std::vector< T > &v, const char *const sep)
Definition: Util.h:15
const std::vector< T > & fVec
Definition: Util.h:35
void Update(std::vector< double > &init, const std::vector< double > &res)
Definition: Util.h:100
Separator2D(const std::vector< T > &v, const char *const sep1, const char *const sep2)
Definition: Util.h:33
Separator< T > Separate(const std::vector< T > &v, const char *const sep)
Definition: Util.h:25
constexpr double s
Definition: AugerUnits.h:163
const char *const fSep2
Definition: Util.h:37
Definition: Util.h:14
const std::vector< T > & fVec
Definition: Util.h:17
std::vector< double > ParseListRange(const std::string &str)
Definition: Util.h:77
const char *const fSep
Definition: Util.h:18
const char *const fSep1
Definition: Util.h:36

, generated on Tue Sep 26 2023.