Rcpp Version 0.9.10
Dimension.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // Dimension.h: Rcpp R/C++ interface class library -- dimensions
00004 //
00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #include <Rcpp/Dimension.h>
00023 
00024 namespace Rcpp{
00025 
00026     Dimension::Dimension() : dims(){}
00027         
00028     Dimension::Dimension(SEXP x): dims(){
00029         dims = as< std::vector<int> >(x) ;
00030     }
00031         
00032     Dimension::Dimension( const Dimension& other ) : dims(){
00033         dims = other.dims ; /* copy */
00034     }
00035         
00036     Dimension& Dimension::operator=(const Dimension& other){
00037         dims = other.dims ; /* copy */
00038         return *this ;
00039     }
00040         
00041     Dimension::Dimension(const size_t& n1) : dims(1){
00042         dims[0] = n1 ;
00043     }
00044         
00045     Dimension::Dimension(const size_t& n1, const size_t& n2) : dims(2){
00046         dims[0] = n1 ;
00047         dims[1] = n2 ;
00048     }
00049         
00050     Dimension::Dimension(const size_t& n1, const size_t& n2, const size_t& n3) : dims(3){
00051         dims[0] = n1 ;
00052         dims[1] = n2 ;
00053         dims[2] = n3 ;
00054     }
00055         
00056     Dimension::operator SEXP() const {
00057         return wrap( dims.begin(), dims.end() ) ;
00058     }
00059         
00060     int Dimension::size() const {
00061         return static_cast<int>( dims.size() ) ;
00062     }
00063         
00064     int Dimension::prod() const {
00065         return std::accumulate( dims.begin(), dims.end(), 1, std::multiplies<int>() ) ;
00066     }
00067         
00068     Dimension::reference Dimension::operator[](int i) {
00069         if( i < 0 || i>=static_cast<int>(dims.size()) ) throw std::range_error("index out of bounds") ;
00070         return dims.at(i) ;
00071     }
00072 
00073     Dimension::const_reference Dimension::operator[](int i) const {
00074         if( i < 0 || i>=static_cast<int>(dims.size()) ) throw std::range_error("index out of bounds") ;
00075         return dims.at(i) ;
00076     }
00077 
00078 } // namespace Rcpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines