Rcpp Version 1.0.9
iterator.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // iterator.h: Rcpp R/C++ interface class library --
4 //
5 // Copyright (C) 2012 - 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__sugar__tools_iterator_h
23 #define Rcpp__sugar__tools_iterator_h
24 
25 namespace Rcpp {
26 namespace sugar {
27 
28  /* generic sugar iterator type */
29  template <typename T>
30  class SugarIterator {
31  public:
32 
33  typedef R_xlen_t difference_type ;
36  typedef STORAGE_TYPE* pointer ;
37  typedef std::random_access_iterator_tag iterator_category ;
39 
40  SugarIterator( const T& ref_ ) :ref(ref_), index(0) {}
41  SugarIterator( const T& ref_, R_xlen_t index_) : ref(ref_), index(index_) {}
42  SugarIterator( const SugarIterator& other) : ref(other.ref), index(other.index){}
43 
44  inline iterator& operator++(){ index++; return *this ; }
45  inline iterator operator++(int){
46  iterator orig(*this) ;
47  ++(*this);
48  return orig ;
49  }
50  inline iterator& operator--(){ index--; return *this ; }
51  inline iterator operator--(int){
52  iterator orig(*this) ;
53  --(*this);
54  return orig ;
55  }
57  return iterator( ref, index+n ) ;
58  }
60  return iterator( ref, index-n ) ;
61  }
63  index += n ;
64  return *this ;
65  }
67  index -= n;
68  return *this ;
69  }
70  inline reference operator[](R_xlen_t i){
71  return ref[index+i] ;
72  }
73 
74  inline reference operator*() {
75  return ref[index] ;
76  }
77  inline pointer operator->(){
78  return &ref[index] ;
79  }
80 
81  inline bool operator==( const iterator& y) const {
82  return ( index == y.index ) ;
83  }
84  inline bool operator!=( const iterator& y) const {
85  return ( index != y.index ) ;
86  }
87  inline bool operator<( const iterator& other ) const {
88  return index < other.index ;
89  }
90  inline bool operator>( const iterator& other ) const {
91  return index > other.index ;
92  }
93  inline bool operator<=( const iterator& other ) const {
94  return index <= other.index ;
95  }
96  inline bool operator>=( const iterator& other ) const {
97  return index >= other.index ;
98  }
99 
100  inline difference_type operator-(const iterator& other) const {
101  return index - other.index ;
102  }
103 
104 
105  private:
106  const T& ref ;
107  R_xlen_t index ;
108  } ;
109 
110  template <typename T> struct sugar_const_iterator_type {
112  } ;
113  template <int RTYPE> struct sugar_const_iterator_type< Rcpp::Vector<RTYPE> >{
115  } ;
117  typedef SEXP* type ;
118  } ;
119 
120 
121  template <typename T> struct is_sugar_vector : public Rcpp::traits::false_type{} ;
122  template <int RTYPE> struct is_sugar_vector< Rcpp::Vector<RTYPE> > : public Rcpp::traits::true_type{} ;
123 
124 
125  template <typename T>
127  return obj.begin() ;
128  }
129  template <typename T>
131  typedef typename sugar_const_iterator_type<T>::type const_iterator ;
132  return const_iterator( obj ) ;
133  }
134 
135 
136 
137  template <typename T>
139  return get_const_begin__impl( obj, typename is_sugar_vector<T>::type() ) ;
140  }
141  /* full specialization for character vectors */
142  template <>
143  inline SEXP* get_const_begin(const CharacterVector& obj){
144  return get_string_ptr(obj) ;
145  }
146 
147  template <typename T>
148  inline typename sugar_const_iterator_type<T>::type get_const_end(const T& obj){
149  return get_const_begin<T>(obj) + obj.size() ;
150  }
151 
152 
153 }
154 }
155 #endif
SEXP * get_string_ptr(SEXP)
Definition: routines.h:234
traits::r_vector_const_iterator< RTYPE, StoragePolicy >::type const_iterator
Definition: Vector.h:47
iterator operator-(difference_type n) const
Definition: iterator.h:59
iterator operator++(int)
Definition: iterator.h:45
iterator operator--(int)
Definition: iterator.h:51
iterator & operator-=(difference_type n)
Definition: iterator.h:66
STORAGE_TYPE * pointer
Definition: iterator.h:36
SugarIterator iterator
Definition: iterator.h:38
bool operator>(const iterator &other) const
Definition: iterator.h:90
bool operator>=(const iterator &other) const
Definition: iterator.h:96
bool operator!=(const iterator &y) const
Definition: iterator.h:84
SugarIterator(const T &ref_)
Definition: iterator.h:40
SugarIterator(const T &ref_, R_xlen_t index_)
Definition: iterator.h:41
SugarIterator(const SugarIterator &other)
Definition: iterator.h:42
iterator & operator--()
Definition: iterator.h:50
iterator & operator++()
Definition: iterator.h:44
difference_type operator-(const iterator &other) const
Definition: iterator.h:100
bool operator<(const iterator &other) const
Definition: iterator.h:87
STORAGE_TYPE reference
Definition: iterator.h:35
bool operator<=(const iterator &other) const
Definition: iterator.h:93
std::random_access_iterator_tag iterator_category
Definition: iterator.h:37
iterator operator+(difference_type n) const
Definition: iterator.h:56
reference operator[](R_xlen_t i)
Definition: iterator.h:70
bool operator==(const iterator &y) const
Definition: iterator.h:81
Rcpp::traits::storage_type< Rcpp::traits::r_sexptype_traits< T >::rtype >::type STORAGE_TYPE
Definition: iterator.h:34
iterator & operator+=(difference_type n)
Definition: iterator.h:62
sugar_const_iterator_type< T >::type get_const_end(const T &obj)
Definition: iterator.h:148
sugar_const_iterator_type< T >::type get_const_begin(const T &obj)
Definition: iterator.h:138
sugar_const_iterator_type< T >::type get_const_begin__impl(const T &obj, Rcpp::traits::true_type)
Definition: iterator.h:126
Rcpp API.
Definition: algo.h:28