MFEM  v3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
stable3d.cpp
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 
13 #include <iostream>
14 #include "error.hpp"
15 #include "stable3d.hpp"
16 
17 using namespace std;
18 
19 namespace mfem
20 {
21 
22 STable3D::STable3D (int nr)
23 {
24  int i;
25 
26  Size = nr;
27  Rows = new STable3DNode *[nr];
28  for (i = 0; i < nr; i++)
29  Rows[i] = NULL;
30  NElem = 0;
31 }
32 
33 inline void Sort3 (int &r, int &c, int &f)
34 {
35  int t;
36 
37  if (r > c)
38  if (c > f)
39  {
40  t = r; r = f; f = t; // (r,c,f) --> (f,c,r)
41  }
42  else
43  if (r > f)
44  {
45  t = r; r = c; c = f; f = t; // (r,c,f) --> (c,f,r)
46  }
47  else
48  {
49  t = r; r = c; c = t; // (r,c,f) --> (c,r,f)
50  }
51  else
52  if (c > f)
53  {
54  if (r > f)
55  {
56  t = f; f = c; c = r; r = t; // (r,c,f) --> (f,r,c)
57  }
58  else
59  {
60  t = c; c = f; f = t; // (r,c,f) --> (r,f,c)
61  }
62  }
63 }
64 
65 int STable3D::Push (int r, int c, int f)
66 {
67  STable3DNode *node;
68 
69  MFEM_ASSERT(r != c && c != f && f != r,
70  "STable3D::Push : r = " << r << ", c = " << c << ", f = " << f);
71 
72  Sort3 (r, c, f);
73 
74  for (node = Rows[r]; node != NULL; node = node->Prev)
75  {
76  if (node->Column == c)
77  if (node->Floor == f)
78  return node->Number;
79  }
80 
81 #ifdef MFEM_USE_MEMALLOC
82  node = NodesMem.Alloc ();
83 #else
84  node = new STable3DNode;
85 #endif
86  node->Column = c;
87  node->Floor = f;
88  node->Number = NElem;
89  node->Prev = Rows[r];
90  Rows[r] = node;
91 
92  NElem++;
93  return (NElem-1);
94 }
95 
96 int STable3D::operator() (int r, int c, int f) const
97 {
98  STable3DNode *node;
99 
100  Sort3 (r, c, f);
101 
102  for (node = Rows[r]; node != NULL; node = node->Prev)
103  {
104  if (node->Column == c)
105  if (node->Floor == f)
106  return node->Number;
107  }
108 
109  MFEM_ABORT("(r,c,f) = (" << r << "," << c << "," << f << ")");
110 
111  return 0;
112 }
113 
114 int STable3D::Index (int r, int c, int f) const
115 {
116  STable3DNode *node;
117 
118  Sort3 (r, c, f);
119 
120  for (node = Rows[r]; node != NULL; node = node->Prev)
121  {
122  if (node->Column == c)
123  if (node->Floor == f)
124  return node->Number;
125  }
126 
127  return -1;
128 }
129 
130 int STable3D::Push4 (int r, int c, int f, int t)
131 {
132  MFEM_ASSERT(r != c && r != f && r != t && c != f && c != t && f != t,
133  " r = " << r << ", c = " << c << ", f = " << f << ", t = " << t);
134 
135  int i = 0;
136  int max = r;
137 
138  if (max < c) max = c, i = 1;
139  if (max < f) max = f, i = 2;
140  if (max < t) max = t, i = 3;
141 
142  switch(i) {
143  case 0:
144  return Push (c,f,t);
145  case 1:
146  return Push (r,f,t);
147  case 2:
148  return Push (r,c,t);
149  case 3:
150  return Push (r,c,f);
151  }
152 
153  return -1;
154 }
155 
156 int STable3D::operator() (int r, int c, int f, int t) const
157 {
158  int i = 0;
159  int max = r;
160 
161  if (max < c) max = c, i = 1;
162  if (max < f) max = f, i = 2;
163  if (max < t) max = t, i = 3;
164 
165  switch(i) {
166  case 0:
167  return (*this)(c,f,t);
168  case 1:
169  return (*this)(r,f,t);
170  case 2:
171  return (*this)(r,c,t);
172  case 3:
173  return (*this)(r,c,f);
174  }
175 
176  return -1;
177 }
178 
179 STable3D::~STable3D ()
180 {
181 #ifdef MFEM_USE_MEMALLOC
182  // NodesMem.Clear(); // this is done implicitly
183 #else
184  for (int i = 0; i < Size; i++)
185  {
186  STable3DNode *aux, *node_p = Rows[i];
187  while (node_p != NULL)
188  {
189  aux = node_p;
190  node_p = node_p->Prev;
191  delete aux;
192  }
193  }
194 #endif
195  delete [] Rows;
196 }
197 
198 }
void Sort3(int &r, int &c, int &f)
Definition: stable3d.cpp:33
STable3DNode * Prev
Definition: stable3d.hpp:23