00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _TUPLEKEYMAPI_H_
00012 #define _TUPLEKEYMAPI_H_
00013
00014 #include <map>
00015 #include <iostream>
00016 #include "parstream.H"
00017 #include "NamespaceHeader.H"
00018
00019 #define Tuple2 std::pair<T1, T2>
00020 #define Tuple3 std::pair<Tuple2, T3>
00021 #define Tuple4 std::pair<Tuple3, T4>
00022 #define TupleTypenames typename T1, typename T2, typename T3, typename T4
00023 #define TupleArgTypes T1,T2,T3,T4
00024 #define TupleArgValues arg1,arg2,arg3,arg4
00025 #define TupleArgDecls T1 arg1, T2 arg2, T3 arg3, T4 arg4
00026
00027
00028
00029
00030
00031
00032
00033
00034 template<TupleTypenames, typename ValueT> class TupleKeyMap
00035 {
00036 typedef std::map< Tuple4, ValueT > RepType;
00037
00038 public:
00039 typedef typename RepType::const_iterator ConstIteratorType;
00040
00041 void insert( TupleArgDecls, ValueT );
00042
00043 bool containsKey( TupleArgDecls, ConstIteratorType* i=0 ) const;
00044
00045 ValueT fetch( TupleArgDecls ) const;
00046
00047 ValueT fetch( ConstIteratorType ) const;
00048
00049 void report() const;
00050
00051 void clear();
00052
00053 unsigned size()
00054 {
00055 return m_rep.size();
00056 }
00057
00058 private:
00059 RepType m_rep;
00060 };
00061
00062
00063
00064
00065
00066 template<TupleTypenames, typename ValueT> ValueT
00067 TupleKeyMap<TupleArgTypes,ValueT>::fetch( TupleArgDecls ) const
00068 {
00069 CH_assert( this->containsKey( TupleArgValues ) );
00070
00071 Tuple2 tuple2(arg1,arg2);
00072 Tuple3 tuple3(tuple2,arg3);
00073 Tuple4 tuple4(tuple3,arg4);
00074
00075 Tuple4 const & item( tuple4 );
00076 typename RepType::const_iterator iter = m_rep.find( item );
00077 return iter->second;
00078 }
00079
00080 template<TupleTypenames, typename ValueT> ValueT
00081 TupleKeyMap<TupleArgTypes,ValueT>::fetch( ConstIteratorType iter ) const
00082 {
00083 return iter->second;
00084 }
00085
00086
00087 template<TupleTypenames, typename ValueT> bool
00088 TupleKeyMap<TupleArgTypes,ValueT>::containsKey( TupleArgDecls,
00089 ConstIteratorType* iter) const
00090 {
00091 Tuple2 tuple2(arg1,arg2);
00092 Tuple3 tuple3(tuple2,arg3);
00093 Tuple4 tuple4(tuple3,arg4);
00094
00095 Tuple4 const & item( tuple4 );
00096 if ( iter )
00097 {
00098 *iter = m_rep.find( item );
00099 return !((*iter) == m_rep.end());
00100 } else
00101 {
00102 ConstIteratorType it = m_rep.find( item );
00103 return !(it == m_rep.end());
00104 }
00105 }
00106
00107 template<TupleTypenames, typename ValueT> void
00108 TupleKeyMap<TupleArgTypes,ValueT>::insert( TupleArgDecls, ValueT value )
00109 {
00110
00111
00112
00113 CH_assert( ! this->containsKey( TupleArgValues ) );
00114
00115 Tuple2 tuple2(arg1,arg2);
00116 Tuple3 tuple3(tuple2,arg3);
00117 Tuple4 tuple4(tuple3,arg4);
00118
00119 Tuple4 const & item( tuple4 );
00120 m_rep[item] = value;
00121 }
00122
00123
00124 template<TupleTypenames, typename ValueT>
00125 void
00126 TupleKeyMap<TupleArgTypes,ValueT>::report() const
00127 {
00128 #ifndef NDEBUG
00129 pout() << "TupleKeyMap::report(): unique arg combinations: "
00130 << m_rep.size() << std::endl;
00131 #endif
00132 }
00133
00134
00135 template<TupleTypenames, typename ValueT> void
00136 TupleKeyMap<TupleArgTypes,ValueT>::clear()
00137 {
00138 m_rep.clear();
00139 }
00140
00141 #undef Tuple2
00142 #undef Tuple3
00143 #undef Tuple4
00144 #undef TupleTypenames
00145 #undef TupleArgTypes
00146 #undef TupleArgDecls
00147
00148 #include "NamespaceFooter.H"
00149 #endif // _TUPLEKEYMAPI_H_