diff --git a/dune/matrix-vector/CMakeLists.txt b/dune/matrix-vector/CMakeLists.txt index ce19f4ce370a5654b850f495fa96e2604feb7de0..fb8e1171a8b2f60152290a54546234f796b7f36b 100644 --- a/dune/matrix-vector/CMakeLists.txt +++ b/dune/matrix-vector/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(test) +add_subdirectory(traits) #install headers install(FILES @@ -12,9 +13,7 @@ install(FILES crossproduct.hh genericvectortools.hh ldlt.hh - matrixtraits.hh promote.hh - scalartraits.hh singlenonzerocolumnmatrix.hh singlenonzerorowmatrix.hh tranpose.hh diff --git a/dune/matrix-vector/matrixtraits.hh b/dune/matrix-vector/matrixtraits.hh deleted file mode 100644 index 73c3c0d9860dae48eadb7e72be4dd752756cc0bc..0000000000000000000000000000000000000000 --- a/dune/matrix-vector/matrixtraits.hh +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef DUNE_MATRIX_VECTOR_MATRIXTRAITS_HH -#define DUNE_MATRIX_VECTOR_MATRIXTRAITS_HH - -#include <dune/common/diagonalmatrix.hh> -#include <dune/common/fmatrix.hh> -#include <dune/istl/bcrsmatrix.hh> -#include <dune/istl/multitypeblockmatrix.hh> -#include <dune/istl/scaledidmatrix.hh> - -namespace Dune { - namespace MatrixVector { - - /** \brief Class to identify matrix types and extract information - * - * Specialize this class for all types that can be used like a matrix. - */ - template<class T> - struct MatrixTraits - { - constexpr static bool isMatrix = false; - constexpr static int rows = -1; - constexpr static int cols = -1; - }; - - template<class T, int n, int m> - struct MatrixTraits<Dune::FieldMatrix<T,n,m> > - { - constexpr static bool isMatrix = true; - constexpr static int rows = n; - constexpr static int cols = m; - }; - - template<class T, int n> - struct MatrixTraits<Dune::DiagonalMatrix<T,n> > - { - constexpr static bool isMatrix = true; - constexpr static int rows = n; - constexpr static int cols = n; - }; - - template<class T, int n> - struct MatrixTraits<Dune::ScaledIdentityMatrix<T,n> > - { - constexpr static bool isMatrix = true; - constexpr static int rows = n; - constexpr static int cols = n; - }; - - template<class T> - struct MatrixTraits<Dune::BCRSMatrix<T> > - { - constexpr static bool isMatrix = true; - }; - - template<class... T> - struct MatrixTraits<MultiTypeBlockMatrix<T...> > - { - constexpr static bool isMatrix = true; - }; - } -} -#endif diff --git a/dune/matrix-vector/scalartraits.hh b/dune/matrix-vector/scalartraits.hh deleted file mode 100644 index 66b47c3bf0f964893f9d7fd99f35fba7ede9eb9f..0000000000000000000000000000000000000000 --- a/dune/matrix-vector/scalartraits.hh +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef DUNE_MATRIX_VECTOR_SCALARTRAITS_HH -#define DUNE_MATRIX_VECTOR_SCALARTRAITS_HH - -#include <dune/common/diagonalmatrix.hh> -#include <dune/common/fmatrix.hh> -#include <dune/common/typetraits.hh> -#include <dune/istl/bcrsmatrix.hh> -#include <dune/istl/scaledidmatrix.hh> - -namespace Dune { -namespace MatrixVector { - /** \brief Class to identify scalar types - * - * Specialize this class for all types that can be used - * like scalar quantities. - */ - template <class T> - struct ScalarTraits { - constexpr static bool isScalar = Dune::IsNumber<T>::value; - }; - - template <class T> - struct ScalarTraits<Dune::FieldVector<T, 1>> { - constexpr static bool isScalar = true; - }; - - template <class T> - struct ScalarTraits<Dune::FieldMatrix<T, 1, 1>> { - constexpr static bool isScalar = true; - }; - - template <class T> - struct ScalarTraits<Dune::DiagonalMatrix<T, 1>> { - constexpr static bool isScalar = true; - }; - - template <class T> - struct ScalarTraits<Dune::ScaledIdentityMatrix<T, 1>> { - constexpr static bool isScalar = true; - }; -} -} -#endif diff --git a/dune/matrix-vector/traits/CMakeLists.txt b/dune/matrix-vector/traits/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1efde8c5ed183f7e4c5783d72484f4de4cad72c3 --- /dev/null +++ b/dune/matrix-vector/traits/CMakeLists.txt @@ -0,0 +1,7 @@ +#install headers +install(FILES + scalartraits.hh + matrixtraits.hh + vectortraits.hh + utilities.hh + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/matrix-vector/traits) diff --git a/dune/matrix-vector/traits/matrixtraits.hh b/dune/matrix-vector/traits/matrixtraits.hh new file mode 100644 index 0000000000000000000000000000000000000000..9d53fae5e766ff019b037c49e92261f474519051 --- /dev/null +++ b/dune/matrix-vector/traits/matrixtraits.hh @@ -0,0 +1,61 @@ +#ifndef DUNE_MATRIX_VECTOR_TRAITS_MATRIXTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_MATRIXTRAITS_HH + +#include <dune/common/diagonalmatrix.hh> +#include <dune/common/fmatrix.hh> + +#include <dune/istl/bcrsmatrix.hh> +#include <dune/istl/multitypeblockmatrix.hh> +#include <dune/istl/scaledidmatrix.hh> + +namespace Dune { +namespace MatrixVector { +namespace Traits { + +/** \brief Class to identify matrix types and extract information + * + * Specialize this class for all types that can be used like a matrix. + */ +template<class T> +struct MatrixTraits { + constexpr static bool isMatrix = false; + constexpr static int rows = -1; + constexpr static int cols = -1; +}; + +template<class T, int n, int m> +struct MatrixTraits<FieldMatrix<T, n, m>> { + constexpr static bool isMatrix = true; + constexpr static int rows = n; + constexpr static int cols = m; +}; + +template<class T, int n> +struct MatrixTraits<DiagonalMatrix<T, n>> { + constexpr static bool isMatrix = true; + constexpr static int rows = n; + constexpr static int cols = n; +}; + +template<class T, int n> +struct MatrixTraits<ScaledIdentityMatrix<T, n>> { + constexpr static bool isMatrix = true; + constexpr static int rows = n; + constexpr static int cols = n; +}; + +template<class T> +struct MatrixTraits<BCRSMatrix<T>> { + constexpr static bool isMatrix = true; +}; + +template<class... T> +struct MatrixTraits<MultiTypeBlockMatrix<T...> > { + constexpr static bool isMatrix = true; +}; + +} // end namespace Traits +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_MATRIXTRAITS_HH diff --git a/dune/matrix-vector/traits/scalartraits.hh b/dune/matrix-vector/traits/scalartraits.hh new file mode 100644 index 0000000000000000000000000000000000000000..2c284b4256addf2cf6b609e6258581840bf80042 --- /dev/null +++ b/dune/matrix-vector/traits/scalartraits.hh @@ -0,0 +1,48 @@ +#ifndef DUNE_MATRIX_VECTOR_TRAITS_SCALARTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_SCALARTRAITS_HH + +#include <dune/common/diagonalmatrix.hh> +#include <dune/common/fmatrix.hh> +#include <dune/common/typetraits.hh> +#include <dune/istl/bcrsmatrix.hh> +#include <dune/istl/scaledidmatrix.hh> + +namespace Dune { +namespace MatrixVector { +namespace Traits { + +/** \brief Class to identify scalar types + * + * Specialize this class for all types that can be used + * like scalar quantities. + */ +template <class T> +struct ScalarTraits { + constexpr static bool isScalar = Dune::IsNumber<T>::value; +}; + +template <class T> +struct ScalarTraits<Dune::FieldVector<T, 1>> { + constexpr static bool isScalar = true; +}; + +template <class T> +struct ScalarTraits<Dune::FieldMatrix<T, 1, 1>> { + constexpr static bool isScalar = true; +}; + +template <class T> +struct ScalarTraits<Dune::DiagonalMatrix<T, 1>> { + constexpr static bool isScalar = true; +}; + +template <class T> +struct ScalarTraits<Dune::ScaledIdentityMatrix<T, 1>> { + constexpr static bool isScalar = true; +}; + +} // end namespace Traits +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_SCALARTRAITS_HH diff --git a/dune/matrix-vector/traits/utilities.hh b/dune/matrix-vector/traits/utilities.hh new file mode 100644 index 0000000000000000000000000000000000000000..ecc17b580b11f45ae2cf34562a092288fa0a44ef --- /dev/null +++ b/dune/matrix-vector/traits/utilities.hh @@ -0,0 +1,85 @@ +#ifndef DUNE_MATRIX_VECTOR_UTILITIES_HH +#define DUNE_MATRIX_VECTOR_UTILITIES_HH + +#include <type_traits> + +#include <dune/common/std/type_traits.hh> +#include <dune/common/typetraits.hh> + +#include <dune/matrix-vector/concepts.hh> +#include <dune/matrix-vector/traits/scalartraits.hh> +#include <dune/matrix-vector/traits/matrixtraits.hh> +#include <dune/matrix-vector/traits/vectortraits.hh> + +namespace Dune { +namespace MatrixVector { + +// convenience compile-time functions to classify types +template <class T> +constexpr auto isNumber() { + return Std::bool_constant<IsNumber<std::decay_t<T>>::value>(); +} + +template <class T> +constexpr auto isScalar() { + return Std::bool_constant<Traits::ScalarTraits<std::decay_t<T>>::isScalar>(); +} + +template <class T> +constexpr auto isVector() { + return Std::bool_constant<Traits::VectorTraits<std::decay_t<T>>::isVector>(); +} + +template <class T> +constexpr auto isMatrix() { + return Std::bool_constant<Traits::MatrixTraits<std::decay_t<T>>::isMatrix>(); +} + +template <class T> +constexpr auto isTupleOrDerived() { + return Std::bool_constant<IsTupleOrDerived<std::decay_t<T>>::value>(); +} + +// enable_if typedefs for traits ... +template <class T, class R = void> +using EnableNumber = std::enable_if_t<isNumber<T>(), R>; + +template <class T, class R = void> +using EnableScalar = std::enable_if_t<isScalar<T>(), R>; + +template <class T, class R = void> +using EnableVector = std::enable_if_t<isVector<T>(), R>; + +template <class T, class R = void> +using EnableMatrix = std::enable_if_t<isMatrix<T>(), R>; + +template <class T, class R = void> +using EnableTupleOrDerived = std::enable_if_t<isTupleOrDerived<T>(), R>; + +// ... and their negations +template <class T, class R = void> +using DisableNumber = std::enable_if_t<not isNumber<T>(), R>; + +template <class T, class R = void> +using DisableScalar = std::enable_if_t<not isScalar<T>(), R>; + +template <class T, class R = void> +using DisableVector = std::enable_if_t<not isVector<T>(), R>; + +template <class T, class R = void> +using DisableMatrix = std::enable_if_t<not isMatrix<T>(), R>; + +template <class T, class R = void> +using DisableTupleOrDerived = std::enable_if_t<not isTupleOrDerived<T>(), R>; + +// compile-time property checks +template <class T> +constexpr auto isSparseRangeIterable() { + return Std::bool_constant<isTupleOrDerived<T>() or + models<Concept::HasBegin, T>()>(); +} + +} // end namespace MatrixVector +} // end namespace Dune + +#endif // DUNE_MATRIX_VECTOR_TRAITS_UTILITIES_HH diff --git a/dune/matrix-vector/vectortraits.hh b/dune/matrix-vector/traits/vectortraits.hh similarity index 77% rename from dune/matrix-vector/vectortraits.hh rename to dune/matrix-vector/traits/vectortraits.hh index e505a7e59ea5228ada12e72625b2ce8018c15293..a58b7c19eb48846ddfef893c5f546f3f868a50bd 100644 --- a/dune/matrix-vector/vectortraits.hh +++ b/dune/matrix-vector/traits/vectortraits.hh @@ -1,5 +1,5 @@ -#ifndef DUNE_MATRIX_VECTOR_VECTORTRAITS_HH -#define DUNE_MATRIX_VECTOR_VECTORTRAITS_HH +#ifndef DUNE_MATRIX_VECTOR_TRAITS_VECTORTRAITS_HH +#define DUNE_MATRIX_VECTOR_TRAITS_VECTORTRAITS_HH #include <dune/common/fvector.hh> #include <dune/istl/bvector.hh> @@ -7,6 +7,7 @@ namespace Dune { namespace MatrixVector { +namespace Traits { /** \brief Class to identify vector types and extract information * @@ -27,7 +28,8 @@ struct VectorTraits<BlockVector<Block>> { constexpr static bool isVector = true; }; +} // end namespace Traits } // end namespace MatrixVector } // end namespace Dune -#endif // DUNE_MATRIX_VECTOR_VECTORTRAITS_HH +#endif // DUNE_MATRIX_VECTOR_TRAITS_VECTORTRAITS_HH diff --git a/dune/matrix-vector/traitutilities.hh b/dune/matrix-vector/traitutilities.hh deleted file mode 100644 index 7be19506cc9669e04bcc31ceccdf9c05459b3fc9..0000000000000000000000000000000000000000 --- a/dune/matrix-vector/traitutilities.hh +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef DUNE_MATRIX_VECTOR_TRAITUTILITIES_HH -#define DUNE_MATRIX_VECTOR_TRAITUTILITIES_HH - -#include <type_traits> - -#include <dune/common/std/type_traits.hh> -#include <dune/common/typetraits.hh> - -#include <dune/matrix-vector/concepts.hh> -#include <dune/matrix-vector/scalartraits.hh> -#include <dune/matrix-vector/matrixtraits.hh> -#include <dune/matrix-vector/vectortraits.hh> - -namespace Dune { -namespace MatrixVector { - -// convenience compile-time functions to classify types -template <class T> -constexpr auto isNumber() { - return Std::bool_constant<IsNumber<T>::value>(); -} - -template <class T> -constexpr auto isScalar() { - return Std::bool_constant<ScalarTraits<T>::isScalar>(); -} - -template <class T> -constexpr auto isVector() { - return Std::bool_constant<VectorTraits<T>::isVector>(); -} - -template <class T> -constexpr auto isMatrix() { - return Std::bool_constant<MatrixTraits<T>::isMatrix>(); -} - -template <class T> -constexpr auto isTupleOrDerived() { - return Std::bool_constant<IsTupleOrDerived<std::decay_t<T>>::value>(); -} - -// enable_if typedefs for traits ... -template <class T> -using EnableNumber = std::enable_if_t<isNumber<T>()>; - -template <class T> -using EnableScalar = std::enable_if_t<isScalar<T>()>; - -template <class T> -using EnableVector = std::enable_if_t<isVector<T>()>; - -template <class T> -using EnableMatrix = std::enable_if_t<isMatrix<T>()>; - -// ... and their negations -template <class T> -using DisableNumber = std::enable_if_t<not isNumber<T>()>; - -template <class T> -using DisableScalar = std::enable_if_t<not isScalar<T>()>; - -template <class T> -using DisableVector = std::enable_if_t<not isVector<T>()>; - -template <class T> -using DisableMatrix = std::enable_if_t<not isMatrix<T>()>; - -// compile-time property checks -template <class T> -constexpr auto isSparseRangeIterable() { - return Std::bool_constant<isTupleOrDerived<T>() or - models<Concept::HasBegin, T>()>(); -} - -} // end namespace MatrixVector -} // end namespace Dune - -#endif // DUNE_MATRIX_VECTOR_TRAITUTILITIES_HH