CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/PatUtils/interface/GenericDuplicateRemover.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_PatUtils_GenericDuplicateRemover_h
00002 #define PhysicsTools_PatUtils_GenericDuplicateRemover_h
00003 
00004 #include <memory>
00005 #include <vector>
00006 
00007 namespace pat {
00008 
00009     template <typename Comparator, typename Arbitrator>
00010     class GenericDuplicateRemover {
00011 
00012         public:
00013 
00014            GenericDuplicateRemover() {}
00015            GenericDuplicateRemover(const Comparator &comp) : comparator_(comp) {}
00016            GenericDuplicateRemover(const Comparator &comp, const Arbitrator &arbiter) : comparator_(comp), arbiter_(arbiter) {}
00017             
00018            ~GenericDuplicateRemover() {}
00019 
00025             template <typename Collection>
00026             std::auto_ptr< std::vector<size_t> >
00027             duplicates(const Collection &items) const ;
00028 
00029         private:
00030             Comparator comparator_;
00031             Arbitrator   arbiter_;
00032 
00033     }; // class
00034 }
00035 
00036 template<typename Comparator, typename Arbitrator>
00037 template<typename Collection>
00038 std::auto_ptr< std::vector<size_t> >
00039 pat::GenericDuplicateRemover<Comparator,Arbitrator>::duplicates(const Collection &items) const 
00040 {
00041     size_t size = items.size();
00042 
00043     std::vector<bool> bad(size, false);
00044 
00045     for (size_t ie = 0; ie < size; ++ie) {
00046         if (bad[ie]) continue; // if already marked bad
00047 
00048         for (size_t je = ie+1; je < size; ++je) {
00049 
00050             if (bad[je]) continue; // if already marked bad
00051 
00052             if ( comparator_(items[ie], items[je]) ) {
00053                 int toRemove = arbiter_(items[ie], items[je]) ? je : ie;
00054                 bad[toRemove] = true;
00055             }
00056         }
00057     }
00058 
00059     std::auto_ptr< std::vector<size_t> > ret(new std::vector<size_t>());
00060 
00061     for (size_t i = 0; i < size; ++i) {
00062         if (bad[i]) ret->push_back(i);
00063     }
00064 
00065     return ret;
00066 }
00067 
00068 
00069 #endif