Fixed Save()
authornvalimak <nvalimak@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Mon, 9 Feb 2009 11:00:19 +0000 (11:00 +0000)
committernvalimak <nvalimak@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Mon, 9 Feb 2009 11:00:19 +0000 (11:00 +0000)
git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/TextCollection@154 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

BSGAP.cpp

index b2db287..9a25b75 100644 (file)
--- a/BSGAP.cpp
+++ b/BSGAP.cpp
@@ -29,15 +29,9 @@ const uchar BSGAP::bit_table[] = {
  * samplerate == number of keys in each gap encoded binary search tree 
  * default value is log^2 n where n is the number of keys.
  */
-BSGAP::BSGAP(ulong *B, ulong u, bool freeB, ulong sampleRate)
+BSGAP::BSGAP(ulong *B, ulong u_, bool freeB, ulong sampleRate)
+    : u(u_), n(0), topCount(0), bitsInP(0), P(0), b(0), offsetA(0), firstKeyA(0)
 {
-    this->u = u;
-    this->n = 0;
-    this->P = 0;
-    this->offsetA = 0;
-    this->firstKeyA = 0;
-    this->topCount = 0; 
-
     // Count keys
     for (ulong i = 0; i < u; i ++)
         if (Tools::GetField(B, 1, i))
@@ -45,7 +39,7 @@ BSGAP::BSGAP(ulong *B, ulong u, bool freeB, ulong sampleRate)
 
     if (n == 0) // Sanity check
     {
-        if (freeB && B)
+        if (freeB)
             delete [] B;
 
         return;
@@ -89,7 +83,7 @@ BSGAP::BSGAP(ulong *B, ulong u, bool freeB, ulong sampleRate)
         if (i == n/b - 1 && n%b == 0)
             lastKey = u;
         
-        tempP[i] = GetSubtree(B, firstKey, firstKey, lastKey, b, true, bitsInBSD[i]); // FIXME: left of right subtree?
+        tempP[i] = GetSubtree(B, firstKey, firstKey, lastKey, b, true, bitsInBSD[i]);
         totalBits += bitsInBSD[i];
         (*firstKeyA)[i] = firstKey;
 
@@ -102,7 +96,7 @@ BSGAP::BSGAP(ulong *B, ulong u, bool freeB, ulong sampleRate)
         while (!Tools::GetField(B, 1, firstKey))
             firstKey ++;
         
-        tempP[n/b] = GetSubtree(B, firstKey, firstKey, u, n - (n/b) * b, true, bitsInBSD[n/b]); // FIXME: left of right subtree?
+        tempP[n/b] = GetSubtree(B, firstKey, firstKey, u, n - (n/b) * b, true, bitsInBSD[n/b]);
         totalBits += bitsInBSD[n/b];
         (*firstKeyA)[n/b] = firstKey;
     }
@@ -727,6 +721,9 @@ void BSGAP::Save(FILE *file) const
     if (std::fwrite(&(this->n), sizeof(ulong), 1, file) != 1)
         throw std::runtime_error("BSGAP::Save(): file write error (n).");
 
+    if (n == 0)
+        return; // Done.
+
     if (std::fwrite(&(this->topCount), sizeof(ulong), 1, file) != 1)
         throw std::runtime_error("BSGAP::Save(): file write error (topCount).");
 
@@ -750,6 +747,7 @@ void BSGAP::Save(FILE *file) const
  * Load from file
  */
 BSGAP::BSGAP(FILE *file)
+    : u(0), n(0), topCount(0), bitsInP(0), P(0), b(0), offsetA(0), firstKeyA(0)
 {
     if (std::fread(&(this->u), sizeof(ulong), 1, file) != 1)
         throw std::runtime_error("BSGAP::Load(): file read error (u).");
@@ -757,6 +755,9 @@ BSGAP::BSGAP(FILE *file)
     if (std::fread(&(this->n), sizeof(ulong), 1, file) != 1)
         throw std::runtime_error("BSGAP::Load(): file read error (n).");
 
+    if (n == 0)
+        return; // Done.
+
     if (std::fread(&(this->topCount), sizeof(ulong), 1, file) != 1)
         throw std::runtime_error("BSGAP::Load(): file read error (topCount).");