cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
metric_traits.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4#include <limits>
5
7
8namespace toolbox::metrics
9{
10
11// Forward declarations
12template<typename T> class L1Metric;
13template<typename T> class L2Metric;
14template<typename T> class LinfMetric;
15template<typename T, int P> class LpMetric;
16template<typename T> class ChiSquaredMetric;
17template<typename T> class HistogramIntersectionMetric;
18template<typename T> class BhattacharyyaMetric;
19template<typename T> class CosineMetric;
20template<typename T> class AngularMetric;
21
22// Base metric traits
23template<typename Metric>
25{
26 using metric_type = Metric;
27 using element_type = typename Metric::element_type;
28 using result_type = typename Metric::result_type;
29
30 static constexpr bool is_symmetric = true;
31 static constexpr bool is_squared = false;
32 static constexpr bool has_squared_form = false;
33 static constexpr bool is_normalized = false;
34 static constexpr bool requires_positive_values = false;
35
36 // Default bounds
37 static constexpr result_type min_distance = 0;
38 static constexpr result_type max_distance = std::numeric_limits<result_type>::max();
39};
40
41// Specializations for specific metrics
42template<typename T>
44{
46 using element_type = T;
47 using result_type = T;
48
49 static constexpr bool is_symmetric = true;
50 static constexpr bool is_squared = false;
51 static constexpr bool has_squared_form = true;
52 static constexpr bool is_normalized = false;
53 static constexpr bool requires_positive_values = false;
54
55 static constexpr result_type min_distance = 0;
56 static constexpr result_type max_distance = std::numeric_limits<result_type>::max();
57};
58
59template<typename T>
61{
63 using element_type = T;
64 using result_type = T;
65
66 static constexpr bool is_symmetric = true;
67 static constexpr bool is_squared = false;
68 static constexpr bool has_squared_form = false;
69 static constexpr bool is_normalized = false;
70 static constexpr bool requires_positive_values = false;
71
72 static constexpr result_type min_distance = 0;
73 static constexpr result_type max_distance = std::numeric_limits<result_type>::max();
74};
75
76// Helper templates for C++17 (instead of concepts)
77template<typename Metric>
79
80template<typename Metric>
82
83template<typename Metric>
85
86// Type traits helpers
87template<typename T>
88struct is_metric : std::false_type {};
89
90template<typename Derived, typename ElementType>
91struct is_metric<base_metric_t<Derived, ElementType>> : std::true_type {};
92
93template<typename T>
94inline constexpr bool is_metric_v = is_metric<T>::value;
95
96} // namespace toolbox::metrics
Definition vector_metrics.hpp:53
Definition vector_metrics.hpp:18
Definition base_metric.hpp:13
Definition angular_metrics.hpp:11
constexpr bool is_metric_v
Definition metric_traits.hpp:94
constexpr bool is_normalized_metric_v
Definition metric_traits.hpp:81
constexpr bool has_squared_form_v
Definition metric_traits.hpp:84
constexpr bool is_symmetric_metric_v
Definition metric_traits.hpp:78
Definition metric_traits.hpp:88
T element_type
Definition metric_traits.hpp:63
T result_type
Definition metric_traits.hpp:64
T element_type
Definition metric_traits.hpp:46
T result_type
Definition metric_traits.hpp:47
Definition metric_traits.hpp:25
static constexpr bool is_symmetric
Definition metric_traits.hpp:30
static constexpr bool has_squared_form
Definition metric_traits.hpp:32
static constexpr bool is_normalized
Definition metric_traits.hpp:33
static constexpr result_type max_distance
Definition metric_traits.hpp:38
typename Metric::result_type result_type
Definition metric_traits.hpp:28
static constexpr bool is_squared
Definition metric_traits.hpp:31
static constexpr bool requires_positive_values
Definition metric_traits.hpp:34
static constexpr result_type min_distance
Definition metric_traits.hpp:37
Metric metric_type
Definition metric_traits.hpp:26
typename Metric::element_type element_type
Definition metric_traits.hpp:27