File: RMatrix.h

package info (click to toggle)
r-cran-rcppparallel 5.1.7%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: cpp: 1,852; sh: 19; makefile: 11
file content (268 lines) | stat: -rw-r--r-- 7,374 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifndef __RCPP_PARALLEL_RMATRIX__
#define __RCPP_PARALLEL_RMATRIX__

#include <cstddef>
#include <iterator>

namespace RcppParallel {

template <typename T>
class RMatrix {
public:
   class Row {
   
   public:   
      
      template <typename V>
      class row_iterator {
      
      public:
         using iterator_category = std::random_access_iterator_tag;
         using value_type = V;
         using difference_type = std::size_t;
         using pointer = value_type*;
         using reference = value_type&;

         inline row_iterator(Row& row, difference_type i)
            : start_(row.start_), parentNrow_(row.parent_.nrow()), index_(i)
         {
         }
         
         inline row_iterator(pointer start, difference_type parentNrow, difference_type index)
            : start_(start), parentNrow_(parentNrow), index_(index)
         {      
         }
         
         inline row_iterator(const row_iterator& other) 
            : start_(other.start_), 
              parentNrow_(other.parentNrow_), 
              index_(other.index_)
         {
         }
         
         inline row_iterator& operator++() { 
            index_++;
            return *this;
         }
         
         inline row_iterator operator++(int) {
            row_iterator tmp(*this); 
            operator++(); 
            return tmp;
         }
         
         inline row_iterator& operator--() {
            index_-- ;
            return *this ;
         }
         
         inline row_iterator operator--(int) {
            row_iterator tmp(*this);
            index_-- ;
            return tmp ;
         }

         row_iterator operator+(difference_type n) const {
            return row_iterator(start_, parentNrow_ ,index_ + n ) ; 
         }
         row_iterator operator-(difference_type n) const {
            return row_iterator(start_, parentNrow_, index_ - n ) ; 
         }
         
         difference_type operator+(const row_iterator& other) const {
            return index_ + other.index_;
         }
         
         difference_type operator-(const row_iterator& other) const {
            return index_ - other.index_ ; 
         }
         
         row_iterator& operator+=(difference_type n) { index_ += n ; return *this; }
         row_iterator& operator-=(difference_type n) { index_ -= n ; return *this; }
         
         bool operator==(const row_iterator& other) const { return index_ == other.index_; }
         bool operator!=(const row_iterator& other) const { return index_ != other.index_; }
         bool operator<(const row_iterator& other) const { return index_ < other.index_; }
         bool operator>(const row_iterator& other) const { return index_ > other.index_; }
         bool operator<=(const row_iterator& other) const { return index_ <= other.index_; }
         bool operator>=(const row_iterator& other) const { return index_ >= other.index_; }
         

         inline reference operator*() { return start_[index_ * parentNrow_]; }
         
         inline pointer operator->() { return &(start_[index_ * parentNrow_]); }
      
         inline reference operator[](int i) { return start_[(index_+i) * parentNrow_]; }
         
      private:
         pointer start_;
         difference_type parentNrow_;
         difference_type index_;
      };
      
      typedef row_iterator<T> iterator;
      typedef row_iterator<const T> const_iterator;
      
      inline Row(RMatrix& parent, std::size_t i)
         : parent_(parent),
           start_(parent.begin() + i)
      {
      }
      
      inline Row(const Row& other)
         : parent_(other.parent_),
           start_(other.start_)
      {        
      }
      
      inline iterator begin() {
         return iterator(*this, 0);
      }
      
      inline iterator end() {
         return iterator(*this, parent_.ncol());
      }
      
      inline const_iterator begin() const {
         return const_iterator(*this, 0);
      }
      
      inline const_iterator end() const {
         return const_iterator(*this, parent_.ncol());
      }
      
      inline size_t length() const {
         return parent_.ncol();
      }

      inline size_t size() const {
        return parent_.ncol();
      }

      inline T& operator[](std::size_t i) {
        return start_[i * parent_.nrow()];
      }
      
      inline const T& operator[](std::size_t i) const {
        return start_[i * parent_.nrow()];
      }
              
   private:
      RMatrix& parent_;
      T* start_;
   };
   
   class Column {
   
   public:
   
      typedef T* iterator;
      typedef const T* const_iterator;
   
      inline Column(RMatrix& parent, std::size_t i) 
         : begin_(parent.begin() + (i * parent.nrow())),
           end_(begin_ + parent.nrow())
      {   
      }
      
      inline Column(const Column& other) 
         : begin_(other.begin_), end_(other.end_)
      {   
      }
      
      inline Column& operator=(const Column& rhs) {
         begin_ = rhs.begin_;
         end_ = rhs.end_;
         return *this;
      }
      
      inline iterator begin() { return begin_; }
      inline iterator end() { return end_; }
      
      inline const_iterator begin() const { return begin_; }
      inline const_iterator end() const { return end_; }
      
      inline size_t length() const { return end_ - begin_; }
      inline size_t size() const { return end_ - begin_; }
      
      inline T& operator[](std::size_t i) {
        return *(begin_ + i);
      }
      
      inline const T& operator[](std::size_t i) const {
        return *(begin_ + i);
      }
      
   private:
      T* begin_;
      T* end_;
   };

   typedef T* iterator;
   typedef const T* const_iterator;

   template <typename Source>
   inline explicit RMatrix(const Source& source) 
      : data_(const_cast<Source&>(source).begin()),
        nrow_(source.nrow()),
        ncol_(source.ncol())
   {
   }

   inline RMatrix(T* data, std::size_t nrow, std::size_t ncol) 
      : data_(data), nrow_(nrow), ncol_(ncol) 
   {
   }
   
   inline iterator begin() { return data_; }
   inline iterator end() { return data_ + length(); }
   
   inline const_iterator begin() const { return data_; }
   inline const_iterator end() const { return data_ + length(); }
     
   inline std::size_t length() const { return nrow_ * ncol_; }  
     
   inline std::size_t nrow() const { return nrow_; }
   inline std::size_t ncol() const { return ncol_; }
   
   inline T& operator()(std::size_t i, std::size_t j) {
      return *(data_ + (i + j * nrow_));
   }
   
   inline const T& operator()(std::size_t i, std::size_t j) const {
      return *(data_ + (i + j * nrow_));
   }
   
   inline Row row(std::size_t i) {
      return Row(*this, i);
   }
   
   inline const Row row(std::size_t i) const {
      return Row(*const_cast<RMatrix*>(this), i);
   }
   
   inline Column column(std::size_t i) {
      return Column(*this, i);
   }
   
   inline const Column column(std::size_t i) const {
      return Column(*const_cast<RMatrix*>(this), i);
   }
   
   inline T& operator[](std::size_t i) {
      return *(data_ + i);
   }
   
   inline const T& operator[](std::size_t i) const {
      return *(data_ + i);
   }
   
private:
   T* data_;
   std::size_t nrow_;
   std::size_t ncol_;
};

} // namespace RcppParallel

#endif // __RCPP_PARALLEL_RMATRIX__