Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/BTAS/BTAS
Browse files Browse the repository at this point in the history
  • Loading branch information
evaleev committed Jul 28, 2014
2 parents 02675d8 + 2ed7f2c commit 22b5477
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
17 changes: 14 additions & 3 deletions btas/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,13 @@ namespace btas {
operator= (const _Tensor& x)
{
range_ = range_type(x.range().lobound(), x.range().upbound());
array_adaptor<storage_type>::resize(storage_, range_.area());
std::copy(std::begin(x), std::end(x), std::begin(storage_));
//Must leave storage_ untouched until done copying elements of x
//into new_storage, because x could be a TensorView referring to
//this Tensor
storage_type new_storage;
array_adaptor<storage_type>::resize(new_storage, range_.area());
std::copy(std::begin(x), std::end(x), std::begin(new_storage));
std::swap(storage_,new_storage);
return *this;
}

Expand All @@ -214,7 +219,13 @@ namespace btas {
operator= (_Tensor&& x)
{
range_ = range_type(x.range().lobound(), x.range().upbound());
storage_ = x.storage();
//Must leave storage_ untouched until done copying elements of x
//into new_storage, because x could be a TensorView referring to
//this Tensor
storage_type new_storage;
array_adaptor<storage_type>::resize(new_storage, range_.area());
std::copy(std::begin(x), std::end(x), std::begin(new_storage));
std::swap(storage_,new_storage);
return *this;
}

Expand Down
2 changes: 1 addition & 1 deletion btas/util/sequence_adaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace btas {
typedef typename std::add_const<pointer>::type const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef btas::size_type size_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;

typedef pointer iterator;
Expand Down
30 changes: 26 additions & 4 deletions unittest/tensorview_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ using DTensor = Tensor<double>;
using btas::TensorView;
using namespace btas;

static
std::ostream&
operator<<(std::ostream& s, const DTensor& X)
{
for(auto i : X.range()) s << i << " " << X(i) << "\n";
return s;
}

// Set the elements of a Tensor T such that
// T(i,j,k) = 1ijk
// assuming individual dimensions are all less than 10.
Expand All @@ -37,10 +45,14 @@ fillEls(DTensor& T)
TEST_CASE("Tensor View Constructors")
{

DTensor T0(2,3,4);
fillEls(T0);

DTensor T2(3,4);
fillEls(T2);

SECTION("Constructed from Full Tensors")
{
DTensor T0(2,3,4);
fillEls(T0);
TensorView<double> T0v(T0);
CHECK(T0v == T0);
T0v(0,0,0) = 1.0;
Expand Down Expand Up @@ -73,8 +85,6 @@ TEST_CASE("Tensor View Constructors")

SECTION("Constructed from Tensor permute")
{
DTensor T0(2,3,4);
fillEls(T0);
const auto T0_cref = T0;
auto prange0 = permute(T0.range(),{2,1,0});

Expand Down Expand Up @@ -110,6 +120,18 @@ TEST_CASE("Tensor View Constructors")
//CHECK(T0ncvr(0,0,0) == 1.0);

}

SECTION("Tensor Assign from TensorView")
{
//
//This is a regression test for bug #64
//The following code was failing due
//to a faulty implementation of Tensor operator=
//
DTensor pT2;
pT2 = permute(T2,{1,0});
CHECK(pT2(1,0) == T2(0,1));
}

}

0 comments on commit 22b5477

Please sign in to comment.