forked from lballabio/QuantLib-SWIG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimebasket.i
107 lines (97 loc) · 3.56 KB
/
timebasket.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright (C) 2000, 2001, 2002 RiskMap srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<[email protected]>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program 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 license for more details.
*/
#ifndef quantlib_timebasket_i
#define quantlib_timebasket_i
%include common.i
%include types.i
%include date.i
%{
using QuantLib::TimeBasket;
%}
class TimeBasket {
#if defined (SWIGPYTHON) || defined(SWIGRUBY)
%rename(__len__) size;
#endif
public:
TimeBasket();
TimeBasket(const std::vector<Date>&, const std::vector<Real>&);
Size size();
TimeBasket rebin(const std::vector<Date>&) const;
%extend {
#if defined(SWIGPYTHON) || defined(SWIGRUBY)
Real __getitem__(const Date& d) {
return (*self)[d];
}
void __setitem__(const Date& d, Real value) {
(*self)[d] = value;
}
#endif
#if defined(SWIGPYTHON)
PyObject* items() {
PyObject* itemList = PyList_New(self->size());
TimeBasket::iterator i;
Size j;
for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) {
Date* d = new Date(i->first);
PyObject* item = PyTuple_New(2);
PyTuple_SetItem(item,0,
SWIG_NewPointerObj((void *) d,
$descriptor(Date *),1));
PyTuple_SetItem(item,1,PyFloat_FromDouble(i->second));
PyList_SetItem(itemList,j,item);
}
return itemList;
}
// Python 2.2 methods
bool __contains__(const Date& d) {
return self->hasDate(d);
}
PyObject* __iter__() {
%#if PY_VERSION_HEX >= 0x02020000
PyObject* keyList = PyList_New(self->size());
TimeBasket::iterator i;
Size j;
for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) {
Date* d = new Date(i->first);
PyList_SetItem(keyList,j,
SWIG_NewPointerObj((void *) d,
$descriptor(Date *),1));
}
PyObject* iter = PyObject_GetIter(keyList);
Py_DECREF(keyList);
return iter;
%#else
throw std::runtime_error("Python 2.2 or later is needed"
" for iterator support");
%#endif
}
#endif
#if defined(SWIGRUBY)
void each() {
TimeBasket::iterator i;
for (i=self->begin(); i!=self->end(); ++i) {
Date* d = new Date(i->first);
VALUE entry = rb_ary_new2(2);
VALUE k = SWIG_NewPointerObj((void *) d,
$descriptor(Date *),1);
VALUE x = rb_float_new(i->second);
rb_ary_store(entry,0,k);
rb_ary_store(entry,1,x);
rb_yield(entry);
}
}
#endif
}
};
#endif