Skip to content

Commit

Permalink
ATLAS-282 (Github #44) Copy constructor & assignment operator for atl…
Browse files Browse the repository at this point in the history
…as::vector
  • Loading branch information
pmarguinaud authored and wdeconinck committed May 5, 2020
1 parent 153e914 commit 37faae7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 27 deletions.
19 changes: 15 additions & 4 deletions src/atlas/util/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#pragma once

#include <utility> // std::swap

#include "atlas/library/config.h"
#include "atlas/parallel/omp/copy.h"
#include "atlas/parallel/omp/fill.h"
Expand Down Expand Up @@ -37,10 +39,19 @@ class vector {
assign( size, value );
}

vector( vector&& other ) : data_( other.data_ ), size_( other.size_ ) {
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
vector( const vector& other ) { assign( other.data_, other.data_ + other.size_ ); }

vector( vector&& other ) {
std::swap( data_, other.data_ );
std::swap( size_, other.size_ );
std::swap( capacity_, other.capacity_ );
}

vector& operator=( vector other ) {
std::swap( data_, other.data_ );
std::swap( size_, other.size_ );
std::swap( capacity_, other.capacity_ );
return *this;
}

~vector() {
Expand Down
7 changes: 7 additions & 0 deletions src/tests/array/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ ecbuild_add_test( TARGET atlas_test_array
# ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT}
#)

ecbuild_add_test( TARGET atlas_test_array_vector
SOURCES test_array_vector.cc
LIBS atlas
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT}
CONDITION atlas_HAVE_GRIDTOOLS_STORAGE
)

ecbuild_add_test( TARGET atlas_test_svector
SOURCES test_svector.cc
LIBS atlas
Expand Down
56 changes: 56 additions & 0 deletions src/tests/array/test_array_vector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* (C) Copyright 2013 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#include "atlas/array/Vector.h"
#include "tests/AtlasTestEnvironment.h"

using namespace atlas::array;

namespace atlas {
namespace test {

//-----------------------------------------------------------------------------

CASE( "test_vector" ) {
Vector<int> vec( 3 );

VectorView<int> vec_view = make_host_vector_view( vec );

vec_view[0] = 3;
vec_view[1] = -3;
vec_view[2] = 1;

EXPECT( vec_view.size() == 3 );
EXPECT( vec_view[0] == 3 );
EXPECT( vec_view[1] == -3 );
EXPECT( vec_view[2] == 1 );

vec.resize( 5 );

// TODO invalidate preview views
VectorView<int> vec_viewb = make_host_vector_view( vec );
vec_viewb[3] = 5;
vec_viewb[4] = 6;

EXPECT( vec_viewb[0] == 3 );
EXPECT( vec_viewb[1] == -3 );
EXPECT( vec_viewb[2] == 1 );
EXPECT( vec_viewb[3] == 5 );
EXPECT( vec_viewb[4] == 6 );
}

//-----------------------------------------------------------------------------

} // namespace test
} // namespace atlas

int main( int argc, char** argv ) {
return atlas::test::run( argc, argv );
}
1 change: 0 additions & 1 deletion src/tests/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ ecbuild_add_test( TARGET atlas_test_vector
SOURCES test_vector.cc
LIBS atlas
ENVIRONMENT ${ATLAS_TEST_ENVIRONMENT}
CONDITION atlas_HAVE_GRIDTOOLS_STORAGE
)

ecbuild_add_test( TARGET atlas_test_metadata
Expand Down
70 changes: 48 additions & 22 deletions src/tests/util/test_vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,68 @@
* nor does it submit to any jurisdiction.
*/

#include "atlas/array/Vector.h"
#include "tests/AtlasTestEnvironment.h"
#include <numeric>

using namespace atlas::array;
#include "atlas/library/config.h"
#include "atlas/util/vector.h"
#include "tests/AtlasTestEnvironment.h"

namespace atlas {
namespace test {

//-----------------------------------------------------------------------------
//


template <typename T>
atlas::vector<T> square( const int n ) {
atlas::vector<T> x( n );
for ( int i = 0; i < n; i++ )
x[i] = static_cast<T>( i ) * static_cast<T>( i );

return x;
}

template <typename T>
void pp( const std::string& t, T& v ) {
Log::info() << t << " = " << std::endl;
Log::info() << v.size() << std::endl;
for ( int i = 0; i < v.size(); i++ )
Log::info() << i << " > " << v[i] << std::endl;
}


CASE( "test_vector" ) {
Vector<int> vec( 3 );
const int N = 100;

// Ctor
atlas::vector<double> x( N );

std::iota( std::begin( x ), std::end( x ), 0 );

// Copy ctor
atlas::vector<double> y = x;

EXPECT( x.size() == y.size() );

VectorView<int> vec_view = make_host_vector_view( vec );
for ( int i = 0; i < x.size(); i++ ) {
EXPECT( x[i] == y[i] );
}

vec_view[0] = 3;
vec_view[1] = -3;
vec_view[2] = 1;

EXPECT( vec_view.size() == 3 );
EXPECT( vec_view[0] == 3 );
EXPECT( vec_view[1] == -3 );
EXPECT( vec_view[2] == 1 );
// Assignment operator
auto z = square<int>( 20 );

vec.resize( 5 );
for ( int i = 0; i < z.size(); i++ ) {
EXPECT( z[i] == i * i );
}

// TODO invalidate preview views
VectorView<int> vec_viewb = make_host_vector_view( vec );
vec_viewb[3] = 5;
vec_viewb[4] = 6;
// Assignment operator
x = y;

EXPECT( vec_viewb[0] == 3 );
EXPECT( vec_viewb[1] == -3 );
EXPECT( vec_viewb[2] == 1 );
EXPECT( vec_viewb[3] == 5 );
EXPECT( vec_viewb[4] == 6 );
for ( int i = 0; i < x.size(); i++ ) {
EXPECT( static_cast<int>( x[i] ) == i );
}
}

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 37faae7

Please sign in to comment.