2020-11-13 15:18:09 -05:00
|
|
|
#ifndef UTIL_HPP
|
2020-11-13 18:17:47 -05:00
|
|
|
#define UTIL_HPP
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
#include <string>
|
2020-11-27 13:21:07 -05:00
|
|
|
#include <set>
|
2020-11-13 15:18:09 -05:00
|
|
|
#include <algorithm>
|
2020-12-05 18:57:53 -05:00
|
|
|
#include <sstream>
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
|
|
|
|
|
using stringvec = std::vector<std::string>;
|
2020-11-27 13:21:07 -05:00
|
|
|
using stringset = std::set<std::string>;
|
2020-11-13 15:18:09 -05:00
|
|
|
|
|
|
|
|
// trim from start
|
|
|
|
|
static inline std::string ltrim(std::string s) {
|
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
|
|
|
|
|
std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// trim from end
|
|
|
|
|
static inline std::string rtrim(std::string s) {
|
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(),
|
|
|
|
|
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// trim from both ends
|
|
|
|
|
static inline std::string trim(std::string s) {
|
|
|
|
|
return ltrim(rtrim(s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stringvec read_lines(const std::string &path);
|
|
|
|
|
const stringvec trim_and_uncomment(const stringvec &lines);
|
2020-12-05 18:57:53 -05:00
|
|
|
std::string get_file_fingerprint(const std::string &path);
|
|
|
|
|
std::string get_file_contents(const std::string &fn);
|
2020-11-13 15:18:09 -05:00
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
double distance_squared(double x1, double y1, double x2, double y2);
|
|
|
|
|
|
|
|
|
|
// Return a pseudorandom number which is a hash function of A,B,C.
|
|
|
|
|
uint32_t hash3(uint32_t a, uint32_t b, uint32_t c);
|
|
|
|
|
|
|
|
|
|
// Returns a floating point value between lo and hi inclusive.
|
|
|
|
|
double hash_to_float(double lo, double hi, uint32_t a, uint32_t b, uint32_t c);
|
|
|
|
|
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
} // namespace util
|
|
|
|
|
#endif // UTIL_HPP
|