Rcpp Version 1.0.9
Dimension.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // Dimension.h: Rcpp R/C++ interface class library -- dimensions
4 //
5 // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp_Dimension_h
23 #define Rcpp_Dimension_h
24 
25 namespace Rcpp{
26 
27  class Dimension {
28  public:
29  typedef std::vector<int>::reference reference ;
30  typedef std::vector<int>::const_reference const_reference ;
31 
32  Dimension() : dims(){}
33 
34  Dimension(SEXP dims) ;
35 
36  Dimension( const Dimension& other ) : dims(other.dims){}
37  Dimension& operator=( const Dimension& other ) {
38  if( *this != other )
39  dims = other.dims ;
40  return *this ;
41  }
42  Dimension(const size_t& n1) : dims(1){
43  dims[0] = static_cast<int>(n1) ;
44  }
45  Dimension(const size_t& n1, const size_t& n2) : dims(2){
46  dims[0] = static_cast<int>(n1) ;
47  dims[1] = static_cast<int>(n2) ;
48  }
49  Dimension(const size_t& n1, const size_t& n2, const size_t& n3) : dims(3){
50  dims[0] = static_cast<int>(n1) ;
51  dims[1] = static_cast<int>(n2) ;
52  dims[2] = static_cast<int>(n3) ;
53  }
54  operator SEXP() const ;
55 
56  inline int size() const {
57  return (int) dims.size() ;
58  }
59  inline R_xlen_t prod() const {
60  return std::accumulate( dims.begin(), dims.end(), static_cast<R_xlen_t>(1), std::multiplies<R_xlen_t>() );
61  }
62 
63  inline reference operator[](int i){
64  if( i < 0 || i>=static_cast<int>(dims.size()) ) throw std::range_error("index out of bounds") ;
65  return dims[i] ;
66  }
67  inline const_reference operator[](int i) const{
68  if( i < 0 || i>=static_cast<int>(dims.size()) ) throw std::range_error("index out of bounds") ;
69  return dims[i] ;
70  }
71 
72  private:
73  std::vector<int> dims;
74  };
75 
76 }
77 #endif
reference operator[](int i)
Definition: Dimension.h:63
int size() const
Definition: Dimension.h:56
std::vector< int > dims
Definition: Dimension.h:73
std::vector< int >::reference reference
Definition: Dimension.h:29
Dimension & operator=(const Dimension &other)
Definition: Dimension.h:37
const_reference operator[](int i) const
Definition: Dimension.h:67
Dimension(const size_t &n1)
Definition: Dimension.h:42
Dimension(const size_t &n1, const size_t &n2, const size_t &n3)
Definition: Dimension.h:49
R_xlen_t prod() const
Definition: Dimension.h:59
std::vector< int >::const_reference const_reference
Definition: Dimension.h:30
Dimension(const size_t &n1, const size_t &n2)
Definition: Dimension.h:45
Dimension(const Dimension &other)
Definition: Dimension.h:36
Rcpp API.
Definition: algo.h:28