aslVectorsDynamicLength.h
Go to the documentation of this file.
/*
* Advanced Simulation Library <http://asl.org.il>
*
* Copyright 2015 Avtech Scientific <http://avtechscientific.com>
*
*
* This file is part of Advanced Simulation Library (ASL).
*
* ASL is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* ASL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ASL. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ASLVECTORSDYNAMICLENGTH_H
#define ASLVECTORSDYNAMICLENGTH_H
#include "../aslUtilities.h"
#include <cmath>
namespace asl
{
template <typename T = double> class AVec
{
private:
T* x;
unsigned int size;
public:
typedef T Type;
inline AVec();
inline explicit AVec(unsigned int s);
inline AVec(unsigned int s, T a);
inline ~AVec();
template <typename Tv>
inline explicit AVec(const AVec<Tv> & a);
inline AVec(const AVec<T> & a);
template <typename Tv>
inline const AVec<T> & operator=(const AVec<Tv> &a);
template <typename Tv>
inline const AVec<T> & operator=(const std::vector<Tv> &a);
inline const AVec<T> & operator=(const AVec<T> &a);
inline T& operator[](unsigned int i);
inline const T& operator[](unsigned int i) const;
inline const unsigned int & getSize() const;
inline void resize(unsigned int newSize);
};
template <typename T>
inline const unsigned int nD(const AVec<T> a);
template <typename T> inline AVec<T> makeAVec(T a1);
template <typename T> inline AVec<T> makeAVec(T a1, T a2);
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3);
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3, T a4);
template <typename T>
inline std::ostream & operator<<(std::ostream & output, const AVec<T> & a);
//---------------- Implementation ----------------
template <typename T> inline AVec<T>::AVec():
x(new T[1]),
size(1)
{
x[0] =0;
}
template <typename T> inline AVec<T>::AVec(unsigned int s):
x(new T[s]),
size(s)
{
if (size<1) errorMessage("Vector size less than 1");
memset(x, 0, sizeof(T)*s);
}
template <typename T> inline AVec<T>::AVec(unsigned int s, T a):
x(new T[s]),
size(s)
{
if (size<1) errorMessage("Vector size less than 1");
for (unsigned int i(0); i<size; ++i)
x[i] =a;
}
template <typename T> inline AVec<T>::~AVec()
{
delete[] x;
}
template <typename T> template<typename Tv> inline AVec<T>::AVec(const AVec<Tv> & a):
x(new T[a.getSize()]),
size(a.getSize())
{
for (unsigned int i(0); i < size; ++i)
x[i] = a[i];
}
template <typename T> inline AVec<T>::AVec(const AVec<T> & a):
x(new T[a.getSize()]),
size(a.getSize())
{
for (unsigned int i(0); i < size; ++i)
x[i] = a[i];
}
template <typename T> template<typename Tv>
inline const AVec<T> & AVec<T>::operator=(const AVec<Tv> & a)
{
resize(a.getSize());
for (unsigned int i(0); i<size; ++i)
x[i] =a[i];
return *this;
}
template <typename T>
inline const AVec<T> & AVec<T>::operator=(const AVec<T> & a)
{
resize(a.getSize());
for (unsigned int i(0); i<size; ++i)
x[i] =a[i];
return *this;
}
template <typename T> template<typename Tv>
inline const AVec<T> & AVec<T>::operator=(const std::vector<Tv> & a)
{
resize(a.size());
for (unsigned int i(0); i<size; ++i)
x[i] =a[i];
return *this;
}
template <typename T>
inline T& AVec<T>::operator[](unsigned int i)
{
return x[i];
}
template <typename T>
inline const T& AVec<T>::operator[](unsigned int i) const
{
return x[i];
}
template <typename T>
inline const unsigned int & AVec<T>::getSize() const
{
return size;
}
template <typename T> inline AVec<T> makeAVec(T a1)
{
return AVec<T>(1, a1);
}
template <typename T> inline AVec<T> makeAVec(T a1, T a2)
{
AVec<T> v(2);
v[0] = a1;
v[1] = a2;
return v;
}
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3)
{
AVec<T> v(3);
v[0] = a1;
v[1] = a2;
v[2] = a3;
return v;
}
template <typename T> inline AVec<T> makeAVec(T a1, T a2, T a3, T a4)
{
AVec<T> v(4);
v[0] = a1;
v[1] = a2;
v[2] = a3;
v[3] = a4;
return v;
}
template <typename T> inline std::ostream & operator<<(std::ostream & output,
const AVec<T> & a)
{
for (unsigned int i(0); i<a.getSize(); ++i)
output << a[i]<< " ";
return output;
}
template <typename T>
inline const unsigned int nD(const AVec<T> a)
{
return a.getSize();
}
template <typename T>
inline void AVec<T>::resize(unsigned int newSize)
{
if (size != newSize){
delete[] x;
x=new T[newSize];
size=newSize;
}
}
} // asl
#endif //ASLVECTORSDYNAMICLENGTH_H
asl::AVec
Definition: aslVectorsDynamicLength.h:40
asl::AVec::AVec
AVec(const AVec< T > &a)
Definition: aslVectorsDynamicLength.h:121
asl::AVec::makeAVec
AVec< T > makeAVec(T a1, T a2, T a3)
Definition: aslVectorsDynamicLength.h:189
asl::AVec::operator[]
const T & operator[](unsigned int i) const
Definition: aslVectorsDynamicLength.h:165
asl::AVec::makeAVec
AVec< T > makeAVec(T a1, T a2)
Definition: aslVectorsDynamicLength.h:181
asl::AVec::AVec
AVec(unsigned int s)
Definition: aslVectorsDynamicLength.h:91
asl::AVec::makeAVec
AVec< T > makeAVec(T a1, T a2, T a3, T a4)
Definition: aslVectorsDynamicLength.h:198
asl::AVec::nD
const unsigned int nD(const AVec< T > a)
Definition: aslVectorsDynamicLength.h:217
asl::AVec::operator[]
T & operator[](unsigned int i)
Definition: aslVectorsDynamicLength.h:159
asl::AVec::~AVec
~AVec()
Definition: aslVectorsDynamicLength.h:108
asl::AVec::AVec
AVec(const AVec< Tv > &a)
Definition: aslVectorsDynamicLength.h:113
asl::AVec::AVec
AVec(unsigned int s, T a)
Definition: aslVectorsDynamicLength.h:99
asl::AVec::AVec
AVec()
Definition: aslVectorsDynamicLength.h:84
asl::AVec::operator=
const AVec< T > & operator=(const std::vector< Tv > &a)
Definition: aslVectorsDynamicLength.h:149
asl::AVec::operator=
const AVec< T > & operator=(const AVec< T > &a)
Definition: aslVectorsDynamicLength.h:140
asl::AVec::getSize
const unsigned int & getSize() const
Definition: aslVectorsDynamicLength.h:171
asl::AVec::operator<<
std::ostream & operator<<(std::ostream &output, const AVec< T > &a)
Definition: aslVectorsDynamicLength.h:208
asl::AVec::operator=
const AVec< T > & operator=(const AVec< Tv > &a)
Definition: aslVectorsDynamicLength.h:131
asl::AVec::makeAVec
AVec< T > makeAVec(T a1)
Definition: aslVectorsDynamicLength.h:176
asl::AVec::Type
T Type
Definition: aslVectorsDynamicLength.h:46
asl::AVec::resize
void resize(unsigned int newSize)
Definition: aslVectorsDynamicLength.h:223
asl::nD
const unsigned int nD(const Block &b)
Definition: aslBlocks.h:220
asl::errorMessage
void errorMessage(cl_int status, const char *errorMessage)
Prints errorMessage and exits depending on the status.
asl
Advanced Simulation Library.
Definition: aslDataInc.h:31
asl::makeAVec
AVec< T > makeAVec(T a1)
Definition: aslVectorsDynamicLength.h:176
asl::operator<<
std::ostream & operator<<(std::ostream &output, const std::vector< T > &vector)
Prints elements of the vector separated by space.
Definition: aslUtilities.h:173