From 530cc17711624682689e4770b173c19ba60f2baa Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Wed, 28 Jul 2021 14:19:46 +0800 Subject: [PATCH] :sparkles: Support STRING type & vector> type --- cnpy.cpp | 11 +++++++-- cnpy.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/cnpy.cpp b/cnpy.cpp index 2d28578..6bf4e33 100644 --- a/cnpy.cpp +++ b/cnpy.cpp @@ -189,7 +189,6 @@ cnpy::NpyArray load_the_npy_file(FILE* fp) { } cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncompr_bytes) { - std::vector buffer_compr(compr_bytes); std::vector buffer_uncompr(uncompr_bytes); size_t nread = fread(&buffer_compr[0],1,compr_bytes,fp); @@ -220,9 +219,17 @@ cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncom cnpy::parse_npy_header(&buffer_uncompr[0],word_size,shape,fortran_order); cnpy::NpyArray array(shape, word_size, fortran_order); + + auto total_bytes = array.num_vals * word_size; + uint8_t ratio = (uncompr_bytes) / total_bytes; + array.set_ratio(ratio); + array.create_data_holder(); size_t offset = uncompr_bytes - array.num_bytes(); - memcpy(array.data(),&buffer_uncompr[0]+offset,array.num_bytes()); + + // cout << "Uncompr_bytes: " << uncompr_bytes << ", WordSize: " << word_size << ", ArrayValue: " << array.num_vals << + // ", Total Bytes: " << total_bytes << ", vecSize: " << array.num_bytes() << endl; + memcpy(array.data(),&buffer_uncompr[0] + offset, array.num_bytes()); return array; } diff --git a/cnpy.h b/cnpy.h index 0d3bb4c..3ad641b 100644 --- a/cnpy.h +++ b/cnpy.h @@ -18,7 +18,8 @@ #include #include #include - +#include +using namespace std; namespace cnpy { struct NpyArray { @@ -27,11 +28,9 @@ namespace cnpy { { num_vals = 1; for(size_t i = 0;i < shape.size();i++) num_vals *= shape[i]; - data_holder = std::shared_ptr>( - new std::vector(num_vals * word_size)); } - NpyArray() : shape(0), word_size(0), fortran_order(0), num_vals(0) { } + NpyArray() : shape(0), word_size(0), fortran_order(0), num_vals(0), char_ratio(1) {} template T* data() { @@ -49,17 +48,80 @@ namespace cnpy { return std::vector(p, p+num_vals); } + template + std::vector>> as_vec(const uint32_t& length) const { + + const char* p = data(); + + std::vector>> vecData; + const uint32_t dataLength = num_vals / length; + for(size_t j = 0; j < num_vals / dataLength; ++j) { + vector> pricingQty; + for(size_t i = 0; i < dataLength; i += 2) { + + auto cValue1 = p + (j*word_size*dataLength*char_ratio + i*word_size*char_ratio); + auto cValue2 = p + (j*word_size*dataLength*char_ratio + (i+1)*word_size*char_ratio); + auto value1 = *reinterpret_cast(const_cast(cValue1)); + auto value2 = *reinterpret_cast(const_cast(cValue2)); + + if (isnan(value1)) { + value1 = -1; + } + if (isnan(value2)) { + value2 = -1; + } + pricingQty.emplace_back(make_pair(value1, value2)); + } + vecData.emplace_back(pricingQty); + } + return vecData; + } + size_t num_bytes() const { return data_holder->size(); } + void set_ratio(uint8_t ratio) { + char_ratio = ratio; + } + + void create_data_holder() { + data_holder = std::shared_ptr>( + new std::vector(num_vals * word_size * char_ratio)); + } + std::shared_ptr> data_holder; std::vector shape; size_t word_size; bool fortran_order; size_t num_vals; + uint8_t char_ratio = 1; }; - + + template <> + inline std::vector NpyArray::as_vec() const { + const char* p = data(); + std::vector vecData; + for(size_t j = 0; j < num_vals; ++j) { + string value = ""; + for(size_t i = 0; i < word_size; ++i) { + auto tempValue = p + (j*word_size*char_ratio + i*char_ratio); + if (!tempValue) { + continue; + } + value += tempValue; + } + vecData.emplace_back(value); + } + return vecData; + } + + template<> + inline std::vector>> NpyArray::as_vec(const uint32_t& length) const { + cout << "Unsupported type!" << endl; + exit(1); + } + using npz_t = std::map; char BigEndianTest();