Skip to content

Commit 000840c

Browse files
committed
Added a move constructor and move assignment operator to Tensor and wrote some tests.
1 parent 3a2dc38 commit 000840c

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

unsupported/Eigen/CXX11/src/Tensor/Tensor.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
398398
internal::TensorExecutor<const Assign, DefaultDevice>::run(assign, DefaultDevice());
399399
}
400400

401+
#if EIGEN_HAS_RVALUE_REFERENCES
402+
EIGEN_DEVICE_FUNC
403+
EIGEN_STRONG_INLINE Tensor(Self&& other)
404+
: Tensor()
405+
{
406+
m_storage.swap(other.m_storage);
407+
}
408+
EIGEN_DEVICE_FUNC
409+
EIGEN_STRONG_INLINE Tensor& operator=(Self&& other)
410+
{
411+
m_storage.swap(other.m_storage);
412+
return *this;
413+
}
414+
#endif
415+
401416
EIGEN_DEVICE_FUNC
402417
EIGEN_STRONG_INLINE Tensor& operator=(const Tensor& other)
403418
{

unsupported/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ if(EIGEN_TEST_CXX11)
225225
ei_add_test(cxx11_tensor_ifft)
226226
ei_add_test(cxx11_tensor_scan)
227227
ei_add_test(cxx11_tensor_trace)
228+
ei_add_test(cxx11_tensor_move)
228229

229230
endif()
230231

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// This file is part of Eigen, a lightweight C++ template library
2+
// for linear algebra.
3+
//
4+
// Copyright (C) 2017 Viktor Csomor <[email protected]>
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+
#include "main.h"
11+
12+
#include <Eigen/CXX11/Tensor>
13+
#include <utility>
14+
15+
using Eigen::Tensor;
16+
using Eigen::RowMajor;
17+
18+
static void calc_indices(int i, int& x, int& y, int& z)
19+
{
20+
x = i / 4;
21+
y = (i % 4) / 2;
22+
z = i % 2;
23+
}
24+
25+
static void test_move()
26+
{
27+
int x;
28+
int y;
29+
int z;
30+
31+
Tensor<int,3> tensor1(2, 2, 2);
32+
Tensor<int,3,RowMajor> tensor2(2, 2, 2);
33+
34+
for (int i = 0; i < 8; i++)
35+
{
36+
calc_indices(i, x, y, z);
37+
tensor1(x,y,z) = i;
38+
tensor2(x,y,z) = 2 * i;
39+
}
40+
41+
// Invokes the move constructor.
42+
Tensor<int,3> moved_tensor1 = std::move(tensor1);
43+
Tensor<int,3,RowMajor> moved_tensor2 = std::move(tensor2);
44+
45+
VERIFY_IS_EQUAL(tensor1.size(), 0);
46+
VERIFY_IS_EQUAL(tensor2.size(), 0);
47+
48+
for (int i = 0; i < 8; i++)
49+
{
50+
calc_indices(i, x, y, z);
51+
VERIFY_IS_EQUAL(moved_tensor1(x,y,z), i);
52+
VERIFY_IS_EQUAL(moved_tensor2(x,y,z), 2 * i);
53+
}
54+
55+
Tensor<int,3> moved_tensor3(2,2,2);
56+
Tensor<int,3,RowMajor> moved_tensor4(2,2,2);
57+
58+
moved_tensor3.setZero();
59+
moved_tensor4.setZero();
60+
61+
// Invokes the move assignment operator.
62+
moved_tensor3 = std::move(moved_tensor1);
63+
moved_tensor4 = std::move(moved_tensor2);
64+
65+
VERIFY_IS_EQUAL(moved_tensor1.size(), 8);
66+
VERIFY_IS_EQUAL(moved_tensor2.size(), 8);
67+
68+
for (int i = 0; i < 8; i++)
69+
{
70+
calc_indices(i, x, y, z);
71+
VERIFY_IS_EQUAL(moved_tensor1(x,y,z), 0);
72+
VERIFY_IS_EQUAL(moved_tensor2(x,y,z), 0);
73+
VERIFY_IS_EQUAL(moved_tensor3(x,y,z), i);
74+
VERIFY_IS_EQUAL(moved_tensor4(x,y,z), 2 * i);
75+
}
76+
}
77+
78+
EIGEN_DECLARE_TEST(cxx11_tensor_move)
79+
{
80+
CALL_SUBTEST(test_move());
81+
}

0 commit comments

Comments
 (0)