summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pyisda/curve.pxd13
-rw-r--r--pyisda/curve.pyx13
2 files changed, 16 insertions, 10 deletions
diff --git a/pyisda/curve.pxd b/pyisda/curve.pxd
index daec974..269199c 100644
--- a/pyisda/curve.pxd
+++ b/pyisda/curve.pxd
@@ -57,6 +57,19 @@ cdef inline void serialize(TCurve* curve, unsigned char* buf) nogil:
buf += sizeof(double)
memcpy(buf, &(curve.fDayCountConv), sizeof(long))
+cdef inline void serialize_vector(vector[TDate]& v, unsigned char* cursor) nogil:
+ cdef size_t size = v.size()
+ memcpy(cursor, &size, sizeof(size_t))
+ cursor += sizeof(size_t)
+ memcpy(cursor, v.data(), sizeof(TDate) * v.size())
+
+cdef inline unsigned char* serialize_string(string s, unsigned char* cursor) nogil:
+ cdef size_t size = s.length()
+ memcpy(cursor, &size, sizeof(size_t))
+ cursor += sizeof(size_t)
+ cursor += s.copy(<char*>cursor, s.length(), 0)
+ return cursor
+
cdef inline unsigned char* deserialize(unsigned char* buf, TCurve* curve) nogil:
memcpy(&curve.fNumItems, buf, sizeof(curve.fNumItems))
buf += sizeof(curve.fNumItems)
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index 438dc7f..5ee9048 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -300,10 +300,7 @@ cdef class YieldCurve(Curve):
unsigned char* buf = <unsigned char*>malloc(buf_size)
unsigned char* cursor = buf + size
serialize(curve, buf)
- size = self.dates.size()
- memcpy(cursor, &size, sizeof(size_t))
- cursor += sizeof(size_t)
- memcpy(cursor, self.dates.data(), sizeof(TDate) * size)
+ serialize_vector(self.dates, cursor)
return <bytes>buf[:buf_size]
def __setstate__(self, bytes state):
@@ -548,12 +545,8 @@ cdef class SpreadCurve(Curve):
unsigned char* buf = <unsigned char*>malloc(buf_size)
unsigned char* cursor = buf + size
serialize(curve, buf)
- size = self.ticker.length()
- memcpy(cursor, &size, sizeof(size_t))
- cursor += sizeof(size_t)
- cursor += self.ticker.copy(<char*>cursor, size, 0)
- size = self._thisptr.get().fNumItems * sizeof(double)
- memcpy(cursor, self.recovery_rates.get(), size)
+ cursor = serialize_string(self.ticker, cursor)
+ memcpy(cursor, self.recovery_rates.get(), sizeof(double) * curve.fNumItems)
return <bytes>buf[:buf_size]
@cython.initializedcheck(False)