Skip to content

Commit

Permalink
define geometry::get_or (#59)
Browse files Browse the repository at this point in the history
Change-Id: Ib981ae0cceea84b385f43a0d25cf8ae7b3f6bdd0
  • Loading branch information
oliverlee authored Jun 18, 2024
1 parent 1ead3bf commit b5c9f6f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 25 deletions.
2 changes: 2 additions & 0 deletions geometry/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cc_library(
srcs = [
"src/algebra.hpp",
"src/algebra_core.hpp",
"src/algebra_fwd.hpp",
"src/algebra_type.hpp",
"src/blade_base.hpp",
"src/blade_list.hpp",
Expand All @@ -62,6 +63,7 @@ cc_library(
"src/exterior_product.hpp",
"src/geometric_product.hpp",
"src/get.hpp",
"src/get_or.hpp",
"src/multivector_base.hpp",
"src/multivector_for.hpp",
"src/regressive_product.hpp",
Expand Down
2 changes: 2 additions & 0 deletions geometry/geometry.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include "geometry/src/algebra.hpp"
#include "geometry/src/algebra_fwd.hpp"
#include "geometry/src/algebra_type.hpp"
#include "geometry/src/common_algebra_type.hpp"
#include "geometry/src/dual.hpp"
#include "geometry/src/equality_comparison.hpp"
#include "geometry/src/exterior_product.hpp"
#include "geometry/src/geometric_product.hpp"
#include "geometry/src/get.hpp"
#include "geometry/src/get_or.hpp"
#include "geometry/src/multivector_for.hpp"
#include "geometry/src/regressive_product.hpp"
#include "geometry/src/sum.hpp"
Expand Down
23 changes: 1 addition & 22 deletions geometry/src/algebra.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "geometry/src/algebra_core.hpp"
#include "geometry/src/algebra_fwd.hpp"
#include "geometry/src/detail/algebra_reference_constant.hpp"
#include "geometry/src/detail/is_constexpr_scalar_type.hpp"
#include "geometry/type_metaprogramming.hpp"
Expand Down Expand Up @@ -107,28 +108,6 @@ struct algebra
using dual_algebra_type = algebra<S, N, not Dual>;
};

namespace detail {

template <class T>
struct is_algebra : std::false_type
{};
template <class S, std::size_t N, bool Dual>
struct is_algebra<algebra<S, N, Dual>> : std::true_type
{};

} // namespace detail

/// check if T is a specialization of algebra
///
/// @{
template <class T>
struct is_algebra : detail::is_algebra<std::remove_cv_t<T>>
{};

template <class T>
inline constexpr auto is_algebra_v = is_algebra<T>::value;
/// @}

/// @name helper type aliases and values
/// @{

Expand Down
1 change: 1 addition & 0 deletions geometry/src/algebra_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "geometry/src/detail/ordered.hpp"
#include "geometry/src/detail/strictly_increasing.hpp"
#include "geometry/src/get.hpp"
#include "geometry/src/get_or.hpp"
#include "geometry/src/multivector_base.hpp"
#include "geometry/type_metaprogramming.hpp"

Expand Down
33 changes: 33 additions & 0 deletions geometry/src/algebra_fwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <cstddef>
#include <type_traits>

namespace geometry {

template <class S, std::size_t N, bool Dual>
struct algebra;

namespace detail {

template <class T>
struct is_algebra : std::false_type
{};
template <class S, std::size_t N, bool Dual>
struct is_algebra<algebra<S, N, Dual>> : std::true_type
{};

} // namespace detail

/// check if T is a specialization of algebra
///
/// @{
template <class T>
struct is_algebra : detail::is_algebra<std::remove_cv_t<T>>
{};

template <class T>
inline constexpr auto is_algebra_v = is_algebra<T>::value;
/// @}

} // namespace geometry
2 changes: 1 addition & 1 deletion geometry/src/common_algebra_type.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "geometry/src/algebra.hpp"
#include "geometry/src/algebra_fwd.hpp"
#include "geometry/src/algebra_type.hpp"

#include <type_traits>
Expand Down
2 changes: 0 additions & 2 deletions geometry/src/get.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "geometry/src/algebra_type.hpp"
#include "geometry/src/blade_base.hpp"
#include "geometry/src/multivector_base.hpp"

#include <type_traits>
Expand Down
39 changes: 39 additions & 0 deletions geometry/src/get_or.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "geometry/src/blade_base.hpp"
#include "geometry/src/common_algebra_type.hpp"
#include "geometry/src/multivector_base.hpp"

#include <type_traits>
#include <utility>

namespace geometry {
namespace detail {

template <class T>
auto get_or() -> void = delete;

struct get_or_fn
{
template <
class V,
class B,
class = std::enable_if_t<
is_multivector_v<V> and is_blade_v<B>,
common_algebra_type_t<V, B>>>
constexpr decltype(auto) operator()(const V & v, const B & value) const
{
return get_or(v, value);
}
};

} // namespace detail

inline namespace cpo {

/// access a blade of a multivector or return a default value
///
inline constexpr auto get_or = detail::get_or_fn{};

} // namespace cpo
} // namespace geometry
10 changes: 10 additions & 0 deletions test/algebra_get_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ auto main() -> int

return expect(true);
};

"get_or multivector"_ctest = [] {
const auto x = 2 * e<> + 3 * e<0>;

using ::skytest::eq;
return expect( //
eq(2 * e<>, get_or(x, e<>)) and //
eq(3 * e<0>, get_or(x, e<0>)) and //
eq(e<1>, get_or(x, e<1>)));
};
}

0 comments on commit b5c9f6f

Please sign in to comment.