[Rcpp-commits] r4514 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Sep 18 17:49:12 CEST 2013


Author: romain
Date: 2013-09-18 17:49:12 +0200 (Wed, 18 Sep 2013)
New Revision: 4514

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/max.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/min.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/range.h
Removed:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/minmax.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
Log:
factor out min, max and range in their own file and classes

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-09-18 15:14:05 UTC (rev 4513)
+++ pkg/Rcpp/ChangeLog	2013-09-18 15:49:12 UTC (rev 4514)
@@ -13,7 +13,10 @@
         so that it works even when we don't know the previous value
         * unitTests/runit.sugar.R : 
         * unitTests/cpp/sugar.cpp :
-        * TODO : 2 less items
+        * include/Rcpp/sugar/functions/range.h : factored out of minmax.h
+        * include/Rcpp/sugar/functions/min.h : factored out of minmax.h
+        * include/Rcpp/sugar/functions/max.h : factored out of minmax.h
+        * TODO : 3 less items
         
 2013-09-17  JJ Allaire  <jj at rstudio.org>
 

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2013-09-18 15:14:05 UTC (rev 4513)
+++ pkg/Rcpp/TODO	2013-09-18 15:49:12 UTC (rev 4514)
@@ -50,11 +50,6 @@
     
     o   operator/ needs to handle the case of division by 0
 
-    o   min, max with specialization of the binary operators, so that we can do 
-        things like this lazily: 
-        
-        min( x ) < 4
-    
     o   matrix functions : apply
     
     o   for character vectors: nchar, grepl, sub, gsub

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-09-18 15:14:05 UTC (rev 4513)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2013-09-18 15:49:12 UTC (rev 4514)
@@ -46,7 +46,9 @@
 #include <Rcpp/sugar/functions/pmin.h>
 #include <Rcpp/sugar/functions/pmax.h>
 #include <Rcpp/sugar/functions/clamp.h>
-#include <Rcpp/sugar/functions/minmax.h>
+#include <Rcpp/sugar/functions/min.h>
+#include <Rcpp/sugar/functions/max.h>
+#include <Rcpp/sugar/functions/range.h>
 #include <Rcpp/sugar/functions/sign.h>
 #include <Rcpp/sugar/functions/diff.h>
 #include <Rcpp/sugar/functions/pow.h>

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/max.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/max.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/max.h	2013-09-18 15:49:12 UTC (rev 4514)
@@ -0,0 +1,87 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// max.h: Rcpp R/C++ interface class library -- max
+//
+// Copyright (C) 2012 - 2013 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__max_h
+#define Rcpp__sugar__max_h
+
+namespace Rcpp{
+namespace sugar{
+
+    template <int RTYPE, bool NA, typename T>
+    class Max {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Max( const T& obj_) : obj(obj_) {}
+        
+        operator STORAGE() {
+            max_ = obj[0] ;
+            if( Rcpp::traits::is_na<RTYPE>( max_ ) ) return max_ ;
+            
+            int n = obj.size() ;
+            for( int i=1; i<n; i++){
+                current = obj[i] ;
+                if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
+                if( current > max_ ) max_ = current ;
+            }
+            return max_ ;
+        }
+        
+    private:
+        const T& obj ;
+        STORAGE min_, max_, current ;
+    } ;
+
+    // version for NA = false
+    template <int RTYPE, typename T>
+    class Max<RTYPE,false,T> {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Max( const T& obj_) : obj(obj_) {}
+        
+        operator STORAGE() {
+            max_ = obj[0] ;
+            
+            int n = obj.size() ;
+            for( int i=1; i<n; i++){
+                current = obj[i] ;
+                if( current > max_ ) max_ = current ;
+            }
+            return max_ ;
+        }
+       
+    private:
+        const T& obj ;
+        STORAGE max_, current ;
+    } ;
+         
+
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+sugar::Max<RTYPE,NA,T> max( const VectorBase<RTYPE,NA,T>& x){
+    return sugar::Max<RTYPE,NA,T>(x.get_ref()) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/min.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/min.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/min.h	2013-09-18 15:49:12 UTC (rev 4514)
@@ -0,0 +1,86 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Min.h: Rcpp R/C++ interface class library -- min
+//
+// Copyright (C) 2012 - 2013 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__min_h
+#define Rcpp__sugar__min_h
+
+namespace Rcpp{
+namespace sugar{
+
+    template <int RTYPE, bool NA, typename T>
+    class Min {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Min( const T& obj_) : obj(obj_) {}
+        
+        operator STORAGE() {
+            min_ = obj[0] ;
+            if( Rcpp::traits::is_na<RTYPE>( min_ ) ) return min_ ;
+            
+            int n = obj.size() ;
+            for( int i=1; i<n; i++){
+                current = obj[i] ;
+                if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
+                if( current < min_ ) min_ = current ;
+            }
+            return min_ ;
+        }
+    
+        const T& obj ;
+        STORAGE min_, max_, current ;
+    } ;
+
+    // version for NA = false
+    template <int RTYPE, typename T>
+    class Min<RTYPE,false,T> {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Min( const T& obj_) : obj(obj_) {}
+        
+        operator STORAGE() {
+            min_ = obj[0] ;
+            
+            int n = obj.size() ;
+            for( int i=1; i<n; i++){
+                current = obj[i] ;
+                if( current < min_ ) min_ = current ;
+            }
+            return min_ ;
+        }
+    
+        const T& obj ;
+        STORAGE min_, current ;
+    } ;
+
+    
+} // sugar
+
+
+template <int RTYPE, bool NA, typename T>
+sugar::Min<RTYPE,NA,T> min( const VectorBase<RTYPE,NA,T>& x){
+    return sugar::Min<RTYPE,NA,T>(x.get_ref()) ;
+}
+
+} // Rcpp
+
+#endif

Deleted: pkg/Rcpp/inst/include/Rcpp/sugar/functions/minmax.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/minmax.h	2013-09-18 15:14:05 UTC (rev 4513)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/minmax.h	2013-09-18 15:49:12 UTC (rev 4514)
@@ -1,148 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// minmax.h: Rcpp R/C++ interface class library -- min, mx, range
-//
-// 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__minmax_h
-#define Rcpp__sugar__minmax_h
-
-namespace Rcpp{
-namespace sugar{
-
-    template <int RTYPE, bool NA, typename T>
-    class MinMax {
-    public:
-        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-        
-        MinMax( const T& obj_) : obj(obj_) {}
-        
-        STORAGE min() {
-            min_ = obj[0] ;
-            if( Rcpp::traits::is_na<RTYPE>( min_ ) ) return min_ ;
-            
-            int n = obj.size() ;
-            for( int i=1; i<n; i++){
-                current = obj[i] ;
-                if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
-                if( current < min_ ) min_ = current ;
-            }
-            return min_ ;
-        }
-        STORAGE max() {
-            max_ = obj[0] ;
-            if( Rcpp::traits::is_na<RTYPE>( max_ ) ) return max_ ;
-            
-            int n = obj.size() ;
-            for( int i=1; i<n; i++){
-                current = obj[i] ;
-                if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
-                if( current > max_ ) max_ = current ;
-            }
-            return max_ ;
-        }
-        Vector<RTYPE> range(){
-            min_ = max_ = obj[0] ;
-            if( Rcpp::traits::is_na<RTYPE>( min_ ) ) return Vector<RTYPE>::create( min_, max_ ) ;
-            
-            int n = obj.size() ;
-            for( int i=1; i<n; i++){
-                current = obj[i] ;
-                if( Rcpp::traits::is_na<RTYPE>( current ) ) return Vector<RTYPE>::create( min_, max_ ) ;
-                if( current < min_ ) min_ = current ;
-                if( current > max_ ) max_ = current ;
-                
-            }
-            return Vector<RTYPE>::create( min_, max_ ) ;
-        }
-        
-        
-    private:
-        const T& obj ;
-        STORAGE min_, max_, current ;
-    } ;
-
-    // version for NA = false
-    template <int RTYPE, typename T>
-    class MinMax<RTYPE,false,T> {
-    public:
-        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
-        
-        MinMax( const T& obj_) : obj(obj_) {}
-        
-        STORAGE min() {
-            min_ = obj[0] ;
-            
-            int n = obj.size() ;
-            for( int i=1; i<n; i++){
-                current = obj[i] ;
-                if( current < min_ ) min_ = current ;
-            }
-            return min_ ;
-        }
-        STORAGE max() {
-            max_ = obj[0] ;
-            
-            int n = obj.size() ;
-            for( int i=1; i<n; i++){
-                current = obj[i] ;
-                if( current > max_ ) max_ = current ;
-            }
-            return max_ ;
-        }
-        Vector<RTYPE> range(){
-            min_ = max_ = obj[0] ;
-            
-            int n = obj.size() ;
-            for( int i=1; i<n; i++){
-                current = obj[i] ;
-                if( current < min_ ) min_ = current ;
-                if( current > max_ ) max_ = current ;
-            }
-            return Vector<RTYPE>::create( min_, max_ ) ;
-        }
-        
-        
-    private:
-        const T& obj ;
-        STORAGE min_, max_, current ;
-    } ;
-         
-
-} // sugar
-
-
-template <int RTYPE, bool NA, typename T>
-typename traits::storage_type<RTYPE>::type min( const VectorBase<RTYPE,NA,T>& x){
-    sugar::MinMax<RTYPE,NA,T> obj(x.get_ref()) ;
-    return obj.min() ;
-}
-template <int RTYPE, bool NA, typename T>
-typename traits::storage_type<RTYPE>::type max( const VectorBase<RTYPE,NA,T>& x){
-    sugar::MinMax<RTYPE,NA,T> obj(x.get_ref()) ;
-    return obj.max() ;
-}
-template <int RTYPE, bool NA, typename T>
-Vector<RTYPE> range( const VectorBase<RTYPE,NA,T>& x){
-    sugar::MinMax<RTYPE,NA,T> obj(x.get_ref()) ;
-    return obj.range() ;
-}
-
-} // Rcpp
-
-#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/range.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/range.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/range.h	2013-09-18 15:49:12 UTC (rev 4514)
@@ -0,0 +1,92 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// range.h: Rcpp R/C++ interface class library -- range
+//
+// Copyright (C) 2012 - 2013 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__range_h
+#define Rcpp__sugar__range_h
+
+namespace Rcpp{
+namespace sugar{
+
+    template <int RTYPE, bool NA, typename T>
+    class Range {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Range( const T& obj_) : obj(obj_) {}
+        
+        operator Vector<RTYPE>(){
+            min_ = max_ = obj[0] ;
+            if( Rcpp::traits::is_na<RTYPE>( min_ ) ) return Vector<RTYPE>::create( min_, max_ ) ;
+            
+            int n = obj.size() ;
+            for( int i=1; i<n; i++){
+                current = obj[i] ;
+                if( Rcpp::traits::is_na<RTYPE>( current ) ) return Vector<RTYPE>::create( min_, max_ ) ;
+                if( current < min_ ) min_ = current ;
+                if( current > max_ ) max_ = current ;
+                
+            }
+            return Vector<RTYPE>::create( min_, max_ ) ;
+        }
+        
+        
+    private:
+        const T& obj ;
+        STORAGE min_, max_, current ;
+    } ;
+
+    // version for NA = false
+    template <int RTYPE, typename T>
+    class Range<RTYPE,false,T> {
+    public:
+        typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+        
+        Range( const T& obj_) : obj(obj_) {}
+        
+        operator Vector<RTYPE>(){
+            min_ = max_ = obj[0] ;
+            
+            int n = obj.size() ;
+            for( int i=1; i<n; i++){
+                current = obj[i] ;
+                if( current < min_ ) min_ = current ;
+                if( current > max_ ) max_ = current ;
+            }
+            return Vector<RTYPE>::create( min_, max_ ) ;
+        }
+        
+        
+    private:
+        const T& obj ;
+        STORAGE min_, max_, current ;
+    } ;
+         
+
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+sugar::Range<RTYPE,NA,T> range( const VectorBase<RTYPE,NA,T>& x){
+    return sugar::Range<RTYPE,NA,T>(x.get_ref()) ;
+}
+
+} // Rcpp
+
+#endif



More information about the Rcpp-commits mailing list