[Rcpp-commits] r1766 - in pkg/Rcpp: inst inst/include/classic inst/unitTests src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 2 16:58:20 CEST 2010


Author: edd
Date: 2010-07-02 16:58:20 +0200 (Fri, 02 Jul 2010)
New Revision: 1766

Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/classic/RcppStringVector.h
   pkg/Rcpp/inst/unitTests/runit.RcppStringVector.R
   pkg/Rcpp/src/RcppStringVector.cpp
Log:
switch RcppString to std::vector<std::string> internally


Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-07-02 13:52:02 UTC (rev 1765)
+++ pkg/Rcpp/inst/ChangeLog	2010-07-02 14:58:20 UTC (rev 1766)
@@ -1,3 +1,10 @@
+2010-07-02  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/RcppStringVector: Now uses std::vector<std::string>
+	* inst/include/classic/RcppStringVector.h: Idem
+
+	* inst/unitTests/runit.List.R: Added simple test for RcppStringVector
+
 2010-07-01  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RcppDateVector: Index argument is int here as well

Modified: pkg/Rcpp/inst/include/classic/RcppStringVector.h
===================================================================
--- pkg/Rcpp/inst/include/classic/RcppStringVector.h	2010-07-02 13:52:02 UTC (rev 1765)
+++ pkg/Rcpp/inst/include/classic/RcppStringVector.h	2010-07-02 14:58:20 UTC (rev 1766)
@@ -1,4 +1,4 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
 //
 // RcppStringVector.h: Rcpp R/C++ interface class library -- string vector support
 //
@@ -32,19 +32,19 @@
 	typedef const std::string* const_iterator ;
 	
     RcppStringVector(SEXP vec);
-    ~RcppStringVector();
-    std::string& operator()(int i) const;
+    ~RcppStringVector() {};
+    const std::string& operator()(int i) const;
+    std::string& operator()(int i);
     int size() const;
     std::vector<std::string> stlVector() const;
     
-    inline const_iterator begin() const { return v ; }
-    inline const_iterator end() const { return v + length ; }
-    inline iterator begin() { return v ; }
-    inline iterator end() { return v + length ; }
+    inline const_iterator begin() const { return &(v[0]); }
+    inline const_iterator end() const { return &(v[v.size()]); }
+    inline iterator begin() { return &(v[0]); }
+    inline iterator end() { return &(v[v.size()]); }
     
 private:
-    std::string *v;
-    int length;
+	std::vector<std::string> v;
 };
 
 #endif

Modified: pkg/Rcpp/inst/unitTests/runit.RcppStringVector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.RcppStringVector.R	2010-07-02 13:52:02 UTC (rev 1765)
+++ pkg/Rcpp/inst/unitTests/runit.RcppStringVector.R	2010-07-02 14:58:20 UTC (rev 1766)
@@ -37,7 +37,7 @@
 
 test.RcppStringVector.begin <- function() {
     src <- 'RcppStringVector s = RcppStringVector(sx);
-            return wrap(*s.begin())';
+            return wrap(*s.begin());';
     fun <- cxxfunction(signature(sx="character"), src, plugin="Rcpp")
     sv <- c("tic", "tac", "toe")
     checkEquals(fun(sv), sv[1], msg = "RcppStringVector.begin")

Modified: pkg/Rcpp/src/RcppStringVector.cpp
===================================================================
--- pkg/Rcpp/src/RcppStringVector.cpp	2010-07-02 13:52:02 UTC (rev 1765)
+++ pkg/Rcpp/src/RcppStringVector.cpp	2010-07-02 14:58:20 UTC (rev 1766)
@@ -1,9 +1,10 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
 //
 // RcppStringVector.cpp: Rcpp R/C++ interface class library -- string vector support
 //
 // Copyright (C) 2005 - 2006 Dominick Samperi
 // Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+// Copyright (C) 2010	     Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -25,38 +26,38 @@
 RcppStringVector::RcppStringVector(SEXP vec) {
     int i;
     if (Rf_isMatrix(vec) || Rf_isLogical(vec))
-	throw std::range_error("RcppStringVector: invalid numeric vector in constructor");
+		throw std::range_error("RcppStringVector: invalid numeric vector in constructor");
     if (!Rf_isString(vec))
-	throw std::range_error("RcppStringVector: invalid string");
+		throw std::range_error("RcppStringVector: invalid string");
     int len = Rf_length(vec);
     if (len == 0)
-	throw std::range_error("RcppStringVector: null vector in constructor");
-    v = new std::string[len];
+		throw std::range_error("RcppStringVector: null vector in constructor");
     for (i = 0; i < len; i++)
-	v[i] = std::string(CHAR(STRING_ELT(vec,i)));
-    length = len;
+		v.push_back(std::string(CHAR(STRING_ELT(vec,i))));
 }
 
-RcppStringVector::~RcppStringVector() {
-    delete [] v;
+const std::string& RcppStringVector::operator()(int i) const {
+    if (i < 0 || i >= static_cast<int>(v.size())) {
+		std::ostringstream oss;
+		oss << "RcppStringVector: subscript out of range: " << i;
+		throw std::range_error(oss.str());
+    }
+    return v[i];
 }
 
-std::string& RcppStringVector::operator()(int i) const {
-    if (i < 0 || i >= length) {
-	std::ostringstream oss;
-	oss << "RcppStringVector: subscript out of range: " << i;
-	throw std::range_error(oss.str());
+std::string& RcppStringVector::operator()(int i) {
+    if (i < 0 || i >= static_cast<int>(v.size())) {
+		std::ostringstream oss;
+		oss << "RcppStringVector: subscript out of range: " << i;
+		throw std::range_error(oss.str());
     }
     return v[i];
 }
 
 int RcppStringVector::size() const { 
-    return length; 
+    return v.size(); 
 }
 
 std::vector<std::string> RcppStringVector::stlVector() const {
-    std::vector<std::string> tmp(length);
-    for (int i = 0; i < length; i++)
-	tmp[i] = v[i];
-    return tmp;
+    return v;
 }



More information about the Rcpp-commits mailing list