| 63 | template <typename Scalar, typename StorageIndex> |
| 64 | template <typename VectorType> |
| 65 | Index SparseLUImpl<Scalar,StorageIndex>::expand(VectorType& vec, Index& length, Index nbElts, Index keep_prev, Index& num_expansions) |
| 66 | { |
| 67 | |
| 68 | float alpha = 1.5; // Ratio of the memory increase |
| 69 | Index new_len; // New size of the allocated memory |
| 70 | |
| 71 | if(num_expansions == 0 || keep_prev) |
| 72 | new_len = length ; // First time allocate requested |
| 73 | else |
| 74 | new_len = (std::max)(length+1,Index(alpha * length)); |
| 75 | |
| 76 | VectorType old_vec; // Temporary vector to hold the previous values |
| 77 | if (nbElts > 0 ) |
| 78 | old_vec = vec.segment(0,nbElts); |
| 79 | |
| 80 | //Allocate or expand the current vector |
| 81 | #ifdef EIGEN_EXCEPTIONS |
| 82 | try |
| 83 | #endif |
| 84 | { |
| 85 | vec.resize(new_len); |
| 86 | } |
| 87 | #ifdef EIGEN_EXCEPTIONS |
| 88 | catch(std::bad_alloc& ) |
| 89 | #else |
| 90 | if(!vec.size()) |
| 91 | #endif |
| 92 | { |
| 93 | if (!num_expansions) |
| 94 | { |
| 95 | // First time to allocate from LUMemInit() |
| 96 | // Let LUMemInit() deals with it. |
| 97 | return -1; |
| 98 | } |
| 99 | if (keep_prev) |
| 100 | { |
| 101 | // In this case, the memory length should not not be reduced |
| 102 | return new_len; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | // Reduce the size and increase again |
| 107 | Index tries = 0; // Number of attempts |
| 108 | do |
| 109 | { |
| 110 | alpha = (alpha + 1)/2; |
| 111 | new_len = (std::max)(length+1,Index(alpha * length)); |
| 112 | #ifdef EIGEN_EXCEPTIONS |
| 113 | try |
| 114 | #endif |
| 115 | { |
| 116 | vec.resize(new_len); |
| 117 | } |
| 118 | #ifdef EIGEN_EXCEPTIONS |
| 119 | catch(std::bad_alloc& ) |
| 120 | #else |
| 121 | if (!vec.size()) |
| 122 | #endif |