Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
compressed_sparsity_pattern.h
1 // ---------------------------------------------------------------------
2 // @f$Id: compressed_sparsity_pattern.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 2001 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__compressed_sparsity_pattern_h
18 #define __deal2__compressed_sparsity_pattern_h
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/subscriptor.h>
23 #include <deal.II/lac/exceptions.h>
24 
25 #include <vector>
26 #include <algorithm>
27 
29 
30 template <typename number> class SparseMatrix;
31 
32 
33 //TODO[WB]: Unify implementation with the CompressedSetSparsityPattern since really all that's different is the Line structure in the two classes. We should have a templatized base class that simply gets the particular Line structure from a derived class.
34 
94 {
95 public:
100 
107  typedef std::vector<size_type>::const_iterator row_iterator;
108 
120 
138 
144  CompressedSparsityPattern (const size_type m,
145  const size_type n);
146 
151  CompressedSparsityPattern (const size_type n);
152 
162 
171  void reinit (const size_type m,
172  const size_type n);
173 
183  void compress ();
184 
192  bool empty () const;
193 
200  size_type max_entries_per_row () const;
201 
207  void add (const size_type i,
208  const size_type j);
209 
216  template <typename ForwardIterator>
217  void add_entries (const size_type row,
218  ForwardIterator begin,
219  ForwardIterator end,
220  const bool indices_are_unique_and_sorted = false);
221 
226  bool exists (const size_type i,
227  const size_type j) const;
228 
240  void symmetrize ();
241 
251  void print (std::ostream &out) const;
252 
276  void print_gnuplot (std::ostream &out) const;
277 
283  size_type n_rows () const;
284 
290  size_type n_cols () const;
291 
295  size_type row_length (const size_type row) const;
296 
302  size_type column_number (const size_type row,
303  const size_type index) const;
304 
311  row_iterator row_begin (const size_type row) const;
312 
316  row_iterator row_end (const size_type row) const;
317 
326  size_type bandwidth () const;
327 
333  size_type n_nonzero_elements () const;
334 
350  static
352 
353 private:
358  size_type rows;
359 
364  size_type cols;
365 
423  struct Line
424  {
425  private:
429  static const unsigned int cache_size = 8;
430 
431  public:
438  mutable std::vector<size_type> entries;
439 
444  mutable size_type cache[cache_size];
445 
449  mutable unsigned int cache_entries;
450 
454  Line ();
455 
460  void add (const size_type col_num);
461 
466  template <typename ForwardIterator>
467  void add_entries (ForwardIterator begin,
468  ForwardIterator end,
469  const bool indices_are_sorted);
470 
475  void flush_cache () const;
476  };
477 
478 
484  std::vector<Line> lines;
485 };
486 
488 /*---------------------- Inline functions -----------------------------------*/
489 
490 
491 inline
492 void
494 {
495  // first check whether this entry is
496  // already in the cache. if so, we can
497  // safely return
498  for (unsigned int i=0; i<cache_entries; ++i)
499  if (cache[i] == j)
500  return;
501 
502  // if not, see whether there is still some
503  // space in the cache. if not, then flush
504  // the cache first
505  if (cache_entries == cache_size && cache_entries != 0)
506  flush_cache ();
507 
508  cache[cache_entries] = j;
509  ++cache_entries;
510 }
511 
512 
513 
514 inline
517 {
518  return rows;
519 }
520 
521 
522 
523 inline
526 {
527  return cols;
528 }
529 
530 
531 
532 inline
533 void
535  const size_type j)
536 {
537  Assert (i<rows, ExcIndexRangeType<size_type>(i, 0, rows));
538  Assert (j<cols, ExcIndexRangeType<size_type>(j, 0, cols));
539 
540  lines[i].add (j);
541 }
542 
543 
544 
545 template <typename ForwardIterator>
546 inline
547 void
549  ForwardIterator begin,
550  ForwardIterator end,
551  const bool indices_are_sorted)
552 {
553  Assert (row < rows, ExcIndexRange (row, 0, rows));
554 
555  lines[row].add_entries (begin, end, indices_are_sorted);
556 }
557 
558 
559 
560 inline
562  :
563  cache_entries (0)
564 {}
565 
566 
567 
568 inline
571 {
572  Assert (row < n_rows(), ExcIndexRangeType<size_type> (row, 0, n_rows()));
573 
574  if (lines[row].cache_entries != 0)
575  lines[row].flush_cache ();
576  return lines[row].entries.size();
577 }
578 
579 
580 
581 inline
584  const size_type index) const
585 {
586  Assert (row < n_rows(), ExcIndexRangeType<size_type> (row, 0, n_rows()));
587  Assert (index < lines[row].entries.size(),
588  ExcIndexRangeType<size_type> (index, 0, lines[row].entries.size()));
589 
590  if (lines[row].cache_entries != 0)
591  lines[row].flush_cache ();
592  return lines[row].entries[index];
593 }
594 
595 
596 
597 inline
600 {
601  Assert (row < n_rows(), ExcIndexRangeType<size_type> (row, 0, n_rows()));
602 
603  if (lines[row].cache_entries != 0)
604  lines[row].flush_cache ();
605  return lines[row].entries.begin();
606 }
607 
608 
609 
610 inline
613 {
614  Assert (row < n_rows(), ExcIndexRangeType<size_type> (row, 0, n_rows()));
615  return lines[row].entries.end();
616 }
617 
618 
619 
620 inline
621 bool
623 {
624  return true;
625 }
626 
627 
628 
629 
630 DEAL_II_NAMESPACE_CLOSE
631 
632 #endif
void add_entries(const size_type row, ForwardIterator begin, ForwardIterator end, const bool indices_are_unique_and_sorted=false)
size_type max_entries_per_row() const
size_type bandwidth() const
void print_gnuplot(std::ostream &out) const
void add_entries(ForwardIterator begin, ForwardIterator end, const bool indices_are_sorted)
bool exists(const size_type i, const size_type j) const
unsigned int global_dof_index
Definition: types.h:100
#define Assert(cond, exc)
Definition: exceptions.h:299
CompressedSparsityPattern & operator=(const CompressedSparsityPattern &)
::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
size_type row_length(const size_type row) const
void add(const size_type i, const size_type j)
size_type column_number(const size_type row, const size_type index) const
std::vector< size_type >::const_iterator row_iterator
size_type n_nonzero_elements() const
void reinit(const size_type m, const size_type n)
void print(std::ostream &out) const
row_iterator row_end(const size_type row) const
row_iterator row_begin(const size_type row) const