CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/DataFormats/Common/interface/OneToOneGeneric.h

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