cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
base_metric.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <vector>
5#include <array>
6#include <type_traits>
7
8namespace toolbox::metrics
9{
10
11template<typename Derived, typename ElementType>
13{
14public:
15 using element_type = ElementType;
16 using result_type = ElementType;
17
18 // Raw pointer interface
19 constexpr ElementType distance(const ElementType* a,
20 const ElementType* b,
21 std::size_t size) const
22 {
23 return static_cast<const Derived*>(this)->distance_impl(a, b, size);
24 }
25
26 constexpr ElementType squared_distance(const ElementType* a,
27 const ElementType* b,
28 std::size_t size) const
29 {
30 return static_cast<const Derived*>(this)->squared_distance_impl(a, b, size);
31 }
32
33 // Container interface for convenience
34 template<typename Container>
35 constexpr ElementType distance(const Container& a, const Container& b) const
36 {
37 static_assert(std::is_same_v<typename Container::value_type, ElementType>,
38 "Container must have matching element type");
39 return distance(a.data(), b.data(), a.size());
40 }
41
42 template<typename Container>
43 constexpr ElementType squared_distance(const Container& a, const Container& b) const
44 {
45 static_assert(std::is_same_v<typename Container::value_type, ElementType>,
46 "Container must have matching element type");
47 return squared_distance(a.data(), b.data(), a.size());
48 }
49
50};
51
52// Type alias for common use cases
53template<typename T>
54using metric_ptr = std::unique_ptr<base_metric_t<void, T>>;
55
56} // namespace toolbox::metrics
Definition base_metric.hpp:13
ElementType element_type
Definition base_metric.hpp:15
constexpr ElementType squared_distance(const Container &a, const Container &b) const
Definition base_metric.hpp:43
constexpr ElementType distance(const ElementType *a, const ElementType *b, std::size_t size) const
Definition base_metric.hpp:19
constexpr ElementType distance(const Container &a, const Container &b) const
Definition base_metric.hpp:35
constexpr ElementType squared_distance(const ElementType *a, const ElementType *b, std::size_t size) const
Definition base_metric.hpp:26
ElementType result_type
Definition base_metric.hpp:16
Definition angular_metrics.hpp:11
std::unique_ptr< base_metric_t< void, T > > metric_ptr
Definition base_metric.hpp:54