SkylineStorage.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Guillaume Saupin <guillaume.saupin@cea.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_SKYLINE_STORAGE_H
00011 #define EIGEN_SKYLINE_STORAGE_H
00012 
00013 namespace Eigen { 
00014 
00021 template<typename Scalar>
00022 class SkylineStorage {
00023     typedef typename NumTraits<Scalar>::Real RealScalar;
00024     typedef SparseIndex Index;
00025 public:
00026 
00027     SkylineStorage()
00028     : m_diag(0),
00029     m_lower(0),
00030     m_upper(0),
00031     m_lowerProfile(0),
00032     m_upperProfile(0),
00033     m_diagSize(0),
00034     m_upperSize(0),
00035     m_lowerSize(0),
00036     m_upperProfileSize(0),
00037     m_lowerProfileSize(0),
00038     m_allocatedSize(0) {
00039     }
00040 
00041     SkylineStorage(const SkylineStorage& other)
00042     : m_diag(0),
00043     m_lower(0),
00044     m_upper(0),
00045     m_lowerProfile(0),
00046     m_upperProfile(0),
00047     m_diagSize(0),
00048     m_upperSize(0),
00049     m_lowerSize(0),
00050     m_upperProfileSize(0),
00051     m_lowerProfileSize(0),
00052     m_allocatedSize(0) {
00053         *this = other;
00054     }
00055 
00056     SkylineStorage & operator=(const SkylineStorage& other) {
00057         resize(other.diagSize(), other.m_upperProfileSize, other.m_lowerProfileSize, other.upperSize(), other.lowerSize());
00058         memcpy(m_diag, other.m_diag, m_diagSize * sizeof (Scalar));
00059         memcpy(m_upper, other.m_upper, other.upperSize() * sizeof (Scalar));
00060         memcpy(m_lower, other.m_lower, other.lowerSize() * sizeof (Scalar));
00061         memcpy(m_upperProfile, other.m_upperProfile, m_upperProfileSize * sizeof (Index));
00062         memcpy(m_lowerProfile, other.m_lowerProfile, m_lowerProfileSize * sizeof (Index));
00063         return *this;
00064     }
00065 
00066     void swap(SkylineStorage& other) {
00067         std::swap(m_diag, other.m_diag);
00068         std::swap(m_upper, other.m_upper);
00069         std::swap(m_lower, other.m_lower);
00070         std::swap(m_upperProfile, other.m_upperProfile);
00071         std::swap(m_lowerProfile, other.m_lowerProfile);
00072         std::swap(m_diagSize, other.m_diagSize);
00073         std::swap(m_upperSize, other.m_upperSize);
00074         std::swap(m_lowerSize, other.m_lowerSize);
00075         std::swap(m_allocatedSize, other.m_allocatedSize);
00076     }
00077 
00078     ~SkylineStorage() {
00079         delete[] m_diag;
00080         delete[] m_upper;
00081         if (m_upper != m_lower)
00082             delete[] m_lower;
00083         delete[] m_upperProfile;
00084         delete[] m_lowerProfile;
00085     }
00086 
00087     void reserve(Index size, Index upperProfileSize, Index lowerProfileSize, Index upperSize, Index lowerSize) {
00088         Index newAllocatedSize = size + upperSize + lowerSize;
00089         if (newAllocatedSize > m_allocatedSize)
00090             reallocate(size, upperProfileSize, lowerProfileSize, upperSize, lowerSize);
00091     }
00092 
00093     void squeeze() {
00094         if (m_allocatedSize > m_diagSize + m_upperSize + m_lowerSize)
00095             reallocate(m_diagSize, m_upperProfileSize, m_lowerProfileSize, m_upperSize, m_lowerSize);
00096     }
00097 
00098     void resize(Index diagSize, Index upperProfileSize, Index lowerProfileSize, Index upperSize, Index lowerSize, float reserveSizeFactor = 0) {
00099         if (m_allocatedSize < diagSize + upperSize + lowerSize)
00100             reallocate(diagSize, upperProfileSize, lowerProfileSize, upperSize + Index(reserveSizeFactor * upperSize), lowerSize + Index(reserveSizeFactor * lowerSize));
00101         m_diagSize = diagSize;
00102         m_upperSize = upperSize;
00103         m_lowerSize = lowerSize;
00104         m_upperProfileSize = upperProfileSize;
00105         m_lowerProfileSize = lowerProfileSize;
00106     }
00107 
00108     inline Index diagSize() const {
00109         return m_diagSize;
00110     }
00111 
00112     inline Index upperSize() const {
00113         return m_upperSize;
00114     }
00115 
00116     inline Index lowerSize() const {
00117         return m_lowerSize;
00118     }
00119 
00120     inline Index upperProfileSize() const {
00121         return m_upperProfileSize;
00122     }
00123 
00124     inline Index lowerProfileSize() const {
00125         return m_lowerProfileSize;
00126     }
00127 
00128     inline Index allocatedSize() const {
00129         return m_allocatedSize;
00130     }
00131 
00132     inline void clear() {
00133         m_diagSize = 0;
00134     }
00135 
00136     inline Scalar& diag(Index i) {
00137         return m_diag[i];
00138     }
00139 
00140     inline const Scalar& diag(Index i) const {
00141         return m_diag[i];
00142     }
00143 
00144     inline Scalar& upper(Index i) {
00145         return m_upper[i];
00146     }
00147 
00148     inline const Scalar& upper(Index i) const {
00149         return m_upper[i];
00150     }
00151 
00152     inline Scalar& lower(Index i) {
00153         return m_lower[i];
00154     }
00155 
00156     inline const Scalar& lower(Index i) const {
00157         return m_lower[i];
00158     }
00159 
00160     inline Index& upperProfile(Index i) {
00161         return m_upperProfile[i];
00162     }
00163 
00164     inline const Index& upperProfile(Index i) const {
00165         return m_upperProfile[i];
00166     }
00167 
00168     inline Index& lowerProfile(Index i) {
00169         return m_lowerProfile[i];
00170     }
00171 
00172     inline const Index& lowerProfile(Index i) const {
00173         return m_lowerProfile[i];
00174     }
00175 
00176     static SkylineStorage Map(Index* upperProfile, Index* lowerProfile, Scalar* diag, Scalar* upper, Scalar* lower, Index size, Index upperSize, Index lowerSize) {
00177         SkylineStorage res;
00178         res.m_upperProfile = upperProfile;
00179         res.m_lowerProfile = lowerProfile;
00180         res.m_diag = diag;
00181         res.m_upper = upper;
00182         res.m_lower = lower;
00183         res.m_allocatedSize = res.m_diagSize = size;
00184         res.m_upperSize = upperSize;
00185         res.m_lowerSize = lowerSize;
00186         return res;
00187     }
00188 
00189     inline void reset() {
00190         memset(m_diag, 0, m_diagSize * sizeof (Scalar));
00191         memset(m_upper, 0, m_upperSize * sizeof (Scalar));
00192         memset(m_lower, 0, m_lowerSize * sizeof (Scalar));
00193         memset(m_upperProfile, 0, m_diagSize * sizeof (Index));
00194         memset(m_lowerProfile, 0, m_diagSize * sizeof (Index));
00195     }
00196 
00197     void prune(Scalar reference, RealScalar epsilon = dummy_precision<RealScalar>()) {
00198         //TODO
00199     }
00200 
00201 protected:
00202 
00203     inline void reallocate(Index diagSize, Index upperProfileSize, Index lowerProfileSize, Index upperSize, Index lowerSize) {
00204 
00205         Scalar* diag = new Scalar[diagSize];
00206         Scalar* upper = new Scalar[upperSize];
00207         Scalar* lower = new Scalar[lowerSize];
00208         Index* upperProfile = new Index[upperProfileSize];
00209         Index* lowerProfile = new Index[lowerProfileSize];
00210 
00211         Index copyDiagSize = (std::min)(diagSize, m_diagSize);
00212         Index copyUpperSize = (std::min)(upperSize, m_upperSize);
00213         Index copyLowerSize = (std::min)(lowerSize, m_lowerSize);
00214         Index copyUpperProfileSize = (std::min)(upperProfileSize, m_upperProfileSize);
00215         Index copyLowerProfileSize = (std::min)(lowerProfileSize, m_lowerProfileSize);
00216 
00217         // copy
00218         memcpy(diag, m_diag, copyDiagSize * sizeof (Scalar));
00219         memcpy(upper, m_upper, copyUpperSize * sizeof (Scalar));
00220         memcpy(lower, m_lower, copyLowerSize * sizeof (Scalar));
00221         memcpy(upperProfile, m_upperProfile, copyUpperProfileSize * sizeof (Index));
00222         memcpy(lowerProfile, m_lowerProfile, copyLowerProfileSize * sizeof (Index));
00223 
00224 
00225 
00226         // delete old stuff
00227         delete[] m_diag;
00228         delete[] m_upper;
00229         delete[] m_lower;
00230         delete[] m_upperProfile;
00231         delete[] m_lowerProfile;
00232         m_diag = diag;
00233         m_upper = upper;
00234         m_lower = lower;
00235         m_upperProfile = upperProfile;
00236         m_lowerProfile = lowerProfile;
00237         m_allocatedSize = diagSize + upperSize + lowerSize;
00238         m_upperSize = upperSize;
00239         m_lowerSize = lowerSize;
00240     }
00241 
00242 public:
00243     Scalar* m_diag;
00244     Scalar* m_upper;
00245     Scalar* m_lower;
00246     Index* m_upperProfile;
00247     Index* m_lowerProfile;
00248     Index m_diagSize;
00249     Index m_upperSize;
00250     Index m_lowerSize;
00251     Index m_upperProfileSize;
00252     Index m_lowerProfileSize;
00253     Index m_allocatedSize;
00254 
00255 };
00256 
00257 } // end namespace Eigen
00258 
00259 #endif // EIGEN_COMPRESSED_STORAGE_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:26:07