CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/DataFormats/Common/interface/OneToValue.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_OneToValue_h
00002 #define DataFormats_Common_OneToValue_h
00003 
00004 #include "DataFormats/Common/interface/AssociationMapHelpers.h"
00005 #include "DataFormats/Common/interface/Ref.h"
00006 #include "DataFormats/Common/interface/RefProd.h"
00007 
00008 #include <map>
00009 
00010 namespace edm {
00011   template<typename CKey, typename Val, typename index = unsigned int>
00012   class OneToValue {
00014     typedef RefProd<CKey> KeyRefProd;
00016     typedef Val map_assoc;
00017   public:
00019     typedef Val val_type;
00021     typedef Ref<CKey> key_type;
00023     typedef Val data_type;
00025     typedef index index_type;
00027     typedef std::map<index_type, map_assoc> map_type;
00029     typedef helpers::Key<KeyRefProd> ref_type;
00031     typedef std::map<typename CKey::value_type const*, Val> transient_map_type;
00033     typedef std::vector<typename CKey::value_type const*> transient_key_vector;
00035     typedef std::vector<Val> transient_val_vector;
00037     static void insert(ref_type& ref, map_type& m,
00038                        key_type const& k, data_type const& v) {
00039       if(k.isNull()) {
00040         Exception::throwThis(errors::InvalidReference,
00041           "can't insert null references in AssociationMap");
00042       }
00043       if(ref.key.isNull()) {
00044         ref.key = KeyRefProd(k);
00045       }
00046       helpers::checkRef(ref.key, k);
00047       index_type ik = index_type(k.key());
00048       m[ik] = v;
00049     }
00051     static val_type val(ref_type const&, map_assoc const& v) {
00052       return v;
00053     }
00055     static typename map_type::size_type size(map_assoc const&) { return 1; }
00057     static void sort(map_type&) { }
00059     static transient_map_type transientMap(ref_type const& ref, map_type const& map) {
00060       transient_map_type m;
00061       CKey const& ckey = *ref.key;
00062       for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
00063         typename CKey::value_type const* k = &ckey[i->first];
00064         m.insert(std::make_pair(k, i->second));
00065       }
00066       return m;
00067     }
00069     static transient_key_vector transientKeyVector(ref_type const& ref, map_type const& map) {
00070       transient_key_vector m;
00071       CKey const& ckey = *ref.key;
00072       for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
00073         m.push_back(& ckey[i->first]);
00074       }
00075       return m;
00076     }
00078     static transient_val_vector transientValVector(ref_type const& ref, map_type const& map) {
00079       transient_val_vector m;
00080       for(typename map_type::const_iterator i = map.begin(); i != map.end(); ++i) {
00081         m.push_back(i->second);
00082       }
00083       return m;
00084     }
00085   };
00086 }
00087 
00088 #endif