diff options
Diffstat (limited to 'gruel/src/lib')
-rw-r--r-- | gruel/src/lib/pmt/pmt.cc | 42 | ||||
-rw-r--r-- | gruel/src/lib/pmt/pmt_serialize.cc | 344 | ||||
-rw-r--r-- | gruel/src/lib/pmt/qa_pmt_prims.cc | 21 | ||||
-rw-r--r-- | gruel/src/lib/pmt/unv_template.cc.t | 19 |
4 files changed, 416 insertions, 10 deletions
diff --git a/gruel/src/lib/pmt/pmt.cc b/gruel/src/lib/pmt/pmt.cc index 1d9125d4e..1d1e9ba7c 100644 --- a/gruel/src/lib/pmt/pmt.cc +++ b/gruel/src/lib/pmt/pmt.cc @@ -388,9 +388,19 @@ pmt_is_complex(pmt_t x) pmt_t pmt_make_rectangular(double re, double im) { + return pmt_from_complex(re, im); +} + +pmt_t pmt_from_complex(double re, double im) +{ return pmt_t(new pmt_complex(std::complex<double>(re, im))); } +pmt_t pmt_from_complex(const std::complex<double> &z) +{ + return pmt_t(new pmt_complex(z)); +} + std::complex<double> pmt_to_complex(pmt_t x) { @@ -1325,6 +1335,38 @@ pmt_list_add(pmt_t list, const pmt_t& item) } pmt_t +pmt_list_rm(pmt_t list, const pmt_t& item) +{ + if(pmt_is_pair(list)){ + pmt_t left = pmt_car(list); + pmt_t right = pmt_cdr(list); + if(!pmt_equal(left, item)){ + return pmt_cons(left, pmt_list_rm(right, item)); + } else { + return pmt_list_rm(right, item); + } + } else { + return list; + } +} + +bool +pmt_list_has(pmt_t list, const pmt_t& item) +{ + if(pmt_is_pair(list)){ + pmt_t left = pmt_car(list); + pmt_t right = pmt_cdr(list); + if(pmt_equal(left,item)) + return true; + return pmt_list_has(right, item); + } else { + if(pmt_is_null(list)) + return false; + throw std::runtime_error("list contains invalid format!"); + } +} + +pmt_t pmt_caar(pmt_t pair) { return (pmt_car(pmt_car(pair))); diff --git a/gruel/src/lib/pmt/pmt_serialize.cc b/gruel/src/lib/pmt/pmt_serialize.cc index a19809a66..24be6b772 100644 --- a/gruel/src/lib/pmt/pmt_serialize.cc +++ b/gruel/src/lib/pmt/pmt_serialize.cc @@ -309,11 +309,192 @@ pmt_serialize(pmt_t obj, std::streambuf &sb) } - if (pmt_is_vector(obj)) - throw pmt_notimplemented("pmt_serialize (vector)", obj); + if (pmt_is_vector(obj)) { + size_t vec_len = pmt::pmt_length(obj); + ok = serialize_untagged_u8(PST_VECTOR, sb); + ok &= serialize_untagged_u32(vec_len, sb); + for(size_t i=0; i<vec_len; i++) { + ok &= pmt_serialize(pmt_vector_ref(obj, i), sb); + } + return ok; + } + + if (pmt_is_uniform_vector(obj)) { + size_t npad = 1; + size_t vec_len = pmt::pmt_length(obj); + + if(pmt_is_u8vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_U8, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u8(pmt_u8vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_s8vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_S8, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u8(pmt_s8vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_u16vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_U16, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u16(pmt_u16vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_s16vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_S16, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u16(pmt_s16vector_ref(obj, i), sb); + } + return ok; + } - if (pmt_is_uniform_vector(obj)) - throw pmt_notimplemented("pmt_serialize (uniform-vector)", obj); + if(pmt_is_u32vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_U32, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u32(pmt_u32vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_s32vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_S32, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u32(pmt_s32vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_u64vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_U64, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u64(pmt_u64vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_s64vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_S64, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_u64(pmt_s64vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_f32vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_F32, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_f64(pmt_f32vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_f64vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_F64, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + ok &= serialize_untagged_f64(pmt_f64vector_ref(obj, i), sb); + } + return ok; + } + + if(pmt_is_c32vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_C32, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + std::complex<float> c = pmt_c32vector_ref(obj, i); + ok &= serialize_untagged_f64(c.real(), sb); + ok &= serialize_untagged_f64(c.imag(), sb); + } + return ok; + } + + if(pmt_is_c64vector(obj)) { + ok = serialize_untagged_u8(PST_UNIFORM_VECTOR, sb); + ok &= serialize_untagged_u8(UVI_C64, sb); + ok &= serialize_untagged_u32(vec_len, sb); + ok &= serialize_untagged_u8(npad, sb); + for(size_t i=0; i<npad; i++) { + ok &= serialize_untagged_u8(0, sb); + } + for(size_t i=0; i<vec_len; i++) { + std::complex<double> c = pmt_c64vector_ref(obj, i); + ok &= serialize_untagged_f64(c.real(), sb); + ok &= serialize_untagged_f64(c.imag(), sb); + } + return ok; + } + } if (pmt_is_dict(obj)) throw pmt_notimplemented("pmt_serialize (dict)", obj); @@ -342,7 +523,7 @@ pmt_t pmt_deserialize(std::streambuf &sb) { uint8_t tag; - //uint8_t u8; + uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; @@ -408,8 +589,159 @@ pmt_deserialize(std::streambuf &sb) } case PST_VECTOR: - case PST_DICT: + { + uint32_t nitems; + if(!deserialize_untagged_u32(&nitems, sb)) + goto error; + pmt_t vec = pmt_make_vector(nitems, PMT_NIL); + for(uint32_t i=0; i<nitems; i++) { + pmt_t item = pmt_deserialize(sb); + pmt_vector_set(vec, i, item); + } + return vec; + } + case PST_UNIFORM_VECTOR: + { + uint8_t utag, npad; + uint32_t nitems; + + if(!deserialize_untagged_u8(&utag, sb)) + return PMT_EOF; + + if(!deserialize_untagged_u32(&nitems, sb)) + goto error; + + deserialize_untagged_u8(&npad, sb); + for(size_t i; i < npad; i++) + deserialize_untagged_u8(&u8, sb); + + switch(utag) { + case(UVI_U8): + { + pmt_t vec = pmt_make_u8vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u8(&u8, sb); + pmt_u8vector_set(vec, i, u8); + } + return vec; + } + case(UVI_S8): + { + pmt_t vec = pmt_make_s8vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u8(&u8, sb); + pmt_s8vector_set(vec, i, u8); + } + return vec; + } + case(UVI_U16): + { + pmt_t vec = pmt_make_u16vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u16(&u16, sb); + pmt_u16vector_set(vec, i, u16); + } + return vec; + } + case(UVI_S16): + { + pmt_t vec = pmt_make_s16vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u16(&u16, sb); + pmt_s16vector_set(vec, i, u16); + } + return vec; + } + case(UVI_U32): + { + pmt_t vec = pmt_make_u32vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u32(&u32, sb); + pmt_u32vector_set(vec, i, u32); + } + return vec; + } + case(UVI_S32): + { + pmt_t vec = pmt_make_s32vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u32(&u32, sb); + pmt_s32vector_set(vec, i, u32); + } + return vec; + } + case(UVI_U64): + { + pmt_t vec = pmt_make_u64vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u64(&u64, sb); + pmt_u64vector_set(vec, i, u64); + } + return vec; + } + case(UVI_S64): + { + pmt_t vec = pmt_make_s64vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_u64(&u64, sb); + pmt_s64vector_set(vec, i, u64); + } + return vec; + } + case(UVI_F32): + { + pmt_t vec = pmt_make_f32vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_f64(&f64, sb); + pmt_f32vector_set(vec, i, static_cast<float>(f64)); + } + return vec; + } + case(UVI_F64): + { + pmt_t vec = pmt_make_f64vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + deserialize_untagged_f64(&f64, sb); + pmt_f64vector_set(vec, i, f64); + } + return vec; + } + case(UVI_C32): + { + pmt_t vec = pmt_make_c32vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + float re, im; + deserialize_untagged_f64(&f64, sb); + re = static_cast<float>(f64); + deserialize_untagged_f64(&f64, sb); + im = static_cast<float>(f64); + pmt_c32vector_set(vec, i, std::complex<float>(re, im)); + } + return vec; + } + + case(UVI_C64): + { + pmt_t vec = pmt_make_c64vector(nitems, 0); + for(uint32_t i=0; i<nitems; i++) { + double re, im; + deserialize_untagged_f64(&f64, sb); + re = f64; + deserialize_untagged_f64(&f64, sb); + im = f64; + pmt_c64vector_set(vec, i, std::complex<double>(re, im)); + } + return vec; + } + + default: + throw pmt_exception("pmt_deserialize: malformed input stream, tag value = ", + pmt_from_long(tag)); + } + } + + case PST_DICT: case PST_COMMENT: throw pmt_notimplemented("pmt_deserialize: tag value = ", pmt_from_long(tag)); diff --git a/gruel/src/lib/pmt/qa_pmt_prims.cc b/gruel/src/lib/pmt/qa_pmt_prims.cc index 6212b8ea4..3ae4d70b6 100644 --- a/gruel/src/lib/pmt/qa_pmt_prims.cc +++ b/gruel/src/lib/pmt/qa_pmt_prims.cc @@ -134,12 +134,24 @@ qa_pmt_prims::test_complexes() { pmt_t p1 = pmt_make_rectangular(2, -3); pmt_t m1 = pmt_make_rectangular(-3, 2); + pmt_t p2 = pmt_from_complex(2, -3); + pmt_t m2 = pmt_from_complex(-3, 2); + pmt_t p3 = pmt_from_complex(std::complex<double>(2, -3)); + pmt_t m3 = pmt_from_complex(std::complex<double>(-3, 2)); CPPUNIT_ASSERT(!pmt_is_complex(PMT_T)); CPPUNIT_ASSERT(pmt_is_complex(p1)); CPPUNIT_ASSERT(pmt_is_complex(m1)); + CPPUNIT_ASSERT(pmt_is_complex(p2)); + CPPUNIT_ASSERT(pmt_is_complex(m2)); + CPPUNIT_ASSERT(pmt_is_complex(p3)); + CPPUNIT_ASSERT(pmt_is_complex(m3)); CPPUNIT_ASSERT_THROW(pmt_to_complex(PMT_T), pmt_wrong_type); CPPUNIT_ASSERT_EQUAL(std::complex<double>(2, -3), pmt_to_complex(p1)); CPPUNIT_ASSERT_EQUAL(std::complex<double>(-3, 2), pmt_to_complex(m1)); + CPPUNIT_ASSERT_EQUAL(std::complex<double>(2, -3), pmt_to_complex(p2)); + CPPUNIT_ASSERT_EQUAL(std::complex<double>(-3, 2), pmt_to_complex(m2)); + CPPUNIT_ASSERT_EQUAL(std::complex<double>(2, -3), pmt_to_complex(p3)); + CPPUNIT_ASSERT_EQUAL(std::complex<double>(-3, 2), pmt_to_complex(m3)); CPPUNIT_ASSERT_EQUAL(std::complex<double>(1.0, 0), pmt_to_complex(pmt_from_long(1))); CPPUNIT_ASSERT_EQUAL(std::complex<double>(1.0, 0), pmt_to_complex(pmt_from_double(1.0))); } @@ -472,7 +484,7 @@ class qa_pmt_msg_accepter_nop : public gruel::msg_accepter { public: qa_pmt_msg_accepter_nop(){}; ~qa_pmt_msg_accepter_nop(); - void post(pmt_t){}; + void post(pmt_t,pmt_t){}; }; qa_pmt_msg_accepter_nop::~qa_pmt_msg_accepter_nop(){} @@ -495,9 +507,10 @@ qa_pmt_prims::test_msg_accepter() CPPUNIT_ASSERT_THROW(pmt_msg_accepter_ref(p0), pmt_wrong_type); // just confirm interfaces on send are OK - gruel::send(ma0.get(), sym); - gruel::send(ma0, sym); - gruel::send(p1, sym); + pmt_t port(pmt_intern("port")); + gruel::send(ma0.get(), port, sym); + gruel::send(ma0, port, sym); + gruel::send(p1, port, sym); } diff --git a/gruel/src/lib/pmt/unv_template.cc.t b/gruel/src/lib/pmt/unv_template.cc.t index 566168c3d..1ed505e29 100644 --- a/gruel/src/lib/pmt/unv_template.cc.t +++ b/gruel/src/lib/pmt/unv_template.cc.t @@ -87,6 +87,13 @@ pmt_init_@TAG@vector(size_t k, const @TYPE@ *data) return pmt_t(new pmt_@TAG@vector(k, data)); } +pmt_t +pmt_init_@TAG@vector(size_t k, const std::vector< @TYPE@ > &data) +{ + + return pmt_t(new pmt_@TAG@vector(k, &data[0])); +} + @TYPE@ pmt_@TAG@vector_ref(pmt_t vector, size_t k) { @@ -111,6 +118,18 @@ pmt_@TAG@vector_elements(pmt_t vector, size_t &len) return _@TAG@vector(vector)->elements(len); } +const std::vector< @TYPE@ > +pmt_@TAG@vector_elements(pmt_t vector) +{ + if (!vector->is_@TAG@vector()) + throw pmt_wrong_type("pmt_@TAG@vector_elements", vector); + size_t len; + const @TYPE@ *array = _@TAG@vector(vector)->elements(len); + const std::vector< @TYPE@ > vec(array, array+len); + return vec; +} + + @TYPE@ * pmt_@TAG@vector_writable_elements(pmt_t vector, size_t &len) { |