[Rcpp-commits] r1531 - in pkg/Rcpp/inst/include: . Rcpp Rcpp/sugar Rcpp/traits

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 14 11:59:15 CEST 2010


Author: romain
Date: 2010-06-14 11:59:15 +0200 (Mon, 14 Jun 2010)
New Revision: 1531

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/
   pkg/Rcpp/inst/include/Rcpp/sugar/SingleLogicalResult.h
   pkg/Rcpp/inst/include/Rcpp/sugar/any.h
   pkg/Rcpp/inst/include/Rcpp/sugar/can_have_na.h
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h
Modified:
   pkg/Rcpp/inst/include/Rcpp.h
   pkg/Rcpp/inst/include/Rcpp/algo.h
   pkg/Rcpp/inst/include/Rcpp/traits/is_na.h
   pkg/Rcpp/inst/include/RcppCommon.h
Log:
moving any to Rcpp/sugar and some more abstraction and lazyness

Modified: pkg/Rcpp/inst/include/Rcpp/algo.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/algo.h	2010-06-13 21:10:03 UTC (rev 1530)
+++ pkg/Rcpp/inst/include/Rcpp/algo.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -156,19 +156,6 @@
 	return __any_if( first, last, pred, typename std::iterator_traits<InputIterator>::iterator_category() ) ;
 }
 
-
-template <typename T>
-SEXP any( const T& t){
-	bool seen_na = false ;
-	int n = t.size() ;
-	for( int i=0 ; i<n ; i++){
-		if( t[i] == TRUE ) return Rf_ScalarLogical( TRUE );
-		if( t[i] == NA_LOGICAL ) seen_na = true;
-	}
-	return seen_na ? Rf_ScalarLogical(NA_LOGICAL) : Rf_ScalarLogical(FALSE) ;
 }
 
-
-}
-
 #endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/SingleLogicalResult.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/SingleLogicalResult.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/SingleLogicalResult.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -0,0 +1,78 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// SingleLogicalResult.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 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__SingleLogicalResult_h
+#define Rcpp__sugar__SingleLogicalResult_h
+
+namespace Rcpp{
+namespace sugar{  
+
+template <typename T>
+class SingleLogicalResult {
+public:
+	const static int UNRESOLVED = -5 ;
+	
+	SingleLogicalResult() : result(UNRESOLVED) {} ;
+	
+	void apply(){
+		if( result == UNRESOLVED ){
+			static_cast<T&>(*this).apply() ;
+		}
+	}
+	
+	inline bool is_true(){
+		apply() ;
+		return result == TRUE ;
+	}
+	
+	inline bool is_false(){
+		apply() ;
+		return result == FALSE ;
+	}
+	
+	inline bool is_na(){
+		apply() ;
+		return Rcpp::traits::is_na<LGLSXP>( result ) ;
+	}
+	
+	operator SEXP(){
+		apply() ;
+		return Rf_ScalarLogical( result ) ;
+	}
+	
+	inline int size(){ return 1 ; }
+	
+private:
+	int result ;
+	
+protected:
+	inline void set(int x){ result = x ;}
+	inline void reset(){ set(UNRESOLVED) ; }
+	inline void set_true(){ set(TRUE); }
+	inline void set_false(){ set(FALSE); }
+	inline void set_na(){ set(NA_LOGICAL); }
+	inline bool is_unresolved(){ return result == UNRESOLVED ; }
+} ;
+
+}
+}
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/any.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/any.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/any.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -0,0 +1,85 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// any.h: Rcpp R/C++ interface class library -- any
+//
+// Copyright (C) 2010 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__any_h
+#define Rcpp__sugar__any_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <typename T>
+class Any : public SingleLogicalResult< Any<T> >{
+public:
+	typedef SingleLogicalResult< Any<T> > PARENT ;
+	Any( const T& t ) : PARENT() , object(t) {}
+	
+	void apply(){
+		apply__impl( typename can_have_na<T>::type() ) ;
+	}	
+private:		
+	const T& object ;
+
+	// version that takes NA into account
+	void apply__impl( Rcpp::traits::true_type ){
+		int n = object.size() ;
+		int current = 0 ;
+		PARENT::reset() ;
+		for( int i=0 ; i<n ; i++){
+			current = object[i] ;
+			if( current == TRUE ) {
+				PARENT::set_true() ; 
+				return ;
+			}
+			if( Rcpp::traits::is_na<LGLSXP>(current)  ) {
+				PARENT::set_na();
+			}
+		}
+		if( PARENT::is_unresolved() ){
+			PARENT::set_false() ;
+		}
+	}
+	
+	// version to use when we know there is no NA
+	void apply__impl( Rcpp::traits::false_type ){
+		int n = object.size() ;
+		PARENT::set_false() ;
+		for( int i=0 ; i<n ; i++){
+			if( object[i] == TRUE ) {
+				PARENT::set_true() ; 
+				return ;
+			}
+		}
+	}
+	
+	
+private:
+} ;
+
+} // sugar
+
+template <typename T>
+inline sugar::Any<T> any( const T& t){
+	return sugar::Any<T>( t ) ;
+}
+
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/can_have_na.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/can_have_na.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/can_have_na.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -0,0 +1,33 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// can_have_na.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 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__can_have_na_h
+#define Rcpp__sugar__can_have_na_h
+
+namespace Rcpp{
+
+template <typename T>
+struct can_have_na : Rcpp::traits::true_type {} ;
+
+
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -0,0 +1,28 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// sugar.h: Rcpp R/C++ interface class library -- main file for Rcpp::sugar
+//
+// Copyright (C) 2010 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_H
+#define RCPP_SUGAR_H
+
+// implementations
+#include <Rcpp/sugar/any.h>
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -0,0 +1,31 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// sugar_forward.h: Rcpp R/C++ interface class library -- forward declaration for Rcpp::sugar
+//
+// Copyright (C) 2010 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_FORWARD_H
+#define RCPP_SUGAR_FORWARD_H
+
+// traits
+#include <Rcpp/sugar/can_have_na.h>
+
+// abstractions
+#include <Rcpp/sugar/SingleLogicalResult.h>
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/is_na.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/is_na.h	2010-06-13 21:10:03 UTC (rev 1530)
+++ pkg/Rcpp/inst/include/Rcpp/traits/is_na.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -50,6 +50,9 @@
 	template <>
 	inline bool is_na<STRSXP>( SEXP x ){ return x == NA_STRING ; }
 	
+	template <>
+	inline bool is_na<LGLSXP>( int x ){ return x == NA_LOGICAL ; }
+	
 }
 }
 

Modified: pkg/Rcpp/inst/include/Rcpp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp.h	2010-06-13 21:10:03 UTC (rev 1530)
+++ pkg/Rcpp/inst/include/Rcpp.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -73,4 +73,6 @@
 #include <Rcpp/Module.h>
 #endif
 
+#include <Rcpp/sugar/sugar.h>
+
 #endif

Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h	2010-06-13 21:10:03 UTC (rev 1530)
+++ pkg/Rcpp/inst/include/RcppCommon.h	2010-06-14 09:59:15 UTC (rev 1531)
@@ -250,4 +250,6 @@
 #include <Rcpp/preprocessor.h>
 #include <Rcpp/algo.h>
 
+#include <Rcpp/sugar/sugar_forward.h>
+
 #endif



More information about the Rcpp-commits mailing list