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