core/vbl/vbl_sparse_array_2d.h
Go to the documentation of this file.
00001 // This is core/vbl/vbl_sparse_array_2d.h
00002 #ifndef vbl_sparse_array_2d_h_
00003 #define vbl_sparse_array_2d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief a space efficient 2d array
00010 // \author Andrew W. Fitzgibbon, Oxford RRG
00011 // \date   02 Oct 96
00012 //
00013 //    vbl_sparse_array_2d is a sparse 2D array allowing space
00014 //    efficient access of the form s(3000,7000) = 2.
00015 //
00016 // \verbatim
00017 //  Modifications
00018 //   26 March 01 cjb updated documentation
00019 //   10 April 01 IMS (Manchester ISBE) modified to use vbl_sparse_array_base
00020 //   11 April 01 Peter Vanroose - vbl_index_2d moved to separate file
00021 //   11 April 01 Ian Scott - replaced used of vbl_index_2d with vcl_pair
00022 // \endverbatim
00023 //---------------------------------------------------------------------------
00024 
00025 #include <vcl_iostream.h>
00026 #include <vcl_utility.h> // for vcl_pair<.,.>
00027 #include <vbl/vbl_sparse_array_base.h>
00028 
00029 //: Sparse 2D array allowing space efficient access of the form  s(300,700) =2
00030 template <class T>
00031 class vbl_sparse_array_2d : public vbl_sparse_array_base<T, vcl_pair<unsigned, unsigned> >
00032 {
00033   typedef typename vbl_sparse_array_base<T,vcl_pair<unsigned,unsigned> >::Index_type Index_type;
00034  public:
00035   typedef typename vbl_sparse_array_base<T,vcl_pair<unsigned,unsigned> >::const_iterator const_iterator;
00036 
00037   //: Put a value into location (i,j).
00038   bool put(unsigned i, unsigned j, const T& t)
00039   {
00040     return vbl_sparse_array_base<T, Index_type>::put(vcl_make_pair(i, j), t);
00041   }
00042 
00043   //: Return contents of location (i,j).
00044   //  Returns an undefined value (in fact
00045   //  a T()) if location (i,j) has not been filled with a value.
00046   T& operator () (unsigned i, unsigned j)
00047   {
00048     return vbl_sparse_array_base<T, Index_type>::operator() (vcl_make_pair(i, j));
00049   }
00050 
00051   //: Return contents of (i,j).  Assertion failure if not yet filled.
00052   const T& operator () (unsigned i, unsigned j) const
00053   {
00054     return vbl_sparse_array_base<T, Index_type>::operator() (vcl_make_pair(i, j));
00055   }
00056 
00057   //: Erase element at location (i,j). Assertion failure if not yet filled.
00058   void erase(unsigned i, unsigned j) {
00059     vbl_sparse_array_base<T, Index_type>::erase(vcl_make_pair(i,j));
00060   }
00061 
00062   //: Return true if location (i,j) has been filled.
00063   bool fullp(unsigned i, unsigned j) const
00064   {
00065     return vbl_sparse_array_base<T, Index_type>::fullp(vcl_make_pair(i, j));
00066   }
00067 
00068   //: Return the address of location (i,j).  0 if not yet filled.
00069   T* get_addr(unsigned i, unsigned j)
00070   {
00071     return vbl_sparse_array_base<T, Index_type>::get_addr(vcl_make_pair(i, j));
00072   }
00073 
00074   //: Print the Array to a stream in "(i,j): value" format.
00075   vcl_ostream& print(vcl_ostream& out) const
00076   {
00077     for (const_iterator p = this->begin(); p != this->end(); ++p)
00078       out << '(' << (*p).first.first
00079           << ',' << (*p).first.second
00080           << "): " << (*p).second << vcl_endl;
00081     return out;
00082   }
00083 };
00084 
00085 //: Stream operator - print the Array to a stream in "(i,j): value" format.
00086 template <class T>
00087 inline vcl_ostream& operator<< (vcl_ostream& s, const vbl_sparse_array_2d<T>& a)
00088 {
00089   return a.print(s);
00090 }
00091 
00092 #ifndef VBL_SPARSE_ARRAY_BASE_INSTANTIATE
00093 #define VBL_SPARSE_ARRAY_BASE_INSTANTIATE(T) \
00094 extern "please include vbl/vbl_sparse_array_base.txx instead"
00095 #endif // VBL_SPARSE_ARRAY_BASE_INSTANTIATE
00096 #define VBL_SPARSE_ARRAY_2D_INSTANTIATE(T) \
00097 extern "please include vbl/vbl_sparse_array_2d.txx instead"
00098 
00099 #endif // vbl_sparse_array_2d_h_