Psi4
libpsi4util.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * @END LICENSE
26  */
27 
28 #ifndef _psi_src_lib_libpsi4util_libpsi4util_h_
29 #define _psi_src_lib_libpsi4util_libpsi4util_h_
30 
31 #include <string>
32 #include <vector>
33 #include <algorithm>
34 #include <regex>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 
40 namespace psi {
41 
42 typedef std::vector <std::string> strvec;
43 
44 std::string file_to_string(std::string const &name);
45 
46 bool space(char c);
47 
48 bool not_space(char c);
49 
50 std::vector <std::string> split(const std::string &str);
51 
52 std::vector <std::string> split_indices(const std::string &str);
53 
54 void to_lower(std::string &str);
55 std::string to_lower_copy(const std::string &str);
56 
57 void to_upper(std::string &str);
58 std::string to_upper_copy(const std::string& str);
59 
60 std::string to_string(const int val);
61 
62 std::string to_string(const double val);
63 
64 double to_double(const std::string str);
65 
66 int to_integer(const std::string inString);
67 
68 void append_reference(std::string &str, int reference);
69 
70 std::string add_reference(std::string &str, int reference);
71 
72 void append_reference(std::string &str, int reference);
73 
74 std::string find_and_replace(std::string &source, const std::string &target, const std::string &replace);
75 
76 void trim_spaces(std::string &str);
77 
78 template<typename Range1T, typename Range2T>
79 inline bool iequals(
80  const Range1T &Input,
81  const Range2T &Test)
82 {
83  if (std::distance(std::begin(Input), std::end(Input)) !=
84  std::distance(std::begin(Test), std::end(Test)))
85  return false;
86 
87  return std::equal(std::begin(Input),
88  std::end(Input),
89  std::begin(Test),
90  [](unsigned char a, unsigned char b) {
91  return std::tolower(a) == std::tolower(b);
92  });
93 }
94 
95 inline std::vector <std::string> split(const std::string &input, const std::string &regex)
96 {
97  // passing -1 as the submatch index parameter performs splitting
98  std::regex re(regex);
99  std::sregex_token_iterator
100  first{input.begin(), input.end(), re, -1},
101  last;
102  return {first, last};
103 }
104 
111 unsigned int edit_distance(const std::string &s1, const std::string &s2);
112 
113 
114 class Timer
115 {
116 public:
117  Timer() : ___start(), ___end(), ___dummy(),
119  { gettimeofday(&___start, &___dummy); }
120 
121  double get()
122  {
123  gettimeofday(&___end, &___dummy);
124  delta_time_seconds = (___end.tv_sec - ___start.tv_sec) + (___end.tv_usec - ___start.tv_usec) / 1000000.0;
127  return (delta_time_seconds);
128  }
129 
130 private:
131  struct timeval ___start, ___end;
132  struct timezone ___dummy;
136 };
137 
138 void generate_combinations(int n, int k, std::vector <std::vector<int>> &combinations);
139 
140 }
141 
142 #endif // _psi_src_lib_libpsi4util_libpsi4util_h_
std::string to_string(const int val)
Definition: stl_string.cc:194
std::string find_and_replace(std::string &source, const std::string &target, const std::string &replace)
Definition: stl_string.cc:112
struct timeval ___start ___end
Definition: libpsi4util.h:131
std::string add_reference(std::string &str, int reference)
Definition: stl_string.cc:216
void to_lower(std::string &str)
Definition: stl_string.cc:165
void generate_combinations(int n, int k, std::vector< std::vector< int > > &combinations)
Definition: combinations.cc:41
std::vector< std::string > split(const std::string &str)
Definition: stl_string.cc:49
Timer()
Definition: libpsi4util.h:117
std::vector< std::string > strvec
Definition: libpsi4util.h:42
int to_integer(const std::string inString)
Definition: stl_string.cc:208
void trim_spaces(std::string &str)
Definition: stl_string.cc:128
double combinations(int n, int k)
Definition: probabil.cc:50
void reference(void)
Definition: reference.cc:48
bool iequals(const Range1T &Input, const Range2T &Test)
Definition: libpsi4util.h:79
void to_upper(std::string &str)
Definition: stl_string.cc:177
std::string file_to_string(std::string const &name)
unsigned int edit_distance(const std::string &s1, const std::string &s2)
Compute the Levenshtein distance between two strings.
Definition: stl_string.cc:141
std::string to_lower_copy(const std::string &str)
Definition: stl_string.cc:170
double delta_time_days
Definition: libpsi4util.h:135
double delta_time_hours
Definition: libpsi4util.h:134
struct timezone ___dummy
Definition: libpsi4util.h:132
double delta_time_seconds
Definition: libpsi4util.h:133
bool not_space(char c)
Definition: stl_string.cc:106
double to_double(const std::string str)
Definition: stl_string.cc:189
std::string to_upper_copy(const std::string &str)
Definition: stl_string.cc:182
bool space(char c)
Definition: stl_string.cc:101
std::vector< std::string > split_indices(const std::string &str)
Definition: stl_string.cc:72
Definition: libpsi4util.h:114
void append_reference(std::string &str, int reference)
Definition: stl_string.cc:221