Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cbegin() and cend() to most containers #748

Merged
merged 8 commits into from
Sep 3, 2017
20 changes: 19 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
2017-09-02 James J Balamuta <balamut2@illinois.edu>
* inst/include/Rcpp/vector/VectorBase.h: Defined both iterator AND
const_iterator for future considerations. Currently, as before,
only const_iterator is available (though it used to be called iterator).
Added cbegin() and cend() functions. We should revisit whether there
should be a non-const iterator on VectorBase in the future.
* inst/include/Rcpp/vector/Matrix.h: Added cbegin() and cend()
functions. Iterator defintions inherited from Vector are appropriate.
* inst/include/Rcpp/vector/MatrixColumn.h: Added cbegin() and cend()
functions. Iterator definitions inherited from Vector are appropriate
because Matrix is column-major.
* inst/include/Rcpp/vector/Vector.h: Added cbegin() and cend()
functions. Iterators are appropriate minus the problem with the const
proxy class of objects (which may or may not actually be const depending
on which one).
* inst/include/Rcpp/vector/traits.h: Removed (accidentally) some
some trailing whitespace on lines.

2017-09-02 Dirk Eddelbuettel <edd@debian.org>

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

2017-08-26 Dirk Eddelbuettel <edd@debian.org>

* .travis.yml (before_install): Use https for curl fetch
* .travis.yml (before_install): Use https for curl fetch

2017-08-14 James J Balamuta <balamut2@illinois.edu>

Expand Down
2 changes: 2 additions & 0 deletions inst/include/Rcpp/vector/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class Matrix : public Vector<RTYPE, StoragePolicy>, public MatrixBase<RTYPE, tru

inline const_iterator begin() const{ return VECTOR::begin() ; }
inline const_iterator end() const{ return VECTOR::end() ; }
inline const_iterator cbegin() const{ return VECTOR::begin() ; }
inline const_iterator cend() const{ return VECTOR::end() ; }
inline iterator begin() { return VECTOR::begin() ; }
inline iterator end() { return VECTOR::end() ; }

Expand Down
30 changes: 23 additions & 7 deletions inst/include/Rcpp/vector/MatrixColumn.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,30 @@ class MatrixColumn : public VectorBase<RTYPE,true,MatrixColumn<RTYPE> > {
return const_start[i] ;
}

inline iterator begin(){
return start ;
}

inline const_iterator begin() const {
return const_start ;
}

inline iterator end(){
return start + n ;
inline const_iterator end() const {
return const_start + n ;
}

inline const_iterator cbegin() const {
return const_start ;
}

inline const_iterator end() const {
inline const_iterator cend() const {
return const_start + n ;
}

inline iterator begin(){
return start ;
}

inline iterator end(){
return start + n ;
}

inline int size() const {
return n ;
}
Expand Down Expand Up @@ -145,6 +153,14 @@ class ConstMatrixColumn : public VectorBase<RTYPE,true,ConstMatrixColumn<RTYPE>
inline const_iterator end() const {
return const_start + n ;
}

inline const_iterator cbegin() const {
return const_start ;
}

inline const_iterator cend() const {
return const_start + n ;
}

inline int size() const {
return n ;
Expand Down
2 changes: 2 additions & 0 deletions inst/include/Rcpp/vector/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ class Vector :
inline iterator end() { return cache.get() + size() ; }
inline const_iterator begin() const{ return cache.get_const() ; }
inline const_iterator end() const{ return cache.get_const() + size() ; }
inline const_iterator cbegin() const{ return cache.get_const() ; }
inline const_iterator cend() const{ return cache.get_const() + size() ; }

inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
Expand Down
78 changes: 50 additions & 28 deletions inst/include/Rcpp/vector/VectorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,68 @@ class VectorBase : public traits::expands_to_logical__impl<RTYPE> {
}

inline R_xlen_t size() const { return static_cast<const VECTOR*>(this)->size() ; }

class iterator {
public:
typedef stored_type reference ;
struct iter_traits
{
typedef stored_type & reference ;
typedef stored_type* pointer ;
typedef R_xlen_t difference_type ;
typedef stored_type value_type;
typedef std::random_access_iterator_tag iterator_category ;

iterator( const VectorBase& object_, R_xlen_t index_ ) : object(object_.get_ref()), index(index_){}
iterator( const iterator& other) : object(other.object), index(other.index){};

inline iterator& operator++(){
};

struct const_iter_traits
{
typedef stored_type reference ;
typedef stored_type const * pointer ;
typedef R_xlen_t difference_type ;
typedef const stored_type value_type;
typedef std::random_access_iterator_tag iterator_category ;
};

template< typename TRAITS >
class iter_base {
public:
typedef typename TRAITS::reference reference;
typedef typename TRAITS::pointer pointer;
typedef typename TRAITS::difference_type difference_type;
typedef typename TRAITS::value_type value_type;
typedef typename TRAITS::iterator_category iterator_category;

iter_base( const VectorBase& object_, R_xlen_t index_ ) : object(object_.get_ref()), index(index_){}

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

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

inline iterator operator+(difference_type n) const {
inline iter_base operator+(difference_type n) const {
return iterator( object, index+n ) ;
}
inline iterator operator-(difference_type n) const {
inline iter_base operator-(difference_type n) const {
return iterator( object, index-n ) ;
}

inline iterator& operator+=(difference_type n) {
inline iter_base& operator+=(difference_type n) {
index += n ;
return *this ;
}
inline iterator& operator-=(difference_type n) {
inline iter_base& operator-=(difference_type n) {
index -= n;
return *this ;
}
Expand All @@ -106,26 +124,26 @@ class VectorBase : public traits::expands_to_logical__impl<RTYPE> {
return &object[index] ;
}

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

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

Expand All @@ -134,11 +152,15 @@ class VectorBase : public traits::expands_to_logical__impl<RTYPE> {
const VECTOR& object ;
R_xlen_t index;
} ;

typedef iterator const_iterator;

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

typedef iter_base< iter_traits > iterator;
typedef iter_base< const_iter_traits > const_iterator;

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

inline const_iterator cbegin() const { return const_iterator(*this, 0) ; }
inline const_iterator cend() const { return const_iterator(*this, size() ) ; }

} ;

Expand Down
26 changes: 13 additions & 13 deletions inst/include/Rcpp/vector/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace traits{
private:
iterator start ;
} ;
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
class proxy_cache{
public:
typedef typename ::Rcpp::Vector<RTYPE, StoragePolicy> VECTOR ;
Expand Down Expand Up @@ -80,23 +80,23 @@ namespace traits{
} ;

// regular types for INTSXP, REALSXP, ...
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
struct r_vector_cache_type {
typedef r_vector_cache<RTYPE, StoragePolicy> type ;
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage>
struct r_vector_cache_type {
typedef r_vector_cache<RTYPE, StoragePolicy> type ;
} ;

// proxy types for VECSXP, STRSXP and EXPRSXP
template <template <class> class StoragePolicy>
struct r_vector_cache_type<VECSXP, StoragePolicy> {
typedef proxy_cache<VECSXP, StoragePolicy> type ;
template <template <class> class StoragePolicy>
struct r_vector_cache_type<VECSXP, StoragePolicy> {
typedef proxy_cache<VECSXP, StoragePolicy> type ;
} ;
template <template <class> class StoragePolicy>
struct r_vector_cache_type<EXPRSXP, StoragePolicy> {
typedef proxy_cache<EXPRSXP, StoragePolicy> type ;
template <template <class> class StoragePolicy>
struct r_vector_cache_type<EXPRSXP, StoragePolicy> {
typedef proxy_cache<EXPRSXP, StoragePolicy> type ;
} ;
template <template <class> class StoragePolicy>
struct r_vector_cache_type<STRSXP, StoragePolicy> {
typedef proxy_cache<STRSXP, StoragePolicy> type ;
template <template <class> class StoragePolicy>
struct r_vector_cache_type<STRSXP, StoragePolicy> {
typedef proxy_cache<STRSXP, StoragePolicy> type ;
} ;

} // traits
Expand Down