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 };
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 using namespace std;
00042
00043 size_t size = items.size();
00044
00045 vector<bool> bad(size, false);
00046
00047 for (size_t ie = 0; ie < size; ++ie) {
00048 if (bad[ie]) continue;
00049
00050 for (size_t je = ie+1; je < size; ++je) {
00051
00052 if (bad[je]) continue;
00053
00054 if ( comparator_(items[ie], items[je]) ) {
00055 int toRemove = arbiter_(items[ie], items[je]) ? je : ie;
00056 bad[toRemove] = true;
00057 }
00058 }
00059 }
00060
00061 auto_ptr< vector<size_t> > ret(new vector<size_t>());
00062
00063 for (size_t i = 0; i < size; ++i) {
00064 if (bad[i]) ret->push_back(i);
00065 }
00066
00067 return ret;
00068 }
00069
00070
00071 #endif