ergo
VectorHierarchicBase.h
Go to the documentation of this file.
00001 /* Ergo, version 3.2, a program for linear scaling electronic structure
00002  * calculations.
00003  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  * 
00018  * Primary academic reference:
00019  * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
00020  * with Linearly Scaling Computational Time and Memory Usage,
00021  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
00022  * J. Chem. Theory Comput. 7, 340 (2011),
00023  * <http://dx.doi.org/10.1021/ct100611z>
00024  * 
00025  * For further information about Ergo, see <http://www.ergoscf.org>.
00026  */
00027 
00038 #ifndef MAT_VECTORHIERARCHICBASE
00039 #define MAT_VECTORHIERARCHICBASE
00040 #include "matInclude.h"
00041 namespace mat{
00048   template<class Treal, class Telement = Treal>
00049     class VectorHierarchicBase {
00050     public:
00051 
00052 #if 1
00053     inline const int& nScalars() const
00054     {return rows.getNScalars();}
00055 #endif
00056     inline const int& n() const   /* Number of elements in Telement matrix */
00057     {return rows.getNBlocks();} 
00058 
00059     inline Telement& operator() /* Returns the element v(ind)        */
00060     (int ind) {
00061       assert(elements);
00062       assert(ind >= 0);
00063       assert(ind < n());
00064       return elements[ind];
00065     }   
00066     inline const Telement& operator() /*Write protected reference returned*/
00067     (int ind) const {
00068       assert(elements);
00069       assert(ind >= 0);
00070       assert(ind < n());
00071       return elements[ind];
00072     }
00073     inline bool is_zero() const {return !elements;}
00074     
00075     inline void resetRows(SizesAndBlocks const & newRows) {
00076       delete[] elements;
00077       elements = 0;
00078       rows = newRows;
00079     }
00080 
00081     protected:
00087     inline bool is_empty() const {
00088       return rows.is_empty();
00089     }
00090 
00091     VectorHierarchicBase()
00092     : elements(0) {}
00093 
00094     explicit VectorHierarchicBase(SizesAndBlocks const & rowsInp)
00095     :elements(0) {}
00096     VectorHierarchicBase
00097     (const VectorHierarchicBase<Treal, Telement>& vec);
00098     VectorHierarchicBase<Treal, Telement>& 
00099     operator=(const VectorHierarchicBase<Treal, Telement>& vec);
00100     virtual ~VectorHierarchicBase();      
00101     
00102     
00103     SizesAndBlocks rows;
00104 
00105     //    const Tperm* perm;
00106     Telement* elements;
00107     //    int cap;             /* The length of the elements array         */
00108     //    int nel;             /* Number of USED elements in the elements  */
00109     /*                 array (can be positive even though content == zero) */
00110 
00111     //    property content; /* content can be one of the properties listed */
00112     /* in the enum type property (matInclude.h) for example:               */
00113     /* zero: An all zero matrix                                            */
00114     /* ful : An ordinary matrix with the values in the elements array      */
00115 
00116 #if 0
00117     inline void assert_alloc() {
00118       if (this->cap < this->nel) {
00119         delete[] this->elements;
00120         this->cap = this->nel;      
00121         this->elements = new Telement[this->cap];
00122         for (int ind = 0; ind < this->cap; ind++)
00123           this->elements[ind] = 0;
00124       }
00125     } 
00126 #endif
00127 
00128 
00129 
00130     private:
00131 
00132   }; /* end class VectorHierarchicBase */
00133 
00134   template<class Treal, class Telement> /* Copy constructor     */
00135     VectorHierarchicBase<Treal, Telement>::
00136     VectorHierarchicBase
00137     (const VectorHierarchicBase<Treal, Telement>& vec) 
00138     : rows(vec.rows) {
00139       if (!vec.is_zero()) {
00140         elements = new Telement[n()];
00141         for (int i = 0; i < n(); i++) 
00142           elements[i] = vec.elements[i];
00143       }
00144     }   
00145   
00146 
00147     template<class Treal, class Telement> /* Assignment operator*/
00148       VectorHierarchicBase<Treal, Telement>& 
00149       VectorHierarchicBase<Treal, Telement>::
00150       operator=(const VectorHierarchicBase<Treal, Telement>& vec) {
00151       if (vec.is_zero()) { /* Condition also matches empty matrices. */
00152         rows = vec.rows;
00153         delete[] elements;
00154         elements = 0;
00155         return *this;
00156       }
00157       if (is_zero() || (n() != vec.n())) {
00158         delete[] elements;
00159         elements = new Telement[vec.n()];
00160       }
00161       rows = vec.rows;
00162       for (int i = 0; i < n(); i++)
00163         elements[i] = vec.elements[i];
00164       return *this;
00165     }
00166 
00167   
00168     template<class Treal, class Telement>
00169       VectorHierarchicBase<Treal, Telement>::
00170       ~VectorHierarchicBase() {
00171       delete[] elements;
00172     }
00173     
00174 } /* end namespace mat */
00175 #endif