contrib/oxl/mvl/PairMatchMulti.h
Go to the documentation of this file.
00001 // This is oxl/mvl/PairMatchMulti.h
00002 #ifndef PairMatchMulti_h_
00003 #define PairMatchMulti_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Set of pairs of integers
00010 //
00011 //  PairMatchMulti is a binary relationship between integers
00012 //  $i_1 \rightarrow i_2$ where there may be multiple $i_2$ for
00013 //  each $i_1$.
00014 //
00015 //  The current implementation uses an STL multimap.
00016 //
00017 // \author
00018 //     Andrew W. Fitzgibbon, Oxford RRG, 16 Sep 96
00019 //
00020 // \verbatim
00021 //  Modifications:
00022 //   June 97 - Peter Vanroose - operator==() added
00023 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00024 // \endverbatim
00025 //-----------------------------------------------------------------------------
00026 
00027 #include <vcl_cstdlib.h> // for vcl_abort()
00028 #include <vcl_iosfwd.h>
00029 #include "PairMatchMultiIterator.h"
00030 
00031 template <class T> class vbl_sparse_array_2d;
00032 
00033 class PairMatchSet;
00034 
00035 class PairMatchMulti
00036 {
00037   // Data Members--------------------------------------------------------------
00038   vcl_multimap_uint_uint matches12_;
00039   vbl_sparse_array_2d<double> *scores_;
00040  public:
00041   friend class PairMatchMultiIterator;
00042 
00043   // Constructors/Destructors--------------------------------------------------
00044 
00045   PairMatchMulti();
00046   PairMatchMulti(vcl_istream& s);
00047   PairMatchMulti(const PairMatchMulti& that);
00048  ~PairMatchMulti();
00049 
00050   PairMatchMulti& operator=(const PairMatchMulti& that);
00051 
00052   // Operations----------------------------------------------------------------
00053 
00054   //: Add a match $(i_1, i_2)$ to the set
00055   void add_match(int i1, int i2) {
00056     matches12_.insert(i1, i2);
00057   }
00058 
00059   bool contains(int i1, int i2) const;
00060 
00061   //: Add a match $(i_1, i_2)$ to the set
00062   void add_match(int i1, int i2, double score);
00063 
00064   double get_score(int i1, int i2) const;
00065   void set_score(int i1, int i2, double score);
00066 
00067   //: Clear all matches
00068   void clear() {
00069     matches12_.erase(matches12_.begin(), matches12_.end());
00070   }
00071 
00072   int size() const { return matches12_.size(); }
00073 
00074   // for compatibility with PairMatchSet
00075   int count() const { return matches12_.size(); }
00076 
00077   //: Return the number of matches for i1.
00078   unsigned count_matches_12(int i1) const { return matches12_.count(i1); }
00079 
00080   //: Return an iterator which will run through the list of matches for feature index i1.
00081   //  Example usage: to print all matches for "target"
00082   // \code
00083   //   for (PairMatchMultiIterator p = mm.get_match_12(target); !p.done(); p.next())
00084   //     vcl_cout << p.get_i1() << ' ' << p.get_i2() << vcl_endl;
00085   // \endcode
00086   // Complexity is O(log n).
00087   PairMatchMultiIterator get_match_12(int i1)
00088   {
00089     return PairMatchMultiIterator(matches12_.lower_bound(i1), matches12_.upper_bound(i1));
00090   }
00091 
00092   //: Return an iterator which will run through the list of matches for feature index i2.
00093   // This may be expensive.  If it is used a lot, it may be worth maintaining
00094   // forward and backward maps.  Right now it's not even implemented.
00095   PairMatchMultiIterator get_match_21(int/*i2*/) { vcl_abort(); return iter(); }
00096 
00097   //: Return an iterator that traverses the entire match set
00098   PairMatchMultiIterator iter() {
00099     return PairMatchMultiIterator(matches12_.begin(), matches12_.end());
00100   }
00101 
00102   // Computations--------------------------------------------------------------
00103   bool is_superset(PairMatchSet& unique_set);
00104 
00105   // Data Access---------------------------------------------------------------
00106   bool operator==(PairMatchMulti const& that) const {
00107     return this == &that; // always false, except when identical memory location
00108   }
00109 
00110   bool load(char const* filename);
00111   bool read_ascii(vcl_istream& s);
00112 };
00113 
00114 vcl_ostream& operator<< (vcl_ostream&, const PairMatchMulti&);
00115 vcl_istream& operator>> (vcl_istream&, PairMatchMulti&);
00116 
00117 #endif // PairMatchMulti_h_