From 3c425b3614e577edafca2b06375cee0acddcc61d Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Tue, 28 Nov 2017 15:44:56 -0500 Subject: put stuff in functions --- pyisda/curve.pxd | 13 +++++++++++++ pyisda/curve.pyx | 13 +++---------- 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(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 = 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 buf[:buf_size] def __setstate__(self, bytes state): @@ -548,12 +545,8 @@ cdef class SpreadCurve(Curve): unsigned char* buf = 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(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 buf[:buf_size] @cython.initializedcheck(False) -- cgit v1.2.3-70-g09d2