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