AmpGen 2.1
Loading...
Searching...
No Matches
Utilities.h
Go to the documentation of this file.
1#ifndef AMPGEN_UTILITIES_H
2#define AMPGEN_UTILITIES_H
3
4#include <cxxabi.h>
5#include <stddef.h>
6#include <algorithm>
7#include <fstream>
8#include <functional>
9#include <map>
10#include <sstream>
11#include <string>
12#include <typeinfo>
13#include <vector>
14#include <future>
15#ifdef _OPENMP
16#include <omp.h>
17#endif
18
19#include "AmpGen/MsgService.h"
20#include "AmpGen/MetaUtils.h"
21
22namespace AmpGen {
23 template <typename iterator_type,
24 typename functor_type>
25 std::string vectorToString( iterator_type begin,
26 iterator_type end,
27 const std::string& delim,
28 functor_type fcn )
29 {
30 std::stringstream ss;
31 if( begin == end ) return "";
32 for ( auto it = begin; it != end-1; ++it)
33 ss << fcn(*it) << delim;
34 ss << fcn(*(end-1));
35 return ss.str();
36 }
37
38 template <typename container_type,
39 typename vtype = typename container_type::value_type,
40 typename functor_type = std::function<vtype(const vtype&)> >
41 std::string vectorToString( const container_type& obj, const std::string& delim = "", const functor_type& f = [](const auto& arg){ return arg; })
42 {
43 return vectorToString( std::begin(obj), std::end(obj), delim, f);
44 }
45
46 template <typename T> std::vector<std::vector<T>> nCr( const T& n, const T& r )
47 {
48 std::vector<bool> mask( n );
49 std::vector<std::vector<T>> combinations;
50 std::fill( mask.begin() + r, mask.end(), true );
51 do {
52 std::vector<T> perm;
53 for ( T i = 0; i < n; ++i ) if ( !mask[i] ) perm.push_back(i);
54 combinations.push_back( perm );
55 } while ( std::next_permutation( mask.begin(), mask.end() ) );
56 return combinations;
57 }
58
59 std::vector<std::string> vectorFromFile( const std::string& filename, const char ignoreLinesThatBeginWith = '#' );
60
61 std::vector<std::string> split( const std::string&, char, bool = true);
62 std::vector<std::string> split( const std::string&, const std::vector<char>&, bool=false );
63
64 std::vector<size_t> findAll( const std::string& input, const std::string& ch );
65
66 std::map<size_t, std::string> vecFindAll( const std::string& input, const std::vector<std::string>& vCh );
67
68 size_t find_next_of( const std::string& input, const std::vector<std::string>& patterns, const size_t& begin = 0 );
69
70 std::string replaceAll( const std::string& input, const std::string& toReplace, const std::string& replaceWith );
71 std::string replaceAll( const std::string& input, const std::vector<std::pair<std::string, std::string>>& rules );
72
73 unsigned int FNV1a_hash( const std::string& toHash );
74
75 std::vector<std::string> getItems( const std::string& tree, const std::vector<std::string>& brackets = {"{", "}"},
76 const std::string& seperator = "," );
77
78 void swapChars(std::string& arg, const char a, const char b );
79
80 unsigned int editDistance( const std::string& s1, const std::string& s2 );
81
82 std::string round( const double& number, const unsigned int& nsf );
83
84 std::string numberWithError( const double& number, const double& error, const unsigned int& nDigits );
85
86 template <typename return_type>
87 return_type lexical_cast( const std::string& word, bool& status )
88 {
89 WARNING( "Only use specialised versions of this template (word = " << word << ", type = " << AmpGen::type_string<return_type>()
90 << ") " );
91 status = 0;
92 return return_type();
93 }
94
95 template <class ...ARGS>
96 std::string mysprintf(const std::string& format,
97 ARGS&&... args){
98 auto size = std::snprintf(nullptr, 0, format.c_str(), std::forward<ARGS>(args)...);
99 std::string output (size+1,'\0');
100 std::snprintf(&output[0],size+1, format.c_str(), std::forward<ARGS>(args)...);
101 return output.substr(0, output.size()-1);
102 }
103
104
105 template <> double lexical_cast( const std::string& word, bool& status );
106 template <> unsigned int lexical_cast( const std::string& word, bool& status );
107 template <> std::string lexical_cast( const std::string& word, bool& status );
108 template <> float lexical_cast( const std::string& word, bool& status );
109 template <> bool lexical_cast( const std::string& word, bool& status );
110 template <> int lexical_cast( const std::string& word, bool& status );
111 template <> unsigned long int lexical_cast( const std::string& word, bool& status );
112 template <> long int lexical_cast( const std::string& word, bool& status );
113
114 template <typename functor_type>
115 void processFile( const std::string& filename, functor_type&& toDo, const char ignoreLinesThatBeginWith = '#' )
116 {
117 std::string tmp;
118 std::ifstream inFile( filename.c_str() );
119 while ( inFile.good() ) {
120 std::getline( inFile, tmp );
121 tmp.erase( std::remove_if( tmp.begin(), tmp.end(), [](const char c){ return c == '\r'; }), tmp.end() );
122 if ( ignoreLinesThatBeginWith != '\0' && ( tmp.size() == 0 || tmp[0] == ignoreLinesThatBeginWith ) ) continue;
123 toDo( tmp );
124 }
125 inFile.close();
126 }
127
128 // calculate number of swaps required to reorder set, where swaps can have different exchange parities.
129 std::pair<size_t,int> minSwaps(const std::vector<size_t>& indices, const std::vector<int>& exchangeParities);
130
131 template<class iterator,
132 class comparator>
133 void parallel_sort(iterator begin,
134 iterator end,
135 const comparator& comp,
136 const size_t& grainsize)
137 {
138 const size_t len = end - begin;
139 if(len < grainsize) std::sort(begin, end, comp);
140 else
141 {
142 const auto middle = begin + len/2;
143 auto future = std::async(parallel_sort<iterator,comparator>, begin, middle, comp, grainsize);
144 parallel_sort(middle, end, comp, grainsize);
145 future.wait();
146 std::inplace_merge(begin, middle, end, comp);
147 }
148 }
149
150 template<class iterator,
151 class initial_value,
152 class functor>
153 initial_value parallel_accumulate(iterator begin,
154 iterator end,
155 const initial_value& init,
156 const functor& f)
157 {
158 auto total = init;
159 auto size = end-begin;
160 #ifdef _OPENMP
161 #pragma omp parallel for reduction( +: total )
162 #endif
163 for( int it = 0; it < size; ++it ){
164 total += f(*(begin+it));
165 }
166 return total;
167 }
168 template <typename return_type, typename contained_type> std::function<return_type(const contained_type&)>
169 arrayToFunctor( const std::vector<return_type>& values)
170 {
171 return [values](const contained_type& event) -> return_type {return *(values.data() + event.index()); };
172 }
173
174 template <typename T> std::vector<std::vector<T>> all_combinations( const std::vector< std::vector<T>>& elements )
175 {
176 int nc = 1;
177 for( const auto & s : elements ) nc *= s.size();
178 std::vector< std::vector<T>> comb (nc, std::vector<T>(elements.size(), 0));
179 for( int i = 0 ; i != comb.size(); ++i )
180 {
181 int counter = i;
182 for( int j = elements.size()-1 ; j >= 0; --j )
183 {
184 int t = counter % elements[j].size();
185 counter = ( counter - t ) / elements[j].size();
186 comb[i][j] = elements[j][t];
187 }
188 }
189 return comb;
190 }
191
192
193 template<class iterator>
194 void parallel_sort(iterator begin,
195 iterator end,
196 const size_t& grainsize)
197 {
198 typedef typename std::iterator_traits<iterator>::value_type value_type;
199 parallel_sort(begin,end,std::less<value_type>(), grainsize);
200 }
201
202 bool stringMatchesWildcard( const std::string& input, const std::string& wildcard_string,
203 const char wildcard_character = '*' );
204
205 bool isDir( const std::string& fname );
206 bool fileExists( const std::string& name );
207
208 std::vector<std::string> getListOfFiles(const std::string& directory, const std::string& patternString = "");
209
211 void printReleaseNotes(const std::string& fname);
212
213 std::string ltrim( std::string s );
214 std::string rtrim( std::string s );
215 std::string trim( const std::string& s );
216 std::string expandGlobals( std::string path );
217
218 std::ostream& bold_on( std::ostream& );
219 std::ostream& bold_off( std::ostream& );
220 std::ostream& italic_on( std::ostream& );
221 std::ostream& italic_off( std::ostream& );
222}
223#endif
#define WARNING(X)
Used for printing warning messages, can be switched off using WARNINGLEVEL.
Definition MsgService.h:97
void parallel_sort(iterator begin, iterator end, const comparator &comp, const size_t &grainsize)
Definition Utilities.h:133
size_t find_next_of(const std::string &input, const std::vector< std::string > &patterns, const size_t &begin=0)
std::vector< size_t > findAll(const std::string &input, const std::string &ch)
bool isDir(const std::string &fname)
std::vector< std::string > getItems(const std::string &tree, const std::vector< std::string > &brackets={"{", "}"}, const std::string &seperator=",")
std::string vectorToString(iterator_type begin, iterator_type end, const std::string &delim, functor_type fcn)
Definition Utilities.h:25
bool stringMatchesWildcard(const std::string &input, const std::string &wildcard_string, const char wildcard_character=' *')
std::vector< std::string > split(const std::string &, char, bool=true)
void printReleaseNotes(const std::string &fname)
std::ostream & italic_off(std::ostream &)
std::pair< size_t, int > minSwaps(const std::vector< size_t > &indices, const std::vector< int > &exchangeParities)
std::vector< std::string > vectorFromFile(const std::string &filename, const char ignoreLinesThatBeginWith='#')
std::vector< std::string > getListOfFiles(const std::string &directory, const std::string &patternString="")
initial_value parallel_accumulate(iterator begin, iterator end, const initial_value &init, const functor &f)
Definition Utilities.h:153
std::function< return_type(const contained_type &)> arrayToFunctor(const std::vector< return_type > &values)
Definition Utilities.h:169
std::map< size_t, std::string > vecFindAll(const std::string &input, const std::vector< std::string > &vCh)
unsigned int FNV1a_hash(const std::string &toHash)
std::string round(const double &number, const unsigned int &nsf)
std::vector< std::vector< T > > all_combinations(const std::vector< std::vector< T > > &elements)
Definition Utilities.h:174
std::string numberWithError(const double &number, const double &error, const unsigned int &nDigits)
std::ostream & bold_on(std::ostream &)
void processFile(const std::string &filename, functor_type &&toDo, const char ignoreLinesThatBeginWith='#')
Definition Utilities.h:115
std::ostream & bold_off(std::ostream &)
std::string trim(const std::string &s)
std::string replaceAll(const std::string &input, const std::string &toReplace, const std::string &replaceWith)
std::string mysprintf(const std::string &format, ARGS &&... args)
Definition Utilities.h:96
std::string rtrim(std::string s)
std::vector< std::vector< T > > nCr(const T &n, const T &r)
Definition Utilities.h:46
void swapChars(std::string &arg, const char a, const char b)
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:18
bool fileExists(const std::string &name)
return_type lexical_cast(const std::string &word, bool &status)
Definition Utilities.h:87
std::ostream & italic_on(std::ostream &)
void printSplash()
unsigned int editDistance(const std::string &s1, const std::string &s2)
std::string expandGlobals(std::string path)
std::string ltrim(std::string s)