[Rcpp-commits] r3238 - in pkg: . int64 int64/R int64/inst int64/inst/doc int64/inst/include int64/inst/include/int64 int64/inst/unitTests int64/man int64/src int64/tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 29 10:59:42 CEST 2011


Author: romain
Date: 2011-10-29 10:59:41 +0200 (Sat, 29 Oct 2011)
New Revision: 3238

Added:
   pkg/int64/
   pkg/int64/DESCRIPTION
   pkg/int64/NAMESPACE
   pkg/int64/R/
   pkg/int64/R/int64.R
   pkg/int64/inst/
   pkg/int64/inst/doc/
   pkg/int64/inst/doc/int64.Rnw
   pkg/int64/inst/include/
   pkg/int64/inst/include/int64.h
   pkg/int64/inst/include/int64/
   pkg/int64/inst/include/int64/LongVector.h
   pkg/int64/inst/include/int64/arith.h
   pkg/int64/inst/include/int64/as_character.h
   pkg/int64/inst/include/int64/as_long.h
   pkg/int64/inst/include/int64/compare.h
   pkg/int64/inst/include/int64/format_binary.h
   pkg/int64/inst/include/int64/get_bits.h
   pkg/int64/inst/include/int64/get_class.h
   pkg/int64/inst/include/int64/get_long.h
   pkg/int64/inst/include/int64/int64.h
   pkg/int64/inst/include/int64/routines.h
   pkg/int64/inst/include/int64/summary.h
   pkg/int64/inst/unitTests/
   pkg/int64/inst/unitTests/runTests.R
   pkg/int64/inst/unitTests/runit.int64.R
   pkg/int64/man/
   pkg/int64/man/as.int64.Rd
   pkg/int64/man/as.uint64.Rd
   pkg/int64/man/binary-class.Rd
   pkg/int64/man/binary-methods.Rd
   pkg/int64/man/int64-class.Rd
   pkg/int64/man/int64-package.Rd
   pkg/int64/man/int64.Rd
   pkg/int64/man/numeric_limits.Rd
   pkg/int64/man/uint64-class.Rd
   pkg/int64/man/uint64.Rd
   pkg/int64/src/
   pkg/int64/src/Makevars
   pkg/int64/src/int64.cpp
   pkg/int64/src/int64_init.c
   pkg/int64/tests/
   pkg/int64/tests/doRUnit.R
Log:
int64 initial commit

Added: pkg/int64/DESCRIPTION
===================================================================
--- pkg/int64/DESCRIPTION	                        (rev 0)
+++ pkg/int64/DESCRIPTION	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,13 @@
+Package: int64
+Type: Package
+Title: 64 bit integer types
+Version: 1.0
+Date: 2011-10-21
+Author: Romain Francois, sponsored by the Google Open Source Programs Office
+Maintainer: Romain Francois <romain at r-enthusiasts.com>
+Description: 64 bit integer types
+License: GPL (>= 2)
+LazyLoad: yes
+Depends: methods
+Suggests: RUnit
+

Added: pkg/int64/NAMESPACE
===================================================================
--- pkg/int64/NAMESPACE	                        (rev 0)
+++ pkg/int64/NAMESPACE	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,18 @@
+useDynLib( int64, .registration = TRUE )
+
+import( methods )
+
+exportClasses( "int64", "uint64", "binary" )
+
+exportMethods( 
+    show, length, "[", Arith, Compare, Summary, as.character, format, 
+    
+    as.data.frame, binary
+)
+export( 
+    int64, uint64, as.int64, as.uint64, numeric_limits
+)
+S3method( format, int64 )
+S3method( format, uint64 )
+
+

Added: pkg/int64/R/int64.R
===================================================================
--- pkg/int64/R/int64.R	                        (rev 0)
+++ pkg/int64/R/int64.R	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,190 @@
+# Copyright (C) 2011 Romain Francois
+# Copyright (C) 2011 Google
+#
+# 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/>.
+
+setClass( "int64", contains = "list" )
+setClass( "uint64", contains = "list" )
+
+setClass( "binary",  
+    representation( data = "character", bits = "integer" )
+)
+
+setGeneric( "binary", function(object) standardGeneric("binary") )
+setMethod( "binary", "integer", function(object){
+   new( "binary", data = .Call( int64_format_binary, object ), bits = 32L ) 
+} )
+setMethod( "binary", "numeric", function(object){
+   new( "binary", data = .Call( int64_format_binary, object ), bits = 64L ) 
+} )
+setMethod( "binary", "int64", function(object){
+   new( "binary", data = .Call( int64_format_binary, object ), bits = 64L ) 
+} )
+setMethod( "binary", "uint64", function(object){
+   new( "binary", data = .Call( int64_format_binary, object ), bits = 64L ) 
+} )
+setMethod( "show", "binary", function(object){
+    print( noquote( object at data ) )
+    invisible(object)
+})
+
+int64 <- function(length=0L){
+    x <- new("int64", rep( list(integer(2L)), length ) )
+    x
+}
+uint64 <- function(length=0L){
+    x <- new("uint64", rep( list(integer(2L)), length ) )
+    x
+}
+
+setMethod( "length", "int64", function(x){
+    length(x at .Data)
+} )
+setMethod( "length", "uint64", function(x){
+    length(x at .Data)
+} )
+setMethod( "show", "int64", function(object){
+    print( noquote( as.character( object ) ) )
+    invisible(object)
+} )
+setMethod( "show", "uint64", function(object){
+    print( noquote( as.character( object ) ) )
+    invisible(object)
+} )
+
+as.int64 <- function(x){
+    new( "int64", .Call(int64_as_int64, x) ) 
+}
+as.uint64 <- function(x){
+    new( "uint64", .Call(int64_as_uint64, x) ) 
+}
+
+setMethod( "[", "int64", function(x, i, j, ...){
+    new( "int64", x at .Data[ i ] )
+} )
+setMethod( "[", "uint64", function(x, i, j, ...){
+    new( "uint64", x at .Data[ i ] )
+} )
+setMethod( "[<-", "int64", function(x, i, j, ..., value ){
+    data <- x at .Data
+    data[i] <- as.int64( value )@.Data
+    new( "int64", data )
+} )
+setMethod( "[<-", "uint64", function(x, i, j, ..., value ){
+    data <- x at .Data
+    data[i] <- as.uint64( value )@.Data
+    new( "uint64", data )
+} )
+
+
+
+setMethod( "Arith", signature(e1 = "int64", e2 = "int64" ), 
+function(e1,e2){
+   numbers <- .Call( int64_arith_int64_int64, .Generic, e1, e2, FALSE )
+   new( "int64", numbers ) 
+} )
+setMethod( "Arith", signature(e1 = "int64", e2 = "ANY" ), 
+function(e1,e2){
+   numbers <- .Call( int64_arith_int64_int64, .Generic, e1, as.int64(e2), FALSE )
+   new( "int64", numbers )     
+} )
+setMethod( "Arith", signature(e1 = "ANY", e2 = "int64" ), 
+function(e1,e2){
+   numbers <- .Call( int64_arith_int64_int64, .Generic, as.int64(e1), e2, FALSE )
+   new( "int64", numbers ) 
+} )
+
+setMethod( "Arith", signature(e1 = "uint64", e2 = "uint64" ), 
+function(e1,e2){
+   numbers <- .Call( int64_arith_int64_int64, .Generic, e1, e2, TRUE )
+   new( "uint64", numbers ) 
+} )
+setMethod( "Arith", signature(e1 = "uint64", e2 = "ANY" ), 
+function(e1,e2){
+   numbers <- .Call( int64_arith_int64_int64, .Generic, e1, as.uint64(e2), TRUE )
+   new( "uint64", numbers )     
+} )
+setMethod( "Arith", signature(e1 = "ANY", e2 = "uint64" ), 
+function(e1,e2){
+   numbers <- .Call( int64_arith_int64_int64, .Generic, as.uint64(e1), e2, TRUE )
+   new( "uint64", numbers ) 
+} )
+
+setMethod( "Compare", signature(e1 = "int64", e2 = "int64" ), 
+function(e1,e2){
+   .Call( int64_compare_int64_int64, .Generic, e1, e2, FALSE )
+} )
+setMethod( "Compare", signature(e1 = "ANY", e2 = "int64" ), 
+function(e1,e2){
+   .Call( int64_compare_int64_int64, .Generic, as.int64(e1), e2, FALSE )
+} )
+setMethod( "Compare", signature(e1 = "int64", e2 = "ANY" ), 
+function(e1,e2){
+   .Call( int64_compare_int64_int64, .Generic, e1, as.int64(e2), FALSE )
+} )
+
+setMethod( "Compare", signature(e1 = "uint64", e2 = "uint64" ), 
+function(e1,e2){
+   .Call( int64_compare_int64_int64, .Generic, e1, e2, TRUE )
+} )
+setMethod( "Compare", signature(e1 = "ANY", e2 = "uint64" ), 
+function(e1,e2){
+   .Call( int64_compare_int64_int64, .Generic, as.uint64(e1), e2, TRUE )
+} )
+setMethod( "Compare", signature(e1 = "uint64", e2 = "ANY" ), 
+function(e1,e2){
+   .Call( int64_compare_int64_int64, .Generic, e1, as.uint64(e2), TRUE)
+} )
+
+
+setMethod( "Summary", "int64", function(x,..., na.rm = FALSE){
+   .Call( int64_summary_int64, .Generic, x, FALSE)
+} )
+setMethod( "Summary", "uint64", function(x,..., na.rm = FALSE){
+   .Call( int64_summary_int64, .Generic, x, TRUE)
+} )
+
+
+setMethod( "as.character", "int64", function(x,...){
+  .Call( int64_as_character_int64, x, FALSE)  
+})
+setMethod( "as.character", "uint64", function(x,...){
+  .Call( int64_as_character_int64, x, TRUE)  
+})
+
+setMethod( "as.data.frame", "int64", function(x,row.names = NULL, optional = FALSE, ...){
+   res <- data.frame( x =  seq_len(length(x)) )
+   names(res) <- deparse( substitute(x ) )
+   res[[1L]] <- x
+   res
+})
+setMethod( "as.data.frame", "uint64", function(x,row.names = NULL, optional = FALSE, ...){
+   res <- data.frame( x =  seq_len(length(x)) )
+   names(res) <- deparse( substitute(x ) )
+   res[[1L]] <- x
+   res
+})
+         
+format.int64 <- format.uint64 <- function(x, ...){
+    as.character(x)   
+}
+setMethod( "format", "int64", format.int64 )
+setMethod( "format", "uint64", format.uint64 )
+
+numeric_limits <- function( type ){
+    .Call( int64_limits, type )
+}
+

Added: pkg/int64/inst/doc/int64.Rnw
===================================================================
--- pkg/int64/inst/doc/int64.Rnw	                        (rev 0)
+++ pkg/int64/inst/doc/int64.Rnw	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,274 @@
+\documentclass[10pt]{article}
+%\VignetteIndexEntry{int64}
+\usepackage{vmargin}
+\setmargrb{0.75in}{0.75in}{0.75in}{0.75in}
+\usepackage{color,alltt}
+\usepackage[authoryear,round,longnamesfirst]{natbib}
+
+\usepackage[colorlinks]{hyperref}
+\definecolor{link}{rgb}{0,0,0.3}	%% next few lines courtesy of RJournal.sty
+\hypersetup{
+    colorlinks,%
+    citecolor=link,%
+    filecolor=link,%
+    linkcolor=link,%
+    urlcolor=link
+}
+
+\newcommand{\proglang}[1]{\textsf{#1}}
+\newcommand{\pkg}[1]{{\fontseries{b}\selectfont #1}}
+
+%% defined as a stop-gap measure til interaction with highlight is sorted out
+\newcommand{\hlboxlessthan}{   \hlnormalsizeboxlessthan}
+\newcommand{\hlboxgreaterthan}{\hlnormalsizeboxgreaterthan}
+\newcommand{\hlboxopenbrace}{  \hlnormalsizeboxopenbrace}
+\newcommand{\hlboxclosebrace}{ \hlnormalsizeboxclosebrace}
+\newcommand{\hlboxbacktick}{   \hlnormalsizeboxbacktick}
+\newcommand{\hlboxunderscore}{ \hlnormalsizeboxunderscore}
+
+<<echo=FALSE,print=FALSE>>=
+prettyVersion <- packageDescription("int64")$Version
+prettyDate <- format(Sys.Date(), "%B %e, %Y")
+@
+
+<<echo=FALSE>>=
+require( int64 )
+@
+
+\begin{document}
+
+\author{Romain Fran\c{c}ois - \texttt{romain at r-enthusiasts.com} }
+\title{int64 : 64 bits integer vectors}
+\date{\pkg{int64} version \Sexpr{prettyVersion}}
+
+\maketitle
+
+\begin{abstract}
+The \texttt{int64} package adds 64 bit integer vectors to \texttt{R}. 
+The package provides the \texttt{int64} and \texttt{uint64} classes for
+signed and unsigned integer vectors. This project has been 
+sponsored by the Google Open Source Programs Office. 
+\end{abstract}
+
+\section{Background}
+
+Integers in \texttt{R} are represented internally as 32 bit \texttt{int}. 
+Aplications now require larger ranges of values to represent large quantities. 
+This package exposes C++ types \texttt{int64\_t} and \texttt{uint64\_t}
+to \texttt{R} for this purpose. The table~\ref{limits} shows the limits of these
+types. 
+               
+\begin{table}[h]
+\centering
+\begin{tabular}{ccrr}
+\hline
+C++ type & R type & \multicolumn{1}{c}{min} & \multicolumn{1}{c}{max} \\
+\hline
+\texttt{int} & \texttt{integer} & 
+    \texttt{\Sexpr{numeric_limits("integer")[1L]}} & 
+    \texttt{\Sexpr{numeric_limits("integer")[2L]}} \\
+\texttt{int64\_t} & \texttt{int64} & 
+    \texttt{\Sexpr{as.character(numeric_limits("int64")[1L])}} & 
+    \texttt{\Sexpr{as.character(numeric_limits("int64")[2L])}} \\
+\texttt{uint64\_t} & \texttt{uint64} & 
+    \texttt{\Sexpr{as.character(numeric_limits("uint64")[1L])}} & 
+    \texttt{\Sexpr{as.character(numeric_limits("uint64")[2L])}} \\
+\hline
+\end{tabular}
+\caption{\label{limits}Numeric limits of integer types}
+\end{table}
+
+
+\section{Usage}
+
+This section shows a few examples on how to use the package. 
+
+<<>>=
+# create a new int64 vector
+x <- int64( 4 )
+
+# set a subset of values
+x[1:2] <- 1:2 # via integers
+x[3:4] <- c("123456789123456", "-9876543219876") # ... or characters
+x
+
+# convert integer or character vectors into int64 vectors
+x <- as.int64( 1:6 )
+x
+y <- as.int64( c("-1234", "1234" ) )
+y
+
+# create a data frame with a column of int64
+df <- data.frame( a = 1:4 )
+df$y <- as.int64( 1:4 )
+df
+@
+
+\section{The int64 and uint64 classes}
+
+\subsection{Class representation}
+
+Both \texttt{int64} and \texttt{uint64} are represented as lists of pairs of
+integers. 
+
+<<>>=
+str( as.int64( 1:2 ) )
+@
+
+Each int64 or uint64 number is represented as a couple of 32 bit integers. 
+Internally, the C++ code goes back and forth between the native representation
+of these numbers as C++ data types (\texttt{int64\_t} and \texttt{uint64\_t})
+and their representation as couples of 32 bit integers by splitting the 
+64 bits. 
+
+For example, the \texttt{int64\_t} value (-123) is represented in memory as: 
+
+\vspace{1em}
+
+\begin{tabular}{|p{1em}cp{1em}|}
+\hline
+& \texttt{\Sexpr{binary( as.int64( "-123" ))@data}} & \\
+\hline
+\end{tabular}
+
+\vspace{1em}
+
+These 64 bits are split into the two following chunks: 
+
+<<echo=FALSE,print=FALSE>>=
+first.int <- as.int64( "-123" )[[1L]][1L]
+first <- binary( first.int)@data
+second.int <- as.int64( "-123" )[[1L]][2L]
+second <- binary( second.int )@data
+@
+
+\vspace{1em}
+
+\begin{tabular}{|cp{.4em}|p{.4em}c|}
+\hline
+\texttt{\Sexpr{first}} & & & \texttt{\Sexpr{second}} \\
+\hline
+\end{tabular}
+
+\vspace{1em}
+
+The R representation of -123 is therefore composed by the two integers
+whose binary representation is above, i.e (\Sexpr{first.int},\Sexpr{second.int}).
+    This representation has been chosen against other alternatives to 
+allow these key requirements: 
+\begin{itemize}
+\item Data must be serializable
+\item int64 and uint64 vectors have to be usable of 
+columns of data frames. 
+\end{itemize}
+            
+\subsection{Creating new vectors}
+
+The functions \texttt{int64} and \texttt{uint64} can be used to create
+new vectors of signed or usigned 64 bit integers of the given length. These
+functions are similar to the usual \texttt{R} functions \texttt{numeric}, 
+\texttt{integer}, etc ...
+
+<<>>=
+int64(3)
+uint64(10)
+@
+
+\subsection{Converting integer or character vectors}
+
+The functions \texttt{as.int64} and \texttt{as.uint64} can be used
+to convert \texttt{integer} or \texttt{character} vectors into signed or 
+unsigned 64 bit integers. 
+
+<<>>=
+as.int64( 1:4 )
+as.uint64( c("123456789", "987654321987654321" ) )
+@
+   
+Internally \texttt{integer} vectors are converted using a reguar cast, and
+\texttt{character} vectors are converted using the \texttt{C} function
+\texttt{atol}. 
+
+\subsection{Subsetting}
+
+Extracting or setting subsets from a \texttt{int64} or \texttt{uint64}
+vector is similar to other vector classes in R. 
+
+<<>>=
+x <- as.int64( 1:4 )
+x[1:2]
+x[3:4] <- 5:6
+x
+@
+
+\subsection{Arithmetic operations}
+
+The \texttt{Arith} group generic is implemented for classes \texttt{int64}
+and \texttt{uint64}. 
+
+<<>>=
+x <- as.int64( 1:4 )
+x + 1L
+x - 1:2
+x * x
+x / 2L
+x %% 2L
+x %/% 2L
+@
+
+\subsection{Logical operations}
+
+The \texttt{Compare} group generic is implemented
+for classes \texttt{int64} and \texttt{uint64}. 
+
+<<>>=
+x <- as.int64( 1:5 )
+x < 3L
+x > 6L - x
+x != 3L
+x == 4L
+x <= 3L
+x >= 5L
+@
+
+\subsection{Summary operations}
+
+The \texttt{Summary} group generic is implemented
+for classes \texttt{int64} and \texttt{uint64}. 
+
+<<>>=
+x <- as.int64( 1:5 )
+min( x )
+max( x )
+range( x )
+prod( x )
+sum( x )
+any( x )
+all( x )
+@
+
+\section{Binary representation}
+
+The \texttt{binary} generic function shows the bit representation
+of \texttt{numeric}, \texttt{integer}, \texttt{int64} and \texttt{uint64}. 
+                             
+<<>>=
+binary( 1:4 ) # integer
+binary( c(1.2, 1.3) ) # numeric
+binary( as.int64( 1:4 ) ) # signed 64 bit integer (int64)
+binary( as.uint64( 1:4 ) ) # unsigned 64 bit integer (uint64)
+@
+
+\section{Numeric limits}
+
+The \texttt{numeric\_limits} function gives the limits for 
+types \texttt{integer}, \texttt{int64}, \texttt{uint64}. 
+
+<<>>=
+numeric_limits( "integer" )
+numeric_limits( "int64" )
+numeric_limits( "uint64" )
+@
+
+\end{document}
+

Added: pkg/int64/inst/include/int64/LongVector.h
===================================================================
--- pkg/int64/inst/include/int64/LongVector.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/LongVector.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,74 @@
+// routines.h: int64 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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_LongVector_h
+#define int64_LongVector_h
+
+#include <stdio.h>
+#include <iostream>
+
+namespace int64{
+    
+    template <class LONG>
+    class LongVector {
+    private :
+        SEXP data ;
+        
+    public:
+        LongVector(SEXP x) : data(x) {
+            R_PreserveObject(data) ;   
+        }
+        
+        operator SEXP(){ return data; }
+        
+        LongVector(int n) : data(R_NilValue) {
+            SEXP x = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
+            for( int i=0; i<n; i++){
+                SET_VECTOR_ELT( x, i, int64::internal::int2(0,0) ) ;    
+            }
+            UNPROTECT(1) ; // x
+            data = x ;
+            R_PreserveObject(data) ;
+        }
+        
+        ~LongVector(){
+            R_ReleaseObject(data) ;   
+        }
+        
+        inline LONG get(int i) const {
+            int* p = INTEGER(VECTOR_ELT(data,i)) ;
+            return int64::internal::get_long<LONG>( p[0], p[1] ) ;
+        }
+        
+        inline void set(int i, LONG x){
+            int* p = INTEGER(VECTOR_ELT(data,i)) ;
+            p[0] = int64::internal::get_high_bits(x) ;
+            p[1] = int64::internal::get_low_bits(x) ;
+        }
+        
+        inline int size() const { return Rf_length(data); }
+        
+    } ;
+
+    
+}
+
+
+#endif

Added: pkg/int64/inst/include/int64/arith.h
===================================================================
--- pkg/int64/inst/include/int64/arith.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/arith.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,92 @@
+// arith.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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__arith__h
+#define int64__arith__h
+              
+namespace int64{
+    namespace internal{
+
+template <typename T> inline T plus(T x1,T x2){ return x1 + x2 ; }        
+template <typename T> inline T minus(T x1,T x2){ return x1 - x2 ; }        
+template <typename T> inline T times(T x1,T x2){ return x1 * x2 ; }        
+template <typename T> inline T divide(T x1,T x2){ return x1/x2 ; }        
+template <typename T> inline T modulo(T x1,T x2){ return x1 % x2 ; }        
+template <typename T> inline T int_div(T x1,T x2){ return x1 / x2 ; }        
+        
+template <typename LONG, LONG Fun(LONG x1, LONG x2)>
+SEXP arith_long_long(SEXP e1, SEXP e2){
+    int64::LongVector<LONG> x1( R_do_slot( e1, Rf_install(".Data") ) ) ;
+    int64::LongVector<LONG> x2( R_do_slot( e2, Rf_install(".Data") ) ) ;
+    
+    int n1 = x1.size(), n2 = x2.size();
+    LONG tmp ;
+    int i1 = 0, i2 = 0, i = 0 ;
+    int n = (n1>n2) ? n1 : n2 ;
+    int64::LongVector<LONG> res(n) ;
+    
+    if( n1 == n2 ){
+        for( i=0; i<n1; i++){
+            res.set( i, Fun( x1.get(i), x2.get(i) ) ) ;
+        }
+    } else if( n1 == 1 ){
+        tmp = x1.get(0) ; 
+        for( i=0; i<n2; i++){
+            res.set(i, Fun( tmp, x2.get(i) ) ) ;
+        }
+    } else if( n2 == 1) {
+        tmp = x2.get(i) ;
+        for( i=0; i<n1; i++){
+            res.set(i, Fun(x1.get(i), tmp) ) ;
+        }
+    } else {
+        // recycling
+        for (i=i1=i2=0; i<n; i1 = (++i1 == n1) ? 0 : i1, i2 = (++i2 == n2) ? 0 : i2, ++i){
+           res.set( i, Fun( x1.get(i1), x2.get(i2) ) ) ;
+        }
+    }
+    return res ;
+}
+
+    template <typename LONG>
+    SEXP int64_arith__impl( const char* op, SEXP e1, SEXP e2){
+        if( ! strcmp(op, "+") ){
+            return int64::internal::arith_long_long<LONG, int64::internal::plus<LONG> >( e1, e2) ;
+        } else if( ! strcmp( op, "-" ) ) {
+            return int64::internal::arith_long_long<LONG, int64::internal::minus<LONG> >( e1, e2) ;
+        } else if( ! strcmp( op, "*" ) ) {
+            return int64::internal::arith_long_long<LONG, int64::internal::times<LONG> >( e1, e2) ;
+        } else if( ! strcmp( op, "^" ) ) {
+             Rf_error( "pow not implemented for long type" ) ;
+        } else if( ! strcmp( op, "/" ) ) {
+            return int64::internal::arith_long_long<LONG, int64::internal::divide<LONG> >( e1, e2) ;
+        } else if( ! strcmp( op, "%%" ) ) {
+            return int64::internal::arith_long_long<LONG, int64::internal::modulo<LONG> >( e1, e2) ;
+        } else if( ! strcmp( op, "%/%" ) ) {
+            return int64::internal::arith_long_long<LONG, int64::internal::int_div<LONG> >( e1, e2) ;
+        } 
+        Rf_error( "unknown operator" ) ;
+        return R_NilValue ; 
+    }
+    } // namespace internal
+    
+} // namespace int64
+
+#endif

Added: pkg/int64/inst/include/int64/as_character.h
===================================================================
--- pkg/int64/inst/include/int64/as_character.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/as_character.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,48 @@
+// as_character.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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__as_character__h
+#define int64__as_character__h
+   
+#include <iostream>
+#include <sstream>
+
+namespace int64{
+    namespace internal{
+
+template <typename LONG>
+SEXP int64_as_character( SEXP x){
+    int64::LongVector<LONG> data( R_do_slot(x, Rf_install(".Data") ) ) ;
+    int n = data.size() ; 
+    SEXP res = PROTECT( Rf_allocVector( STRSXP, n) ) ;
+    std::ostringstream stream ;
+    for( int i=0; i<n; i++){ 
+        stream << data.get(i) ;
+        SET_STRING_ELT( res, i, Rf_mkChar(stream.str().c_str()) ) ;
+        stream.str("") ;
+    }
+    UNPROTECT(1) ; // res
+    return res ;
+}
+        
+    } // namespace internal
+} // namespace int64
+
+#endif

Added: pkg/int64/inst/include/int64/as_long.h
===================================================================
--- pkg/int64/inst/include/int64/as_long.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/as_long.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,72 @@
+// as_long.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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__as_long__h
+#define int64__as_long__h
+              
+namespace int64{
+    namespace internal{
+
+    template <typename LONG>
+    LONG read_string(const char* s) ;
+    
+    template <>
+    int64_t read_string<int64_t>(const char* s ){
+        return strtoll( s, NULL, 0 ) ; 
+    }
+        
+    template <>
+    uint64_t read_string<uint64_t>(const char* s){
+        return strtoull( s, NULL, 0 ) ;
+    } 
+        
+        
+template <typename LONG>
+SEXP as_long(SEXP x){
+    int n = Rf_length(x) ;
+    int64::LongVector<LONG> data(n) ;
+    switch( TYPEOF(x) ){
+    case INTSXP:
+        {
+            int* p_x = INTEGER(x) ;
+            for( int i=0;i<n; i++){
+                data.set( i, (LONG)p_x[i] ) ;
+            }
+            break ;
+        }
+    case STRSXP:
+        {
+            for( int i=0;i<n; i++){
+                LONG tmp = read_string<LONG>(CHAR(STRING_ELT(x,i))) ;
+                data.set( i, tmp ) ;
+            }
+            break;                               
+        }     
+    default:
+        Rf_error( "incompatible type" ) ;
+    }
+    return data ;
+       
+}
+        
+    } // namespace internal
+} // namespace int64
+
+#endif

Added: pkg/int64/inst/include/int64/compare.h
===================================================================
--- pkg/int64/inst/include/int64/compare.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/compare.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,94 @@
+// compare.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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__compare__h
+#define int64__compare__h
+
+namespace int64{
+    namespace internal{
+
+template <typename T> inline bool equals(T x1,T x2){ return x1 == x2 ; }        
+template <typename T> inline bool not_equals(T x1,T x2){ return x1 != x2 ; }        
+template <typename T> inline bool lower_than(T x1,T x2){ return x1 < x2 ; }        
+template <typename T> inline bool lower_than_or_equal(T x1,T x2){ return x1 <= x2 ; }        
+template <typename T> inline bool greater_than(T x1,T x2){ return x1 > x2 ; }        
+template <typename T> inline bool greater_than_or_equal(T x1,T x2){ return x1 >= x2 ; }        
+        
+template <typename LONG, bool Fun(LONG x1, LONG x2)>
+SEXP compare_long_long(SEXP e1, SEXP e2){
+    int64::LongVector<LONG> x1( R_do_slot( e1, Rf_install(".Data") ) ) ;
+    int64::LongVector<LONG> x2( R_do_slot( e2, Rf_install(".Data") ) ) ;
+    
+    int n1 = x1.size(), n2 = x2.size() ;
+    LONG tmp ;
+    int i1=0, i2=0, i=0 ;
+    int n = (n1>n2) ? n1 : n2 ;
+    SEXP res = PROTECT(Rf_allocVector(LGLSXP, n)); 
+    int* p_res = INTEGER(res) ;       
+    
+    if( n1 == n2 ){
+        for( i=0; i<n1; i++){
+            p_res[i] = Fun(x1.get(i), x2.get(i)) ;
+        }
+    } else if( n1 == 1 ){
+        tmp = x1.get(i) ;
+        for( i=0; i<n2; i++){
+            p_res[i] = Fun(tmp,x2.get(i)) ;
+        }
+    } else if( n2 == 1) {
+        tmp = x2.get(i) ;
+        for( i=0; i<n1; i++){
+            p_res[i] = Fun(x1.get(i),tmp) ;
+        }
+    } else {
+        // recycling
+        for (i=i1=i2=0; i<n; i1 = (++i1 == n1) ? 0 : i1, i2 = (++i2 == n2) ? 0 : i2, ++i){
+           p_res[i] = Fun(x1.get(i1), x2.get(i2)) ;
+        }
+    }
+    UNPROTECT(1) ; // res
+    return res ;
+}
+
+template <typename LONG>
+SEXP int64_compare(const char* op, SEXP e1, SEXP e2){
+    if( ! strcmp(op, "==") ){
+        return int64::internal::compare_long_long<LONG,  int64::internal::equals<LONG> >( e1, e2) ;
+    } else if( ! strcmp( op, "!=" ) ) {
+        return int64::internal::compare_long_long<LONG,  int64::internal::not_equals<LONG> >( e1, e2) ;
+    } else if( ! strcmp( op, "<" ) ) {
+        return int64::internal::compare_long_long<LONG,  int64::internal::lower_than<LONG> >( e1, e2) ;
+    } else if( ! strcmp( op, ">" ) ) {
+         return int64::internal::compare_long_long<LONG,  int64::internal::greater_than<LONG> >( e1, e2) ;
+    } else if( ! strcmp( op, "<=" ) ) {
+        return int64::internal::compare_long_long<LONG,  int64::internal::lower_than_or_equal<LONG> >( e1, e2) ;
+    } else if( ! strcmp( op, ">=" ) ) {
+        return int64::internal::compare_long_long<LONG,  int64::internal::greater_than_or_equal<LONG> >( e1, e2) ;
+    } 
+    Rf_error( "unknown operator" ) ;
+    return R_NilValue ;
+}
+
+
+    } // namespace internal
+    
+} // namespace int64
+
+#endif

Added: pkg/int64/inst/include/int64/format_binary.h
===================================================================
--- pkg/int64/inst/include/int64/format_binary.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/format_binary.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,65 @@
+// format_binary.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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__binary__h
+#define int64__binary__h
+
+namespace int64{
+    namespace internal{
+
+        template <typename T>
+        inline std::string format_binary__impl(T x) {
+            const int SIZE = sizeof(T)*8 ;
+            static char b[SIZE+1];
+            b[SIZE+1] = '\0';
+        
+            for (int z = 0; z < SIZE; z++) {
+                b[SIZE-1-z] = ((x>>z) & 0x1) ? '1' : '0';
+            }
+        
+            return b;
+        }    
+        
+        template <>
+        inline std::string format_binary__impl<double>(double x){
+                int64_t* y = (int64_t*)&x ;
+                return format_binary__impl<int64_t>(*y) ;
+        }
+
+        template <typename LONG>
+        SEXP int64_format_binary_long(SEXP x){
+            SEXP data = R_do_slot(x, Rf_install(".Data") ) ;
+            int n = Rf_length(data) ; 
+            
+            SEXP res = PROTECT( Rf_allocVector( STRSXP, n ) ) ;
+            int* p_x ;
+            LONG tmp ;
+            for( int i=0; i<n; i++){
+                p_x = INTEGER( VECTOR_ELT( data, i) ) ;
+                tmp = int64::internal::get_long<LONG>( p_x[0], p_x[1] ) ;
+                SET_STRING_ELT( res, i, Rf_mkChar( format_binary__impl(tmp).c_str()) ) ;
+            }
+            UNPROTECT(1) ; // res
+            return res ;
+        }
+        
+    } // namespace internal
+} // namespace int64
+#endif

Added: pkg/int64/inst/include/int64/get_bits.h
===================================================================
--- pkg/int64/inst/include/int64/get_bits.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/get_bits.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,40 @@
+// get_bits.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// 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__get_bits__h
+#define int64__get_bits__h
+
+namespace int64{
+    namespace internal{
+
+template <typename T64>
+inline int get_low_bits( T64 x){
+    return (int)( x & 0x00000000FFFFFFFF ) ;
+}
+
+template <typename T64>
+inline int get_high_bits( T64 x){
+    return (int)( x >> 32 ) ;
+}
+
+    } // namespace internal
+    
+} // namespace int64
+#endif

Added: pkg/int64/inst/include/int64/get_class.h
===================================================================
--- pkg/int64/inst/include/int64/get_class.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/get_class.h	2011-10-29 08:59:41 UTC (rev 3238)
@@ -0,0 +1,43 @@
+// get_class.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google
+//
+// This file is part of int64.
+//
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rcpp -r 3238


More information about the Rcpp-commits mailing list