@@ -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
0 commit comments