Go to the documentation of this file.00001 #ifndef DataFormats_Common_OneToOneGeneric_h
00002 #define DataFormats_Common_OneToOneGeneric_h
00003 #include "DataFormats/Common/interface/AssociationMapHelpers.h"
00004 #include "DataFormats/Common/interface/MapRefViewTrait.h"
00005 #include <map>
00006
00007 namespace edm {
00008 template<typename CKey, typename CVal, typename index = unsigned int,
00009 typename KeyRefProd = typename helper::MapRefViewTrait<CKey>::refprod_type,
00010 typename ValRefProd = typename helper::MapRefViewTrait<CVal>::refprod_type,
00011 typename KeyRef = typename helper::MapRefViewTrait<CKey>::ref_type,
00012 typename ValRef = typename helper::MapRefViewTrait<CVal>::ref_type>
00013 class OneToOneGeneric {
00015 typedef KeyRefProd keyrefprod_type;
00017 typedef ValRefProd valrefprod_type;
00019 typedef index map_assoc;
00020
00021 public:
00023 typedef ValRef val_type;
00025 typedef KeyRef key_type;
00027 typedef ValRef data_type;
00029 typedef index index_type;
00031 typedef std::map<index_type, map_assoc> map_type;
00033 typedef helpers::KeyVal<keyrefprod_type, valrefprod_type> ref_type;
00035 typedef std::map<const typename CKey::value_type *,
00036 const typename CVal::value_type *> transient_map_type;
00038 typedef std::vector<const typename CKey::value_type *> transient_key_vector;
00040 typedef std::vector<const typename CVal::value_type *> transient_val_vector;
00042 static void insert(ref_type & ref, map_type & m,
00043 const key_type & k, const data_type & v) {
00044 if (k.isNull() || v.isNull()) {
00045 Exception::throwThis(errors::InvalidReference,
00046 "can't insert null references in AssociationMap");
00047 }
00048 if (ref.key.isNull()) {
00049 ref.key = keyrefprod_type(k);
00050 ref.val = valrefprod_type(v);
00051 }
00052 helpers::checkRef(ref.key, k); helpers::checkRef(ref.val, v);
00053 index_type ik = index_type(k.key()), iv = index_type(v.key());
00054 m[ik] = iv;
00055 }
00057 static val_type val(const ref_type & ref, map_assoc iv) {
00058 return val_type(ref.val, iv);
00059 }
00061 static typename map_type::size_type size(const map_assoc & v) { return 1; }
00063 static void sort(map_type &) { }
00065 static transient_map_type transientMap(const ref_type & ref, const map_type & map) {
00066 transient_map_type m;
00067 const CKey & ckey = * ref.key;
00068 const CVal & cval = * ref.val;
00069 for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++ i) {
00070 const typename CKey::value_type * k = & ckey[i->first];
00071 const typename CVal::value_type * v = & cval[i->second];
00072 m.insert(std::make_pair(k, v));
00073 }
00074 return m;
00075 }
00077 static transient_key_vector transientKeyVector(const ref_type & ref, const map_type & map) {
00078 transient_key_vector m;
00079 const CKey & ckey = * ref.key;
00080 for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++ i)
00081 m.push_back(& ckey[i->first]);
00082 return m;
00083 }
00085 static transient_val_vector transientValVector(const ref_type & ref, const map_type & map) {
00086 transient_val_vector m;
00087 const CVal & cval = * ref.val;
00088 for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++ i)
00089 m.push_back(& cval[i->second]);
00090 return m;
00091 }
00092 };
00093 }
00094
00095 #endif