From 3809e4071827a5959f27e472514eaed08ba6d15e Mon Sep 17 00:00:00 2001 From: Arnur Nigmetov Date: Wed, 4 Mar 2020 00:33:51 +0100 Subject: Make matching distance header-only. --- matching/include/common_util.h | 113 ++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 68 deletions(-) (limited to 'matching/include/common_util.h') diff --git a/matching/include/common_util.h b/matching/include/common_util.h index 2d8dcb0..778536f 100644 --- a/matching/include/common_util.h +++ b/matching/include/common_util.h @@ -11,22 +11,24 @@ #include #include +#include +#include + +namespace spd = spdlog; + #include "common_defs.h" #include "phat/helpers/misc.h" - namespace md { - - using Real = double; - using RealVec = std::vector; using Index = phat::index; using IndexVec = std::vector; - static constexpr Real pi = M_PI; + //static constexpr Real pi = M_PI; using Column = std::vector; + template struct Point { Real x; Real y; @@ -71,59 +73,56 @@ namespace md { }; - using PointVec = std::vector; - - Point operator+(const Point& u, const Point& v); + template + using PointVec = std::vector>; - Point operator-(const Point& u, const Point& v); + template + Point operator+(const Point& u, const Point& v); - Point least_upper_bound(const Point& u, const Point& v); + template + Point operator-(const Point& u, const Point& v); - Point greatest_lower_bound(const Point& u, const Point& v); - Point max_point(); + template + Point least_upper_bound(const Point& u, const Point& v); - Point min_point(); + template + Point greatest_lower_bound(const Point& u, const Point& v); - std::ostream& operator<<(std::ostream& ostr, const Point& vec); + template + Point max_point(); - Real L_infty(const Point& v); + template + Point min_point(); - Real l_2_norm(const Point& v); + template + std::ostream& operator<<(std::ostream& ostr, const Point& vec); - Real l_2_dist(const Point& x, const Point& y); + template + using DiagramPoint = std::pair; - Real l_infty_dist(const Point& x, const Point& y); + template + using Diagram = std::vector>; - using Interval = std::pair; - - // return minimal interval that contains both a and b - inline Interval minimal_covering_interval(Interval a, Interval b) - { - return {std::min(a.first, b.first), std::max(a.second, b.second)}; - } // to keep diagrams in all dimensions // TODO: store in Hera format? + template class DiagramKeeper { public: - using DiagramPoint = std::pair; - using Diagram = std::vector; DiagramKeeper() { }; void add_point(int dim, Real birth, Real death); - Diagram get_diagram(int dim) const; + Diagram get_diagram(int dim) const; void clear() { data_.clear(); } private: - std::map data_; + std::map> data_; }; - using Diagram = std::vector>; - template std::string container_to_string(const C& cont) { @@ -140,42 +139,18 @@ namespace md { return ss.str(); } - int gcd(int a, int b); - - struct Rational { - int numerator {0}; - int denominator {1}; - Rational() = default; - Rational(int n, int d) : numerator(n / gcd(n, d)), denominator(d / gcd(n, d)) {} - Rational(std::pair p) : Rational(p.first, p.second) {} - Rational(int n) : numerator(n), denominator(1) {} - Real to_real() const { return (Real)numerator / (Real)denominator; } - void reduce(); - Rational& operator+=(const Rational& rhs); - Rational& operator-=(const Rational& rhs); - Rational& operator*=(const Rational& rhs); - Rational& operator/=(const Rational& rhs); - }; - - using namespace std::rel_ops; - - bool operator==(const Rational& a, const Rational& b); - bool operator<(const Rational& a, const Rational& b); - std::ostream& operator<<(std::ostream& os, const Rational& a); - - // arithmetic - Rational operator+(Rational a, const Rational& b); - Rational operator-(Rational a, const Rational& b); - Rational operator*(Rational a, const Rational& b); - Rational operator/(Rational a, const Rational& b); - - Rational reduce(Rational frac); - - Rational midpoint(Rational a, Rational b); - // return true, if s is empty or starts with # (commented out line) // whitespaces in the beginning of s are ignored - bool ignore_line(const std::string& s); + inline bool ignore_line(const std::string& s) + { + for(auto c : s) { + if (isspace(c)) + continue; + return (c == '#'); + } + return true; + } + // split string by delimeter template @@ -195,10 +170,10 @@ namespace md { } namespace std { - template<> - struct hash + template + struct hash> { - std::size_t operator()(const md::Point& p) const + std::size_t operator()(const md::Point& p) const { auto hx = std::hash()(p.x); auto hy = std::hash()(p.y); @@ -207,5 +182,7 @@ namespace std { }; }; +#include "common_util.hpp" + #endif //MATCHING_DISTANCE_COMMON_UTIL_H -- cgit v1.2.3