[Rcpp-commits] r3923 - in pkg/Rcpp: . inst/include/Rcpp inst/include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 9 09:44:44 CET 2012


Author: romain
Date: 2012-11-09 09:44:43 +0100 (Fri, 09 Nov 2012)
New Revision: 3923

Added:
   pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/Vector.h
Log:
CharacterVectorExtractionIterator input iterator for STRSXPs

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-09 07:32:55 UTC (rev 3922)
+++ pkg/Rcpp/ChangeLog	2012-11-09 08:44:43 UTC (rev 3923)
@@ -2,6 +2,9 @@
 
         * R/Attributes.R: Passing verbose from evalCpp to cppFunction
         * include/RcppCommon.h: include <limits> instead of <limits.h>
+        * include/Rcpp/vector/CharacterVectorExtractionIterator.h: iterator class
+        to quickly iterate over the content of a CharacterVector. The content that 
+        is iterated over is to be considered read-only. 
 
 2012-11-08  JJ Allaire <jj at rstudio.org>
 

Modified: pkg/Rcpp/inst/include/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Vector.h	2012-11-09 07:32:55 UTC (rev 3922)
+++ pkg/Rcpp/inst/include/Rcpp/Vector.h	2012-11-09 08:44:43 UTC (rev 3923)
@@ -2,7 +2,7 @@
 //
 // Vector.h: Rcpp R/C++ interface class library -- vectors
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -66,6 +66,8 @@
 
 #include <Rcpp/vector/LazyVector.h>
 
+#include <Rcpp/vector/CharacterVectorExtractionIterator.h>
+
 }  // Rcpp
 
 #include <Rcpp/vector/swap.h>

Added: pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/vector/CharacterVectorExtractionIterator.h	2012-11-09 08:44:43 UTC (rev 3923)
@@ -0,0 +1,69 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// CharacterVectorExtractionIterator.h: Rcpp R/C++ interface class library -- extract string from CharacterVector
+//
+// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__vector__CharacterVectorExtractionIterator_h
+#define Rcpp__vector__CharacterVectorExtractionIterator_h
+
+class CharacterVectorExtractionIterator {
+public:
+    typedef int difference_type;
+    typedef const char* value_type;
+    typedef const char** pointer;
+    typedef const char*& reference;
+    typedef std::input_iterator_tag iterator_category;
+    
+    CharacterVectorExtractionIterator( const CharacterVector& vec) : ptr( get_string_ptr( vec) ){}
+    
+    CharacterVectorExtractionIterator( const CharacterVectorExtractionIterator& other) : ptr(other.ptr){}
+    CharacterVectorExtractionIterator& operator=(const CharacterVectorExtractionIterator& other){ ptr = other.ptr ; return *this ;}
+    
+    int operator-( const CharacterVectorExtractionIterator& other){ return ptr - other.ptr ; }
+    
+    CharacterVectorExtractionIterator operator+( int n){ return CharacterVectorExtractionIterator(ptr+n); }
+    CharacterVectorExtractionIterator operator-( int n){ return CharacterVectorExtractionIterator(ptr-n); }
+    
+    CharacterVectorExtractionIterator& operator++(){ ptr++ ; return *this ; }
+    CharacterVectorExtractionIterator& operator--(){ ptr-- ; return *this ; }
+    CharacterVectorExtractionIterator& operator+=(int n){ ptr += n; return *this ; }
+    CharacterVectorExtractionIterator& operator-=(int n){ ptr -= n; return *this ; }
+    
+    bool operator<( const CharacterVectorExtractionIterator& other ){ return ptr < other.ptr ; }
+    bool operator>( const CharacterVectorExtractionIterator& other ){ return ptr > other.ptr ; }
+    bool operator<=( const CharacterVectorExtractionIterator& other ){ return ptr <= other.ptr ; }
+    bool operator>=( const CharacterVectorExtractionIterator& other ){ return ptr >= other.ptr ; }
+    
+    value_type  operator*(){ return char_nocheck(*ptr) ; }
+    value_type  operator[](int n){ return char_nocheck(ptr[n]) ; }
+    bool operator==(const CharacterVectorExtractionIterator& other) const { return ptr == other.ptr ;}
+    bool operator!=(const CharacterVectorExtractionIterator& other) const { return ptr != other.ptr ;}    
+      
+        
+private:
+    SEXP* ptr ; 
+    CharacterVectorExtractionIterator( SEXP* ptr_ ) : ptr(ptr_){}
+    
+};
+
+inline CharacterVectorExtractionIterator input_iterator( const CharacterVector& vec){
+    return CharacterVectorExtractionIterator(vec) ;  
+}
+
+#endif



More information about the Rcpp-commits mailing list