/* * Advanced Simulation Library * * Copyright 2015 Avtech Scientific * * * 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 . * */ /// \file aslVectors.h definition of class АVec #ifndef ASLVECTORSDYNAMICLENGTH_H #define ASLVECTORSDYNAMICLENGTH_H #include "../aslUtilities.h" #include namespace asl { /// class algebraic vector. /// The class is an implementation of a dynamic array with defined algebraic operations template 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 inline explicit AVec(const AVec & a); inline AVec(const AVec & a); template inline const AVec & operator=(const AVec &a); template inline const AVec & operator=(const std::vector &a); inline const AVec & operator=(const AVec &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); }; /// \relates AVec template inline const unsigned int nD(const AVec a); /// \relates AVec template inline AVec makeAVec(T a1); /// \relates AVec template inline AVec makeAVec(T a1, T a2); /// \relates AVec template inline AVec makeAVec(T a1, T a2, T a3); /// \relates AVec template inline AVec makeAVec(T a1, T a2, T a3, T a4); /// \relates AVec template inline std::ostream & operator<<(std::ostream & output, const AVec & a); //---------------- Implementation ---------------- template inline AVec::AVec(): x(new T[1]), size(1) { x[0] =0; } template inline AVec::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 inline AVec::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 inline AVec::~AVec() { delete[] x; } template template inline AVec::AVec(const AVec & a): x(new T[a.getSize()]), size(a.getSize()) { for (unsigned int i(0); i < size; ++i) x[i] = a[i]; } template inline AVec::AVec(const AVec & a): x(new T[a.getSize()]), size(a.getSize()) { for (unsigned int i(0); i < size; ++i) x[i] = a[i]; } template template inline const AVec & AVec::operator=(const AVec & a) { resize(a.getSize()); for (unsigned int i(0); i inline const AVec & AVec::operator=(const AVec & a) { resize(a.getSize()); for (unsigned int i(0); i template inline const AVec & AVec::operator=(const std::vector & a) { resize(a.size()); for (unsigned int i(0); i inline T& AVec::operator[](unsigned int i) { return x[i]; } template inline const T& AVec::operator[](unsigned int i) const { return x[i]; } template inline const unsigned int & AVec::getSize() const { return size; } template inline AVec makeAVec(T a1) { return AVec(1, a1); } template inline AVec makeAVec(T a1, T a2) { AVec v(2); v[0] = a1; v[1] = a2; return v; } template inline AVec makeAVec(T a1, T a2, T a3) { AVec v(3); v[0] = a1; v[1] = a2; v[2] = a3; return v; } template inline AVec makeAVec(T a1, T a2, T a3, T a4) { AVec v(4); v[0] = a1; v[1] = a2; v[2] = a3; v[3] = a4; return v; } template inline std::ostream & operator<<(std::ostream & output, const AVec & a) { for (unsigned int i(0); i inline const unsigned int nD(const AVec a) { return a.getSize(); } template inline void AVec::resize(unsigned int newSize) { if (size != newSize){ delete[] x; x=new T[newSize]; size=newSize; } } } // asl #endif //ASLVECTORSDYNAMICLENGTH_H