Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support STRING type & vector<pair<T,T>> type #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char> buffer_compr(compr_bytes);
std::vector<unsigned char> buffer_uncompr(uncompr_bytes);
size_t nread = fread(&buffer_compr[0],1,compr_bytes,fp);
Expand Down Expand Up @@ -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<unsigned char>(),&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<unsigned char>(),&buffer_uncompr[0] + offset, array.num_bytes());

return array;
}
Expand Down
72 changes: 67 additions & 5 deletions cnpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include<memory>
#include<stdint.h>
#include<numeric>

#include<math.h>
using namespace std;
namespace cnpy {

struct NpyArray {
Expand All @@ -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<std::vector<char>>(
new std::vector<char>(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<typename T>
T* data() {
Expand All @@ -49,17 +48,80 @@ namespace cnpy {
return std::vector<T>(p, p+num_vals);
}

template<typename T>
std::vector<vector<pair<T, T>>> as_vec(const uint32_t& length) const {

const char* p = data<char>();

std::vector<vector<pair<T, T>>> vecData;
const uint32_t dataLength = num_vals / length;
for(size_t j = 0; j < num_vals / dataLength; ++j) {
vector<pair<T, T>> 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<T*>(const_cast<char*>(cValue1));
auto value2 = *reinterpret_cast<T*>(const_cast<char*>(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<std::vector<char>>(
new std::vector<char>(num_vals * word_size * char_ratio));
}

std::shared_ptr<std::vector<char>> data_holder;
std::vector<size_t> shape;
size_t word_size;
bool fortran_order;
size_t num_vals;
uint8_t char_ratio = 1;
};


template <>
inline std::vector<string> NpyArray::as_vec() const {
const char* p = data<char>();
std::vector<string> 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<vector<pair<string, string>>> NpyArray::as_vec(const uint32_t& length) const {
cout << "Unsupported type!" << endl;
exit(1);
}

using npz_t = std::map<std::string, NpyArray>;

char BigEndianTest();
Expand Down