Skip to content

Commit

Permalink
Cleanup comparisons (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Jan 31, 2024
2 parents a7720b2 + f3afc40 commit 1add646
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 49 deletions.
18 changes: 18 additions & 0 deletions Python/test/test_money.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import QuantLib as ql
import operator
import unittest


class MoneyTest(unittest.TestCase):
def test_order(self):
ops = [operator.eq, operator.ne, operator.lt, operator.le, operator.gt, operator.ge]
usd = lambda v: ql.Money(v, ql.USDCurrency())
for m1 in (usd(1), usd(2)):
for m2 in (usd(1), usd(2)):
for op in ops:
self.assertEqual(op(m1, m2), op(m1.value(), m2.value()))


if __name__ == "__main__":
print("testing QuantLib", ql.__version__)
unittest.main(verbosity=2)
6 changes: 0 additions & 6 deletions SWIG/common.i
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ namespace ext {
return (*self).operator->();
}
#if defined(SWIGPYTHON)
bool __nonzero__() {
return !!(*self);
}
bool __bool__() {
return !!(*self);
}
Expand All @@ -111,9 +108,6 @@ class Handle {
ext::shared_ptr<T> currentLink();
#if defined(SWIGPYTHON)
%extend {
bool __nonzero__() {
return !self->empty();
}
bool __bool__() {
return !self->empty();
}
Expand Down
3 changes: 0 additions & 3 deletions SWIG/currencies.i
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ class Currency {
Money __rmul__(Decimal x) {
return *self*x;
}
bool __nonzero__() {
return !self->empty();
}
bool __bool__() {
return !self->empty();
}
Expand Down
50 changes: 23 additions & 27 deletions SWIG/date.i
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,16 @@ enum Frequency {
}
%}
#endif

#if defined(SWIGJAVA)
%typemap(javainterfaces) Period QL_JAVA_INTERFACES "Comparable<Period>"
#endif

%{
using QuantLib::Period;
using QuantLib::PeriodParser;
%}

class Period {
#if defined(SWIGJAVA)
%rename("repr") __repr__;
%rename("compare") __cmp__;
#endif
public:
Period();
Period(Integer n, TimeUnit units);
Expand Down Expand Up @@ -327,6 +325,7 @@ class Period {
return *self * n;
}
#endif
#if defined(SWIGPYTHON) || defined(SWIGR) || defined(SWIGJAVA)
#if defined(SWIGPYTHON)
Period __rmul__(Integer n) {
return *self * n;
Expand All @@ -343,8 +342,13 @@ class Period {
bool __ge__(const Period& other) {
return !(*self < other);
}
#else
int __cmp__(const Period& other) {
return *self < other ? -1 :
*self == other ? 0 :
1;
}
#endif
#if defined(SWIGPYTHON) || defined(SWIGR) || defined(SWIGJAVA)
bool __eq__(const Period& other) {
return *self == other;
}
Expand All @@ -358,11 +362,6 @@ class Period {
boost::hash_combine(seed, p.units());
return seed;
}
int __cmp__(const Period& other) {
return *self < other ? -1 :
*self == other ? 0 :
1;
}
#endif

#if defined(SWIGCSHARP)
Expand Down Expand Up @@ -548,6 +547,9 @@ function(from) {Period(from)})
}
%}
#endif
#if defined(SWIGJAVA)
%typemap(javainterfaces) Date QL_JAVA_INTERFACES "Comparable<Date>"
#endif

%{
// used in Date(string, string) defined below
Expand All @@ -560,10 +562,6 @@ function(from) {Period(from)})
%}

class Date {
#if defined(SWIGJAVA)
%rename("repr") __repr__;
%rename("compare") __cmp__;
#endif
public:
Date();
Date(Day d, Month m, Year y);
Expand Down Expand Up @@ -739,19 +737,7 @@ class Date {
hash_t __hash__() {
return std::hash<Date>()(*self);
}
int __cmp__(const Date& other) {
if (*self < other)
return -1;
else if (*self == other)
return 0;
else
return 1;
}
#endif
#if defined(SWIGPYTHON)
bool __nonzero__() {
return (*self != Date());
}
bool __bool__() {
return (*self != Date());
}
Expand All @@ -776,6 +762,16 @@ class Date {
return Date(PyDateTime_GET_DAY(date), Month(PyDateTime_GET_MONTH(date)),
PyDateTime_GET_YEAR(date));
}
#else
int __cmp__(const Date& other) {
if (*self < other)
return -1;
else if (*self == other)
return 0;
else
return 1;
}
#endif
#endif
}

Expand Down
3 changes: 0 additions & 3 deletions SWIG/linearalgebra.i
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,6 @@ class Array {
"arrays are not resizable");
std::copy(rhs.begin(),rhs.end(),self->begin()+i);
}
bool __nonzero__() {
return (self->size() != 0);
}
bool __bool__() {
return (self->size() != 0);
}
Expand Down
16 changes: 12 additions & 4 deletions SWIG/money.i
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@

%include currencies.i

#if defined(SWIGJAVA)
%typemap(javainterfaces) Money QL_JAVA_INTERFACES "Comparable<Money>"
#endif

%{
using QuantLib::Money;
%}

class Money {
#if defined(SWIGJAVA)
%rename("compare") __cmp__;
#endif
public:
Money(const Currency& currency, Decimal value);
Money(Decimal value, const Currency& currency);
Expand Down Expand Up @@ -60,7 +61,7 @@ class Money {
bool __ge__(const Money& other) {
return !(*self < other);
}
#endif
#else
int __cmp__(const Money& other) {
if (*self < other)
return -1;
Expand All @@ -69,6 +70,13 @@ class Money {
else
return 1;
}
#endif
bool __eq__(const Money& other) {
return *self == other;
}
bool __ne__(const Money& other) {
return *self != other;
}
std::string __str__() {
std::ostringstream out;
out << *self;
Expand Down
3 changes: 3 additions & 0 deletions SWIG/ql.i
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ GNU autoconf configure script.
%rename(getValue) operator();
%rename(equals) __eq__;
%rename(unEquals) __ne__;
%rename(compareTo) __cmp__;
%javamethodmodifiers __cmp__ "@Override public"
%rename(hashCode) __hash__;
%rename(toString) __str__;
%rename(repr) __repr__;
#elif defined(SWIGCSHARP)
%rename(Add) operator+;
%rename(Add) __add__;
Expand Down
9 changes: 6 additions & 3 deletions SWIG/quantlib.i
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ const char* __version__;
%typemap(javaimports) SWIGTYPE %{
import java.lang.AutoCloseable;
%}
%typemap(javainterfaces) SWIGTYPE "AutoCloseable";
#define QL_JAVA_INTERFACES "AutoCloseable, "
%typemap(javainterfaces) SWIGTYPE "AutoCloseable"
%extend std::vector {
%typemap(javainterfaces) std::vector "AutoCloseable, java.util.RandomAccess";
%typemap(javainterfaces) std::vector QL_JAVA_INTERFACES "java.util.RandomAccess"
};
%extend std::vector<bool> {
%typemap(javainterfaces) std::vector "AutoCloseable, java.util.RandomAccess"
%typemap(javainterfaces) std::vector QL_JAVA_INTERFACES "java.util.RandomAccess"
}
%typemap(javacode) SWIGTYPE %{
@Override
public void close() {
this.delete();
}
%}
#else
#define QL_JAVA_INTERFACES
#endif

#if !defined(JAVA_FINALIZER)
Expand Down
3 changes: 0 additions & 3 deletions SWIG/timeseries.i
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class TimeSeries {
Size size();
#if defined(SWIGPYTHON)
%extend {
bool __nonzero__() {
return !self->empty();
}
bool __bool__() {
return !self->empty();
}
Expand Down

0 comments on commit 1add646

Please sign in to comment.