From 36d473dfb9e0f0a8bf78c0a58b0955594e38ae43 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 13 May 2021 14:16:05 +0200 Subject: [PATCH] implemented NRVec_from1 (indexing from 1) --- vec.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/vec.h b/vec.h index 3bc34aa..47ba7d6 100644 --- a/vec.h +++ b/vec.h @@ -364,6 +364,60 @@ public: namespace LA { + +/***************************************************************************//** + * implements \c NRVec functionality with indexing from 1 + * all possible constructors have to be given explicitly, other stuff is inherited + * with exception of the operator() which differs + ******************************************************************************/ +template +class NRVec_from1 : public NRVec { +public: + NRVec_from1(): NRVec() {}; + explicit NRVec_from1(const int n): NRVec(n) {}; + NRVec_from1(const NRVec &rhs): NRVec(rhs) {};//!< be able to convert the parent class transparently to this + NRVec_from1(const T &a, const int n): NRVec(a, n) {}; + NRVec_from1(const T *a, const int n): NRVec(a, n) {}; + inline const T& operator[] (const int i) const; + inline T& operator[] (const int i); +}; + +/***************************************************************************//** + * indexing operator giving the element at given position with range checking in + * the DEBUG mode + * @param[in] i position of the required vector element (starting from 0) + * @return reference to the requested element + ******************************************************************************/ +template +inline T& NRVec_from1::operator[](const int i) { +#ifdef DEBUG + if(_LA_count_check && *NRVec::count != 1) laerror("possible use of NRVec[] with count>1 as l-value"); + if(i < 1 || i > NRVec::nn) laerror("out of range"); + if(!NRVec::v) laerror("unallocated NRVec"); +#endif + NOT_GPU(*this); + + return NRVec::v[i-1]; +} + +/***************************************************************************//** + * indexing operator giving the element at given position with range checking in + * the DEBUG mode + * @param[in] i position of the required vector element (starting from 0) + * @return constant reference to the requested element + ******************************************************************************/ +template +inline const T& NRVec_from1::operator[](const int i) const { +#ifdef DEBUG + if(i < 1 || i > NRVec::nn) laerror("out of range"); + if(!NRVec::v) laerror("unallocated NRVec"); +#endif + NOT_GPU(*this); + + return NRVec::v[i-1]; +} + + /***************************************************************************//** * output operator * @param[in,out] s output stream