Skip to content

Commit cb54c77

Browse files
dcdilloneddelbuettel
authored andcommitted
Added cbegin() and cend() to most containers (#748)
* Prefixed const before const_iterator where appropriate. * Created a real const_iterator for VectorBase and removed some unnecessary const's * Cheating to fix compile issues. Need to remove before we are finished, but we're not getting anywhere without it for now. * Oops...fixed const. * Added cbegin()/cend() to most vector derivatives. * Final cleanup for PR
1 parent 30faace commit cb54c77

File tree

6 files changed

+109
-49
lines changed

6 files changed

+109
-49
lines changed

ChangeLog

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
2017-09-02 James J Balamuta <[email protected]>
2+
* inst/include/Rcpp/vector/VectorBase.h: Defined both iterator AND
3+
const_iterator for future considerations. Currently, as before,
4+
only const_iterator is available (though it used to be called iterator).
5+
Added cbegin() and cend() functions. We should revisit whether there
6+
should be a non-const iterator on VectorBase in the future.
7+
* inst/include/Rcpp/vector/Matrix.h: Added cbegin() and cend()
8+
functions. Iterator defintions inherited from Vector are appropriate.
9+
* inst/include/Rcpp/vector/MatrixColumn.h: Added cbegin() and cend()
10+
functions. Iterator definitions inherited from Vector are appropriate
11+
because Matrix is column-major.
12+
* inst/include/Rcpp/vector/Vector.h: Added cbegin() and cend()
13+
functions. Iterators are appropriate minus the problem with the const
14+
proxy class of objects (which may or may not actually be const depending
15+
on which one).
16+
* inst/include/Rcpp/vector/traits.h: Removed (accidentally) some
17+
some trailing whitespace on lines.
18+
119
2017-09-02 Dirk Eddelbuettel <[email protected]>
220

321
* DESCRIPTION (Version): Roll minor version
@@ -23,7 +41,7 @@
2341

2442
2017-08-26 Dirk Eddelbuettel <[email protected]>
2543

26-
* .travis.yml (before_install): Use https for curl fetch
44+
* .travis.yml (before_install): Use https for curl fetch
2745

2846
2017-08-14 James J Balamuta <[email protected]>
2947

inst/include/Rcpp/vector/Matrix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru
113113

114114
inline const_iterator begin() const{ return VECTOR::begin() ; }
115115
inline const_iterator end() const{ return VECTOR::end() ; }
116+
inline const_iterator cbegin() const{ return VECTOR::begin() ; }
117+
inline const_iterator cend() const{ return VECTOR::end() ; }
116118
inline iterator begin() { return VECTOR::begin() ; }
117119
inline iterator end() { return VECTOR::end() ; }
118120

inst/include/Rcpp/vector/MatrixColumn.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,30 @@ class MatrixColumn : public VectorBase<RTYPE,true,MatrixColumn<RTYPE> > {
8484
return const_start[i] ;
8585
}
8686

87-
inline iterator begin(){
88-
return start ;
89-
}
90-
9187
inline const_iterator begin() const {
9288
return const_start ;
9389
}
9490

95-
inline iterator end(){
96-
return start + n ;
91+
inline const_iterator end() const {
92+
return const_start + n ;
93+
}
94+
95+
inline const_iterator cbegin() const {
96+
return const_start ;
9797
}
9898

99-
inline const_iterator end() const {
99+
inline const_iterator cend() const {
100100
return const_start + n ;
101101
}
102102

103+
inline iterator begin(){
104+
return start ;
105+
}
106+
107+
inline iterator end(){
108+
return start + n ;
109+
}
110+
103111
inline int size() const {
104112
return n ;
105113
}
@@ -145,6 +153,14 @@ class ConstMatrixColumn : public VectorBase<RTYPE,true,ConstMatrixColumn<RTYPE>
145153
inline const_iterator end() const {
146154
return const_start + n ;
147155
}
156+
157+
inline const_iterator cbegin() const {
158+
return const_start ;
159+
}
160+
161+
inline const_iterator cend() const {
162+
return const_start + n ;
163+
}
148164

149165
inline int size() const {
150166
return n ;

inst/include/Rcpp/vector/Vector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ class Vector :
333333
inline iterator end() { return cache.get() + size() ; }
334334
inline const_iterator begin() const{ return cache.get_const() ; }
335335
inline const_iterator end() const{ return cache.get_const() + size() ; }
336+
inline const_iterator cbegin() const{ return cache.get_const() ; }
337+
inline const_iterator cend() const{ return cache.get_const() + size() ; }
336338

337339
inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
338340
inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }

inst/include/Rcpp/vector/VectorBase.h

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,50 +47,68 @@ class VectorBase : public traits::expands_to_logical__impl<RTYPE> {
4747
}
4848

4949
inline R_xlen_t size() const { return static_cast<const VECTOR*>(this)->size() ; }
50-
51-
class iterator {
52-
public:
53-
typedef stored_type reference ;
50+
51+
struct iter_traits
52+
{
53+
typedef stored_type & reference ;
5454
typedef stored_type* pointer ;
5555
typedef R_xlen_t difference_type ;
5656
typedef stored_type value_type;
5757
typedef std::random_access_iterator_tag iterator_category ;
58-
59-
iterator( const VectorBase& object_, R_xlen_t index_ ) : object(object_.get_ref()), index(index_){}
60-
iterator( const iterator& other) : object(other.object), index(other.index){};
61-
62-
inline iterator& operator++(){
58+
};
59+
60+
struct const_iter_traits
61+
{
62+
typedef stored_type reference ;
63+
typedef stored_type const * pointer ;
64+
typedef R_xlen_t difference_type ;
65+
typedef const stored_type value_type;
66+
typedef std::random_access_iterator_tag iterator_category ;
67+
};
68+
69+
template< typename TRAITS >
70+
class iter_base {
71+
public:
72+
typedef typename TRAITS::reference reference;
73+
typedef typename TRAITS::pointer pointer;
74+
typedef typename TRAITS::difference_type difference_type;
75+
typedef typename TRAITS::value_type value_type;
76+
typedef typename TRAITS::iterator_category iterator_category;
77+
78+
iter_base( const VectorBase& object_, R_xlen_t index_ ) : object(object_.get_ref()), index(index_){}
79+
80+
inline iter_base& operator++(){
6381
index++ ;
6482
return *this ;
6583
}
66-
inline iterator operator++(int){
84+
inline iter_base operator++(int){
6785
iterator orig(*this);
6886
++(*this) ;
6987
return orig ;
7088
}
7189

72-
inline iterator& operator--(){
90+
inline iter_base& operator--(){
7391
index-- ;
7492
return *this ;
7593
}
76-
inline iterator operator--(int){
94+
inline iter_base operator--(int){
7795
iterator orig(*this);
7896
--(*this) ;
7997
return orig ;
8098
}
8199

82-
inline iterator operator+(difference_type n) const {
100+
inline iter_base operator+(difference_type n) const {
83101
return iterator( object, index+n ) ;
84102
}
85-
inline iterator operator-(difference_type n) const {
103+
inline iter_base operator-(difference_type n) const {
86104
return iterator( object, index-n ) ;
87105
}
88106

89-
inline iterator& operator+=(difference_type n) {
107+
inline iter_base& operator+=(difference_type n) {
90108
index += n ;
91109
return *this ;
92110
}
93-
inline iterator& operator-=(difference_type n) {
111+
inline iter_base& operator-=(difference_type n) {
94112
index -= n;
95113
return *this ;
96114
}
@@ -106,26 +124,26 @@ class VectorBase : public traits::expands_to_logical__impl<RTYPE> {
106124
return &object[index] ;
107125
}
108126

109-
inline bool operator==( const iterator& y) const {
127+
inline bool operator==( const iter_base& y) const {
110128
return ( index == y.index ) ;
111129
}
112-
inline bool operator!=( const iterator& y) const {
130+
inline bool operator!=( const iter_base& y) const {
113131
return ( index != y.index ) ;
114132
}
115-
inline bool operator<( const iterator& other ) const {
133+
inline bool operator<( const iter_base& other ) const {
116134
return index < other.index ;
117135
}
118-
inline bool operator>( const iterator& other ) const {
136+
inline bool operator>( const iter_base& other ) const {
119137
return index > other.index ;
120138
}
121-
inline bool operator<=( const iterator& other ) const {
139+
inline bool operator<=( const iter_base& other ) const {
122140
return index <= other.index ;
123141
}
124-
inline bool operator>=( const iterator& other ) const {
142+
inline bool operator>=( const iter_base& other ) const {
125143
return index >= other.index ;
126144
}
127145

128-
inline difference_type operator-(const iterator& other) const {
146+
inline difference_type operator-(const iter_base& other) const {
129147
return index - other.index ;
130148
}
131149

@@ -134,11 +152,15 @@ class VectorBase : public traits::expands_to_logical__impl<RTYPE> {
134152
const VECTOR& object ;
135153
R_xlen_t index;
136154
} ;
137-
138-
typedef iterator const_iterator;
139-
140-
inline iterator begin() const { return iterator(*this, 0) ; }
141-
inline iterator end() const { return iterator(*this, size() ) ; }
155+
156+
typedef iter_base< iter_traits > iterator;
157+
typedef iter_base< const_iter_traits > const_iterator;
158+
159+
inline const_iterator begin() const { return const_iterator(*this, 0) ; }
160+
inline const_iterator end() const { return const_iterator(*this, size() ) ; }
161+
162+
inline const_iterator cbegin() const { return const_iterator(*this, 0) ; }
163+
inline const_iterator cend() const { return const_iterator(*this, size() ) ; }
142164

143165
} ;
144166

inst/include/Rcpp/vector/traits.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace traits{
5151
private:
5252
iterator start ;
5353
} ;
54-
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
54+
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
5555
class proxy_cache{
5656
public:
5757
typedef typename ::Rcpp::Vector<RTYPE, StoragePolicy> VECTOR ;
@@ -80,23 +80,23 @@ namespace traits{
8080
} ;
8181

8282
// regular types for INTSXP, REALSXP, ...
83-
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
84-
struct r_vector_cache_type {
85-
typedef r_vector_cache<RTYPE, StoragePolicy> type ;
83+
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
84+
struct r_vector_cache_type {
85+
typedef r_vector_cache<RTYPE, StoragePolicy> type ;
8686
} ;
8787

8888
// proxy types for VECSXP, STRSXP and EXPRSXP
89-
template <template <class> class StoragePolicy>
90-
struct r_vector_cache_type<VECSXP, StoragePolicy> {
91-
typedef proxy_cache<VECSXP, StoragePolicy> type ;
89+
template <template <class> class StoragePolicy>
90+
struct r_vector_cache_type<VECSXP, StoragePolicy> {
91+
typedef proxy_cache<VECSXP, StoragePolicy> type ;
9292
} ;
93-
template <template <class> class StoragePolicy>
94-
struct r_vector_cache_type<EXPRSXP, StoragePolicy> {
95-
typedef proxy_cache<EXPRSXP, StoragePolicy> type ;
93+
template <template <class> class StoragePolicy>
94+
struct r_vector_cache_type<EXPRSXP, StoragePolicy> {
95+
typedef proxy_cache<EXPRSXP, StoragePolicy> type ;
9696
} ;
97-
template <template <class> class StoragePolicy>
98-
struct r_vector_cache_type<STRSXP, StoragePolicy> {
99-
typedef proxy_cache<STRSXP, StoragePolicy> type ;
97+
template <template <class> class StoragePolicy>
98+
struct r_vector_cache_type<STRSXP, StoragePolicy> {
99+
typedef proxy_cache<STRSXP, StoragePolicy> type ;
100100
} ;
101101

102102
} // traits

0 commit comments

Comments
 (0)