vnl_crs_index.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_crs_index.h
2 #ifndef vnl_crs_index_h_
3 #define vnl_crs_index_h_
4 //:
5 // \file
6 // \brief Compressed Row Storage (CRS) indexing
7 // \author Matt Leotta (Brown)
8 // \date April 13, 2005
9 //
10 // \verbatim
11 // Modifications
12 // \endverbatim
13 //
14 #include <vector>
15 #include <utility>
16 #ifdef _MSC_VER
17 # include <vcl_msvc_warnings.h>
18 #endif
19 #include "vnl/vnl_export.h"
20 
21 //: Represents the configuration of a sparse matrix but not the data
22 // This is essentially a sparse matrix of indices into a data vector
23 // Compressed row storage is used for representation
24 // This class is useful when working with several sparse matrices that
25 // share a common sparse structure.
26 class VNL_EXPORT vnl_crs_index
27 {
28  public:
29  typedef std::pair<int,int> idx_pair;
30  typedef std::vector<idx_pair> sparse_vector;
31 
32  //: Constructor - default
33  vnl_crs_index() : num_cols_(0), col_idx_(), row_ptr_() {}
34 
35  //: Constructor - from a binary mask
36  vnl_crs_index(const std::vector<std::vector<bool> >& mask);
37 
38  //: Destructor
39  ~vnl_crs_index()= default;
40 
41  //: number of rows in the sparse matrix
42  int num_rows() const { return int(row_ptr_.size())-1; }
43 
44  //: number of columns in the sparse matrix
45  int num_cols() const { return num_cols_; }
46 
47  //: number of non-zero elements
48  int num_non_zero() const { return int(col_idx_.size()); }
49 
50  //: returns row \p i as a vector of index-column pairs
51  sparse_vector sparse_row(int i) const;
52 
53  //: returns column \p j as a vector of index-row pairs
54  // \note because of CRS this method is a bit less efficient than sparse_row
55  sparse_vector sparse_col(int j) const;
56 
57  //: return the index at location (i,j)
58  // returns -1 if the entry is 0
59  int operator() (int i, int j) const;
60 
61  private:
62  //: The number of columns in the matrix
63  unsigned int num_cols_;
64  //: The column for each non-zero element
65  std::vector<int> col_idx_;
66  //: The index of the first non-zero element in each row
67  std::vector<int> row_ptr_;
68 };
69 
70 #endif // vnl_crs_index_h_
unsigned int num_cols_
The number of columns in the matrix.
Definition: vnl_crs_index.h:63
int num_rows() const
number of rows in the sparse matrix.
Definition: vnl_crs_index.h:42
vnl_crs_index()
Constructor - default.
Definition: vnl_crs_index.h:33
std::pair< int, int > idx_pair
Definition: vnl_crs_index.h:29
Represents the configuration of a sparse matrix but not the data.
Definition: vnl_crs_index.h:26
int num_cols() const
number of columns in the sparse matrix.
Definition: vnl_crs_index.h:45
std::vector< idx_pair > sparse_vector
Definition: vnl_crs_index.h:30
std::vector< int > row_ptr_
The index of the first non-zero element in each row.
Definition: vnl_crs_index.h:67
int num_non_zero() const
number of non-zero elements.
Definition: vnl_crs_index.h:48
std::vector< int > col_idx_
The column for each non-zero element.
Definition: vnl_crs_index.h:65