From 336bf9126a04d48fa84e3bc09704f12d17bb5d66 Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Sat, 21 Nov 2015 14:00:57 +0200 Subject: [PATCH] Use v.data() instead of &v[0] for C interop When passing std::vector arrays to C code, use C++11 .data() instead of &v[0], since this works also for empty arrays, even with various debug versions of STL. arrayref.h still has an &v[0] protected by an emptiness check, since that is a potentially widely used header. Change-Id: I3a342a0086c7d0f101c9a91227625c062bedfd5e --- src/gromacs/analysisdata/modules/plot.cpp | 2 +- src/gromacs/correlationfunctions/tests/autocorr.cpp | 2 +- src/gromacs/fileio/tests/confio.cpp | 2 +- src/gromacs/math/tests/vectypes.cpp | 4 ++-- src/gromacs/mdlib/tests/shake.cpp | 12 ++++++------ src/gromacs/selection/nbsearch.cpp | 4 ++-- src/gromacs/selection/nbsearch.h | 2 +- src/gromacs/selection/tests/nbsearch.cpp | 4 ++-- src/gromacs/simd/tests/bootstrap_loadstore.cpp | 6 +++--- src/gromacs/trajectoryanalysis/modules/rdf.cpp | 2 +- src/gromacs/trajectoryanalysis/modules/sasa.cpp | 2 +- src/gromacs/trajectoryanalysis/modules/select.cpp | 4 ++-- src/gromacs/trajectoryanalysis/modules/surfacearea.cpp | 4 ++-- src/gromacs/trajectoryanalysis/tests/surfacearea.cpp | 4 ++-- src/gromacs/utility/stringutil.cpp | 2 +- src/gromacs/utility/tests/arrayref.cpp | 4 ++-- src/testutils/mpi-printer.cpp | 2 +- 17 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/gromacs/analysisdata/modules/plot.cpp b/src/gromacs/analysisdata/modules/plot.cpp index f897691880..e24160f28f 100644 --- a/src/gromacs/analysisdata/modules/plot.cpp +++ b/src/gromacs/analysisdata/modules/plot.cpp @@ -367,7 +367,7 @@ AbstractPlotModule::dataStarted(AbstractAnalysisData * /* data */) { legend.push_back(impl_->legend_[i].c_str()); } - xvgr_legend(impl_->fp_, legend.size(), &legend[0], oenv); + xvgr_legend(impl_->fp_, legend.size(), legend.data(), oenv); } } } diff --git a/src/gromacs/correlationfunctions/tests/autocorr.cpp b/src/gromacs/correlationfunctions/tests/autocorr.cpp index 2b2c86dd06..d0be9b987b 100644 --- a/src/gromacs/correlationfunctions/tests/autocorr.cpp +++ b/src/gromacs/correlationfunctions/tests/autocorr.cpp @@ -126,7 +126,7 @@ class AutocorrTest : public ::testing::Test result.push_back(data_->getValue(m, i)); } } - real *ptr = static_cast(&(result[0])); + real *ptr = result.data(); low_do_autocorr(0, 0, 0, nrFrames_, 1, get_acfnout(), &ptr, data_->getDt(), mode, nrRestart, bAverage, bNormalize, diff --git a/src/gromacs/fileio/tests/confio.cpp b/src/gromacs/fileio/tests/confio.cpp index c5a7642dcb..3ca91423cd 100644 --- a/src/gromacs/fileio/tests/confio.cpp +++ b/src/gromacs/fileio/tests/confio.cpp @@ -104,7 +104,7 @@ class StructureIORoundtripTest : public gmx::test::StringTestBase, void writeReferenceFile() { write_sto_conf(referenceFilename_.c_str(), *refTop_->name, - &refTop_->atoms, as_rvec_array(&refX_[0]), NULL, -1, + &refTop_->atoms, as_rvec_array(refX_.data()), NULL, -1, refBox_); } diff --git a/src/gromacs/math/tests/vectypes.cpp b/src/gromacs/math/tests/vectypes.cpp index e1ddb19a3e..94fe43128c 100644 --- a/src/gromacs/math/tests/vectypes.cpp +++ b/src/gromacs/math/tests/vectypes.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2014, by the GROMACS development team, led by + * Copyright (c) 2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -105,7 +105,7 @@ TEST(RVecTest, WorksAs_rvec_Array) std::vector v; v.push_back(RVec(1, 2, 3)); v.push_back(RVec(2, 3, 4)); - const rvec *r = as_rvec_array(&v[0]); + const rvec *r = as_rvec_array(v.data()); EXPECT_EQ(1, r[0][XX]); EXPECT_EQ(2, r[0][YY]); EXPECT_EQ(3, r[0][ZZ]); diff --git a/src/gromacs/mdlib/tests/shake.cpp b/src/gromacs/mdlib/tests/shake.cpp index b36d847f06..97f3a44e3d 100644 --- a/src/gromacs/mdlib/tests/shake.cpp +++ b/src/gromacs/mdlib/tests/shake.cpp @@ -194,12 +194,12 @@ class ShakeTest : public ::testing::Test int numIterations = 0; int numErrors = 0; - cshake(&iatom[0], numConstraints, &numIterations, - ShakeTest::maxNumIterations_, &constrainedDistancesSquared[0], - &finalPositions[0], &initialDisplacements[0], &halfOfReducedMasses[0], - omega_, &inverseMasses[0], - &distanceSquaredTolerances[0], - &lagrangianValues[0], + cshake(iatom.data(), numConstraints, &numIterations, + ShakeTest::maxNumIterations_, constrainedDistancesSquared.data(), + finalPositions.data(), initialDisplacements.data(), + halfOfReducedMasses.data(), omega_, inverseMasses.data(), + distanceSquaredTolerances.data(), + lagrangianValues.data(), &numErrors); std::vector finalDisplacements = computeDisplacements(iatom, finalPositions); diff --git a/src/gromacs/selection/nbsearch.cpp b/src/gromacs/selection/nbsearch.cpp index dcbd1a19b3..c68c9e6a81 100644 --- a/src/gromacs/selection/nbsearch.cpp +++ b/src/gromacs/selection/nbsearch.cpp @@ -927,7 +927,7 @@ void AnalysisNeighborhoodSearchImpl::init( if (bGrid_) { xrefAlloc_.resize(nref_); - xref_ = as_rvec_array(&xrefAlloc_[0]); + xref_ = as_rvec_array(xrefAlloc_.data()); for (int i = 0; i < nref_; ++i) { @@ -940,7 +940,7 @@ void AnalysisNeighborhoodSearchImpl::init( else if (refIndices_ != NULL) { xrefAlloc_.resize(nref_); - xref_ = as_rvec_array(&xrefAlloc_[0]); + xref_ = as_rvec_array(xrefAlloc_.data()); for (int i = 0; i < nref_; ++i) { copy_rvec(positions.x_[refIndices_[i]], xrefAlloc_[i]); diff --git a/src/gromacs/selection/nbsearch.h b/src/gromacs/selection/nbsearch.h index 34c89467ff..cbd3fdc268 100644 --- a/src/gromacs/selection/nbsearch.h +++ b/src/gromacs/selection/nbsearch.h @@ -123,7 +123,7 @@ class AnalysisNeighborhoodPositions * Initializes positions from a vector of position vectors. */ AnalysisNeighborhoodPositions(const std::vector &x) - : count_(x.size()), index_(-1), x_(as_rvec_array(&x[0])), + : count_(x.size()), index_(-1), x_(as_rvec_array(x.data())), exclusionIds_(NULL), indices_(NULL) { } diff --git a/src/gromacs/selection/tests/nbsearch.cpp b/src/gromacs/selection/tests/nbsearch.cpp index 471dae9ab8..a4a2837982 100644 --- a/src/gromacs/selection/tests/nbsearch.cpp +++ b/src/gromacs/selection/tests/nbsearch.cpp @@ -390,9 +390,9 @@ void ExclusionsHelper::generateExclusions() exclsIndex_.push_back(exclsAtoms_.size()); } excls_.nr = exclsIndex_.size(); - excls_.index = &exclsIndex_[0]; + excls_.index = exclsIndex_.data(); excls_.nra = exclsAtoms_.size(); - excls_.a = &exclsAtoms_[0]; + excls_.a = exclsAtoms_.data(); } /******************************************************************** diff --git a/src/gromacs/simd/tests/bootstrap_loadstore.cpp b/src/gromacs/simd/tests/bootstrap_loadstore.cpp index 671f95f93f..46fe0913b9 100644 --- a/src/gromacs/simd/tests/bootstrap_loadstore.cpp +++ b/src/gromacs/simd/tests/bootstrap_loadstore.cpp @@ -106,8 +106,8 @@ simdLoadStoreTester(TSimd simdLoadFn(T* mem), void simdStoreFn(T* mem, TSimd), std::vector src(simdWidth*5); std::vector dst(simdWidth*5); // Make sure we have memory to check both before and after the test pointers - T * pCopySrc = simdAlignFn(&src[0]) + simdWidth + loadOffset; - T * pCopyDst = simdAlignFn(&dst[0]) + simdWidth + storeOffset; + T * pCopySrc = simdAlignFn(src.data()) + simdWidth + loadOffset; + T * pCopyDst = simdAlignFn(dst.data()) + simdWidth + storeOffset; int i; for (i = 0; i < simdWidth*5; i++) @@ -126,7 +126,7 @@ simdLoadStoreTester(TSimd simdLoadFn(T* mem), void simdStoreFn(T* mem, TSimd), for (i = 0; i < simdWidth*5; i++) { EXPECT_EQ(src[i], (T)(1+i)) << "Side effect on source memory, i = " << i; - if (&dst[0]+i < pCopyDst || &dst[0]+i >= pCopyDst+simdWidth) + if (dst.data()+i < pCopyDst || dst.data()+i >= pCopyDst+simdWidth) { EXPECT_EQ(dst[i], (T)(-1-i)) << "Side effect on destination memory, i = " << i; } diff --git a/src/gromacs/trajectoryanalysis/modules/rdf.cpp b/src/gromacs/trajectoryanalysis/modules/rdf.cpp index 841f5f85dc..2026d6378d 100644 --- a/src/gromacs/trajectoryanalysis/modules/rdf.cpp +++ b/src/gromacs/trajectoryanalysis/modules/rdf.cpp @@ -620,7 +620,7 @@ Rdf::finishAnalysis(int /*nframes*/) invBinVolume[i] = 1.0 / binVolume; prevSphereVolume = sphereVolume; } - finalRdf->scaleAllByVector(&invBinVolume[0]); + finalRdf->scaleAllByVector(invBinVolume.data()); if (normalization_ == Normalization_Rdf) { diff --git a/src/gromacs/trajectoryanalysis/modules/sasa.cpp b/src/gromacs/trajectoryanalysis/modules/sasa.cpp index 4fb4b53d80..d8084c82ad 100644 --- a/src/gromacs/trajectoryanalysis/modules/sasa.cpp +++ b/src/gromacs/trajectoryanalysis/modules/sasa.cpp @@ -927,7 +927,7 @@ Sasa::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc, real *area = NULL, *surfacedots = NULL; int nsurfacedots; calculator_.calculate(surfaceSel.coordinates().data(), pbc, - frameData.index_.size(), &frameData.index_[0], flag, + frameData.index_.size(), frameData.index_.data(), flag, &totarea, &totvolume, &area, &surfacedots, &nsurfacedots); // Unpack the atomwise areas into the frameData.atomAreas_ array for easier diff --git a/src/gromacs/trajectoryanalysis/modules/select.cpp b/src/gromacs/trajectoryanalysis/modules/select.cpp index b43d09ff5b..5b742a6f7a 100644 --- a/src/gromacs/trajectoryanalysis/modules/select.cpp +++ b/src/gromacs/trajectoryanalysis/modules/select.cpp @@ -724,7 +724,7 @@ Select::writeOutput() atomIndicesSet.end()); t_trxstatus *status = open_trx(fnPDB_.c_str(), "w"); write_trxframe_indexed(status, &fr, allAtomIndices.size(), - &allAtomIndices[0], NULL); + allAtomIndices.data(), NULL); close_trx(status); break; } @@ -739,7 +739,7 @@ Select::writeOutput() } } t_trxstatus *status = open_trx(fnPDB_.c_str(), "w"); - write_trxframe_indexed(status, &fr, indices.size(), &indices[0], NULL); + write_trxframe_indexed(status, &fr, indices.size(), indices.data(), NULL); close_trx(status); break; } diff --git a/src/gromacs/trajectoryanalysis/modules/surfacearea.cpp b/src/gromacs/trajectoryanalysis/modules/surfacearea.cpp index e5173c8665..afdd05e07c 100644 --- a/src/gromacs/trajectoryanalysis/modules/surfacearea.cpp +++ b/src/gromacs/trajectoryanalysis/modules/surfacearea.cpp @@ -143,7 +143,7 @@ static std::vector ico_dot_arc(int densit) GMX_RELEASE_ASSERT(ndot >= densit, "Inconsistent surface dot formula"); std::vector xus(3*ndot); - const real rh = icosaeder_vertices(&xus[0]); + const real rh = icosaeder_vertices(xus.data()); if (tess > 1) { @@ -266,7 +266,7 @@ static std::vector ico_dot_dod(int densit) GMX_RELEASE_ASSERT(ndot >= densit, "Inconsistent surface dot formula"); std::vector xus(3*ndot); - const real rh = icosaeder_vertices(&xus[0]); + const real rh = icosaeder_vertices(xus.data()); tn = 12; /* square of the edge of an icosaeder */ diff --git a/src/gromacs/trajectoryanalysis/tests/surfacearea.cpp b/src/gromacs/trajectoryanalysis/tests/surfacearea.cpp index 0445a94324..8653f8e5f1 100644 --- a/src/gromacs/trajectoryanalysis/tests/surfacearea.cpp +++ b/src/gromacs/trajectoryanalysis/tests/surfacearea.cpp @@ -159,8 +159,8 @@ class SurfaceAreaTest : public ::testing::Test gmx::SurfaceAreaCalculator calculator; calculator.setDotCount(ndots); calculator.setRadii(radius_); - calculator.calculate(as_rvec_array(&x_[0]), bPBC ? &pbc : NULL, - index_.size(), &index_[0], flags, + calculator.calculate(as_rvec_array(x_.data()), bPBC ? &pbc : NULL, + index_.size(), index_.data(), flags, &area_, &volume_, &atomArea_, &dots_, &dotCount_); }); diff --git a/src/gromacs/utility/stringutil.cpp b/src/gromacs/utility/stringutil.cpp index f019512d1b..cc3b1bfa3b 100644 --- a/src/gromacs/utility/stringutil.cpp +++ b/src/gromacs/utility/stringutil.cpp @@ -160,7 +160,7 @@ std::string formatString(const char *fmt, ...) length *= 2; } dynamicBuf.resize(length); - buf = &dynamicBuf[0]; + buf = dynamicBuf.data(); } } diff --git a/src/gromacs/utility/tests/arrayref.cpp b/src/gromacs/utility/tests/arrayref.cpp index 3df7d0e41f..f50b7cf100 100644 --- a/src/gromacs/utility/tests/arrayref.cpp +++ b/src/gromacs/utility/tests/arrayref.cpp @@ -192,7 +192,7 @@ TYPED_TEST(ArrayRefTest, ConstructFromVectorWorks) DEFINE_ARRAY(a, aSize); std::vector v(a, a + aSize); typename TestFixture::ArrayRefType arrayRef(v); - this->runTests(a, aSize, &v[0], arrayRef); + this->runTests(a, v.size(), v.data(), arrayRef); } TYPED_TEST(ArrayRefTest, MakeFromVectorWorks) @@ -201,7 +201,7 @@ TYPED_TEST(ArrayRefTest, MakeFromVectorWorks) std::vector v(a, a + aSize); typename TestFixture::ArrayRefType arrayRef = TestFixture::ArrayRefType::fromVector(v.begin(), v.end()); - this->runTests(a, aSize, &v[0], arrayRef); + this->runTests(a, v.size(), v.data(), arrayRef); } //! Helper struct for the case actually used in mdrun signalling diff --git a/src/testutils/mpi-printer.cpp b/src/testutils/mpi-printer.cpp index ecbe85cb48..b449182f98 100644 --- a/src/testutils/mpi-printer.cpp +++ b/src/testutils/mpi-printer.cpp @@ -139,7 +139,7 @@ void MPIEventForward::OnTestEnd(const ::testing::TestInfo &test_info) } std::vector bDidRankPass(size_); - MPI_Gather(&localPassed, 1, MPI_INT, &(bDidRankPass[0]), 1, MPI_INT, 0, MPI_COMM_WORLD); + MPI_Gather(&localPassed, 1, MPI_INT, bDidRankPass.data(), 1, MPI_INT, 0, MPI_COMM_WORLD); if (rank_ == 0) { -- 2.11.4.GIT