[Rcpp-commits] r3265 - in pkg/int64: . R inst/include/int64 man src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 2 12:02:21 CET 2011


Author: romain
Date: 2011-11-02 12:02:21 +0100 (Wed, 02 Nov 2011)
New Revision: 3265

Added:
   pkg/int64/inst/include/int64/sort.h
   pkg/int64/man/sort-methods.Rd
Modified:
   pkg/int64/ChangeLog
   pkg/int64/NAMESPACE
   pkg/int64/R/int64.R
   pkg/int64/inst/include/int64/LongVector.h
   pkg/int64/inst/include/int64/routines.h
   pkg/int64/src/int64.cpp
   pkg/int64/src/int64_init.c
Log:
adding sort.[u]int64

Modified: pkg/int64/ChangeLog
===================================================================
--- pkg/int64/ChangeLog	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/ChangeLog	2011-11-02 11:02:21 UTC (rev 3265)
@@ -1,3 +1,9 @@
+2011-11-012  Romain Francois  <romain at r-enthusiasts.com>
+
+    * R/int64.R: implementation of sort for int64 and uint64
+    
+    * inst/include/int64/LongVector.h: LongVector<>::sort
+
 2011-11-01  Romain Francois  <romain at r-enthusiasts.com>
 
     * R/int64.R: implementation of unique for int64 and uint64

Modified: pkg/int64/NAMESPACE
===================================================================
--- pkg/int64/NAMESPACE	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/NAMESPACE	2011-11-02 11:02:21 UTC (rev 3265)
@@ -7,7 +7,7 @@
 exportMethods( 
     show, length, "[", Arith, Compare, Summary, as.character, format, 
     
-    as.data.frame, binary, unique
+    as.data.frame, binary, unique, sort
 )
 export( 
     int64, uint64, as.int64, as.uint64, numeric_limits

Modified: pkg/int64/R/int64.R
===================================================================
--- pkg/int64/R/int64.R	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/R/int64.R	2011-11-02 11:02:21 UTC (rev 3265)
@@ -197,4 +197,11 @@
     new( "uint64", .Data = unique( x at .Data, incomparables, ... ) )  
 } )
 
+setGeneric( "sort" )
+setMethod( "sort", "int64", function(x, decreasing = FALSE, ...){
+    .Call( int64_sort, x, FALSE, decreasing )
+} )
+setMethod( "sort", "uint64", function(x, decreasing = FALSE, ...){
+    .Call( int64_sort, x, TRUE, decreasing )
+} )
 

Modified: pkg/int64/inst/include/int64/LongVector.h
===================================================================
--- pkg/int64/inst/include/int64/LongVector.h	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/inst/include/int64/LongVector.h	2011-11-02 11:02:21 UTC (rev 3265)
@@ -23,6 +23,8 @@
 
 #include <stdio.h>
 #include <iostream>
+#include <vector>
+#include <algorithm>
 
 namespace int64{
     
@@ -36,7 +38,16 @@
             R_PreserveObject(data) ;   
         }
         
-        operator SEXP(){ return data; }
+        operator SEXP(){ 
+            std::string klass = int64::internal::get_class<LONG>() ;
+            SEXP res = PROTECT( 
+                R_do_slot_assign( 
+                    R_do_new_object( R_do_MAKE_CLASS( klass.c_str() ) ), 
+                    Rf_install(".Data"), 
+                    data ) ) ;
+            UNPROTECT(1) ;
+            return res ;    
+        }
         
         LongVector(int n) : data(R_NilValue) {
             SEXP x = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
@@ -48,6 +59,21 @@
             R_PreserveObject(data) ;
         }
         
+        template <typename ITERATOR>
+        LongVector(int n, ITERATOR start, ITERATOR end) : data(R_NilValue) {
+            SEXP x = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
+            int hb, lb ;
+            for( int i=0; i<n; i++, ++start){
+                hb = int64::internal::get_high_bits<LONG>(*start) ;
+                lb = int64::internal::get_low_bits<LONG>(*start) ;
+                SET_VECTOR_ELT( x, i, int64::internal::int2(hb,lb) ) ;    
+            }
+            UNPROTECT(1) ; // x
+            data = x ;
+            R_PreserveObject(data) ;
+        }
+        
+        
         ~LongVector(){
             R_ReleaseObject(data) ;   
         }
@@ -65,6 +91,17 @@
         
         inline int size() const { return Rf_length(data); }
         
+        LongVector<LONG> sort(bool decreasing) const {
+            int n = size() ;
+            std::vector<LONG> x( n ) ;
+            for( int i=0; i<n; i++){
+                x[i] = get(i) ;
+            }
+            // FIXME: deal with decreasing
+            std::sort( x.begin(), x.end() ) ;
+            return LongVector<LONG>( n, x.begin(), x.end() ) ;
+        }
+        
     } ;
 
     

Modified: pkg/int64/inst/include/int64/routines.h
===================================================================
--- pkg/int64/inst/include/int64/routines.h	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/inst/include/int64/routines.h	2011-11-02 11:02:21 UTC (rev 3265)
@@ -46,6 +46,8 @@
 
 CALLFUN_1(int64_limits) ;
 
+CALLFUN_3(int64_sort) ;
+
 #ifdef __cplusplus
 }
 #endif

Added: pkg/int64/inst/include/int64/sort.h
===================================================================
--- pkg/int64/inst/include/int64/sort.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/sort.h	2011-11-02 11:02:21 UTC (rev 3265)
@@ -0,0 +1,35 @@
+// sort.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google Inc.  All rights reserved.
+//
+// This file is part of int64.
+//
+// int64 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.
+//
+// int64 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 int64.  If not, see <http://www.gnu.org/licenses/>.    
+    
+#ifndef int64__sort__h
+#define int64__sort__h
+
+namespace int64{
+    namespace internal{
+
+        template <typename LONG>
+        SEXP sort( SEXP x_, bool decreasing ){
+            LongVector<LONG> x(x_) ;
+            std::vector<LONG> vec = x.std_vector() ;
+        }
+        
+    } // namespace internal
+} // namespace int64
+#endif

Added: pkg/int64/man/sort-methods.Rd
===================================================================
--- pkg/int64/man/sort-methods.Rd	                        (rev 0)
+++ pkg/int64/man/sort-methods.Rd	2011-11-02 11:02:21 UTC (rev 3265)
@@ -0,0 +1,26 @@
+\name{sort-methods}
+\docType{methods}
+\alias{sort-methods}
+\alias{sort,ANY-method}
+\alias{sort,int64-method}
+\alias{sort,uint64-method}
+\title{Sorting 64 bits integer vector}
+\description{
+ Sorting 64 bits integer vector
+}
+\section{Methods}{
+\describe{
+
+\item{\code{signature(x = "ANY")}}{
+    Standard method (from base)
+}
+
+\item{\code{signature(x = "int64")}}{
+    Sorting signed 64 bit integer vectors (\code{\linkS4class{int64}}
+}
+
+\item{\code{signature(x = "uint64")}}{
+    Sorting unsigned 64 bit integer vectors (\code{\linkS4class{uint64}}
+}
+}}
+\keyword{methods}

Modified: pkg/int64/src/int64.cpp
===================================================================
--- pkg/int64/src/int64.cpp	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/src/int64.cpp	2011-11-02 11:02:21 UTC (rev 3265)
@@ -1,4 +1,4 @@
-// routines.h: int64 64 bit integers
+// int64.cpp: int64 64 bit integers
 //
 // Copyright (C) 2011 Romain Francois
 // Copyright (C) 2011 Google Inc.  All rights reserved.
@@ -150,3 +150,15 @@
     Rf_error( "unsupported type" ) ;
     return R_NilValue ;
 }
+
+extern "C" SEXP int64_sort( SEXP x, SEXP unsign, SEXP decr ){
+    bool is_unsigned = INTEGER(unsign)[0] ;
+    bool decreasing = INTEGER(decr)[0] ;
+    
+    if( is_unsigned ){
+        return int64::LongVector<uint64_t>(x).sort(decreasing ) ;   
+    } else {
+        return int64::LongVector<int64_t>(x).sort(decreasing ) ;
+    }
+}
+

Modified: pkg/int64/src/int64_init.c
===================================================================
--- pkg/int64/src/int64_init.c	2011-11-02 10:23:08 UTC (rev 3264)
+++ pkg/int64/src/int64_init.c	2011-11-02 11:02:21 UTC (rev 3265)
@@ -40,6 +40,8 @@
     
     CALLDEF(int64_limits,1),
     
+    CALLDEF(int64_sort,3),
+    
     {NULL, NULL, 0}
 }; 
 



More information about the Rcpp-commits mailing list