From 9693f140d91e751aabe46b76b89c332c7f307e17 Mon Sep 17 00:00:00 2001 From: Arnur Nigmetov Date: Sat, 19 May 2018 20:33:33 +0200 Subject: Add inline qualifier to Wasserstein code. To avoid linking problems. --- geom_matching/wasserstein/include/basic_defs_ws.h | 28 +++++++++---------- .../wasserstein/include/basic_defs_ws.hpp | 18 ++++++------ geom_matching/wasserstein/include/diagonal_heap.h | 2 +- geom_matching/wasserstein/include/diagram_reader.h | 32 +++++++++++----------- geom_matching/wasserstein/include/wasserstein.h | 10 +++---- .../wasserstein/include/wasserstein_pure_geom.hpp | 4 +-- 6 files changed, 46 insertions(+), 48 deletions(-) diff --git a/geom_matching/wasserstein/include/basic_defs_ws.h b/geom_matching/wasserstein/include/basic_defs_ws.h index 58d6fd2..28f7452 100644 --- a/geom_matching/wasserstein/include/basic_defs_ws.h +++ b/geom_matching/wasserstein/include/basic_defs_ws.h @@ -60,19 +60,19 @@ namespace hera { template -bool is_infinity(const Real& x) +inline bool is_infinity(const Real& x) { return x == Real(-1); }; template -Real get_infinity() +inline Real get_infinity() { return Real( -1 ); } template -bool is_p_valid_norm(const Real& p) +inline bool is_p_valid_norm(const Real& p) { return is_infinity(p) or p >= Real(1); } @@ -101,10 +101,8 @@ namespace ws template using IdxValPair = std::pair; - - template - std::ostream& operator<<(std::ostream& output, const IdxValPair p) + inline std::ostream& operator<<(std::ostream& output, const IdxValPair p) { output << fmt::format("({0}, {1})", p.first, p.second); return output; @@ -112,7 +110,7 @@ namespace ws enum class OwnerType { k_none, k_normal, k_diagonal }; - std::ostream& operator<<(std::ostream& s, const OwnerType t) + inline std::ostream& operator<<(std::ostream& s, const OwnerType t) { switch(t) { @@ -210,11 +208,11 @@ namespace ws #ifndef FOR_R_TDA template - std::ostream& operator<<(std::ostream& output, const DiagramPoint p); + inline std::ostream& operator<<(std::ostream& output, const DiagramPoint p); #endif template - void format_arg(fmt::BasicFormatter &f, const char *&format_str, const DiagramPoint&p) { + inline void format_arg(fmt::BasicFormatter &f, const char *&format_str, const DiagramPoint&p) { if (p.is_diagonal()) { f.writer().write("({0},{1}, DIAG)", p.x, p.y); } else { @@ -269,14 +267,14 @@ namespace ws }; template - R dist_lp(const Pt& a, const Pt& b, const R p, const int dim) + inline R dist_lp(const Pt& a, const Pt& b, const R p, const int dim) { return DistImpl()(a, b, p, dim); } // TODO template - double getFurthestDistance3Approx(DiagPointContainer& A, DiagPointContainer& B, const Real p) + inline double getFurthestDistance3Approx(DiagPointContainer& A, DiagPointContainer& B, const Real p) { int dim = 2; Real result { 0.0 }; @@ -297,7 +295,7 @@ namespace ws } template - Real getFurthestDistance3Approx_pg(const hera::ws::dnn::DynamicPointVector& A, const hera::ws::dnn::DynamicPointVector& B, const Real p, const int dim) + inline Real getFurthestDistance3Approx_pg(const hera::ws::dnn::DynamicPointVector& A, const hera::ws::dnn::DynamicPointVector& B, const Real p, const int dim) { Real result { 0.0 }; int opt_b_idx = 0; @@ -317,13 +315,13 @@ namespace ws template - std::string format_container_to_log(const Container& cont); + inline std::string format_container_to_log(const Container& cont); template - std::string format_point_set_to_log(const IndexContainer& indices, const std::vector>& points); + inline std::string format_point_set_to_log(const IndexContainer& indices, const std::vector>& points); template - std::string format_int(T i); + inline std::string format_int(T i); } // ws } // hera diff --git a/geom_matching/wasserstein/include/basic_defs_ws.hpp b/geom_matching/wasserstein/include/basic_defs_ws.hpp index 1750b4e..a1153af 100644 --- a/geom_matching/wasserstein/include/basic_defs_ws.hpp +++ b/geom_matching/wasserstein/include/basic_defs_ws.hpp @@ -64,7 +64,7 @@ bool Point::operator!=(const Point& other) const #ifndef FOR_R_TDA template -std::ostream& operator<<(std::ostream& output, const Point p) +inline std::ostream& operator<<(std::ostream& output, const Point p) { output << "(" << p.x << ", " << p.y << ")"; return output; @@ -72,20 +72,20 @@ std::ostream& operator<<(std::ostream& output, const Point p) #endif template -Real sqr_dist(const Point& a, const Point& b) +inline Real sqr_dist(const Point& a, const Point& b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } template -Real dist(const Point& a, const Point& b) +inline Real dist(const Point& a, const Point& b) { return sqrt(sqr_dist(a, b)); } template -Real DiagramPoint::persistence_lp(const Real p) const +inline Real DiagramPoint::persistence_lp(const Real p) const { if (is_diagonal()) return 0.0; @@ -100,7 +100,7 @@ Real DiagramPoint::persistence_lp(const Real p) const #ifndef FOR_R_TDA template -std::ostream& operator<<(std::ostream& output, const DiagramPoint p) +inline std::ostream& operator<<(std::ostream& output, const DiagramPoint p) { if ( p.type == DiagramPoint::DIAG ) { output << "(" << p.x << ", " << p.y << ", " << 0.5 * (p.x + p.y) << " DIAG )"; @@ -142,7 +142,7 @@ Real DiagramPoint::getRealY() const } template -std::string format_container_to_log(const Container& cont) +inline std::string format_container_to_log(const Container& cont) { std::stringstream result; result << "["; @@ -157,7 +157,7 @@ std::string format_container_to_log(const Container& cont) } template -std::string format_pair_container_to_log(const Container& cont) +inline std::string format_pair_container_to_log(const Container& cont) { std::stringstream result; result << "["; @@ -173,7 +173,7 @@ std::string format_pair_container_to_log(const Container& cont) template -std::string format_point_set_to_log(const IndexContainer& indices, +inline std::string format_point_set_to_log(const IndexContainer& indices, const std::vector>& points) { std::stringstream result; @@ -189,7 +189,7 @@ std::string format_point_set_to_log(const IndexContainer& indices, } template -std::string format_int(T i) +inline std::string format_int(T i) { std::stringstream ss; ss.imbue(std::locale("")); diff --git a/geom_matching/wasserstein/include/diagonal_heap.h b/geom_matching/wasserstein/include/diagonal_heap.h index 9ffee70..3b3c8bc 100644 --- a/geom_matching/wasserstein/include/diagonal_heap.h +++ b/geom_matching/wasserstein/include/diagonal_heap.h @@ -129,7 +129,7 @@ using LossesHeapOld = IdxValHeap>; #endif template -std::string losses_heap_to_string(const LossesHeapOld& h) +inline std::string losses_heap_to_string(const LossesHeapOld& h) { std::stringstream result; result << "["; diff --git a/geom_matching/wasserstein/include/diagram_reader.h b/geom_matching/wasserstein/include/diagram_reader.h index 84bf49c..b52fcbd 100644 --- a/geom_matching/wasserstein/include/diagram_reader.h +++ b/geom_matching/wasserstein/include/diagram_reader.h @@ -55,31 +55,31 @@ namespace hera { // cannot choose stod, stof or stold based on RealType, // lazy solution: partial specialization template -RealType parse_real_from_str(const std::string& s); +inline RealType parse_real_from_str(const std::string& s); template <> -double parse_real_from_str(const std::string& s) +inline double parse_real_from_str(const std::string& s) { return std::stod(s); } template <> -long double parse_real_from_str(const std::string& s) +inline long double parse_real_from_str(const std::string& s) { return std::stold(s); } template <> -float parse_real_from_str(const std::string& s) +inline float parse_real_from_str(const std::string& s) { return std::stof(s); } template -RealType parse_real_from_str(const std::string& s) +inline RealType parse_real_from_str(const std::string& s) { static_assert(sizeof(RealType) != sizeof(RealType), "Must be specialized for each type you want to use, see above"); } @@ -90,7 +90,7 @@ RealType parse_real_from_str(const std::string& s) // decPrecision is the maximal decimal precision in the input, // it is zero if all coordinates in the input are integers template>> -bool read_diagram_point_set(const char* fname, ContType_& result, int& decPrecision) +inline bool read_diagram_point_set(const char* fname, ContType_& result, int& decPrecision) { size_t lineNumber { 0 }; result.clear(); @@ -182,7 +182,7 @@ bool read_diagram_point_set(const char* fname, ContType_& result, int& decPrecis // wrappers template>> -bool read_diagram_point_set(const std::string& fname, ContType_& result, int& decPrecision) +inline bool read_diagram_point_set(const std::string& fname, ContType_& result, int& decPrecision) { return read_diagram_point_set(fname.c_str(), result, decPrecision); } @@ -190,21 +190,21 @@ bool read_diagram_point_set(const std::string& fname, ContType_& result, int& de // these two functions are now just wrappers for the previous ones, // in case someone needs them; decPrecision is ignored template>> -bool read_diagram_point_set(const char* fname, ContType_& result) +inline bool read_diagram_point_set(const char* fname, ContType_& result) { int decPrecision; return read_diagram_point_set(fname, result, decPrecision); } template>> -bool read_diagram_point_set(const std::string& fname, ContType_& result) +inline bool read_diagram_point_set(const std::string& fname, ContType_& result) { int decPrecision; return read_diagram_point_set(fname.c_str(), result, decPrecision); } template > > -bool read_diagram_dipha(const std::string& fname, unsigned int dim, ContType_& result) +inline bool read_diagram_dipha(const std::string& fname, unsigned int dim, ContType_& result) { std::ifstream file; file.open(fname, std::ios::in | std::ios::binary); @@ -274,7 +274,7 @@ bool read_diagram_dipha(const std::string& fname, unsigned int dim, ContType_& r template -void remove_duplicates(ContType& dgm_A, ContType& dgm_B) +inline void remove_duplicates(ContType& dgm_A, ContType& dgm_B) { std::map, int> map_A, map_B; // copy points to maps @@ -328,7 +328,7 @@ void remove_duplicates(ContType& dgm_A, ContType& dgm_B) #ifdef WASSERSTEIN_PURE_GEOM template -int get_point_dimension(const std::string& line) +inline int get_point_dimension(const std::string& line) { Real x; int dim = 0; @@ -341,7 +341,7 @@ int get_point_dimension(const std::string& line) template -bool read_point_cloud(const char* fname, hera::ws::dnn::DynamicPointVector& result, int& dimension, int& decPrecision) +inline bool read_point_cloud(const char* fname, hera::ws::dnn::DynamicPointVector& result, int& dimension, int& decPrecision) { using DynamicPointTraitsR = typename hera::ws::dnn::DynamicPointTraits; @@ -423,20 +423,20 @@ bool read_point_cloud(const char* fname, hera::ws::dnn::DynamicPointVector -bool read_point_cloud(const char* fname, hera::ws::dnn::DynamicPointVector& result, int& dimension) +inline bool read_point_cloud(const char* fname, hera::ws::dnn::DynamicPointVector& result, int& dimension) { int dec_precision; return read_point_cloud(fname, result, dimension, dec_precision); } template -bool read_point_cloud(std::string fname, hera::ws::dnn::DynamicPointVector& result, int& dimension, int& dec_precision) +inline bool read_point_cloud(std::string fname, hera::ws::dnn::DynamicPointVector& result, int& dimension, int& dec_precision) { return read_point_cloud(fname.c_str(), result, dimension, dec_precision); } template -bool read_point_cloud(std::string fname, hera::ws::dnn::DynamicPointVector& result, int& dimension) +inline bool read_point_cloud(std::string fname, hera::ws::dnn::DynamicPointVector& result, int& dimension) { return read_point_cloud(fname.c_str(), result, dimension); } diff --git a/geom_matching/wasserstein/include/wasserstein.h b/geom_matching/wasserstein/include/wasserstein.h index b90a545..2d0cffc 100644 --- a/geom_matching/wasserstein/include/wasserstein.h +++ b/geom_matching/wasserstein/include/wasserstein.h @@ -73,7 +73,7 @@ namespace ws // compare as multisets template - bool are_equal(const PairContainer& dgm1, const PairContainer& dgm2) + inline bool are_equal(const PairContainer& dgm1, const PairContainer& dgm2) { if (dgm1.size() != dgm2.size()) { return false; @@ -97,7 +97,7 @@ namespace ws // to handle points with one coordinate = infinity template - RealType get_one_dimensional_cost(std::vector& set_A, + inline RealType get_one_dimensional_cost(std::vector& set_A, std::vector& set_B, const RealType wasserstein_power) { @@ -210,7 +210,7 @@ namespace ws // this function assumes that all coordinates are finite // points at infinity are processed in wasserstein_cost template - RealType wasserstein_cost_vec(const std::vector>& A, + inline RealType wasserstein_cost_vec(const std::vector>& A, const std::vector>& B, const AuctionParams& params, const std::string& _log_filename_prefix) @@ -245,7 +245,7 @@ namespace ws template -typename DiagramTraits::RealType +inline typename DiagramTraits::RealType wasserstein_cost(const PairContainer& A, const PairContainer& B, const AuctionParams< typename DiagramTraits::RealType >& params, @@ -332,7 +332,7 @@ wasserstein_cost(const PairContainer& A, } template -typename DiagramTraits::RealType +inline typename DiagramTraits::RealType wasserstein_dist(PairContainer& A, PairContainer& B, const AuctionParams::RealType> params, diff --git a/geom_matching/wasserstein/include/wasserstein_pure_geom.hpp b/geom_matching/wasserstein/include/wasserstein_pure_geom.hpp index 2a57599..13a94d5 100644 --- a/geom_matching/wasserstein/include/wasserstein_pure_geom.hpp +++ b/geom_matching/wasserstein/include/wasserstein_pure_geom.hpp @@ -30,7 +30,7 @@ namespace ws using AuctionRunnerJacR = typename hera::ws::AuctionRunnerJac, hera::ws::dnn::DynamicPointVector>; -double wasserstein_cost(const DynamicPointVector& set_A, const DynamicPointVector& set_B, const AuctionParams& params) +inline double wasserstein_cost(const DynamicPointVector& set_A, const DynamicPointVector& set_B, const AuctionParams& params) { if (params.wasserstein_power < 1.0) { throw std::runtime_error("Bad q in Wasserstein " + std::to_string(params.wasserstein_power)); @@ -75,7 +75,7 @@ double wasserstein_cost(const DynamicPointVector& set_A, const DynamicPo } -double wasserstein_dist(const DynamicPointVector& set_A, const DynamicPointVector& set_B, const AuctionParams& params) +inline double wasserstein_dist(const DynamicPointVector& set_A, const DynamicPointVector& set_B, const AuctionParams& params) { return std::pow(wasserstein_cost(set_A, set_B, params), 1.0 / params.wasserstein_power); } -- cgit v1.2.3