diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2017-11-28 15:44:56 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2017-11-29 13:57:27 -0500 |
| commit | 3c425b3614e577edafca2b06375cee0acddcc61d (patch) | |
| tree | 5427aac9a6a6ecb3fe68d07286c5da05a46c18f2 | |
| parent | f40a29328c1e21d45c4c9d2729ce88c768008af4 (diff) | |
| download | pyisda-3c425b3614e577edafca2b06375cee0acddcc61d.tar.gz | |
put stuff in functions
| -rw-r--r-- | pyisda/curve.pxd | 13 | ||||
| -rw-r--r-- | 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(<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) |
