10 #ifndef EIGEN_RANDOMSETTER_H 11 #define EIGEN_RANDOMSETTER_H 22 typedef std::map<KeyType,Scalar>
Type;
30 #ifdef EIGEN_UNORDERED_MAP_SUPPORT 47 template<
typename Scalar>
struct StdUnorderedMapTraits
50 typedef std::unordered_map<KeyType,Scalar>
Type;
57 #endif // EIGEN_UNORDERED_MAP_SUPPORT 59 #ifdef _DENSE_HASH_MAP_H_ 64 template<
typename Scalar>
struct GoogleDenseHashMapTraits
67 typedef google::dense_hash_map<KeyType,Scalar>
Type;
73 { map.set_empty_key(k); }
77 #ifdef _SPARSE_HASH_MAP_H_ 82 template<
typename Scalar>
struct GoogleSparseHashMapTraits
85 typedef google::sparse_hash_map<KeyType,Scalar>
Type;
144 template<
typename SparseMatrixType,
145 template <
typename T>
class MapTraits =
146 #if defined _DENSE_HASH_MAP_H_ 147 GoogleDenseHashMapTraits
148 #elif defined _HASH_MAP 153 ,
int OuterPacketBits = 6>
156 typedef typename SparseMatrixType::Scalar
Scalar;
157 typedef typename SparseMatrixType::Index
Index;
164 typedef typename MapTraits<ScalarWrapper>::KeyType
KeyType;
166 static const int OuterPacketMask = (1 << OuterPacketBits) - 1;
168 SwapStorage = 1 - MapTraits<ScalarWrapper>::IsSorted,
170 SetterRowMajor = SwapStorage ? 1-TargetRowMajor : TargetRowMajor
184 const Index outerSize = SwapStorage ? target.innerSize() : target.outerSize();
185 const Index innerSize = SwapStorage ? target.outerSize() : target.innerSize();
186 m_outerPackets = outerSize >> OuterPacketBits;
187 if (outerSize&OuterPacketMask)
189 m_hashmaps =
new HashMapType[m_outerPackets];
191 Index aux = innerSize - 1;
198 KeyType ik = (1<<(OuterPacketBits+m_keyBitsOffset));
199 for (Index k=0; k<m_outerPackets; ++k)
200 MapTraits<ScalarWrapper>::setInvalidKey(m_hashmaps[k],ik);
203 for (Index j=0; j<mp_target->outerSize(); ++j)
204 for (
typename SparseMatrixType::InnerIterator it(*mp_target,j); it; ++it)
205 (*
this)(TargetRowMajor?j:it.index(), TargetRowMajor?it.index():j) = it.value();
211 KeyType keyBitsMask = (1<<m_keyBitsOffset)-1;
214 mp_target->setZero();
215 mp_target->makeCompressed();
216 mp_target->reserve(nonZeros());
217 Index prevOuter = -1;
218 for (Index k=0; k<m_outerPackets; ++k)
220 const Index outerOffset = (1<<OuterPacketBits) * k;
221 typename HashMapType::iterator end = m_hashmaps[k].end();
222 for (
typename HashMapType::iterator it = m_hashmaps[k].begin(); it!=end; ++it)
224 const Index outer = (it->first >> m_keyBitsOffset) + outerOffset;
225 const Index inner = it->first & keyBitsMask;
226 if (prevOuter!=outer)
228 for (Index j=prevOuter+1;j<=outer;++j)
229 mp_target->startVec(j);
232 mp_target->insertBackByOuterInner(outer, inner) = it->second.value;
235 mp_target->finalize();
239 VectorXi positions(mp_target->outerSize());
242 for (Index k=0; k<m_outerPackets; ++k)
244 typename HashMapType::iterator end = m_hashmaps[k].end();
245 for (
typename HashMapType::iterator it = m_hashmaps[k].begin(); it!=end; ++it)
247 const Index outer = it->first & keyBitsMask;
253 for (Index j=0; j<mp_target->outerSize(); ++j)
255 Index tmp = positions[j];
256 mp_target->outerIndexPtr()[j] = count;
257 positions[j] = count;
260 mp_target->makeCompressed();
261 mp_target->outerIndexPtr()[mp_target->outerSize()] = count;
262 mp_target->resizeNonZeros(count);
264 for (Index k=0; k<m_outerPackets; ++k)
266 const Index outerOffset = (1<<OuterPacketBits) * k;
267 typename HashMapType::iterator end = m_hashmaps[k].end();
268 for (
typename HashMapType::iterator it = m_hashmaps[k].begin(); it!=end; ++it)
270 const Index inner = (it->first >> m_keyBitsOffset) + outerOffset;
271 const Index outer = it->first & keyBitsMask;
276 Index posStart = mp_target->outerIndexPtr()[outer];
277 Index i = (positions[outer]++) - 1;
278 while ( (i >= posStart) && (mp_target->innerIndexPtr()[i] > inner) )
280 mp_target->valuePtr()[i+1] = mp_target->valuePtr()[i];
281 mp_target->innerIndexPtr()[i+1] = mp_target->innerIndexPtr()[i];
284 mp_target->innerIndexPtr()[i+1] = inner;
285 mp_target->valuePtr()[i+1] = it->second.value;
293 Scalar& operator() (Index
row, Index
col)
295 const Index outer = SetterRowMajor ? row :
col;
296 const Index inner = SetterRowMajor ? col :
row;
297 const Index outerMajor = outer >> OuterPacketBits;
298 const Index outerMinor = outer & OuterPacketMask;
299 const KeyType key = (
KeyType(outerMinor)<<m_keyBitsOffset) | inner;
300 return m_hashmaps[outerMajor][key].value;
311 for (Index k=0; k<m_outerPackets; ++k)
312 nz += static_cast<Index>(m_hashmaps[k].size());
327 #endif // EIGEN_RANDOMSETTER_H MapTraits< ScalarWrapper >::KeyType KeyType
RandomSetter(SparseMatrixType &target)
SparseMatrixType::Index Index
unsigned char m_keyBitsOffset
MapTraits< ScalarWrapper >::Type HashMapType
static void setInvalidKey(Type &, const KeyType &)
iterative scaling algorithm to equilibrate rows and column norms in matrices
SparseMatrixType::Scalar Scalar
const unsigned int RowMajorBit
SparseMatrixType * mp_target
The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access...
std::map< KeyType, Scalar > Type