Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
VectorBase.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // VectorBase.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__vector__VectorBase_h
23 #define Rcpp__vector__VectorBase_h
24 
25 namespace Rcpp{
26 
28 template <int RTYPE, bool na, typename VECTOR>
30 public:
32  struct r_type : traits::integral_constant<int,RTYPE>{} ;
33  struct can_have_na : traits::integral_constant<bool,na>{} ;
36 
37  VECTOR& get_ref(){
38  return static_cast<VECTOR&>(*this) ;
39  }
40 
41  const VECTOR& get_ref() const {
42  return static_cast<const VECTOR&>(*this) ;
43  }
44 
45  inline stored_type operator[]( int i) const {
46  return static_cast<const VECTOR*>(this)->operator[](i) ;
47  }
48 
49  inline int size() const { return static_cast<const VECTOR*>(this)->size() ; }
50 
51  class iterator {
52  public:
54  typedef stored_type* pointer ;
55  typedef int difference_type ;
57  typedef std::random_access_iterator_tag iterator_category ;
58 
59  iterator( const VectorBase& object_, int index_ ) : object(object_.get_ref()), index(index_){}
60  iterator( const iterator& other) : object(other.object), index(other.index){};
61 
62  inline iterator& operator++(){
63  index++ ;
64  return *this ;
65  }
66  inline iterator operator++(int){
67  iterator orig(*this);
68  ++(*this) ;
69  return orig ;
70  }
71 
72  inline iterator& operator--(){
73  index-- ;
74  return *this ;
75  }
76  inline iterator operator--(int){
77  iterator orig(*this);
78  --(*this) ;
79  return orig ;
80  }
81 
83  return iterator( object, index+n ) ;
84  }
86  return iterator( object, index-n ) ;
87  }
88 
90  index += n ;
91  return *this ;
92  }
94  index -= n;
95  return *this ;
96  }
97 
98  inline reference operator[](int i){
99  return object[index+i] ;
100  }
101 
102  inline reference operator*() {
103  return object[index] ;
104  }
105  inline pointer operator->(){
106  return &object[index] ;
107  }
108 
109  inline bool operator==( const iterator& y) const {
110  return ( index == y.index ) ;
111  }
112  inline bool operator!=( const iterator& y) const {
113  return ( index != y.index ) ;
114  }
115  inline bool operator<( const iterator& other ) const {
116  return index < other.index ;
117  }
118  inline bool operator>( const iterator& other ) const {
119  return index > other.index ;
120  }
121  inline bool operator<=( const iterator& other ) const {
122  return index <= other.index ;
123  }
124  inline bool operator>=( const iterator& other ) const {
125  return index >= other.index ;
126  }
127 
128  inline difference_type operator-(const iterator& other) const {
129  return index - other.index ;
130  }
131 
132 
133  private:
134  const VECTOR& object ;
135  int index;
136  } ;
137 
138  inline iterator begin() const { return iterator(*this, 0) ; }
139  inline iterator end() const { return iterator(*this, size() ) ; }
140 
141 } ;
142 
143 } // namespace Rcpp
144 #endif