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