Chombo + EB + MF  3.2
TupleKeyMapI.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 _TUPLEKEYMAPI_H_
12 #define _TUPLEKEYMAPI_H_
13 
14 #include <map>
15 #include <iostream>
16 #include "parstream.H"
17 #include "NamespaceHeader.H"
18 
19 #define Tuple2 std::pair<T1, T2>
20 #define Tuple3 std::pair<Tuple2, T3>
21 #define Tuple4 std::pair<Tuple3, T4>
22 #define TupleTypenames typename T1, typename T2, typename T3, typename T4
23 #define TupleArgTypes T1,T2,T3,T4
24 #define TupleArgValues arg1,arg2,arg3,arg4
25 #define TupleArgDecls T1 arg1, T2 arg2, T3 arg3, T4 arg4
26 
27 
28 /**
29  Map whose key is a 4-tuple. The 4 is hardcoded but can be pretty easily changed
30  by making obvious modifications to the #define's above and going up to Tuple<n>
31  in the functions below.
32  Tuple types (T1,...) must implement both operator==() and operator<().
33 */
34 template<TupleTypenames, typename ValueT> class TupleKeyMap
35 {
36  typedef std::map< Tuple4, ValueT > RepType;
37 
38 public:
39  typedef typename RepType::const_iterator ConstIteratorType;
40 
41  void insert( TupleArgDecls, ValueT );
42 
43  bool containsKey( TupleArgDecls, ConstIteratorType* i=0 ) const;
44 
45  ValueT fetch( TupleArgDecls ) const;
46 
47  ValueT fetch( ConstIteratorType ) const;
48 
49  void report() const;
50 
51  void clear();
52 
53  unsigned size()
54  {
55  return m_rep.size();
56  }
57 
58 private:
59  RepType m_rep;
60 };
61 
62 
63 /**
64  Returns the value associated with the tuple key.
65 */
66 template<TupleTypenames, typename ValueT> ValueT
68 {
70 
71  Tuple2 tuple2(arg1,arg2);
72  Tuple3 tuple3(tuple2,arg3);
73  Tuple4 tuple4(tuple3,arg4);
74 
75  Tuple4 const & item( tuple4 );
76  typename RepType::const_iterator iter = m_rep.find( item );
77  return iter->second;
78 }
79 
80 template<TupleTypenames, typename ValueT> ValueT
82 {
83  return iter->second;
84 }
85 
86 
87 template<TupleTypenames, typename ValueT> bool
89  ConstIteratorType* iter) const
90 {
91  Tuple2 tuple2(arg1,arg2);
92  Tuple3 tuple3(tuple2,arg3);
93  Tuple4 tuple4(tuple3,arg4);
94 
95  Tuple4 const & item( tuple4 );
96  if ( iter )
97  {
98  *iter = m_rep.find( item );
99  return !((*iter) == m_rep.end());
100  } else
101  {
102  ConstIteratorType it = m_rep.find( item );
103  return !(it == m_rep.end());
104  }
105 }
106 
107 template<TupleTypenames, typename ValueT> void
109 {
110  // Unlike std::map, which lets you insert a duplicate key (and thereupon
111  // silently doesn't insert it), we consider that an error. This is because
112  // checking that the key is already in the map is costing us.
113  CH_assert( ! this->containsKey( TupleArgValues ) );
114 
115  Tuple2 tuple2(arg1,arg2);
116  Tuple3 tuple3(tuple2,arg3);
117  Tuple4 tuple4(tuple3,arg4);
118 
119  Tuple4 const & item( tuple4 );
120  m_rep[item] = value;
121 }
122 
123 
124 template<TupleTypenames, typename ValueT>
125 void
127 {
128 #ifndef NDEBUG
129  pout() << "TupleKeyMap::report(): unique arg combinations: "
130  << m_rep.size() << std::endl;
131 #endif
132 }
133 
134 
135 template<TupleTypenames, typename ValueT> void
137 {
138  m_rep.clear();
139 }
140 
141 #undef Tuple2
142 #undef Tuple3
143 #undef Tuple4
144 #undef TupleTypenames
145 #undef TupleArgTypes
146 #undef TupleArgDecls
147 
148 #include "NamespaceFooter.H"
149 #endif // _TUPLEKEYMAPI_H_
std::ostream & pout()
Use this in place of std::cout for program output.
#define CH_assert(cond)
Definition: CHArray.H:37
unsigned size()
Definition: TupleKeyMapI.H:53
bool containsKey(TupleArgDecls, ConstIteratorType *i=0) const
Definition: TupleKeyMapI.H:88
ValueT fetch(TupleArgDecls) const
Definition: TupleKeyMapI.H:67
Definition: TupleKeyMapI.H:34
#define Tuple2
Definition: TupleKeyMapI.H:19
#define TupleArgDecls
Definition: TupleKeyMapI.H:25
RepType m_rep
Definition: TupleKeyMapI.H:59
void insert(TupleArgDecls, ValueT)
Definition: TupleKeyMapI.H:108
std::map< Tuple4, ValueT > RepType
Definition: TupleKeyMapI.H:36
#define Tuple4
Definition: TupleKeyMapI.H:21
#define Tuple3
Definition: TupleKeyMapI.H:20
void clear()
Definition: TupleKeyMapI.H:136
#define TupleArgValues
Definition: TupleKeyMapI.H:24
RepType::const_iterator ConstIteratorType
Definition: TupleKeyMapI.H:39
void report() const
Definition: TupleKeyMapI.H:126