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 throw edm::Exception(edm::errors::InvalidReference) 00046 << "can't insert null references in AssociationMap"; 00047 if (ref.key.isNull()) { 00048 ref.key = keyrefprod_type(k); 00049 ref.val = valrefprod_type(v); 00050 } 00051 helpers::checkRef(ref.key, k); helpers::checkRef(ref.val, v); 00052 index_type ik = index_type(k.key()), iv = index_type(v.key()); 00053 m[ik] = iv; 00054 } 00056 static val_type val(const ref_type & ref, map_assoc iv) { 00057 return val_type(ref.val, iv); 00058 } 00060 static typename map_type::size_type size(const map_assoc & v) { return 1; } 00062 static void sort(map_type &) { } 00064 static transient_map_type transientMap(const ref_type & ref, const map_type & map) { 00065 transient_map_type m; 00066 const CKey & ckey = * ref.key; 00067 const CVal & cval = * ref.val; 00068 for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++ i) { 00069 const typename CKey::value_type * k = & ckey[i->first]; 00070 const typename CVal::value_type * v = & cval[i->second]; 00071 m.insert(std::make_pair(k, v)); 00072 } 00073 return m; 00074 } 00076 static transient_key_vector transientKeyVector(const ref_type & ref, const map_type & map) { 00077 transient_key_vector m; 00078 const CKey & ckey = * ref.key; 00079 for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++ i) 00080 m.push_back(& ckey[i->first]); 00081 return m; 00082 } 00084 static transient_val_vector transientValVector(const ref_type & ref, const map_type & map) { 00085 transient_val_vector m; 00086 const CVal & cval = * ref.val; 00087 for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++ i) 00088 m.push_back(& cval[i->second]); 00089 return m; 00090 } 00091 }; 00092 } 00093 00094 #endif