X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=BlockArray.h;h=1ee30c1c48825213b0b2b60f057c04f0a84642be;hb=a56dda3a95d2df268969bd3c2c994b5d2f020410;hp=cc60c3558dedb8b8333b47807e3a5f9f6111b1e5;hpb=33f321f97182b61a044e8508c2014ad7ce2bdec6;p=SXSI%2FTextCollection.git diff --git a/BlockArray.h b/BlockArray.h index cc60c35..1ee30c1 100644 --- a/BlockArray.h +++ b/BlockArray.h @@ -1,7 +1,8 @@ #ifndef _BLOCK_ARRAY_H_ #define _BLOCK_ARRAY_H_ -#include #include "Tools.h" +#include +#include class BlockArray { @@ -15,6 +16,8 @@ public: n = len; blockLength = blockLen; data = new ulong[n*blockLength/W +1]; + for (ulong i = 0; i < n*blockLength/W +1; ++i) + data[i] = 0; } ~BlockArray() { delete [] data; @@ -43,7 +46,38 @@ public: ulong spaceInBits() { return n*blockLength+W; // plus 4 ulong's } + + /** + * Saving data fields: + * ulong n; + * ulong blockLength; + * ulong* data; + */ + void Save(FILE *file) const + { + if (std::fwrite(&(this->n), sizeof(ulong), 1, file) != 1) + throw std::runtime_error("BlockArray::Save(): file write error (n)."); + if (std::fwrite(&(this->blockLength), sizeof(ulong), 1, file) != 1) + throw std::runtime_error("BlockArray::Save(): file write error (blockLength)."); + if (std::fwrite(this->data, sizeof(ulong), n*blockLength/W+1, file) != n*blockLength/W+1) + throw std::runtime_error("BlockArray::Save(): file write error (data)."); + } + + /** + * Load from file + */ + BlockArray(FILE *file) + { + if (std::fread(&(this->n), sizeof(ulong), 1, file) != 1) + throw std::runtime_error("BlockArray::Load(): file read error (n)."); + if (std::fread(&(this->blockLength), sizeof(ulong), 1, file) != 1) + throw std::runtime_error("BlockArray::Load(): file read error (blockLength)."); + + data = new ulong[n*blockLength/W+1]; + if (std::fread(this->data, sizeof(ulong), n*blockLength/W+1, file) != n*blockLength/W+1) + throw std::runtime_error("BlockArray::Load(): file read error (data)."); + } }; #endif