Added FILE* functionality
[SXSI/TextCollection.git] / incbwt / bits / bitvector.cpp
index b76fa9d..b2ed324 100644 (file)
@@ -26,6 +26,25 @@ BitVector::BitVector(std::ifstream& file) :
   this->indexForSelect();
 }
 
+BitVector::BitVector(std::FILE * file) :
+  rank_index(0), select_index(0)
+{
+    std::fread(&(this->size), sizeof(this->size), 1, file);
+    std::fread(&(this->items), sizeof(this->items), 1, file);
+    std::fread(&(this->number_of_blocks), sizeof(this->number_of_blocks), 1, file);
+    std::fread(&(this->block_size), sizeof(this->block_size), 1, file);
+
+  this->array = new usint[this->block_size * this->number_of_blocks];
+  std::fread(this->array, sizeof(usint), this->block_size * this->number_of_blocks, file);
+  this->buffer = new FastBitBuffer(this->array, this->block_size);
+
+  this->integer_bits = length(this->size);
+  this->samples = new FastBitBuffer(file, 2 * (this->number_of_blocks + 1), this->integer_bits);
+
+  this->indexForRank();
+  this->indexForSelect();
+}
+
 BitVector::BitVector(VectorEncoder& encoder, usint universe_size) :
   size(universe_size), items(encoder.items),
   block_size(encoder.block_size),
@@ -87,6 +106,18 @@ BitVector::writeTo(std::ofstream& file)
   this->samples->writeBuffer(file, false);
 }
 
+void
+BitVector::writeTo(FILE* file)
+{
+    std::fwrite(&(this->size), sizeof(this->size), 1, file);
+    std::fwrite(&(this->items), sizeof(this->items), 1, file);
+    std::fwrite(&(this->number_of_blocks), sizeof(this->number_of_blocks), 1, file);
+    std::fwrite(&(this->block_size), sizeof(this->block_size), 1, file);
+    std::fwrite(this->array, sizeof(usint), this->block_size * this->number_of_blocks, file);
+    this->samples->writeBuffer(file, false);
+}
+
+
 //--------------------------------------------------------------------------
 
 usint