|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // VectorBase.h: Rcpp R/C++ interface class library -- 00004 // 00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #ifndef Rcpp__vector__VectorBase_h 00023 #define Rcpp__vector__VectorBase_h 00024 00025 namespace Rcpp{ 00026 00028 template <int RTYPE, bool na, typename VECTOR> 00029 class VectorBase : public traits::expands_to_logical__impl<RTYPE> { 00030 public: 00031 struct rcpp_sugar_expression{} ; 00032 struct r_type : traits::integral_constant<int,RTYPE>{} ; 00033 struct can_have_na : traits::integral_constant<bool,na>{} ; 00034 typedef typename traits::storage_type<RTYPE>::type stored_type ; 00035 typedef typename traits::storage_type<RTYPE>::type elem_type ; 00036 00037 VECTOR& get_ref(){ 00038 return static_cast<VECTOR&>(*this) ; 00039 } 00040 00041 const VECTOR& get_ref() const { 00042 return static_cast<const VECTOR&>(*this) ; 00043 } 00044 00045 inline stored_type operator[]( int i) const { 00046 return static_cast<const VECTOR*>(this)->operator[](i) ; 00047 } 00048 00049 inline int size() const { return static_cast<const VECTOR*>(this)->size() ; } 00050 00051 class iterator { 00052 public: 00053 typedef stored_type reference ; 00054 typedef stored_type* pointer ; 00055 typedef int difference_type ; 00056 typedef stored_type value_type; 00057 typedef std::random_access_iterator_tag iterator_category ; 00058 00059 iterator( const VectorBase& object_, int index_ ) : object(object_.get_ref()), index(index_){} 00060 iterator( const iterator& other) : object(other.object), index(other.index){}; 00061 00062 inline iterator& operator++(){ 00063 index++ ; 00064 return *this ; 00065 } 00066 inline iterator& operator++(int){ 00067 index++; 00068 return *this ; 00069 } 00070 00071 inline iterator& operator--(){ 00072 index-- ; 00073 return *this ; 00074 } 00075 inline iterator& operator--(int){ 00076 index--; 00077 return *this ; 00078 } 00079 00080 inline iterator operator+(difference_type n) const { 00081 return iterator( object, index+n ) ; 00082 } 00083 inline iterator operator-(difference_type n) const { 00084 return iterator( object, index-n ) ; 00085 } 00086 00087 inline iterator& operator+=(difference_type n) { 00088 index += n ; 00089 return *this ; 00090 } 00091 inline iterator& operator-=(difference_type n) { 00092 index -= n; 00093 return *this ; 00094 } 00095 00096 inline reference operator[](int i){ 00097 return object[index+i] ; 00098 } 00099 00100 inline reference operator*() { 00101 return object[index] ; 00102 } 00103 inline pointer operator->(){ 00104 return &object[index] ; 00105 } 00106 00107 inline bool operator==( const iterator& y) const { 00108 return ( index == y.index ) ; 00109 } 00110 inline bool operator!=( const iterator& y) const { 00111 return ( index != y.index ) ; 00112 } 00113 inline bool operator<( const iterator& other ) const { 00114 return index < other.index ; 00115 } 00116 inline bool operator>( const iterator& other ) const { 00117 return index > other.index ; 00118 } 00119 inline bool operator<=( const iterator& other ) const { 00120 return index <= other.index ; 00121 } 00122 inline bool operator>=( const iterator& other ) const { 00123 return index >= other.index ; 00124 } 00125 00126 inline difference_type operator-(const iterator& other) const { 00127 return index - other.index ; 00128 } 00129 00130 00131 private: 00132 const VECTOR& object ; 00133 int index; 00134 } ; 00135 00136 inline iterator begin() const { return iterator(*this, 0) ; } 00137 inline iterator end() const { return iterator(*this, size() ) ; } 00138 00139 } ; 00140 00141 } // namespace Rcpp 00142 #endif