1 #ifndef READXL_CELLLIMITS_
2 #define READXL_CELLLIMITS_
3 
4 #include <Rcpp.h>
5 #include "XlsCell.h"
6 
7 class CellLimits {
8   int minRow_, maxRow_, minCol_, maxCol_;
9 
10 public:
CellLimits()11   CellLimits() {
12     minRow_ = -1;
13     maxRow_ = -1;
14     minCol_ = -1;
15     maxCol_ = -1;
16   }
CellLimits(Rcpp::IntegerVector limits)17   CellLimits(Rcpp::IntegerVector limits) {
18     minRow_ = limits[0];
19     maxRow_ = limits[1];
20     minCol_ = limits[2];
21     maxCol_ = limits[3];
22   }
23 
minRow()24   int minRow() const {
25     return minRow_;
26   }
maxRow()27   int maxRow() const {
28     return maxRow_;
29   }
minCol()30   int minCol() const {
31     return minCol_;
32   }
maxCol()33   int maxCol() const {
34     return maxCol_;
35   }
36 
update(const XlsCell cell)37   void update(const XlsCell cell) {
38     update(cell.row(), cell.col());
39   }
40 
update(const int row,const int col)41   void update(const int row, const int col) {
42     if (minRow_ < 0 || row < minRow_) {
43       minRow_ = row;
44     }
45     if (row > maxRow_) {
46       maxRow_ = row;
47     }
48     if (minCol_ < 0 || col < minCol_) {
49       minCol_ = col;
50     }
51     if (col > maxCol_) {
52       maxCol_ = col;
53     }
54   }
55 
update(const int minRow,const int maxRow,const int minCol,const int maxCol)56   void update(const int minRow, const int maxRow,
57               const int minCol, const int maxCol) {
58     minRow_ = minRow;
59     maxRow_ = maxRow;
60     minCol_ = minCol;
61     maxCol_ = maxCol;
62   }
63 
contains(const XlsCell cell)64   bool contains(const XlsCell cell) const {
65     return contains(cell.row(), cell.col());
66   }
67 
contains(const int i,const int j)68   bool contains(const int i, const int j) const {
69     return contains(minRow_, maxRow_, i) && contains(minCol_, maxCol_, j);
70   }
71 
contains(const int i)72   bool contains(const int i) const {
73     return contains(minRow_, maxRow_, i);
74   }
75 
print()76   void print() {
77     Rcpp::Rcout << "row min, max: " << minRow_ << ", "
78                 << maxRow_ << "\t"
79                 << "col min, max: " << minCol_<< ", "
80                 << maxCol_ << std::endl;
81   }
82 
83 private:
84 
contains(int min,int max,int val)85   bool contains(int min, int max, int val) const {
86     if (min < 0) {
87       if (max < 0) {
88         // min = max = -1 is our convention for 'no limits specified'
89         return true;
90       } else {
91         // min < 0, max >= 0 should never happen, because cellranger should
92         // always turn (-inf, max] into [0, max], but it's harmless to handle
93         return val <= max;
94       }
95     } else {
96       if (max < 0) {
97         // min >= 0, max < 0 is our convention for [min, +inf)
98         return val >= min;
99       } else {
100         // min >= 0, max >= 0 is the straightforward case: [min, max]
101         return val >= min && val <= max;
102       }
103     }
104   }
105 };
106 
107 #endif
108