MFEM  v3.2
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 
113  void SortRows();
114 
115  void SetIJ(int *newI, int *newJ, int newsize = -1);
116 
122  int Push( int i, int j );
123 
129  void Finalize();
130 
135  void MakeFromList(int nrows, const Array<Connection> &list);
136 
138  int Width() const;
139 
141  void LoseData() { size = -1; I = J = NULL; }
142 
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 Copy(Table & copy) const;
149  void Swap(Table & other);
150 
151  void Clear();
152 
153  long MemoryUsage() const;
154 
156  ~Table();
157 };
158 
160 template <> inline void Swap<Table>(Table &a, Table &b)
161 {
162  a.Swap(b);
163 }
164 
166 void Transpose (const Table &A, Table &At, int _ncols_A = -1);
167 Table * Transpose (const Table &A);
168 
170 void Transpose(const Array<int> &A, Table &At, int _ncols_A = -1);
171 
173 void Mult (const Table &A, const Table &B, Table &C);
174 Table * Mult (const Table &A, const Table &B);
175 
176 
181 class STable : public Table
182 {
183 public:
185  STable (int dim, int connections_per_row = 3);
186 
190  int operator() (int i, int j) const;
191 
197  int Push( int i, int j );
198 
200  ~STable() {}
201 };
202 
203 
204 class DSTable
205 {
206 private:
207  class Node
208  {
209  public:
210  Node *Prev;
211  int Column, Index;
212  };
213 
214  int NumRows, NumEntries;
215  Node **Rows;
216 #ifdef MFEM_USE_MEMALLOC
217  MemAlloc <Node, 1024> NodesMem;
218 #endif
219 
220  int Push_(int r, int c);
221  int Index(int r, int c) const;
222 
223 public:
224  DSTable(int nrows);
225  int NumberOfRows() const { return (NumRows); }
226  int NumberOfEntries() const { return (NumEntries); }
227  int Push(int a, int b)
228  { return ((a <= b) ? Push_(a, b) : Push_(b, a)); }
229  int operator()(int a, int b) const
230  { return ((a <= b) ? Index(a, b) : Index(b, a)); }
231  ~DSTable();
232 
234  {
235  private:
236  Node *n;
237  public:
238  RowIterator (const DSTable &t, int r) { n = t.Rows[r]; }
239  int operator!() { return (n != NULL); }
240  void operator++() { n = n->Prev; }
241  int Column() { return (n->Column); }
242  int Index() { return (n->Index); }
243  };
244 };
245 
246 }
247 
248 #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:160
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:372
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:451
void SetDims(int rows, int nnz)
Definition: table.cpp:132
int Push(int a, int b)
Definition: table.hpp:227
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:545
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:238
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:533
int dim
Definition: ex3.cpp:47
void MakeFromList(int nrows, const Array< Connection > &list)
Definition: table.cpp:264
void Clear()
Definition: table.cpp:346
void AddConnection(int r, int c)
Definition: table.hpp:74
int * I
Definition: table.hpp:49
long MemoryUsage() const
Definition: table.cpp:379
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:391
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:529
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
~Table()
Destroys Table.
Definition: table.cpp:385
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:200
int operator()(int a, int b) const
Definition: table.hpp:229
int NumberOfEntries() const
Definition: table.hpp:226
int * GetI()
Definition: table.hpp:107
int RowSize(int i) const
Definition: table.hpp:102
int NumberOfRows() const
Definition: table.hpp:225
void Copy(Table &copy) const
Definition: table.cpp:354
DSTable(int nrows)
Definition: table.cpp:558
const int * GetJ() const
Definition: table.hpp:110
Table()
Creates an empty table.
Definition: table.hpp:53