MFEM  v3.0
 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.googlecode.com.
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 
26 class Table
27 {
28 protected:
29 
31  int size;
32 
36  int *I, *J;
37 
38 public:
40  Table() { size = -1; I = J = NULL; }
41 
43  explicit Table (int dim, int connections_per_row = 3);
44 
47  Table (int nrows, int *partitioning);
48 
50  void MakeI (int nrows);
51  void AddAColumnInRow (int r) { I[r]++; }
52  void AddColumnsInRow (int r, int ncol) { I[r] += ncol; }
53  void MakeJ();
54  void AddConnection (int r, int c) { J[I[r]++] = c; }
55  void AddConnections (int r, const int *c, int nc);
56  void ShiftUpI();
57 
59  void SetSize(int dim, int connections_per_row);
60 
63  void SetDims(int rows, int nnz);
64 
66  inline int Size() const { return size; }
67 
72  inline int Size_of_connections() const { return I[size]; }
73 
77  int operator() (int i, int j) const;
78 
80  void GetRow(int i, Array<int> &row) const;
81 
82  int RowSize(int i) const { return I[i+1]-I[i]; }
83 
84  const int *GetRow(int i) const { return J+I[i]; }
85  int *GetRow(int i) { return J+I[i]; }
86 
87  int *GetI() { return I; };
88  int *GetJ() { return J; };
89  const int *GetI() const { return I; };
90  const int *GetJ() const { return J; };
91 
92  void SetIJ(int *newI, int *newJ, int newsize = -1);
93 
99  int Push( int i, int j );
100 
106  void Finalize();
107 
109  int Width() const;
110 
112  void LoseData() { size = -1; I = J = NULL; }
113 
115  void Print(std::ostream & out = std::cout, int width = 4) const;
116  void PrintMatlab(std::ostream & out) const;
117 
118  void Save(std::ostream & out) const;
119  void Copy(Table & copy) const;
120  void Swap(Table & other);
121 
122  void Clear();
123 
125  ~Table();
126 };
127 
129 void Transpose (const Table &A, Table &At, int _ncols_A = -1);
130 Table * Transpose (const Table &A);
131 
133 void Transpose(const Array<int> &A, Table &At, int _ncols_A = -1);
134 
136 void Mult (const Table &A, const Table &B, Table &C);
137 Table * Mult (const Table &A, const Table &B);
138 
139 
144 class STable : public Table
145 {
146 public:
148  STable (int dim, int connections_per_row = 3);
149 
153  int operator() (int i, int j) const;
154 
160  int Push( int i, int j );
161 
163  ~STable() {}
164 };
165 
166 
167 class DSTable
168 {
169 private:
170  class Node
171  {
172  public:
173  Node *Prev;
174  int Column, Index;
175  };
176 
177  int NumRows, NumEntries;
178  Node **Rows;
179 #ifdef MFEM_USE_MEMALLOC
180  MemAlloc <Node, 1024> NodesMem;
181 #endif
182 
183  int Push_(int r, int c);
184  int Index(int r, int c) const;
185 
186 public:
187  DSTable(int nrows);
188  int NumberOfRows() const { return(NumRows); }
189  int NumberOfEntries() const { return(NumEntries); }
190  int Push(int a, int b)
191  { return((a <= b) ? Push_(a, b) : Push_(b, a)); }
192  int operator()(int a, int b) const
193  { return((a <= b) ? Index(a, b) : Index(b, a)); }
194  ~DSTable();
195 
197  {
198  private:
199  Node *n;
200  public:
201  RowIterator (const DSTable &t, int r) { n = t.Rows[r]; }
202  int operator!() { return(n != NULL); }
203  void operator++() { n = n->Prev; }
204  int Column() { return(n->Column); }
205  int Index() { return(n->Index); }
206  };
207 };
208 
209 }
210 
211 #endif
int Push(int i, int j)
Definition: table.cpp:167
int * GetJ()
Definition: table.hpp:88
void SetSize(int dim, int connections_per_row)
Set the size and the number of connections for the table.
Definition: table.cpp:91
void AddColumnsInRow(int r, int ncol)
Definition: table.hpp:52
int operator()(int i, int j) const
Definition: table.cpp:132
void MakeI(int nrows)
Next 7 methods are used together with the default constructor.
Definition: table.cpp:57
void Swap(Table &other)
Definition: table.cpp:284
void Mult(const Table &A, const Table &B, Table &C)
C = A * B (as boolean matrices)
Definition: table.cpp:351
void SetDims(int rows, int nnz)
Definition: table.cpp:107
int Push(int a, int b)
Definition: table.hpp:190
const int * GetI() const
Definition: table.hpp:89
int * J
Definition: table.hpp:36
void GetRow(int i, Array< int > &row) const
Return row i in array row (the Table must be finalized)
Definition: table.cpp:148
int Push(int i, int j)
Definition: table.cpp:437
void Save(std::ostream &out) const
Definition: table.cpp:253
void LoseData()
Call this if data has been stolen.
Definition: table.hpp:112
int Size_of_connections() const
Definition: table.hpp:72
RowIterator(const DSTable &t, int r)
Definition: table.hpp:201
void AddConnections(int r, const int *c, int nc)
Definition: table.cpp:75
int Width() const
Returns the number of TYPE II elements (after Finalize() is called).
Definition: table.cpp:216
void Print(std::ostream &out=std::cout, int width=4) const
Prints the table to stream out.
Definition: table.cpp:224
int operator()(int i, int j) const
Definition: table.cpp:429
void Clear()
Definition: table.cpp:265
void AddConnection(int r, int c)
Definition: table.hpp:54
int * I
Definition: table.hpp:36
void Transpose(const Table &A, Table &At, int _ncols_A)
Transpose a Table.
Definition: table.cpp:305
int Size() const
Returns the number of TYPE I elements.
Definition: table.hpp:66
void PrintMatlab(std::ostream &out) const
Definition: table.cpp:242
STable(int dim, int connections_per_row=3)
Creates table with fixed number of connections.
Definition: table.cpp:425
void Finalize()
Definition: table.cpp:186
int size
size is the number of TYPE I elements.
Definition: table.hpp:31
void AddAColumnInRow(int r)
Definition: table.hpp:51
int * GetRow(int i)
Definition: table.hpp:85
void ShiftUpI()
Definition: table.cpp:84
~Table()
Destroys Table.
Definition: table.cpp:299
void SetIJ(int *newI, int *newJ, int newsize=-1)
Definition: table.cpp:157
void MakeJ()
Definition: table.cpp:65
const int * GetRow(int i) const
Definition: table.hpp:84
~STable()
Destroys STable.
Definition: table.hpp:163
int operator()(int a, int b) const
Definition: table.hpp:192
int NumberOfEntries() const
Definition: table.hpp:189
int * GetI()
Definition: table.hpp:87
int RowSize(int i) const
Definition: table.hpp:82
int NumberOfRows() const
Definition: table.hpp:188
void Copy(Table &copy) const
Definition: table.cpp:273
DSTable(int nrows)
Definition: table.cpp:445
const int * GetJ() const
Definition: table.hpp:90
Table()
Creates an empty table.
Definition: table.hpp:40