Chombo + EB  3.2
STLUtil.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _STLUTIL_H_
12 #define _STLUTIL_H_
13 
14 /**********************************************************************/
15 // to switch between ordered/sorted maps and unordered hash tables,
16 // define this to be anything, and make sure to compile with -std=c++0x
17 /**********************************************************************/
18 
19 // #define STL_UNORDERED_MAP 1
20 
21 #ifndef STL_UNORDERED_MAP
22 #include <map>
23 #else
24 #include <unordered_map>
25 #endif
26 
27 #include "RealVect.H"
28 #include "IntVect.H"
29 #include "MayDay.H"
30 #include "CellEdge.H"
31 
32 using namespace std;
33 
34 #include "NamespaceHeader.H"
35 
36 namespace STLUtil
37 {
38  // returns true if a[0]<b[0];
39  // else if a[0]==b[0], returns true if a[1]<b[1], etc.
40  // allows sorting by each component of an IntVect sequentially
41  // "Strict Weak Ordering" of IntVects
42  //
43  // Yes, this is just like IntVect::lexLT except that
44  // you can specify a direction to compare first
45  // i.e. if you want to sort by y, then z and x,
46  // you can create a comparator via
47  // IVCompareSWO myComparator(1);
48  struct IVCompareSWO
49  {
50  IVCompareSWO(int a_dir) {
51  m_dir = a_dir;
52  if (m_dir<0 || m_dir>(SpaceDim-1))
53  MayDay::Abort("IVCompareSWO: cannot compare IntVects along a direction that doesn't exist");
54  }
55 
57  m_dir = 0; // default to x,y,z order
58  }
59 
60  inline bool operator()(const IntVect& a, const IntVect& b) const
61  {
62  if (a[m_dir%SpaceDim] < b[m_dir%SpaceDim])
63  return true;
64  else if (SpaceDim>1 && \
65  a[ m_dir %SpaceDim] == b[ m_dir %SpaceDim] && \
66  a[(m_dir+1)%SpaceDim] < b[(m_dir+1)%SpaceDim])
67  return true;
68  else if (SpaceDim>2 && \
69  a[ m_dir %SpaceDim] == b[ m_dir %SpaceDim] && \
70  a[(m_dir+1)%SpaceDim] == b[(m_dir+1)%SpaceDim] && \
71  a[(m_dir+2)%SpaceDim] < b[(m_dir+2)%SpaceDim])
72  return true;
73  else
74  return false;
75  }
76 
77  private:
78  int m_dir; // compare along this direction first
79  };
80 
81  // a functor to compare edges for map/sort purposes
82  // first sort based on node0, then compare the direction of the edge
84  {
85  inline bool operator()(const CellEdge& a,const CellEdge& b) const
86  {
87  if (a.m_node0==b.m_node0)
88  return (a.m_dir<b.m_dir); // compare directions, first x, then y,z
89 
90  IVCompareSWO comparator;
91  return comparator(a.m_node0,b.m_node0);
92  }
93  };
94 
95  // a functor to compare pairs of real & int based on the real
97  {
98  inline bool operator()(const pair<Real,int>& a,const pair<Real,int>& b)
99  {
100  return a.first<b.first;
101  }
102  };
103 
104 #ifdef STL_UNORDERED_MAP
105  // for combining hashes, stolen from Boost
106  template <class T>
107  inline void hash_combine(std::size_t& seed, const T& v)
108  {
109  std::hash<T> hasher;
110  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
111  }
112 
113  // now make a hash function for intvects
114  struct IVHash
115  : std::unary_function<IntVect, std::size_t>
116  {
117  inline std::size_t operator()(const IntVect& a) const
118  {
119  std::size_t seed = 0;
120  for (int i=0; i<SpaceDim; i++)
121  hash_combine(seed,a[i]);
122  return seed;
123  }
124  };
125 
126  // use this to make a hash f'n for a CellEdge, using just m_node0 and m_dir
127  // yeah, this could be defined as CellEdge::hash_value(), but whatever
128  struct CellEdgeHash
129  : std::unary_function<CellEdge, std::size_t>
130  {
131  inline std::size_t operator()(const CellEdge& a) const
132  {
133  IVHash node0hasher;
134  std::size_t seed = node0hasher(a.m_node0);
135  hash_combine(seed,a.m_dir);
136  return seed;
137  }
138  };
139 #endif
140 
141  typedef struct {
142  // single cell has vertices and triangles in it
145  } TriInCell;
146 
147  // map definitions, depending on whether or not we can use unordered maps
148 #ifndef STL_UNORDERED_MAP
149  typedef map<IntVect, TriInCell, IVCompareSWO> CellMap;
150  typedef map<IntVect, bool, IVCompareSWO> NodeMap;
151  typedef map<CellEdge, RealVect, EdgeCompareSWO> EdgeMap;
152 #else
153  typedef unordered_map<IntVect, TriInCell, IVHash> CellMap;
154  typedef unordered_map<IntVect, bool, IVHash> NodeMap;
155  typedef unordered_map<CellEdge, RealVect, CellEdgeHash> EdgeMap;
156 #endif
157 
158  // printing functions
159  void PMap(const CellMap& m); // print a cell map
160  void PMap(const NodeMap& m); // print a node map
161  void PMap(const pair<IntVect, TriInCell>& p); // print a cell map entry
162  void PIV(const IntVect& iv); // print an intvect
163  void PRV(const RealVect& iv); // print a realvect
164  void PVec(const Vector<int>& v); // print a vector of things
165  void PVec(const Vector<IntVect>& v);
166  void PVec(const Vector<RealVect>& v);
167  void PVec(const Vector< Vector<IntVect> >& v);
168  void PVec(const Vector< Vector<int> >& v);
169 
170  // convert IntVect to it's physical location
171  RealVect IVToRV(const IntVect& iv,
172  const RealVect& a_origin,
173  const RealVect& a_dx);
174 
175  // get signum of the values in an IntVect
176  IntVect RVSign(const RealVect& rv);
177 
178  typedef CellMap::iterator CellMapIt;
179  typedef NodeMap::iterator NodeMapIt;
180  typedef EdgeMap::iterator EdgeMapIt;
181 
182 }
183 
184 #include "NamespaceFooter.H"
185 #endif
186 
void PMap(const pair< IntVect, TriInCell > &p)
Vector< int > triangles
Definition: STLUtil.H:144
Definition: STLUtil.H:83
Definition: STLUtil.H:36
Definition: STLUtil.H:48
bool operator()(const IntVect &a, const IntVect &b) const
Definition: STLUtil.H:60
IVCompareSWO()
Definition: STLUtil.H:56
Definition: IntVect.H:700
Vector< int > vertices
Definition: STLUtil.H:143
RealVect IVToRV(const IntVect &iv, const RealVect &a_origin, const RealVect &a_dx)
void PVec(const Vector< Vector< int > > &v)
const int SpaceDim
Definition: SPACE.H:38
bool operator()(const pair< Real, int > &a, const pair< Real, int > &b)
Definition: STLUtil.H:98
int m_dir
Definition: STLUtil.H:78
Definition: STLUtil.H:96
Definition: STLUtil.H:141
void PRV(const RealVect &iv)
Definition: CellEdge.H:21
NodeMap::iterator NodeMapIt
Definition: STLUtil.H:179
map< IntVect, TriInCell, IVCompareSWO > CellMap
Definition: STLUtil.H:149
IVCompareSWO(int a_dir)
Definition: STLUtil.H:50
EdgeMap::iterator EdgeMapIt
Definition: STLUtil.H:180
CellMap::iterator CellMapIt
Definition: STLUtil.H:178
A Real vector in SpaceDim-dimensional space.
Definition: RealVect.H:41
map< IntVect, bool, IVCompareSWO > NodeMap
Definition: STLUtil.H:150
map< CellEdge, RealVect, EdgeCompareSWO > EdgeMap
Definition: STLUtil.H:151
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
IntVect RVSign(const RealVect &rv)
void PIV(const IntVect &iv)
bool operator()(const CellEdge &a, const CellEdge &b) const
Definition: STLUtil.H:85
IntVect m_node0
Definition: CellEdge.H:51
int m_dir
Definition: CellEdge.H:49
static void Abort(const char *const a_msg=m_nullString)
Print out message to cerr and exit via abort() (if serial) or MPI_Abort() (if parallel).