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