cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

rvalue_types.cpp (5668B)


      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2013 Hauke Heibel <hauke.heibel@gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #define EIGEN_RUNTIME_NO_MALLOC
     11 
     12 #include "main.h"
     13 #if EIGEN_HAS_CXX11
     14 #include "MovableScalar.h"
     15 #endif
     16 #include "SafeScalar.h"
     17 
     18 #include <Eigen/Core>
     19 
     20 using internal::UIntPtr;
     21 
     22 #if EIGEN_HAS_RVALUE_REFERENCES
     23 template <typename MatrixType>
     24 void rvalue_copyassign(const MatrixType& m)
     25 {
     26 
     27   typedef typename internal::traits<MatrixType>::Scalar Scalar;
     28   
     29   // create a temporary which we are about to destroy by moving
     30   MatrixType tmp = m;
     31   UIntPtr src_address = reinterpret_cast<UIntPtr>(tmp.data());
     32   
     33   Eigen::internal::set_is_malloc_allowed(false); // moving from an rvalue reference shall never allocate
     34   // move the temporary to n
     35   MatrixType n = std::move(tmp);
     36   UIntPtr dst_address = reinterpret_cast<UIntPtr>(n.data());
     37   if (MatrixType::RowsAtCompileTime==Dynamic|| MatrixType::ColsAtCompileTime==Dynamic)
     38   {
     39     // verify that we actually moved the guts
     40     VERIFY_IS_EQUAL(src_address, dst_address);
     41     VERIFY_IS_EQUAL(tmp.size(), 0);
     42     VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(tmp.data()), UIntPtr(0));
     43   }
     44 
     45   // verify that the content did not change
     46   Scalar abs_diff = (m-n).array().abs().sum();
     47   VERIFY_IS_EQUAL(abs_diff, Scalar(0));
     48   Eigen::internal::set_is_malloc_allowed(true);
     49 }
     50 template<typename TranspositionsType>
     51 void rvalue_transpositions(Index rows)
     52 {
     53   typedef typename TranspositionsType::IndicesType PermutationVectorType;
     54 
     55   PermutationVectorType vec;
     56   randomPermutationVector(vec, rows);
     57   TranspositionsType t0(vec);
     58 
     59   Eigen::internal::set_is_malloc_allowed(false); // moving from an rvalue reference shall never allocate
     60 
     61   UIntPtr t0_address = reinterpret_cast<UIntPtr>(t0.indices().data());
     62 
     63   // Move constructors:
     64   TranspositionsType t1 = std::move(t0);
     65   UIntPtr t1_address = reinterpret_cast<UIntPtr>(t1.indices().data());
     66   VERIFY_IS_EQUAL(t0_address, t1_address);
     67   // t0 must be de-allocated:
     68   VERIFY_IS_EQUAL(t0.size(), 0);
     69   VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(t0.indices().data()), UIntPtr(0));
     70 
     71 
     72   // Move assignment:
     73   t0 = std::move(t1);
     74   t0_address = reinterpret_cast<UIntPtr>(t0.indices().data());
     75   VERIFY_IS_EQUAL(t0_address, t1_address);
     76   // t1 must be de-allocated:
     77   VERIFY_IS_EQUAL(t1.size(), 0);
     78   VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(t1.indices().data()), UIntPtr(0));
     79 
     80   Eigen::internal::set_is_malloc_allowed(true);
     81 }
     82 
     83 template <typename MatrixType>
     84 void rvalue_move(const MatrixType& m)
     85 {
     86     // lvalue reference is copied
     87     MatrixType b(m);
     88     VERIFY_IS_EQUAL(b, m);
     89 
     90     // lvalue reference is copied
     91     MatrixType c{m};
     92     VERIFY_IS_EQUAL(c, m);
     93 
     94     // lvalue reference is copied
     95     MatrixType d = m;
     96     VERIFY_IS_EQUAL(d, m);
     97 
     98     // rvalue reference is moved - copy constructor.
     99     MatrixType e_src(m);
    100     VERIFY_IS_EQUAL(e_src, m);
    101     MatrixType e_dst(std::move(e_src));
    102     VERIFY_IS_EQUAL(e_dst, m);
    103 
    104     // rvalue reference is moved - copy constructor.
    105     MatrixType f_src(m);
    106     VERIFY_IS_EQUAL(f_src, m);
    107     MatrixType f_dst = std::move(f_src);
    108     VERIFY_IS_EQUAL(f_dst, m);
    109     
    110     // rvalue reference is moved - copy assignment.
    111     MatrixType g_src(m);
    112     VERIFY_IS_EQUAL(g_src, m);
    113     MatrixType g_dst;
    114     g_dst = std::move(g_src);
    115     VERIFY_IS_EQUAL(g_dst, m);
    116 }
    117 #else
    118 template <typename MatrixType>
    119 void rvalue_copyassign(const MatrixType&) {}
    120 template<typename TranspositionsType>
    121 void rvalue_transpositions(Index) {}
    122 template <typename MatrixType>
    123 void rvalue_move(const MatrixType&) {}
    124 #endif
    125 
    126 EIGEN_DECLARE_TEST(rvalue_types)
    127 {
    128   for(int i = 0; i < g_repeat; i++) {
    129     CALL_SUBTEST_1(rvalue_copyassign( MatrixXf::Random(50,50).eval() ));
    130     CALL_SUBTEST_1(rvalue_copyassign( ArrayXXf::Random(50,50).eval() ));
    131 
    132     CALL_SUBTEST_1(rvalue_copyassign( Matrix<float,1,Dynamic>::Random(50).eval() ));
    133     CALL_SUBTEST_1(rvalue_copyassign( Array<float,1,Dynamic>::Random(50).eval() ));
    134 
    135     CALL_SUBTEST_1(rvalue_copyassign( Matrix<float,Dynamic,1>::Random(50).eval() ));
    136     CALL_SUBTEST_1(rvalue_copyassign( Array<float,Dynamic,1>::Random(50).eval() ));
    137 
    138     CALL_SUBTEST_2(rvalue_copyassign( Array<float,2,1>::Random().eval() ));
    139     CALL_SUBTEST_2(rvalue_copyassign( Array<float,3,1>::Random().eval() ));
    140     CALL_SUBTEST_2(rvalue_copyassign( Array<float,4,1>::Random().eval() ));
    141 
    142     CALL_SUBTEST_2(rvalue_copyassign( Array<float,2,2>::Random().eval() ));
    143     CALL_SUBTEST_2(rvalue_copyassign( Array<float,3,3>::Random().eval() ));
    144     CALL_SUBTEST_2(rvalue_copyassign( Array<float,4,4>::Random().eval() ));
    145   
    146     CALL_SUBTEST_3((rvalue_transpositions<PermutationMatrix<Dynamic, Dynamic, int> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
    147     CALL_SUBTEST_3((rvalue_transpositions<PermutationMatrix<Dynamic, Dynamic, Index> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
    148     CALL_SUBTEST_4((rvalue_transpositions<Transpositions<Dynamic, Dynamic, int> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
    149     CALL_SUBTEST_4((rvalue_transpositions<Transpositions<Dynamic, Dynamic, Index> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
    150 
    151 #if EIGEN_HAS_CXX11
    152     CALL_SUBTEST_5(rvalue_move(Eigen::Matrix<MovableScalar<float>,1,3>::Random().eval()));
    153     CALL_SUBTEST_5(rvalue_move(Eigen::Matrix<SafeScalar<float>,1,3>::Random().eval()));
    154     CALL_SUBTEST_5(rvalue_move(Eigen::Matrix<SafeScalar<float>,Eigen::Dynamic,Eigen::Dynamic>::Random(1,3).eval()));
    155 #endif
    156   }
    157 }