Skip to content

Commit 3987738

Browse files
using GreedyVector template instead of manual copy and paste
1 parent 54c7ebf commit 3987738

File tree

4 files changed

+113
-174
lines changed

4 files changed

+113
-174
lines changed

inst/include/Rcpp/DateVector.h

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// DateVector.h: Rcpp R/C++ interface class library -- Date vector support
44
//
5-
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -23,38 +23,19 @@
2323
#define Rcpp__DateVector_h
2424

2525
#include <RcppCommon.h>
26+
#include <Rcpp/internal/GreedyVector.h>
2627

2728
namespace Rcpp {
2829

29-
class DateVector {
30+
class DateVector : public GreedyVector<Date, DateVector> {
3031
public:
31-
typedef std::vector<Date>::iterator iterator;
32-
typedef std::vector<Date>::const_iterator const_iterator;
32+
DateVector(SEXP vec) : GreedyVector(vec){}
33+
DateVector(int n) : GreedyVector(n){}
3334

34-
DateVector(SEXP vec);
35-
DateVector(int n);
36-
~DateVector() {};
37-
38-
const Date& operator()(int i) const;
39-
Date& operator()(int i);
40-
41-
const Date& operator[](int i) const;
42-
Date& operator[](int i);
43-
44-
int size() const;
45-
46-
std::vector<Date> getDates() const;
47-
48-
inline iterator begin(){ return v.begin(); }
49-
inline iterator end(){ return v.end(); }
50-
51-
inline const_iterator begin() const { return v.begin(); }
52-
inline const_iterator end() const { return v.end(); }
53-
54-
inline operator SEXP() const { return wrap( v ) ; }
35+
inline std::vector<Date> getDates() const{
36+
return v ;
37+
}
5538

56-
private:
57-
std::vector<Date> v;
5839
};
5940
}
6041

inst/include/Rcpp/DatetimeVector.h

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// DatetimeVector.h: Rcpp R/C++ interface class library -- Datetime vector
44
//
5-
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -24,37 +24,19 @@
2424

2525
#include <RcppCommon.h>
2626

27+
#include <Rcpp/internal/GreedyVector.h>
28+
2729
namespace Rcpp {
2830

29-
class DatetimeVector {
31+
class DatetimeVector : public GreedyVector<Datetime, DatetimeVector> {
3032
public:
31-
typedef std::vector<Datetime>::iterator iterator;
32-
typedef std::vector<Datetime>::const_iterator const_iterator;
33-
34-
DatetimeVector(SEXP vec);
35-
DatetimeVector(int n);
36-
~DatetimeVector() {};
37-
38-
const Datetime& operator()(int i) const;
39-
Datetime& operator()(int i);
40-
41-
const Datetime& operator[](int i) const;
42-
Datetime& operator[](int i);
43-
44-
int size() const;
45-
46-
std::vector<Datetime> getDatetimes() const;
47-
48-
inline iterator begin(){ return v.begin(); }
49-
inline iterator end(){ return v.end(); }
50-
51-
inline const_iterator begin() const { return v.begin(); }
52-
inline const_iterator end() const { return v.end(); }
33+
DatetimeVector(SEXP vec) : GreedyVector(vec){}
34+
DatetimeVector(int n) : GreedyVector(n){}
5335

54-
inline operator SEXP() const { return wrap( v ) ; }
36+
std::vector<Datetime> getDatetimes() const{
37+
return v ;
38+
}
5539

56-
private:
57-
std::vector<Datetime> v;
5840
};
5941
}
6042

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (C) 2013 Romain Francois
2+
//
3+
// This file is part of Rcpp.
4+
//
5+
// Rcpp is free software: you can redistribute it and/or modify it
6+
// under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Rcpp is distributed in the hope that it will be useful, but
11+
// WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#ifndef RCPP_INTERNAL_GREEDYVECTOR_H
19+
#define RCPP_INTERNAL_GREEDYVECTOR_H
20+
21+
namespace Rcpp {
22+
23+
template <typename T, typename CLASS>
24+
class GreedyVector {
25+
public:
26+
typedef typename std::vector<T>::iterator iterator;
27+
typedef typename std::vector<T>::const_iterator const_iterator;
28+
29+
GreedyVector(SEXP vec) : v(0){
30+
if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
31+
throw std::range_error("invalid numeric vector in constructor");
32+
int len = Rf_length(vec);
33+
if (len == 0)
34+
throw std::range_error("null vector in constructor");
35+
v.resize(len);
36+
for (int i = 0; i < len; i++)
37+
v[i] = T( static_cast<double>(REAL(vec)[i]));
38+
}
39+
40+
GreedyVector(int n) : v(n){}
41+
42+
inline const T& operator()(int i) const{
43+
return at(i) ;
44+
}
45+
inline T& operator()(int i){
46+
return at(i) ;
47+
}
48+
49+
inline const T& operator[](int i) const{
50+
return at(i) ;
51+
}
52+
inline T& operator[](int i){
53+
return at(i) ;
54+
}
55+
56+
inline int size() const {
57+
return (int)v.size();
58+
}
59+
60+
inline iterator begin(){ return v.begin(); }
61+
inline iterator end(){ return v.end(); }
62+
63+
inline const_iterator begin() const { return v.begin(); }
64+
inline const_iterator end() const { return v.end(); }
65+
66+
inline operator SEXP() const {
67+
return wrap( v ) ;
68+
}
69+
70+
protected:
71+
std::vector<T> v;
72+
73+
private:
74+
const T& at(int i) const{
75+
if (i < 0 || i >= static_cast<int>(v.size())) {
76+
std::ostringstream oss;
77+
oss << "subscript out of range: " << i;
78+
throw std::range_error(oss.str());
79+
}
80+
return v[i];
81+
}
82+
83+
T& at(int i) {
84+
if (i < 0 || i >= static_cast<int>(v.size())) {
85+
std::ostringstream oss;
86+
oss << "subscript out of range: " << i;
87+
throw std::range_error(oss.str());
88+
}
89+
return v[i];
90+
}
91+
92+
} ;
93+
94+
}
95+
96+
#endif

src/Date.cpp

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -83,126 +83,6 @@ namespace Rcpp {
8383
#undef isleap
8484
#undef days_in_year
8585

86-
87-
88-
DatetimeVector::DatetimeVector(SEXP vec) : v() {
89-
int i;
90-
if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
91-
throw std::range_error("DatetimeVector: invalid numeric vector in constructor");
92-
int len = Rf_length(vec);
93-
if (len == 0)
94-
throw std::range_error("DatetimeVector: null vector in constructor");
95-
v.resize(len);
96-
for (i = 0; i < len; i++)
97-
v[i] = Datetime( static_cast<double>(REAL(vec)[i]));
98-
}
99-
100-
101-
DatetimeVector::DatetimeVector(int n) : v(n) {}
102-
103-
const Datetime & DatetimeVector::operator()(int i) const {
104-
if (i < 0 || i >= static_cast<int>(v.size())) {
105-
std::ostringstream oss;
106-
oss << "DatetimeVector: subscript out of range: " << i;
107-
throw std::range_error(oss.str());
108-
}
109-
return v[i];
110-
}
111-
112-
Datetime & DatetimeVector::operator()(int i) {
113-
if (i < 0 || i >= static_cast<int>(v.size())) {
114-
std::ostringstream oss;
115-
oss << "DatetimeVector: subscript out of range: " << i;
116-
throw std::range_error(oss.str());
117-
}
118-
return v[i];
119-
}
120-
121-
const Datetime & DatetimeVector::operator[](int i) const {
122-
if (i < 0 || i >= static_cast<int>(v.size())) {
123-
std::ostringstream oss;
124-
oss << "DatetimeVector: subscript out of range: " << i;
125-
throw std::range_error(oss.str());
126-
}
127-
return v[i];
128-
}
129-
130-
Datetime & DatetimeVector::operator[](int i) {
131-
if (i < 0 || i >= static_cast<int>(v.size())) {
132-
std::ostringstream oss;
133-
oss << "DatetimeVector: subscript out of range: " << i;
134-
throw std::range_error(oss.str());
135-
}
136-
return v[i];
137-
}
138-
139-
int DatetimeVector::size() const {
140-
return v.size();
141-
}
142-
143-
std::vector<Datetime> DatetimeVector::getDatetimes() const {
144-
return v;
145-
}
146-
147-
DateVector::DateVector(SEXP vec) : v() {
148-
int i;
149-
if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
150-
throw std::range_error("DateVector: invalid numeric vector in constructor");
151-
int len = Rf_length(vec);
152-
if (len == 0)
153-
throw std::range_error("DateVector: null vector in constructor");
154-
v.resize(len);
155-
for (i = 0; i < len; i++)
156-
v[i] = Date( static_cast<double>(REAL(vec)[i]));
157-
}
158-
159-
160-
DateVector::DateVector(int n) : v(n){}
161-
162-
const Date & DateVector::operator()(int i) const {
163-
if (i < 0 || i >= static_cast<int>(v.size())) {
164-
std::ostringstream oss;
165-
oss << "DateVector: subscript out of range: " << i;
166-
throw std::range_error(oss.str());
167-
}
168-
return v[i];
169-
}
170-
171-
Date & DateVector::operator()(int i) {
172-
if (i < 0 || i >= static_cast<int>(v.size())) {
173-
std::ostringstream oss;
174-
oss << "DateVector: subscript out of range: " << i;
175-
throw std::range_error(oss.str());
176-
}
177-
return v[i];
178-
}
179-
180-
const Date & DateVector::operator[](int i) const {
181-
if (i < 0 || i >= static_cast<int>(v.size())) {
182-
std::ostringstream oss;
183-
oss << "DatetimeVector: subscript out of range: " << i;
184-
throw std::range_error(oss.str());
185-
}
186-
return v[i];
187-
}
188-
189-
Date & DateVector::operator[](int i) {
190-
if (i < 0 || i >= static_cast<int>(v.size())) {
191-
std::ostringstream oss;
192-
oss << "DatetimeVector: subscript out of range: " << i;
193-
throw std::range_error(oss.str());
194-
}
195-
return v[i];
196-
}
197-
198-
int DateVector::size() const {
199-
return v.size();
200-
}
201-
202-
std::vector<Date> DateVector::getDates() const {
203-
return v;
204-
}
205-
20686
#include "sys/types.h" /* for time_t */
20787
#include "string.h"
20888
#include "limits.h" /* for CHAR_BIT et al. */

0 commit comments

Comments
 (0)