[Rcpp-commits] r4021 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/include/Rcpp/traits
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Nov 22 21:08:42 CET 2012
Author: romain
Date: 2012-11-22 21:08:42 +0100 (Thu, 22 Nov 2012)
New Revision: 4021
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/functions/self_match.h
pkg/Rcpp/inst/include/Rcpp/traits/comparator_type.h
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
Log:
self_match
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2012-11-22 17:42:30 UTC (rev 4020)
+++ pkg/Rcpp/ChangeLog 2012-11-22 20:08:42 UTC (rev 4021)
@@ -1,6 +1,8 @@
2012-11-22 Romain Francois <romain at r-enthusiasts.com>
* include/Rcpp/iostream.h: use delete rdbuf() as recommended by Martyn Plummer
+ * include/Rcpp/sugar/functions/self_match.h: self_match, giving a
+ functionality similar to match( x, unique(x) )
2012-11-21 JJ Allaire <jj at rstudio.org>
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2012-11-22 17:42:30 UTC (rev 4020)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2012-11-22 20:08:42 UTC (rev 4021)
@@ -63,6 +63,7 @@
#include <Rcpp/sugar/functions/unique.h>
#include <Rcpp/sugar/functions/match.h>
+#include <Rcpp/sugar/functions/self_match.h>
#include <Rcpp/sugar/functions/setdiff.h>
#endif
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h 2012-11-22 17:42:30 UTC (rev 4020)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/match.h 2012-11-22 20:08:42 UTC (rev 4021)
@@ -88,15 +88,6 @@
return hash.match( x.get_ref() ) ;
}
-
-template <int RTYPE, bool NA, typename T>
-inline IntegerVector self_match( const VectorBase<RTYPE,NA,T>& x ){
- const T& ref = x.get_ref() ;
- sugar::IndexHash<RTYPE,T> hash( ref ) ;
- return hash.match( ref ) ;
-}
-
-
} // Rcpp
#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/self_match.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/self_match.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/self_match.h 2012-11-22 20:08:42 UTC (rev 4021)
@@ -0,0 +1,76 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// self_match.h: Rcpp R/C++ interface class library -- self match
+//
+// 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__sugar__self_match_h
+#define Rcpp__sugar__self_match_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <typename HASH, typename STORAGE>
+class SelfInserter {
+public:
+ SelfInserter( HASH& hash_ ) : hash(hash_), index(0) {}
+
+ inline int operator()( STORAGE value ){
+ typename HASH::iterator it = hash.find( value ) ;
+ if( it == hash.end() ){
+ hash.insert( std::make_pair(value, ++index) ) ;
+ return index ;
+ } else {
+ return it->second ;
+ }
+ }
+
+private:
+ HASH& hash ;
+ int index;
+} ;
+
+template <int RTYPE, typename TABLE_T>
+class SelfMatch {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ SelfMatch( const TABLE_T& table ): hash(), result(table.size()) {
+ std::transform( table.begin(), table.end(), result.begin(), Inserter(hash) ) ;
+ }
+
+ inline operator IntegerVector() const { return result ; }
+
+private:
+ typedef RCPP_UNORDERED_MAP<STORAGE, int> HASH ;
+ typedef SelfInserter<HASH,STORAGE> Inserter ;
+ HASH hash ;
+ IntegerVector result ;
+};
+
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+inline IntegerVector self_match( const VectorBase<RTYPE,NA,T>& x ){
+ return sugar::SelfMatch<RTYPE,T>(x.get_ref()) ;
+}
+
+
+} // Rcpp
+#endif
+
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h 2012-11-22 17:42:30 UTC (rev 4020)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/unique.h 2012-11-22 20:08:42 UTC (rev 4021)
@@ -75,10 +75,7 @@
template <>
class Unique<STRSXP,CharacterVector> {
public:
- Unique( const CharacterVector& vec ) :
- set( get_string_ptr(vec), get_string_ptr(vec) + vec.size() )
- {
- }
+ Unique( const CharacterVector& vec ) : set( vec.begin(), vec.end() ) {}
CharacterVector get( ) {
return CharacterVector( set.begin(), set.end() ) ;
Added: pkg/Rcpp/inst/include/Rcpp/traits/comparator_type.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/comparator_type.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/traits/comparator_type.h 2012-11-22 20:08:42 UTC (rev 4021)
@@ -0,0 +1,48 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
+//
+// comparator_type.h: Rcpp R/C++ interface class library -- comparator
+//
+// 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__traits__comparator_type__h
+#define Rcpp__traits__comparator_type__h
+
+namespace Rcpp{
+namespace traits{
+
+class StringCompare {
+public:
+ inline bool operator()( SEXP x, SEXP y){
+ return strcmp( char_nocheck(x), char_nocheck(y) ) < 0 ;
+ }
+} ;
+
+template <int RTYPE> struct comparator_type {
+ typedef std::less< typename storage_type<RTYPE>::type > type ;
+} ;
+template <> struct comparator_type<STRSXP>{
+ typedef StringCompare type ;
+} ;
+
+
+}
+}
+
+#endif
+
More information about the Rcpp-commits
mailing list