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