oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE > Class Template Reference

#include <map_matrix.h>

+ Inheritance diagram for oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >:

Public Types

typedef std::map< KEY_TYPE_COL, VALUE_TYPE > InnerMapMixed
 Typedef to keep the code more readable. More...
 
typedef InnerMapMixed::iterator InnerMixedIt
 Typedef to keep the code more readable. More...
 
typedef InnerMapMixed::const_iterator ConstInnerMixedIt
 Typedef to keep the code more readable const version. More...
 
typedef std::map< KEY_TYPE_ROW, std::map< KEY_TYPE_COL, VALUE_TYPE > * > OuterMapMixed
 Typedef to keep the code more readable. More...
 
typedef OuterMapMixed::iterator OuterMixedIt
 Typedef to keep the code more readable. More...
 
typedef OuterMapMixed::const_iterator ConstOuterMixedIt
 Typedef to keep the code more readable const version. More...
 

Public Member Functions

 MapMatrixMixed ()
 Default (empty) constructor. More...
 
void operator= (const MapMatrixMixed &)=delete
 Broken assignment operator. More...
 
 MapMatrixMixed (const MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE > &map_mat)
 Copy constructor. More...
 
void copy_column (const KEY_TYPE_COL &j, std::map< KEY_TYPE_ROW, VALUE_TYPE > &copied_map)
 Copy a single column into its own map. More...
 
virtual ~MapMatrixMixed ()
 Destructor. More...
 
void clear ()
 Wipe all entries. More...
 
VALUE_TYPE & operator() (const KEY_TYPE_ROW &i, const KEY_TYPE_COL &j)
 
VALUE_TYPE get (const KEY_TYPE_ROW &i, const KEY_TYPE_COL &j) const
 
void output (std::ostream &outfile)
 
unsigned long nnz ()
 Work out number of non-‘zero’ entries. More...
 
unsigned long nnz () const
 Work out number of non-‘zero’ entries, const version. More...
 
unsigned long size ()
 Work out total number of entries. More...
 
unsigned long size () const
 Work out total number of entries const version. More...
 

Protected Member Functions

VALUE_TYPE * entry_pt (const KEY_TYPE_ROW &i, const KEY_TYPE_COL &j)
 Return pointer to entry. More...
 

Protected Attributes

std::map< KEY_TYPE_ROW, std::map< KEY_TYPE_COL, VALUE_TYPE > * > Row_pt
 

Detailed Description

template<class KEY_TYPE_ROW, class KEY_TYPE_COL, class VALUE_TYPE>
class oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >

MapMatrixMixed is a generalised, STL-map-based, sparse(ish) matrix class with mixed indices

The matrix is indexed by indices of type KEY_TYPE_ROW and KEY_TYPE_COL and has entries of type VALUE_TYPE.

Careful: If a zero entry is referenced then it is created in memory. Therefore this isn't really a practical sparse matrix scheme. Do not loop over ‘all’ possible indices as even looking at them will inflate the matrix until it occupies as much space as a full one – use (modification of) output routine to retrieve all nonzero entries.

However, this is not a serious restriction, as the main purpose of this class is to allow non-integer indices.

Example of usage:

// Assume we have a Vector of pointers to objects:
Vector<Rubbish*> object_pt;
[...]
// Number of objects
int nentry=object_pt.size();
// Use the pointers to the objects and associated integers as indices
// in a MapMatrixMixed whose entries are of type int
MapMatrixMixed<Rubbish*,int,int> like_a_matrix;
for (int i=1;i<nentry;i++)
{
for (int j=1;j<nentry;j++)
{
int number=100*i+j;
like_a_matrix(object_pt[i],j)=number;
}
}
oomph_info << "Matrix has nnz() " << like_a_matrix.nnz() <<
" and size() " << like_a_matrix.size() << std::endl;
oomph_info << "\n\n\n Here are the nonzero entries: i, j, a(i,j)\n";
like_a_matrix.output(oomph_info);
// Can be used like a normal matrix:
like_a_matrix(object_pt[1],20)+=13;
like_a_matrix(object_pt[1],1)+=13;
oomph_info << "\n\n\n Here are the nonzero entries: i, j, a(i,j)\n";
like_a_matrix.output(oomph_info);
int i
Definition: BiCGSTAB_step_by_step.cpp:9
OomphInfo oomph_info
Definition: oomph_definitions.cc:319
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

Member Typedef Documentation

◆ ConstInnerMixedIt

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
typedef InnerMapMixed::const_iterator oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::ConstInnerMixedIt

Typedef to keep the code more readable const version.

◆ ConstOuterMixedIt

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
typedef OuterMapMixed::const_iterator oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::ConstOuterMixedIt

Typedef to keep the code more readable const version.

◆ InnerMapMixed

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
typedef std::map<KEY_TYPE_COL, VALUE_TYPE> oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::InnerMapMixed

Typedef to keep the code more readable.

◆ InnerMixedIt

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
typedef InnerMapMixed::iterator oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::InnerMixedIt

Typedef to keep the code more readable.

◆ OuterMapMixed

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
typedef std::map<KEY_TYPE_ROW, std::map<KEY_TYPE_COL, VALUE_TYPE>*> oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::OuterMapMixed

Typedef to keep the code more readable.

◆ OuterMixedIt

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
typedef OuterMapMixed::iterator oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::OuterMixedIt

Typedef to keep the code more readable.

Constructor & Destructor Documentation

◆ MapMatrixMixed() [1/2]

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::MapMatrixMixed ( )
inline

Default (empty) constructor.

112 {};

◆ MapMatrixMixed() [2/2]

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::MapMatrixMixed ( const MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE > &  map_mat)
inline

Copy constructor.

140  {
141  // Step through the row pointers
142  for (ConstOuterMixedIt it = map_mat.Row_pt.begin();
143  it != map_mat.Row_pt.end();
144  it++)
145  {
146  // Is the row pointer nonzero, i.e. are there any entries in this row?
147  if (it->second != 0)
148  {
149  // Identify the map that holds the entries in this row:
150  InnerMapMixed inner_map = *(it->second);
151 
152  // Loop over entries in the row
153  for (ConstInnerMixedIt it2 = inner_map.begin();
154  it2 != inner_map.end();
155  it2++)
156  {
157  // If the entry is nonzero: Copy
158  if (it2->second != 0)
159  {
160  // key1, key2, value
161  (*this)(it->first, it2->first) = it2->second;
162  }
163  }
164  }
165  }
166  }
InnerMapMixed::const_iterator ConstInnerMixedIt
Typedef to keep the code more readable const version.
Definition: map_matrix.h:124
std::map< KEY_TYPE_COL, VALUE_TYPE > InnerMapMixed
Typedef to keep the code more readable.
Definition: map_matrix.h:118
OuterMapMixed::const_iterator ConstOuterMixedIt
Typedef to keep the code more readable const version.
Definition: map_matrix.h:134

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ ~MapMatrixMixed()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
virtual oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::~MapMatrixMixed ( )
inlinevirtual

Destructor.

195  {
196  // Step through the pointers to row maps
197  for (OuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
198  {
199  // Is the row pointer nonzero, i.e. are there any entries in this row?
200  if (it->second != 0)
201  {
202  // it->second is the stored object
203  delete it->second;
204  }
205  }
206  }
std::map< KEY_TYPE_ROW, std::map< KEY_TYPE_COL, VALUE_TYPE > * > Row_pt
Definition: map_matrix.h:432
OuterMapMixed::iterator OuterMixedIt
Typedef to keep the code more readable.
Definition: map_matrix.h:131

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

Member Function Documentation

◆ clear()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
void oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::clear ( )
inline

Wipe all entries.

210  {
211  // Step through the pointers to row maps
212  for (OuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
213  {
214  // Is the row pointer nonzero, i.e. are there any entries in this row?
215  if (it->second != 0)
216  {
217  // it->second is the stored object: a map which can be cleared!
218  it->second->clear();
219  }
220  }
221  }

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ copy_column()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
void oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::copy_column ( const KEY_TYPE_COL &  j,
std::map< KEY_TYPE_ROW, VALUE_TYPE > &  copied_map 
)
inline

Copy a single column into its own map.

172  {
173  // Step through the row pointers
174  for (OuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
175  {
176  // Is the row pointer nonzero, i.e. are there any entries in this row?
177  if (it->second != 0)
178  {
179  // Identify the map that holds the entries in this row:
180  InnerMapMixed inner_map = *(it->second);
181  // If the desired column of the inner map is non-zero
182  if (inner_map[j] != 0)
183  {
184  // Set the value of the copied map to be the desired column of the
185  // inner map
186  copied_map[it->first] = inner_map[j];
187  }
188  }
189  }
190  }

References j, and oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ entry_pt()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
VALUE_TYPE* oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::entry_pt ( const KEY_TYPE_ROW &  i,
const KEY_TYPE_COL &  j 
)
inlineprotected

Return pointer to entry.

413  {
414  // There's not a single entry in this row: Entry must be zero.
415  if (Row_pt[i] == 0)
416  {
417  // Create row and entry in row and set the value to zero
418  Row_pt[i] = new std::map<KEY_TYPE_COL, VALUE_TYPE>;
419  (*Row_pt[i])[j] = VALUE_TYPE(0);
420  return &(*Row_pt[i])[j];
421  }
422  // Simply return pointer to existing entry
423  else
424  {
425  return &(*Row_pt[i])[j];
426  }
427  }

References i, j, and oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

Referenced by oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::operator()().

◆ get()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
VALUE_TYPE oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::get ( const KEY_TYPE_ROW &  i,
const KEY_TYPE_COL &  j 
) const
inline

Get an element corresponding to the key (i,j) Searches the container for an element with a key equivalent to (i,j) and returns the element if found, otherwise the default 0 value for the value type is returned. The container is not modified.

237  {
238  if (Row_pt.count(i) > 0)
239  {
240  // Get the pointer to the row and check the j key
241  InnerMapMixed* inner_map_mixed_pt = Row_pt.find(i)->second;
242  if (inner_map_mixed_pt->count(j) > 0)
243  {
244  return inner_map_mixed_pt->find(j)->second;
245  }
246  else
247  {
248  return VALUE_TYPE(0);
249  }
250  }
251  else
252  {
253  // The key does not exist, return VALUE_TYPE(0)
254  return VALUE_TYPE(0);
255  }
256  }

References i, j, and oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ nnz() [1/2]

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
unsigned long oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::nnz ( )
inline

Work out number of non-‘zero’ entries.

296  {
297  // Initialise counter for # of nonzero entries
298  unsigned long count = 0;
299 
300  // Step through the row pointers
301  for (OuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
302  {
303  // Is the row pointer nonzero, i.e. are there any entries in this row?
304  if (it->second != 0)
305  {
306  // Identify the map that holds the entries in this row:
307  InnerMapMixed inner_map = *(it->second);
308 
309  // Loop over entries in the row
310  for (InnerMixedIt it2 = inner_map.begin(); it2 != inner_map.end();
311  it2++)
312  {
313  // If the entry is nonzero: Increment counter
314  if (it2->second != 0)
315  {
316  count++;
317  }
318  }
319  }
320  }
321  return count;
322  }
InnerMapMixed::iterator InnerMixedIt
Typedef to keep the code more readable.
Definition: map_matrix.h:121

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ nnz() [2/2]

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
unsigned long oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::nnz ( ) const
inline

Work out number of non-‘zero’ entries, const version.

326  {
327  // Initialise counter for # of nonzero entries
328  unsigned long count = 0;
329 
330  // Step through the row pointers
331  for (ConstOuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
332  {
333  // Is the row pointer nonzero, i.e. are there any entries in this row?
334  if (it->second != 0)
335  {
336  // Identify the map that holds the entries in this row:
337  InnerMapMixed inner_map = *(it->second);
338 
339  // Loop over entries in the row
340  for (ConstInnerMixedIt it2 = inner_map.begin();
341  it2 != inner_map.end();
342  it2++)
343  {
344  // If the entry is nonzero: Increment counter
345  if (it2->second != 0)
346  {
347  count++;
348  }
349  }
350  }
351  }
352  return count;
353  }

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ operator()()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
VALUE_TYPE& oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::operator() ( const KEY_TYPE_ROW &  i,
const KEY_TYPE_COL &  j 
)
inline

Return (reference to) entry. Careful: If the entry does not exist then it is created and set to zero

228  {
229  return *entry_pt(i, j);
230  }
VALUE_TYPE * entry_pt(const KEY_TYPE_ROW &i, const KEY_TYPE_COL &j)
Return pointer to entry.
Definition: map_matrix.h:412

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::entry_pt(), i, and j.

◆ operator=()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
void oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::operator= ( const MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE > &  )
delete

Broken assignment operator.

◆ output()

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
void oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::output ( std::ostream &  outfile)
inline

Dump all non-‘zero’ entries to file. Output is in the format ‘i’, ‘j’, ‘entry[i][j]’

263  {
264  // NOTE:
265  //------
266  // map.first = key
267  // map.second = value
268 
269  // Step through the row pointers
270  for (OuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
271  {
272  // Is the row pointer nonzero, i.e. are there any entries in this row?
273  if (it->second != 0)
274  {
275  // Identify the map that holds the entries in this row:
276  InnerMapMixed inner_map = *(it->second);
277 
278  // Loop over entries in the row
279  for (InnerMixedIt it2 = inner_map.begin(); it2 != inner_map.end();
280  it2++)
281  {
282  // If the entry is nonzero: Doc
283  if (it2->second != 0)
284  {
285  // Output key1, key2, value
286  outfile << it->first << " " << it2->first << " " << it2->second
287  << std::endl;
288  }
289  }
290  }
291  }
292  }

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ size() [1/2]

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
unsigned long oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::size ( )
inline

Work out total number of entries.

358  {
359  // Initialise counter for # of nonzero entries
360  unsigned long count = 0;
361 
362  // Step through the row pointers
363  for (OuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
364  {
365  // Is the row pointer nonzero, i.e. are there any entries in this row?
366  if (it->second != 0)
367  {
368  // Identify the map that holds the entries in this row:
369  InnerMapMixed inner_map = *(it->second);
370 
371  // Loop over all (!) entries in the row (incl. zero ones!)
372  for (InnerMixedIt it2 = inner_map.begin(); it2 != inner_map.end();
373  it2++)
374  {
375  count++;
376  }
377  }
378  }
379  return count;
380  }

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

◆ size() [2/2]

template<class KEY_TYPE_ROW , class KEY_TYPE_COL , class VALUE_TYPE >
unsigned long oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::size ( ) const
inline

Work out total number of entries const version.

384  {
385  // Initialise counter for # of nonzero entries
386  unsigned long count = 0;
387 
388  // Step through the row pointers
389  for (ConstOuterMixedIt it = Row_pt.begin(); it != Row_pt.end(); it++)
390  {
391  // Is the row pointer nonzero, i.e. are there any entries in this row?
392  if (it->second != 0)
393  {
394  // Identify the map that holds the entries in this row:
395  InnerMapMixed inner_map = *(it->second);
396 
397  // Loop over all (!) entries in the row (incl. zero ones!)
398  for (ConstInnerMixedIt it2 = inner_map.begin();
399  it2 != inner_map.end();
400  it2++)
401  {
402  count++;
403  }
404  }
405  }
406  return count;
407  }

References oomph::MapMatrixMixed< KEY_TYPE_ROW, KEY_TYPE_COL, VALUE_TYPE >::Row_pt.

Member Data Documentation

◆ Row_pt


The documentation for this class was generated from the following file: