Last active
January 4, 2016 15:23
-
-
Save telishev/516dba19737a498ce72d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tuple> | |
#include <initializer_list> | |
#include <math.h> | |
#include <utility> | |
using namespace std; | |
template <typename Tup1, typename Tup2, size_t...s> | |
double euclidean_distance_impl (Tup1 const &tup1, Tup2 const &tup2, index_sequence<s...>) | |
{ | |
double res = 0.0; | |
auto x = {res += ((std::get<s> (tup2) - std::get<s> (tup1))*(std::get<s> (tup2) - std::get<s> (tup1)))...}; | |
(void)x; | |
return sqrt (res); | |
} | |
template <typename Tup1, typename Tup2> | |
double euclidean_distance(Tup1 const &tup1, Tup2 const &tup2) | |
{ | |
static_assert(tuple_size<Tup1>::value == tuple_size<Tup2>::value, "tuples must be of the same size"); | |
return euclidean_distance_impl (tup1, tup2, make_index_sequence<tuple_size<Tup1>::value> ()); | |
} | |
////////////////////////////////////////////////////////////////////////// | |
template<typename T, typename... Ts> | |
constexpr size_t count_impl() | |
{ | |
size_t count = 0; | |
auto x = {(count += std::is_same<T, Ts>::value)...}; | |
(void)x; | |
return count; | |
} | |
template<typename T, typename... Ts> | |
struct count | |
{ | |
static constexpr size_t value = count_impl<T, Ts...> (); | |
}; | |
template<typename T, typename... Ts> | |
constexpr size_t find_impl() | |
{ | |
size_t pos = (size_t)-1; | |
size_t cur_pos = 0; | |
size_t dummy = 0; | |
auto x = {((dummy = std::is_same<T, Ts>::value ? pos = min (pos, cur_pos) : 0), cur_pos++)...}; | |
(void)x; | |
(void)dummy; | |
return pos; | |
} | |
template<typename T, typename... Ts> | |
struct find | |
{ | |
static constexpr size_t value = find_impl<T, Ts...> (); | |
static_assert(value != (size_t)-1, "Could not find type"); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment