core/vnl/vnl_crs_index.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_crs_index.h
00002 #ifndef vnl_crs_index_h_
00003 #define vnl_crs_index_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Compressed Row Storage (CRS) indexing
00010 // \author Matt Leotta (Brown)
00011 // \date   April 13, 2005
00012 //
00013 // \verbatim
00014 //  Modifications
00015 // \endverbatim
00016 //
00017 #include <vcl_vector.h>
00018 #include <vcl_utility.h>
00019 
00020 
00021 //: Represents the configuration of a sparse matrix but not the data
00022 //  This is essentially a sparse matrix of indices into a data vector
00023 //  Compressed row storage is used for representation
00024 //  This class is useful when working with several sparse matrices that
00025 //  share a common sparse structure.
00026 class vnl_crs_index
00027 {
00028  public:
00029   typedef vcl_pair<int,int> idx_pair;
00030   typedef vcl_vector<idx_pair> sparse_vector;
00031 
00032   //: Constructor - default
00033   vnl_crs_index() : num_cols_(0), col_idx_(), row_ptr_() {}
00034 
00035   //: Constructor - from a binary mask
00036   vnl_crs_index(const vcl_vector<vcl_vector<bool> >& mask);
00037 
00038   //: Destructor
00039   ~vnl_crs_index(){}
00040 
00041   //: number of rows in the sparse matrix
00042   int num_rows() const { return int(row_ptr_.size())-1; }
00043 
00044   //: number of columns in the sparse matrix
00045   int num_cols() const { return num_cols_; }
00046 
00047   //: number of non-zero elements
00048   int num_non_zero() const { return int(col_idx_.size()); }
00049 
00050   //: returns row \p i as a vector of index-column pairs
00051   sparse_vector sparse_row(int i) const;
00052 
00053   //: returns column \p j as a vector of index-row pairs
00054   // \note because of CRS this method is a bit less efficient than sparse_row
00055   sparse_vector sparse_col(int j) const;
00056 
00057   //: return the index at location (i,j)
00058   //  returns -1 if the entry is 0
00059   int operator() (int i, int j) const;
00060 
00061  private:
00062   //: The number of columns in the matrix
00063   unsigned int num_cols_;
00064   //: The column for each non-zero element
00065   vcl_vector<int> col_idx_;
00066   //: The index of the first non-zero element in each row
00067   vcl_vector<int> row_ptr_;
00068 };
00069 
00070 #endif // vnl_crs_index_h_