Browse Source

Replaced Implementation::Sequence with std::index_sequence etc.

pull/217/head
Criss 9 years ago
parent
commit
00e27e4784
  1. 6
      src/Magnum/Array.h
  2. 10
      src/Magnum/Math/Bezier.h
  3. 25
      src/Magnum/Math/BoolVector.h
  4. 4
      src/Magnum/Math/Matrix.h
  5. 40
      src/Magnum/Math/RectangularMatrix.h
  6. 18
      src/Magnum/Math/Vector.h

6
src/Magnum/Array.h

@ -32,7 +32,7 @@
#include <type_traits>
#include "Magnum/Magnum.h"
#include "Magnum/Math/BoolVector.h" /* for Math::Implementation::Sequence */
#include "Magnum/Math/BoolVector.h" /* for Math::Implementation::repeat() */
namespace Magnum {
@ -77,7 +77,7 @@ template<UnsignedInt dimensions, class T> class Array {
constexpr /*implicit*/ Array(T value);
#else
template<class U, class V = typename std::enable_if<std::is_same<T, U>::value && dimensions != 1, T>::type>
constexpr /*implicit*/ Array(U value): Array(typename Math::Implementation::GenerateSequence<dimensions>::Type(), value) {}
constexpr /*implicit*/ Array(U value): Array(std::make_index_sequence<dimensions>{}, value) {}
#endif
/** @brief Equality */
@ -106,7 +106,7 @@ template<UnsignedInt dimensions, class T> class Array {
private:
/* Implementation for Array<dimensions, T>::Array(U) */
template<std::size_t ...sequence> constexpr explicit Array(Math::Implementation::Sequence<sequence...>, T value): _data{Math::Implementation::repeat(value, sequence)...} {}
template<std::size_t ...sequence> constexpr explicit Array(std::index_sequence<sequence...>, T value): _data{Math::Implementation::repeat(value, sequence)...} {}
T _data[dimensions];
};

10
src/Magnum/Math/Bezier.h

@ -72,7 +72,7 @@ template<UnsignedInt order, UnsignedInt dimensions, class T> class Bezier {
constexpr /*implicit*/ Bezier(ZeroInitT = ZeroInit) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: Bezier<order, dimensions, T>{typename Implementation::GenerateSequence<order + 1>::Type{}, ZeroInit}
: Bezier<order, dimensions, T>{std::make_index_sequence<order + 1>{}, ZeroInit}
#endif
{}
@ -80,7 +80,7 @@ template<UnsignedInt order, UnsignedInt dimensions, class T> class Bezier {
explicit Bezier(NoInitT) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: Bezier<order, dimensions, T>{typename Implementation::GenerateSequence<order + 1>::Type{}, NoInit}
: Bezier<order, dimensions, T>{std::make_index_sequence<order + 1>{}, NoInit}
#endif
{}
@ -95,7 +95,7 @@ template<UnsignedInt order, UnsignedInt dimensions, class T> class Bezier {
* Performs only default casting on the values, no rounding or
* anything else.
*/
template<class U> constexpr explicit Bezier(const Bezier<order, dimensions, U>& other) noexcept: Bezier{typename Implementation::GenerateSequence<order + 1>::Type(), other} {}
template<class U> constexpr explicit Bezier(const Bezier<order, dimensions, U>& other) noexcept: Bezier{std::make_index_sequence<order + 1>{}, other} {}
/** @brief Construct Bézier from external representation */
template<class U, class V = decltype(Implementation::BezierConverter<order, dimensions, T, U>::from(std::declval<U>()))> constexpr explicit Bezier(const U& other) noexcept: Bezier<order, dimensions, T>{Implementation::BezierConverter<order, dimensions, T, U>::from(other)} {}
@ -156,11 +156,11 @@ template<UnsignedInt order, UnsignedInt dimensions, class T> class Bezier {
private:
/* Implementation for Bezier<order, dimensions, T>::Bezier(const Bezier<order, dimensions, U>&) */
template<class U, std::size_t ...sequence> constexpr explicit Bezier(Implementation::Sequence<sequence...>, const Bezier<order, dimensions, U>& other) noexcept: _data{Vector<dimensions, T>(other._data[sequence])...} {}
template<class U, std::size_t ...sequence> constexpr explicit Bezier(std::index_sequence<sequence...>, const Bezier<order, dimensions, U>& other) noexcept: _data{Vector<dimensions, T>(other._data[sequence])...} {}
/* Implementation for Bezier<order, dimensions, T>::Bezier(ZeroInitT) and Bezier<order, dimensions, T>::Bezier(NoInitT) */
/* MSVC 2015 can't handle {} here */
template<class U, std::size_t ...sequence> constexpr explicit Bezier(Implementation::Sequence<sequence...>, U): _data{Vector<dimensions, T>((static_cast<void>(sequence), U{typename U::Init{}}))...} {}
template<class U, std::size_t ...sequence> constexpr explicit Bezier(std::index_sequence<sequence...>, U): _data{Vector<dimensions, T>((static_cast<void>(sequence), U{typename U::Init{}}))...} {}
/* Calculates and returns all intermediate points generated when using De Casteljau's algorithm */
std::array<Bezier<order, dimensions, T>, order + 1> calculateIntermediatePoints(Float t) const {

25
src/Magnum/Math/BoolVector.h

@ -29,6 +29,7 @@
* @brief Class @ref Magnum::Math::BoolVector
*/
#include <utility>
#include <Corrade/configure.h>
#include <Corrade/Utility/Debug.h>
@ -38,26 +39,6 @@
namespace Magnum { namespace Math {
namespace Implementation {
/** @todo C++14: use std::make_index_sequence and std::integer_sequence */
template<std::size_t ...> struct Sequence {};
#ifndef DOXYGEN_GENERATING_OUTPUT
/* E.g. GenerateSequence<3>::Type is Sequence<0, 1, 2> */
template<std::size_t N, std::size_t ...sequence> struct GenerateSequence:
GenerateSequence<N-1, N-1, sequence...> {};
template<std::size_t ...sequence> struct GenerateSequence<0, sequence...> {
typedef Sequence<sequence...> Type;
};
template<std::size_t N, std::size_t ...sequence> struct GenerateReverseSequence:
GenerateReverseSequence<N-1, sequence..., N-1> {};
template<std::size_t ...sequence> struct GenerateReverseSequence<0, sequence...> {
typedef Sequence<sequence...> Type;
};
#endif
template<class T> constexpr T repeat(T value, std::size_t) { return value; }
}
@ -100,7 +81,7 @@ template<std::size_t size> class BoolVector {
#ifdef DOXYGEN_GENERATING_OUTPUT
explicit BoolVector(T value) noexcept;
#else
template<class T, class U = typename std::enable_if<std::is_same<bool, T>::value && size != 1, bool>::type> constexpr explicit BoolVector(T value) noexcept: BoolVector(typename Implementation::GenerateSequence<DataSize>::Type(), value ? FullSegmentMask : 0) {}
template<class T, class U = typename std::enable_if<std::is_same<bool, T>::value && size != 1, bool>::type> constexpr explicit BoolVector(T value) noexcept: BoolVector(std::make_index_sequence<DataSize>{}, value ? FullSegmentMask : 0) {}
#endif
/** @brief Copy constructor */
@ -237,7 +218,7 @@ template<std::size_t size> class BoolVector {
};
/* Implementation for Vector<size, T>::Vector(U) */
template<std::size_t ...sequence> constexpr explicit BoolVector(Implementation::Sequence<sequence...>, UnsignedByte value): _data{Implementation::repeat(value, sequence)...} {}
template<std::size_t ...sequence> constexpr explicit BoolVector(std::index_sequence<sequence...>, UnsignedByte value): _data{Implementation::repeat(value, sequence)...} {}
UnsignedByte _data[(size-1)/8+1];
};

4
src/Magnum/Math/Matrix.h

@ -86,7 +86,7 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
constexpr /*implicit*/ Matrix(IdentityInitT = IdentityInit, T value = T(1)) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: RectangularMatrix<size, size, T>{typename Implementation::GenerateSequence<size>::Type(), Vector<size, T>(value)}
: RectangularMatrix<size, size, T>{std::make_index_sequence<size>{}, Vector<size, T>(value)}
#endif
{}
@ -113,7 +113,7 @@ template<std::size_t size, class T> class Matrix: public RectangularMatrix<size,
constexpr explicit Matrix(T value) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: RectangularMatrix<size, size, T>{typename Implementation::GenerateSequence<size>::Type(), value}
: RectangularMatrix<size, size, T>{std::make_index_sequence<size>{}, value}
#endif
{}

40
src/Magnum/Math/RectangularMatrix.h

@ -104,14 +104,14 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
* @see @ref diagonal()
*/
constexpr static RectangularMatrix<cols, rows, T> fromDiagonal(const Vector<DiagonalSize, T>& diagonal) noexcept {
return RectangularMatrix(typename Implementation::GenerateSequence<cols>::Type(), diagonal);
return RectangularMatrix(std::make_index_sequence<cols>{}, diagonal);
}
/** @brief Construct zero-filled matrix */
constexpr /*implicit*/ RectangularMatrix(ZeroInitT = ZeroInit) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: RectangularMatrix<cols, rows, T>{typename Implementation::GenerateSequence<cols>::Type{}, ZeroInit}
: RectangularMatrix<cols, rows, T>{std::make_index_sequence<cols>{}, ZeroInit}
#endif
{}
@ -119,7 +119,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
explicit RectangularMatrix(NoInitT) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: RectangularMatrix<cols, rows, T>{typename Implementation::GenerateSequence<cols>::Type{}, NoInit}
: RectangularMatrix<cols, rows, T>{std::make_index_sequence<cols>{}, NoInit}
#endif
{}
@ -132,7 +132,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
constexpr explicit RectangularMatrix(T value) noexcept
/** @todoc remove workaround when doxygen is sane */
#ifndef DOXYGEN_GENERATING_OUTPUT
: RectangularMatrix{typename Implementation::GenerateSequence<cols>::Type(), value}
: RectangularMatrix{std::make_index_sequence<cols>{}, value}
#endif
{}
@ -147,7 +147,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
* // integral == {1, 2, -15, 7}
* @endcode
*/
template<class U> constexpr explicit RectangularMatrix(const RectangularMatrix<cols, rows, U>& other) noexcept: RectangularMatrix(typename Implementation::GenerateSequence<cols>::Type(), other) {}
template<class U> constexpr explicit RectangularMatrix(const RectangularMatrix<cols, rows, U>& other) noexcept: RectangularMatrix(std::make_index_sequence<cols>{}, other) {}
/** @brief Construct matrix from external representation */
template<class U, class V = decltype(Implementation::RectangularMatrixConverter<cols, rows, T, U>::from(std::declval<U>()))> constexpr explicit RectangularMatrix(const U& other): RectangularMatrix(Implementation::RectangularMatrixConverter<cols, rows, T, U>::from(other)) {}
@ -400,7 +400,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
* @see @ref transposed(), @ref flippedRows(), @ref Vector::flipped()
*/
constexpr RectangularMatrix<cols, rows, T> flippedCols() const {
return flippedColsInternal(typename Implementation::GenerateReverseSequence<cols>::Type{});
return flippedColsInternal(std::make_index_sequence<cols>{});
}
/**
@ -410,7 +410,7 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
* @see @ref transposed(), @ref flippedCols(), @ref Vector::flipped()
*/
constexpr RectangularMatrix<cols, rows, T> flippedRows() const {
return flippedRowsInternal(typename Implementation::GenerateSequence<cols>::Type{});
return flippedRowsInternal(std::make_index_sequence<cols>{});
}
/**
@ -439,29 +439,29 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
private:
#endif
/* Implementation for RectangularMatrix<cols, rows, T>::fromDiagonal() and Matrix<size, T>(IdentityInitT, T) */
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, const Vector<DiagonalSize, T>& diagonal);
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(std::index_sequence<sequence...>, const Vector<DiagonalSize, T>& diagonal);
/* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(T) and Matrix<size, T>(T) */
/* MSVC 2015 can't handle {} here */
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, T value) noexcept: _data{Vector<rows, T>((static_cast<void>(sequence), value))...} {}
template<std::size_t ...sequence> constexpr explicit RectangularMatrix(std::index_sequence<sequence...>, T value) noexcept: _data{Vector<rows, T>((static_cast<void>(sequence), value))...} {}
private:
/* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(const RectangularMatrix<cols, rows, U>&) */
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, const RectangularMatrix<cols, rows, U>& matrix) noexcept: _data{Vector<rows, T>(matrix[sequence])...} {}
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(std::index_sequence<sequence...>, const RectangularMatrix<cols, rows, U>& matrix) noexcept: _data{Vector<rows, T>(matrix[sequence])...} {}
/* Implementation for RectangularMatrix<cols, rows, T>::RectangularMatrix(ZeroInitT) and RectangularMatrix<cols, rows, T>::RectangularMatrix(NoInitT) */
/* MSVC 2015 can't handle {} here */
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(Implementation::Sequence<sequence...>, U) noexcept: _data{Vector<rows, T>((static_cast<void>(sequence), U{typename U::Init{}}))...} {}
template<class U, std::size_t ...sequence> constexpr explicit RectangularMatrix(std::index_sequence<sequence...>, U) noexcept: _data{Vector<rows, T>((static_cast<void>(sequence), U{typename U::Init{}}))...} {}
template<std::size_t ...sequence> constexpr RectangularMatrix<cols, rows, T> flippedColsInternal(Implementation::Sequence<sequence...>) const {
return {(*this)[sequence]...};
template<std::size_t ...sequence> constexpr RectangularMatrix<cols, rows, T> flippedColsInternal(std::index_sequence<sequence...>) const {
return {(*this)[cols - 1 - sequence]...};
}
template<std::size_t ...sequence> constexpr RectangularMatrix<cols, rows, T> flippedRowsInternal(Implementation::Sequence<sequence...>) const {
template<std::size_t ...sequence> constexpr RectangularMatrix<cols, rows, T> flippedRowsInternal(std::index_sequence<sequence...>) const {
return {(*this)[sequence].flipped()...};
}
template<std::size_t ...sequence> constexpr Vector<DiagonalSize, T> diagonalInternal(Implementation::Sequence<sequence...>) const;
template<std::size_t ...sequence> constexpr Vector<DiagonalSize, T> diagonalInternal(std::index_sequence<sequence...>) const;
Vector<rows, T> _data[cols];
};
@ -694,15 +694,15 @@ extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utili
#endif
namespace Implementation {
template<std::size_t rows, std::size_t i, class T, std::size_t ...sequence> constexpr Vector<rows, T> diagonalMatrixColumn2(Implementation::Sequence<sequence...>, const T& number) {
template<std::size_t rows, std::size_t i, class T, std::size_t ...sequence> constexpr Vector<rows, T> diagonalMatrixColumn2(std::index_sequence<sequence...>, const T& number) {
return {(sequence == i ? number : T(0))...};
}
template<std::size_t rows, std::size_t i, class T> constexpr Vector<rows, T> diagonalMatrixColumn(const T& number) {
return diagonalMatrixColumn2<rows, i, T>(typename Implementation::GenerateSequence<rows>::Type(), number);
return diagonalMatrixColumn2<rows, i, T>(std::make_index_sequence<rows>{}, number);
}
}
template<std::size_t cols, std::size_t rows, class T> template<std::size_t ...sequence> constexpr RectangularMatrix<cols, rows, T>::RectangularMatrix(Implementation::Sequence<sequence...>, const Vector<DiagonalSize, T>& diagonal): _data{Implementation::diagonalMatrixColumn<rows, sequence>(sequence < DiagonalSize ? diagonal[sequence] : T{})...} {}
template<std::size_t cols, std::size_t rows, class T> template<std::size_t ...sequence> constexpr RectangularMatrix<cols, rows, T>::RectangularMatrix(std::index_sequence<sequence...>, const Vector<DiagonalSize, T>& diagonal): _data{Implementation::diagonalMatrixColumn<rows, sequence>(sequence < DiagonalSize ? diagonal[sequence] : T{})...} {}
template<std::size_t cols, std::size_t rows, class T> inline Vector<cols, T> RectangularMatrix<cols, rows, T>::row(std::size_t row) const {
Vector<cols, T> out;
@ -748,10 +748,10 @@ template<std::size_t cols, std::size_t rows, class T> inline RectangularMatrix<r
return out;
}
template<std::size_t cols, std::size_t rows, class T> constexpr auto RectangularMatrix<cols, rows, T>::diagonal() const -> Vector<DiagonalSize, T> { return diagonalInternal(typename Implementation::GenerateSequence<DiagonalSize>::Type()); }
template<std::size_t cols, std::size_t rows, class T> constexpr auto RectangularMatrix<cols, rows, T>::diagonal() const -> Vector<DiagonalSize, T> { return diagonalInternal(std::make_index_sequence<DiagonalSize>{}); }
#ifndef DOXYGEN_GENERATING_OUTPUT
template<std::size_t cols, std::size_t rows, class T> template<std::size_t ...sequence> constexpr auto RectangularMatrix<cols, rows, T>::diagonalInternal(Implementation::Sequence<sequence...>) const -> Vector<DiagonalSize, T> {
template<std::size_t cols, std::size_t rows, class T> template<std::size_t ...sequence> constexpr auto RectangularMatrix<cols, rows, T>::diagonalInternal(std::index_sequence<sequence...>) const -> Vector<DiagonalSize, T> {
return {(*this)[sequence][sequence]...};
}
#endif

18
src/Magnum/Math/Vector.h

@ -145,7 +145,7 @@ template<std::size_t size, class T> class Vector {
* @see @ref Vector4::pad(const Vector<otherSize, T>&, T, T)
*/
template<std::size_t otherSize> constexpr static Vector<size, T> pad(const Vector<otherSize, T>& a, T value = T(0)) {
return padInternal<otherSize>(typename Implementation::GenerateSequence<size>::Type(), a, value);
return padInternal<otherSize>(std::make_index_sequence<size>{}, a, value);
}
#ifdef MAGNUM_BUILD_DEPRECATED
@ -191,7 +191,7 @@ template<std::size_t size, class T> class Vector {
#ifdef DOXYGEN_GENERATING_OUTPUT
constexpr explicit Vector(T value) noexcept;
#else
template<class U, class V = typename std::enable_if<std::is_same<T, U>::value && size != 1, T>::type> constexpr explicit Vector(U value) noexcept: Vector(typename Implementation::GenerateSequence<size>::Type(), value) {}
template<class U, class V = typename std::enable_if<std::is_same<T, U>::value && size != 1, T>::type> constexpr explicit Vector(U value) noexcept: Vector(std::make_index_sequence<size>{}, value) {}
#endif
/**
@ -205,7 +205,7 @@ template<std::size_t size, class T> class Vector {
* // integral == {1, 2, -15, 7}
* @endcode
*/
template<class U> constexpr explicit Vector(const Vector<size, U>& other) noexcept: Vector(typename Implementation::GenerateSequence<size>::Type(), other) {}
template<class U> constexpr explicit Vector(const Vector<size, U>& other) noexcept: Vector(std::make_index_sequence<size>{}, other) {}
/** @brief Construct vector from external representation */
template<class U, class V = decltype(Implementation::VectorConverter<size, T, U>::from(std::declval<U>()))> constexpr explicit Vector(const U& other) noexcept: Vector(Implementation::VectorConverter<size, T, U>::from(other)) {}
@ -537,7 +537,7 @@ template<std::size_t size, class T> class Vector {
* @ref RectangularMatrix::flippedRows()
*/
constexpr Vector<size, T> flipped() const {
return flippedInternal(typename Implementation::GenerateReverseSequence<size>::Type{});
return flippedInternal(std::make_index_sequence<size>{});
}
/**
@ -577,17 +577,17 @@ template<std::size_t size, class T> class Vector {
private:
/* Implementation for Vector<size, T>::Vector(const Vector<size, U>&) */
template<class U, std::size_t ...sequence> constexpr explicit Vector(Implementation::Sequence<sequence...>, const Vector<size, U>& vector) noexcept: _data{T(vector._data[sequence])...} {}
template<class U, std::size_t ...sequence> constexpr explicit Vector(std::index_sequence<sequence...>, const Vector<size, U>& vector) noexcept: _data{T(vector._data[sequence])...} {}
/* Implementation for Vector<size, T>::Vector(U) */
template<std::size_t ...sequence> constexpr explicit Vector(Implementation::Sequence<sequence...>, T value) noexcept: _data{Implementation::repeat(value, sequence)...} {}
template<std::size_t ...sequence> constexpr explicit Vector(std::index_sequence<sequence...>, T value) noexcept: _data{Implementation::repeat(value, sequence)...} {}
template<std::size_t otherSize, std::size_t ...sequence> constexpr static Vector<size, T> padInternal(Implementation::Sequence<sequence...>, const Vector<otherSize, T>& a, T value) {
template<std::size_t otherSize, std::size_t ...sequence> constexpr static Vector<size, T> padInternal(std::index_sequence<sequence...>, const Vector<otherSize, T>& a, T value) {
return {sequence < otherSize ? a[sequence] : value...};
}
template<std::size_t ...sequence> constexpr Vector<size, T> flippedInternal(Implementation::Sequence<sequence...>) const {
return {(*this)[sequence]...};
template<std::size_t ...sequence> constexpr Vector<size, T> flippedInternal(std::index_sequence<sequence...>) const {
return {(*this)[size - 1 - sequence]...};
}
T _data[size];

Loading…
Cancel
Save