MFEM  v3.1
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 
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 
39 class Table
40 {
41 protected:
42 
44  int size;
45 
49  int *I, *J;
50 
51 public:
53  Table() { size = -1; I = J = NULL; }
54 
56  Table(const Table &);
57 
59  explicit Table (int dim, int connections_per_row = 3);
60 
62  Table(int nrows, Array<Connection> &list) : size(-1), I(NULL), J(NULL)
63  { MakeFromList(nrows, list); }
64 
67  Table (int nrows, int *partitioning);
68 
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 
79  void SetSize(int dim, int connections_per_row);
80 
83  void SetDims(int rows, int nnz);
84 
86  inline int Size() const { return size; }
87 
92  inline int Size_of_connections() const { return I[size]; }
93 
97  int operator() (int i, int j) const;
98 
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  void SetIJ(int *newI, int *newJ, int newsize = -1);
113 
119  int Push( int i, int j );
120 
126  void Finalize();
127 
132  void MakeFromList(int nrows, const Array<Connection> &list);
133 
135  int Width() const;
136 
138  void LoseData() { size = -1; I = J = NULL; }
139 
141  void Print(std::ostream & out = std::cout, int width = 4) const;
142  void PrintMatlab(std::ostream & out) const;
143 
144  void Save(std::ostream & out) const;
145  void Copy(Table & copy) const;
146  void Swap(Table & other);
147 
148  void Clear();
149 
150  long MemoryUsage() const;
151 
153  ~Table();
154 };
155 
157 template <> inline void Swap<Table>(Table &a, Table &b)
158 {
159  a.Swap(b);
160 }
161 
163 void Transpose (const Table &A, Table &At, int _ncols_A = -1);
164 Table * Transpose (const Table &A);
165 
167 void Transpose(const Array<int> &A, Table &At, int _ncols_A = -1);
168 
170 void Mult (const Table &A, const Table &B, Table &C);
171 Table * Mult (const Table &A, const Table &B);
172 
173 
178 class STable : public Table
179 {
180 public:
182  STable (int dim, int connections_per_row = 3);
183 
187  int operator() (int i, int j) const;
188 
194  int Push( int i, int j );
195 
197  ~STable() {}
198 };
199 
200 
201 class DSTable
202 {
203 private:
204  class Node
205  {
206  public:
207  Node *Prev;
208  int Column, Index;
209  };
210 
211  int NumRows, NumEntries;
212  Node **Rows;
213 #ifdef MFEM_USE_MEMALLOC
214  MemAlloc <Node, 1024> NodesMem;
215 #endif
216 
217  int Push_(int r, int c);
218  int Index(int r, int c) const;
219 
220 public:
221  DSTable(int nrows);
222  int NumberOfRows() const { return (NumRows); }
223  int NumberOfEntries() const { return (NumEntries); }
224  int Push(int a, int b)
225  { return ((a <= b) ? Push_(a, b) : Push_(b, a)); }
226  int operator()(int a, int b) const
227  { return ((a <= b) ? Index(a, b) : Index(b, a)); }
228  ~DSTable();
229 
231  {
232  private:
233  Node *n;
234  public:
235  RowIterator (const DSTable &t, int r) { n = t.Rows[r]; }
236  int operator!() { return (n != NULL); }
237  void operator++() { n = n->Prev; }
238  int Column() { return (n->Column); }
239  int Index() { return (n->Index); }
240  };
241 };
242 
243 }
244 
245 #endif
Table(int nrows, Array< Connection > &list)
Definition: table.hpp:62
int Push(int i, int j)
Definition: table.cpp:204
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:157
bool operator<(const Connection &rhs) const
Definition: table.hpp:31
void Swap(Table &other)
Definition: table.cpp:366
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:445
void SetDims(int rows, int nnz)
Definition: table.cpp:132
int Push(int a, int b)
Definition: table.hpp:224
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:539
void Save(std::ostream &out) const
Definition: table.cpp:324
void LoseData()
Call this if data has been stolen.
Definition: table.hpp:138
int Size_of_connections() const
Definition: table.hpp:92
RowIterator(const DSTable &t, int r)
Definition: table.hpp:235
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:281
void Print(std::ostream &out=std::cout, int width=4) const
Prints the table to stream out.
Definition: table.cpp:289
int operator()(int i, int j) const
Definition: table.cpp:527
int dim
Definition: ex3.cpp:48
void MakeFromList(int nrows, const Array< Connection > &list)
Definition: table.cpp:260
void Clear()
Definition: table.cpp:340
void AddConnection(int r, int c)
Definition: table.hpp:74
int * I
Definition: table.hpp:49
long MemoryUsage() const
Definition: table.cpp:373
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:385
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:311
STable(int dim, int connections_per_row=3)
Creates table with fixed number of connections.
Definition: table.cpp:523
void Finalize()
Definition: table.cpp:225
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
~Table()
Destroys Table.
Definition: table.cpp:379
void SetIJ(int *newI, int *newJ, int newsize=-1)
Definition: table.cpp:192
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:197
int operator()(int a, int b) const
Definition: table.hpp:226
int NumberOfEntries() const
Definition: table.hpp:223
int * GetI()
Definition: table.hpp:107
int RowSize(int i) const
Definition: table.hpp:102
int NumberOfRows() const
Definition: table.hpp:222
void Copy(Table &copy) const
Definition: table.cpp:348
DSTable(int nrows)
Definition: table.cpp:552
const int * GetJ() const
Definition: table.hpp:110
Table()
Creates an empty table.
Definition: table.hpp:53