MFEM  v3.3
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
table.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_TABLE
13 #define MFEM_TABLE
14 
15 // Data types for Table.
16 
17 #include "mem_alloc.hpp"
18 #include "array.hpp"
19 
20 namespace mfem
21 {
22 
23 /// Helper struct for defining a connectivity table, see Table::MakeFromList.
24 struct Connection
25 {
26  int from, to;
27  Connection(int from, int to) : from(from), to(to) {}
28 
29  bool operator== (const Connection &rhs) const
30  { return (from == rhs.from) && (to == rhs.to); }
31  bool operator< (const Connection &rhs) const
32  { return (from == rhs.from) ? (to < rhs.to) : (from < rhs.from); }
33 };
34 
35 
36 /** Data type Table. Table stores the connectivity of elements of TYPE I
37  to elements of TYPE II, for example, it may be Element-To-Face
38  connectivity table, etc. */
39 class Table
40 {
41 protected:
42 
43  /// size is the number of TYPE I elements.
44  int size;
45 
46  /** Arrays for the connectivity information in the CSR storage.
47  I is of size "size+1", J is of size the number of connections
48  between TYPE I to TYPE II elements (actually stored I[size]). */
49  int *I, *J;
50 
51 public:
52  /// Creates an empty table
53  Table() { size = -1; I = J = NULL; }
54 
55  /// Copy constructor
56  Table(const Table &);
57 
58  /// Create a table with an upper limit for the number of connections.
59  explicit Table (int dim, int connections_per_row = 3);
60 
61  /** Create a table from a list of connections, see MakeFromList. */
62  Table(int nrows, Array<Connection> &list) : size(-1), I(NULL), J(NULL)
63  { MakeFromList(nrows, list); }
64 
65  /** Create a table with one entry per row with column indices given
66  by 'partitioning'. */
67  Table (int nrows, int *partitioning);
68 
69  /// Next 7 methods are used together with the default constructor
70  void MakeI (int nrows);
71  void AddAColumnInRow (int r) { I[r]++; }
72  void AddColumnsInRow (int r, int ncol) { I[r] += ncol; }
73  void MakeJ();
74  void AddConnection (int r, int c) { J[I[r]++] = c; }
75  void AddConnections (int r, const int *c, int nc);
76  void ShiftUpI();
77 
78  /// Set the size and the number of connections for the table.
79  void SetSize(int dim, int connections_per_row);
80 
81  /** Set the rows and the number of all connections for the table.
82  Does NOT initialize the whole array I ! (I[0]=0 and I[rows]=nnz only) */
83  void SetDims(int rows, int nnz);
84 
85  /// Returns the number of TYPE I elements.
86  inline int Size() const { return size; }
87 
88  /** Returns the number of connections in the table. If Finalize() is
89  not called, it returns the number of possible connections established
90  by the used constructor. Otherwise, it is exactly the number of
91  established connections before calling Finalize(). */
92  inline int Size_of_connections() const { return I[size]; }
93 
94  /** Returns index of the connection between element i of TYPE I and
95  element j of TYPE II. If there is no connection between element i
96  and element j established in the table, then the return value is -1. */
97  int operator() (int i, int j) const;
98 
99  /// Return row i in array row (the Table must be finalized)
100  void GetRow(int i, Array<int> &row) const;
101 
102  int RowSize(int i) const { return I[i+1]-I[i]; }
103 
104  const int *GetRow(int i) const { return J+I[i]; }
105  int *GetRow(int i) { return J+I[i]; }
106 
107  int *GetI() { return I; }
108  int *GetJ() { return J; }
109  const int *GetI() const { return I; }
110  const int *GetJ() const { return J; }
111 
112  /// @brief Sort the column (TYPE II) indices in each row.
113  void SortRows();
114 
115  void SetIJ(int *newI, int *newJ, int newsize = -1);
116 
117  /** Establish connection between element i and element j in the table.
118  The return value is the index of the connection. It returns -1 if it
119  fails to establish the connection. Possibilities are there is not
120  enough memory on row i to establish connection to j, an attempt to
121  establish new connection after calling Finalize(). */
122  int Push( int i, int j );
123 
124  /** Finalize the table initialization. The function may be called
125  only once, after the table has been initialized, in order to compress
126  array J (by getting rid of -1's in array J). Calling this function
127  will "freeze" the table and function Push will work no more.
128  Note: The table is functional even without calling Finalize(). */
129  void Finalize();
130 
131  /** Create the table from a list of connections {(from, to)}, where 'from'
132  is a TYPE I index and 'to' is a TYPE II index. The list is assumed to be
133  sorted and free of duplicities, i.e., you need to call Array::Sort and
134  Array::Unique before calling this method. */
135  void MakeFromList(int nrows, const Array<Connection> &list);
136 
137  /// Returns the number of TYPE II elements (after Finalize() is called).
138  int Width() const;
139 
140  /// Call this if data has been stolen.
141  void LoseData() { size = -1; I = J = NULL; }
142 
143  /// Prints the table to stream out.
144  void Print(std::ostream & out = std::cout, int width = 4) const;
145  void PrintMatlab(std::ostream & out) const;
146 
147  void Save(std::ostream &out) const;
148  void Load(std::istream &in);
149 
150  void Copy(Table & copy) const;
151  void Swap(Table & other);
152 
153  void Clear();
154 
155  long MemoryUsage() const;
156 
157  /// Destroys Table.
158  ~Table();
159 };
160 
161 /// Specialization of the template function Swap<> for class Table
162 template <> inline void Swap<Table>(Table &a, Table &b)
163 {
164  a.Swap(b);
165 }
166 
167 /// Transpose a Table
168 void Transpose (const Table &A, Table &At, int _ncols_A = -1);
169 Table * Transpose (const Table &A);
170 
171 /// Transpose an Array<int>
172 void Transpose(const Array<int> &A, Table &At, int _ncols_A = -1);
173 
174 /// C = A * B (as boolean matrices)
175 void Mult (const Table &A, const Table &B, Table &C);
176 Table * Mult (const Table &A, const Table &B);
177 
178 
179 /** Data type STable. STable is similar to Table, but it's for symmetric
180  connectivity, i.e. TYPE I is equivalent to TYPE II. In the first
181  dimension we put the elements with smaller index. */
182 
183 class STable : public Table
184 {
185 public:
186  /// Creates table with fixed number of connections.
187  STable (int dim, int connections_per_row = 3);
188 
189  /** Returns index of the connection between element i of TYPE I and
190  element j of TYPE II. If there is no connection between element i
191  and element j established in the table, then the return value is -1. */
192  int operator() (int i, int j) const;
193 
194  /** Establish connection between element i and element j in the table.
195  The return value is the index of the connection. It returns -1 if it
196  fails to establish the connection. Possibilities are there is not
197  enough memory on row i to establish connection to j, an attempt to
198  establish new connection after calling Finalize(). */
199  int Push( int i, int j );
200 
201  /// Destroys STable.
202  ~STable() {}
203 };
204 
205 
206 class DSTable
207 {
208 private:
209  class Node
210  {
211  public:
212  Node *Prev;
213  int Column, Index;
214  };
215 
216  int NumRows, NumEntries;
217  Node **Rows;
218 #ifdef MFEM_USE_MEMALLOC
219  MemAlloc <Node, 1024> NodesMem;
220 #endif
221 
222  int Push_(int r, int c);
223  int Index(int r, int c) const;
224 
225 public:
226  DSTable(int nrows);
227  int NumberOfRows() const { return (NumRows); }
228  int NumberOfEntries() const { return (NumEntries); }
229  int Push(int a, int b)
230  { return ((a <= b) ? Push_(a, b) : Push_(b, a)); }
231  int operator()(int a, int b) const
232  { return ((a <= b) ? Index(a, b) : Index(b, a)); }
233  ~DSTable();
234 
236  {
237  private:
238  Node *n;
239  public:
240  RowIterator (const DSTable &t, int r) { n = t.Rows[r]; }
241  int operator!() { return (n != NULL); }
242  void operator++() { n = n->Prev; }
243  int Column() { return (n->Column); }
244  int Index() { return (n->Index); }
245  };
246 };
247 
248 }
249 
250 #endif
Table(int nrows, Array< Connection > &list)
Definition: table.hpp:62
int Push(int i, int j)
Definition: table.cpp:208
int * GetJ()
Definition: table.hpp:108
void SetSize(int dim, int connections_per_row)
Set the size and the number of connections for the table.
Definition: table.cpp:116
void AddColumnsInRow(int r, int ncol)
Definition: table.hpp:72
int operator()(int i, int j) const
Definition: table.cpp:157
void MakeI(int nrows)
Next 7 methods are used together with the default constructor.
Definition: table.cpp:74
void Swap< Table >(Table &a, Table &b)
Specialization of the template function Swap&lt;&gt; for class Table.
Definition: table.hpp:162
void SortRows()
Sort the column (TYPE II) indices in each row.
Definition: table.cpp:188
bool operator<(const Connection &rhs) const
Definition: table.hpp:31
void Swap(Table &other)
Definition: table.cpp:389
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:468
void SetDims(int rows, int nnz)
Definition: table.cpp:132
int Push(int a, int b)
Definition: table.hpp:229
const int * GetI() const
Definition: table.hpp:109
int * J
Definition: table.hpp:49
void GetRow(int i, Array< int > &row) const
Return row i in array row (the Table must be finalized)
Definition: table.cpp:179
int Push(int i, int j)
Definition: table.cpp:562
void Save(std::ostream &out) const
Definition: table.cpp:330
void LoseData()
Call this if data has been stolen.
Definition: table.hpp:141
int Size_of_connections() const
Definition: table.hpp:92
RowIterator(const DSTable &t, int r)
Definition: table.hpp:240
void AddConnections(int r, const int *c, int nc)
Definition: table.cpp:96
int Width() const
Returns the number of TYPE II elements (after Finalize() is called).
Definition: table.cpp:285
void Print(std::ostream &out=std::cout, int width=4) const
Prints the table to stream out.
Definition: table.cpp:295
int operator()(int i, int j) const
Definition: table.cpp:550
int dim
Definition: ex3.cpp:47
void MakeFromList(int nrows, const Array< Connection > &list)
Definition: table.cpp:264
void Clear()
Definition: table.cpp:363
void AddConnection(int r, int c)
Definition: table.hpp:74
int * I
Definition: table.hpp:49
long MemoryUsage() const
Definition: table.cpp:396
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:408
int Size() const
Returns the number of TYPE I elements.
Definition: table.hpp:86
Connection(int from, int to)
Definition: table.hpp:27
void PrintMatlab(std::ostream &out) const
Definition: table.cpp:317
STable(int dim, int connections_per_row=3)
Creates table with fixed number of connections.
Definition: table.cpp:546
void Finalize()
Definition: table.cpp:229
int size
size is the number of TYPE I elements.
Definition: table.hpp:44
void AddAColumnInRow(int r)
Definition: table.hpp:71
int * GetRow(int i)
Definition: table.hpp:105
Helper struct for defining a connectivity table, see Table::MakeFromList.
Definition: table.hpp:24
void ShiftUpI()
Definition: table.cpp:107
void Load(std::istream &in)
Definition: table.cpp:344
~Table()
Destroys Table.
Definition: table.cpp:402
void SetIJ(int *newI, int *newJ, int newsize=-1)
Definition: table.cpp:196
void MakeJ()
Definition: table.cpp:84
bool operator==(const Connection &rhs) const
Definition: table.hpp:29
const int * GetRow(int i) const
Definition: table.hpp:104
~STable()
Destroys STable.
Definition: table.hpp:202
int operator()(int a, int b) const
Definition: table.hpp:231
int NumberOfEntries() const
Definition: table.hpp:228
int * GetI()
Definition: table.hpp:107
int RowSize(int i) const
Definition: table.hpp:102
int NumberOfRows() const
Definition: table.hpp:227
void Copy(Table &copy) const
Definition: table.cpp:371
DSTable(int nrows)
Definition: table.cpp:575
const int * GetJ() const
Definition: table.hpp:110
Table()
Creates an empty table.
Definition: table.hpp:53