Update: libcds WT, empty texts in bitv.
[SXSI/TextCollection.git] / BlockArray.h
index cc60c35..99aff91 100644 (file)
@@ -1,7 +1,8 @@
 #ifndef _BLOCK_ARRAY_H_
 #define _BLOCK_ARRAY_H_
-#include <iostream>
 #include "Tools.h"
+#include <iostream>
+#include <stdexcept>
 
 class BlockArray
 {
@@ -43,7 +44,44 @@ 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).");
     
+        for (ulong offset = 0; offset < n*blockLength/W+1; offset ++)
+        {
+            if (std::fwrite(this->data + offset, sizeof(ulong), 1, file) != 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];
+        for (ulong offset = 0; offset < n*blockLength/W+1; offset ++)
+        {
+            if (std::fread(this->data + offset, sizeof(ulong), 1, file) != 1)
+                throw std::runtime_error("BlockArray::Load(): file read error (data).");
+        }
+    }
 };
 
 #endif