// Match.h #ifndef Match_H #define Match_H // Match is a template class which matches the elements in two // STL collections. The matching is at most one-to-one: each // element in each collection is matched to the nearest element // in the other collection or is left unmatched. // // Match is abstract. Derived classes provide the method _quality // which produces a quality (object of class Quality) using one // object from each container. // // The derived class has the responsibility to fill the base lists // (_list1 and _list2) and call the appropriate matching routine. // Presently the lists are private and can only be filled when the // base constructor is invoked. The method match may be invoked to // perform the matching. Additional matching routines may be added // later. // // The template has four arguments: the type of data in each collection // and the type of each collection. // #include #include "ptr/Ptr.h" #include "ptr/AutoPolicy.h" #include "Quality.h" #include // Abstract Match class template. // L1 is an STL collection of elements of type T1; // L2 is an STL collection of elements of type T2; template class Match { private: // typedefs // reference counting pointer to abstract quality typedef Ptr QualityPtr; // Map of type 2 indexed by type 1 (12 map). typedef std::map< T1, T2, std::less > Map12; // Map of type 1 indexed by type 2 (21 map). typedef std::map< T2, T1, std::less > Map21; private: // attributes // first complete list L1 _list1; // second complete list L2 _list2; // Map of matched elements keyed by element in list 1. Map12 _map12; // Map of matched elements keyed by element in list 2. Map21 _map21; private: // methods // Quality method to be provided by subclass. // Caller is responsible for deleting the returned quality. virtual Quality* _quality(const T1& x1, const T2& x2) const =0; // output stream void ostr(std::ostream& stream) const; protected: //methods // Copy the input lists and perform matching. // This should be called by the constructor of any subclass. void match(const L1& list1, const L2& list2); public: //methods // constructor Match(); // destructor virtual ~Match(); // return the first list const L1& get_list1() const { return _list1; }; // return the second list const L2& get_list2() const { return _list2; }; // Return the elements of list 1 which have been matched. L1 get_matched1() const; // Return the elements of list 2 which have been matched. L2 get_matched2() const; // Return the elements of list 1 which were not matched. L1 get_unmatched1() const; // Return the elements of list 2 which were not matched. L2 get_unmatched2() const; // Return the elements of list 1 which have been matched. // Order is that of list 2. L1 get_matched1_in_order2() const; // Return the elements of list 2 which have been matched. // Order is that of list 1. L2 get_matched2_in_order1() const; #if defined(DEFECT_GUIDING_DECLS) friend std::ostream& operator<<(std::ostream& stream, const Match& match); #elif defined(DEFECT_NO_EXPLICIT_QUALIFICATION) template friend std::ostream& operator<<(std::ostream& stream, const Match& match); #else friend std::ostream& operator<< <> (std::ostream& stream, const Match& match); #endif }; #ifndef __xlC__ #include "Match.c" #endif #endif