src/ColumnView.h
7b9a7da9
 #ifndef ColumnView_h
 #define ColumnView_h
84931e26
 
 #include <Rcpp.h>
7b9a7da9
 #include "SparseMatrixView.h"
 #include "VectorSubsetView.h"
84931e26
 
 
 class ColumnView {
   const dgCMatrixView* matrix;
 
 public:
   class col_container {
   public:
     VectorSubsetView<REALSXP> values;
     VectorSubsetView<INTSXP> row_indices;
     const R_len_t number_of_zeros;
 
     col_container(VectorSubsetView<REALSXP> values_, VectorSubsetView<INTSXP> row_indices_, R_len_t number_of_zeros_):
       values(values_), row_indices(row_indices_), number_of_zeros(number_of_zeros_) {}
   };
 
 
   class iterator {
     ColumnView* cv;
     int index;
   public:
     using iterator_category= std::input_iterator_tag;
     using value_type = col_container;
     using reference = col_container&;
     using pointer = col_container*;
     using difference_type = void;
 
f4221ff8
     iterator(ColumnView* cv_): cv(cv_), index(0) {
6135dbfe
       if(cv != nullptr && cv->matrix->ncol == 0){
f4221ff8
         cv = nullptr;
       }
     }
84931e26
 
     col_container operator*() const {
       int start_pos = cv->matrix->col_ptrs[index];
       int end_pos = cv->matrix->col_ptrs[index + 1];
       int number_of_zeros = cv->matrix->nrow - (end_pos - start_pos);
       VectorSubsetView<REALSXP> values(cv->matrix->values, start_pos, end_pos);
       VectorSubsetView<INTSXP> row_indices(cv->matrix->row_indices, start_pos, end_pos);
 
       return col_container(values, row_indices, number_of_zeros);
     }
 
     iterator& operator++(){ // preincrement
       ++index;
       if(index == cv->matrix->ncol)
         cv=nullptr;
       return *this;
     }
 
     friend bool operator==(iterator const& lhs,iterator const& rhs){
       return lhs.cv == rhs.cv;
     }
     friend bool operator!=(iterator const& lhs,iterator const& rhs){
       return !(lhs==rhs);
     }
 
   };
 
   ColumnView(dgCMatrixView* matrix_): matrix(matrix_) {}
   iterator begin() { return iterator(this); }
   iterator end() { return iterator(nullptr); }
 
 };
 
 
 
 
 
 
 
 
 
 
 
7b9a7da9
 #endif /* ColumnView_h */