Rcpp Version 0.9.10
DatetimeVector.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 // DatetimeVector.cpp: Rcpp R/C++ interface class library -- Datetime vector
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/DatetimeVector.h>
00023 #include <Rcpp/Datetime.h>
00024 
00025 namespace Rcpp {
00026 
00027     DatetimeVector::DatetimeVector(SEXP vec) : v()  {
00028         int i;
00029         if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
00030             throw std::range_error("DatetimeVector: invalid numeric vector in constructor");
00031         int len = Rf_length(vec);
00032         if (len == 0)
00033             throw std::range_error("DatetimeVector: null vector in constructor");
00034         v.resize(len);
00035         for (i = 0; i < len; i++)
00036             v[i] = Datetime( static_cast<double>(REAL(vec)[i]));
00037     }
00038 
00039 
00040     DatetimeVector::DatetimeVector(int n) : v(n) {}
00041 
00042     const Datetime & DatetimeVector::operator()(int i) const {
00043         if (i < 0 || i >= static_cast<int>(v.size())) {
00044             std::ostringstream oss;
00045             oss << "DatetimeVector: subscript out of range: " << i;
00046             throw std::range_error(oss.str());
00047         }
00048         return v[i];
00049     }
00050 
00051     Datetime & DatetimeVector::operator()(int i) {
00052         if (i < 0 || i >= static_cast<int>(v.size())) {
00053             std::ostringstream oss;
00054             oss << "DatetimeVector: subscript out of range: " << i;
00055             throw std::range_error(oss.str());
00056         }
00057         return v[i];
00058     }
00059 
00060     const Datetime & DatetimeVector::operator[](int i) const {
00061         if (i < 0 || i >= static_cast<int>(v.size())) {
00062             std::ostringstream oss;
00063             oss << "DatetimeVector: subscript out of range: " << i;
00064             throw std::range_error(oss.str());
00065         }
00066         return v[i];
00067     }
00068 
00069     Datetime & DatetimeVector::operator[](int i) {
00070         if (i < 0 || i >= static_cast<int>(v.size())) {
00071             std::ostringstream oss;
00072             oss << "DatetimeVector: subscript out of range: " << i;
00073             throw std::range_error(oss.str());
00074         }
00075         return v[i];
00076     }
00077 
00078     int DatetimeVector::size() const { 
00079         return v.size(); 
00080     }
00081 
00082     std::vector<Datetime> DatetimeVector::getDatetimes() const {
00083         return v;
00084     }
00085 
00086 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines