File: MatrixRow.h

package info (click to toggle)
rcpp 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,432 kB
  • sloc: ansic: 44,024; cpp: 40,602; sh: 53; makefile: 2
file content (341 lines) | stat: -rw-r--r-- 11,366 bytes parent folder | download | duplicates (3)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// MatrixRow.h: Rcpp R/C++ interface class library -- matrices row
//
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__vector__MatrixRow_h
#define Rcpp__vector__MatrixRow_h

namespace Rcpp{

template <int RTYPE>
class MatrixRow : public VectorBase< RTYPE, true, MatrixRow<RTYPE> > {
public:
    typedef Matrix<RTYPE> MATRIX ;
    typedef typename MATRIX::Proxy Proxy ;
    typedef typename MATRIX::Proxy reference ;
    typedef typename MATRIX::const_Proxy const_reference ;
    typedef typename MATRIX::value_type value_type ;

    // We now have the structure to correct const-correctness, but without
    // major changes to the proxy mechanism, we cannot do it correctly.  As
    // a result, it turns out that both MatrixRow iterators are effectively
    // const, but this has always been the way it is so we won't break any
    // compatibility.  TODO: Fix proxies and make this const correct.
    struct iter_traits
    {
        typedef typename traits::r_vector_iterator<RTYPE>::type vector_iterator;
        
        typedef int difference_type;
        typedef typename traits::r_vector_proxy<RTYPE>::type value_type;
        typedef value_type reference;
        typedef typename std::iterator_traits<vector_iterator>::pointer pointer;
    };
    
    struct const_iter_traits
    {
        typedef typename traits::r_vector_const_iterator<RTYPE>::type vector_iterator;
        
        typedef int difference_type;
        typedef typename traits::r_vector_proxy<RTYPE>::type value_type;
        typedef value_type reference;
        typedef typename std::iterator_traits<vector_iterator>::pointer pointer;
    };
    
    template< typename TRAITS >
    class iter_base {
    public:
        typedef typename TRAITS::vector_iterator vector_iterator;

        typedef typename TRAITS::difference_type difference_type ;
        typedef typename TRAITS::value_type value_type ;
        typedef typename TRAITS::reference reference ;
        typedef typename TRAITS::pointer pointer ;

        typedef std::random_access_iterator_tag iterator_category ;

        iter_base( const iter_base& other) : row(other.row), index(other.index){}
        iter_base( MatrixRow& row_, int index_ ) : row(row_), index(index_){}

        iter_base& operator++(){
            index++;
            return *this ;
        }
        iter_base operator++(int) {
            iterator orig(*this);
            index++ ;
            return orig ;
        }

        iter_base& operator--(){
            index-- ;
            return *this ;
        }
        iter_base operator--(int){
            iter_base orig(*this);
            index-- ;
            return orig ;
        }

        iter_base operator+(difference_type n) const { return iter_base( row, index + n ) ; }
        iter_base operator-(difference_type n) const { return iter_base( row, index - n ) ; }
        difference_type operator-(const iter_base& other) const { return index - other.index ; }

        iter_base& operator+=(difference_type n) { index += n ; return *this ;}
        iter_base& operator-=(difference_type n) { index -= n ; return *this ;}

        reference operator*() {
            return row[index] ;
        }
        pointer operator->(){
            return &row[index] ;
        }

        bool operator==( const iter_base& other) { return index == other.index ; }
        bool operator!=( const iter_base& other) { return index != other.index ; }
        bool operator<( const iter_base& other ) { return index < other.index ;}
        bool operator>( const iter_base& other ) { return index > other.index ;}
        bool operator<=( const iter_base& other ) { return index <= other.index ; }
        bool operator>=( const iter_base& other ) { return index >= other.index ; }

        inline reference operator[]( int i) const {
            return row[ index + i ] ;
        }

        difference_type operator-(const iter_base& other) {
            return index - other.index ;
        }

    private:
        MatrixRow& row ;
        int index ;
    };
    
    typedef iter_base< iter_traits > iterator;
    typedef iter_base< const_iter_traits > const_iterator;

    MatrixRow( MATRIX& object, int i ) :
        parent(object),
        start(parent.begin() + i),
        parent_nrow(parent.nrow()),
        row(i)
    {
        if( i < 0 || i >= parent.nrow() ) {
            const char* fmt = "Row index is out of bounds: "
                              "[index=%i; row extent=%i].";
            throw index_out_of_bounds(fmt, i, parent.nrow()) ;
        }
    }

    MatrixRow( const MatrixRow& other ) :
        parent(other.parent),
        start(other.start),
        parent_nrow(other.parent_nrow),
        row(other.row)
    {} ;

    template <int RT, bool NA, typename T>
    MatrixRow& operator=( const Rcpp::VectorBase<RT,NA,T>& rhs ){
        int n = size() ;
        const T& ref = rhs.get_ref() ;
        RCPP_LOOP_UNROLL_LHSFUN(start,get_parent_index,ref)
            return *this ;
    }

    MatrixRow& operator=( const MatrixRow& rhs ){
        int n = size() ;
        RCPP_LOOP_UNROLL_LHSFUN(start,get_parent_index,rhs)
            return *this ;
    }

    inline reference operator[]( int i ){
        return start[ get_parent_index(i) ] ;
    }

    inline reference operator[]( int i ) const {
        return parent[ row + i * parent_nrow ] ;
    }

    inline iterator begin(){
        return iterator( *this, 0 ) ;
    }

    inline iterator end(){
        return iterator( *this, size() ) ;
    }

    inline const_iterator begin() const {
        return const_iterator( const_cast<MatrixRow&>(*this), 0 ) ;
    }

    inline const_iterator end() const {
        return const_iterator( const_cast<MatrixRow&>(*this), size() ) ;
    }
    
    inline const_iterator cbegin() const {
        return const_iterator( const_cast<MatrixRow&>(*this), 0 ) ;
    }

    inline const_iterator cend() const {
        return const_iterator( const_cast<MatrixRow&>(*this), size() ) ;
    }

    inline int size() const {
        return parent.ncol() ;
    }

private:
    MATRIX& parent;
    typename MATRIX::iterator start ;
    int parent_nrow ;
    int row ;

    inline int get_parent_index(int i) const {
        RCPP_DEBUG_4( "MatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
        return i * parent_nrow ;
    }
} ;

template <int RTYPE>
class ConstMatrixRow : public VectorBase< RTYPE, true, ConstMatrixRow<RTYPE> > {
public:
    typedef Matrix<RTYPE> MATRIX ;
    typedef typename MATRIX::const_Proxy const_reference ;
    typedef typename MATRIX::value_type value_type ;

    class const_iterator {
    public:
        typedef typename traits::r_vector_iterator<RTYPE>::type vector_iterator ;

        typedef int difference_type ;
        typedef typename traits::r_vector_const_proxy<RTYPE>::type value_type ;
        typedef typename traits::r_vector_const_proxy<RTYPE>::type reference ;
        typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;

        typedef std::random_access_iterator_tag iterator_category ;

        const_iterator( const const_iterator& other) : row(other.row), index(other.index){}
        const_iterator( const ConstMatrixRow& row_, int index_ ) : row(row_), index(index_){}

        const_iterator& operator++(){
            index++;
            return *this ;
        }
        const_iterator operator++(int) {
            const_iterator orig(*this);
            index++ ;
            return orig ;
        }

        const_iterator& operator--(){
            index-- ;
            return *this ;
        }
        const_iterator operator--(int){
            const_iterator orig(*this);
            index-- ;
            return orig ;
        }

        const_iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }
        const_iterator operator-(difference_type n) const { return iterator( row, index - n ) ; }
        difference_type operator-(const const_iterator& other) const { return index - other.index ; }

        const_iterator& operator+=(difference_type n) { index += n ; return *this ;}
        const_iterator& operator-=(difference_type n) { index -= n ; return *this ;}

        const reference operator*() {
            return row[index] ;
        }
        const pointer operator->(){
            return &row[index] ;
        }

        bool operator==( const const_iterator& other) { return index == other.index ; }
        bool operator!=( const const_iterator& other) { return index != other.index ; }
        bool operator<( const const_iterator& other ) { return index < other.index ;}
        bool operator>( const const_iterator& other ) { return index > other.index ;}
        bool operator<=( const const_iterator& other ) { return index <= other.index ; }
        bool operator>=( const const_iterator& other ) { return index >= other.index ; }

        inline const reference operator[]( int i) const {
            return row[ index + i ] ;
        }

        difference_type operator-(const const_iterator& other) {
            return index - other.index ;
        }

    private:
        const ConstMatrixRow& row ;
        int index ;
    } ;

    typedef const_iterator iterator;

    ConstMatrixRow( const MATRIX& object, int i ) :
        parent(object),
        start(parent.begin() + i),
        parent_nrow(parent.nrow()),
        row(i)
    {
        if( i < 0 || i >= parent.nrow() ) {
            const char* fmt = "Row index is out of bounds: "
                              "[index=%i; row extent=%i].";
            throw index_out_of_bounds(fmt, i, parent.nrow()) ;
        }
    }

    ConstMatrixRow( const ConstMatrixRow& other ) :
        parent(other.parent),
        start(other.start),
        parent_nrow(other.parent_nrow),
        row(other.row)
    {} ;

    inline const_reference operator[]( int i ) const {
        return parent[ row + i * parent_nrow ] ;
    }

    inline const_iterator begin() const {
        return const_iterator( *this, 0 ) ;
    }

    inline const_iterator end() const {
        return const_iterator( *this, size() ) ;
    }

    inline int size() const {
        return parent.ncol() ;
    }

private:
    const MATRIX& parent;
    typename MATRIX::const_iterator start ;
    int parent_nrow ;
    int row ;

    inline int get_parent_index(int i) const {
        RCPP_DEBUG_4( "ConstMatrixRow<%d>::get_parent_index(int = %d), parent_nrow = %d >> %d\n", RTYPE, i, parent_nrow, i*parent_nrow )
        return i * parent_nrow ;
    }
} ;
}

#endif