rogersce / cnpy

library to read/write .npy and .npz files in C/C++
MIT License
1.34k stars 304 forks source link

Would cnpy work with a vector of structs? #74

Open TheTomer opened 3 years ago

TheTomer commented 3 years ago

Hi, I'm not sure how cnpy is supposed to work in the following case: Let's say I have a vector of structs. The struct is made of an int field and a double field. The is supposed to be translated into a 2D matrix, with each column having a different variable type. How would you use cnpy to save that structure into a npy file?

In Python, with Numpy, it's possible to use a record array, as described in this thread.

eedalong commented 3 years ago

@TheTomer I am afraid cnpy can not work in this case, numpy use pickle to serialize array when your array is not a normal one

TheTomer commented 3 years ago

Thanks @eedalong . I ended up going with another approach, I've converted the structs vector into multiple vectors of the same type and saved them as an npz file. My only problem now is that I couldn't manage to save a vector of strings using cnpy.

eedalong commented 3 years ago

yeah,i guess that is because string'e length is variant, so if we want serialize a vector of strings, we need to serialize it as [length1][string1][length2][string2][length3][string3]