Index: trunk/Mars/mcore/zfits.h
===================================================================
--- trunk/Mars/mcore/zfits.h	(revision 16415)
+++ trunk/Mars/mcore/zfits.h	(revision 16416)
@@ -6,20 +6,16 @@
  */
 
-#ifndef ZFITS_H_
-#define ZFITS_H_
+#ifndef MARS_ZFITS
+#define MARS_ZFITS
+
+#include <stdexcept>
 
 #include "fits.h"
-
-#include <stdexcept>
 #include "huffman.h"
 
-#define COMPRESSED_FLAG 0x1
-#define UNCOMPRESSED_FLAG 0x0
 
 #ifndef __MARS__
 namespace std
 {
-#else
-using namespace std;
 #endif
 
@@ -28,69 +24,50 @@
 public:
 
-    /*
-     *   Basic constructor
-     */
-    zfits(const string& fname,
-          const string& tableName="",
-          bool          force=false,
-          bool          mightFail=false) : fits(fname, tableName, force, mightFail),
-                                           fBuffer(0),
-                                           fTransposedBuffer(0),
-                                           fCompressedBuffer(0),
-                                           fNumTiles(0),
-                                           fNumRowsPerTile(0),
-                                           fHeapPtr(0),
-                                           fCurrentRow(-1)
+    // Basic constructor
+    zfits(const string& fname, const string& tableName="",
+          bool force=false) : fits(fname, tableName, force),
+        fBuffer(0),
+        fTransposedBuffer(0),
+        fCompressedBuffer(0),
+        fNumTiles(0),
+        fNumRowsPerTile(0),
+        fCurrentRow(-1),
+        fHeapOff(0)
     {
         InitCompressionReading();
     }
 
-    /*
-     *   Alternative contstructor
-     */
-    zfits(const string& fname,
-          const string& fout,
-          const string& tableName="",
-          bool          force=false,
-          bool          mightFail=false): fits(fname, fout, tableName, force, mightFail),
-                                          fBuffer(0),
-                                          fTransposedBuffer(0),
-                                          fCompressedBuffer(0),
-                                          fNumTiles(0),
-                                          fNumRowsPerTile(0),
-                                          fHeapPtr(0),
-                                          fCurrentRow(-1)
+    // Alternative contstructor
+    zfits(const string& fname, const string& fout, const string& tableName,
+          bool force=false) : fits(fname, fout, tableName, force),
+              fBuffer(0),
+              fTransposedBuffer(0),
+              fCompressedBuffer(0),
+              fNumTiles(0),
+              fNumRowsPerTile(0),
+              fCurrentRow(-1),
+              fHeapOff(0)
     {
         InitCompressionReading();
     }
 
-    /*
-     *  Default destructor
-     */
-    ~zfits()
-    {}
-
-    /*
-     *  Skip the next row
-     */
-    virtual bool SkipNextRow()
-    {
-        if (fTable.isCompressed)
-        {
-            fRow++;
-            return true;
-        }
-        else
+    //  Skip the next row
+    bool SkipNextRow()
+    {
+        if (!fTable.isCompressed)
             return fits::SkipNextRow();
+
+        fRow++;
+        return true;
     }
 
 private:
-    /*
-     * Do what it takes to initialize the compressed structured
-     */
+
+    // Do what it takes to initialize the compressed structured
     void InitCompressionReading()
     {
         //The constructor may have failed
-        if (!good()) return;
+        if (!good())
+            return;
 
         //Get compressed specific keywords
@@ -104,33 +81,40 @@
         ReadCatalog();
     }
-    /*
-     *  Stage the requested row to internal buffer
-     *  Does NOT return data to users
-     */
-    virtual void StageRow(size_t row, char* dest)
-    {
-        if (fTable.isCompressed)
-            ReadBinaryRow(row, dest);
-        else
+
+    //  Stage the requested row to internal buffer
+    //  Does NOT return data to users
+    void StageRow(size_t row, char* dest)
+    {
+        if (!fTable.isCompressed)
+        {
             fits::StageRow(row, dest);
-    }
-    /*
-     *  Copy decompressed data to location requested by user
-     */
-    virtual void MoveColumnDataToUserSpace(char* dest, const char* src, const Table::Column& c)
-    {
-        if (fTable.isCompressed)
-            memcpy(dest, src, c.num*c.size);
-        else
+            return;
+        }
+
+        ReadBinaryRow(row, dest);
+    }
+
+    // Copy decompressed data to location requested by user
+    void MoveColumnDataToUserSpace(char* dest, const char* src, const Table::Column& c)
+    {
+        if (!fTable.isCompressed)
+        {
             fits::MoveColumnDataToUserSpace(dest, src, c);
-    }
+            return;
+        }
+
+        memcpy(dest, src, c.num*c.size);
+    }
+
     vector<char> fBuffer;           ///<store the uncompressed rows
     vector<char> fTransposedBuffer; ///<intermediate buffer to transpose the rows
     vector<char> fCompressedBuffer; ///<compressed rows
+
     size_t fNumTiles;       ///< Total number of tiles
     size_t fNumRowsPerTile; ///< Number of rows per compressed tile
-    size_t fHeapPtr;        ///< offset from the beginning of the file of the binary data
     size_t fCurrentRow;     ///< current row in memory.
 
+    streamoff fHeapOff;     ///< offset from the beginning of the file of the binary data
+
     vector<vector<pair<int64_t, int64_t> > > fCatalog;///< Catalog, i.e. the main table that points to the compressed data.
 
@@ -141,10 +125,10 @@
 
         fBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile);
+
         fTransposedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile);
         fCompressedBuffer.resize(fTable.bytes_per_row*fNumRowsPerTile + 1024*1024); //use a bit more memory, in case the compression algorithms uses more
     }
-    /*
-     *  Read catalog data. I.e. the address of the compressed data inside the heap
-     */
+
+    // Read catalog data. I.e. the address of the compressed data inside the heap
     void ReadCatalog()
     {
@@ -162,23 +146,23 @@
                 //swap the bytes
                 int64_t tempValues[2] = {0,0};
-                revcpy<8>((char*)(&tempValues[0]), readBuf, 2);
+                revcpy<8>(reinterpret_cast<char*>(tempValues[0]), readBuf, 2);
 
                 //add catalog entry
-                fCatalog[i].push_back(make_pair(tempValues[0], tempValues[1]));
+                fCatalog[i].emplace_back(tempValues[0], tempValues[1]);
             }
 
         //see if there is a gap before heap data
-        const off_t heapShift = ComputeGapFromDataToHeap(fTable);
-        fHeapPtr = tellg() + heapShift;
-    }
-    /*
-     *  Compressed versin of the read row
-     */
+        fHeapOff = fTable.GetHeapShift();
+    }
+
+    // Compressed versin of the read row
     virtual bool ReadBinaryRow(uint64_t rowNum, char* bufferToRead)
     {
-        if (rowNum >= GetNumRows()) return false;
+        if (rowNum >= GetNumRows())
+            return false;
 
         const int requestedTile = rowNum/fNumRowsPerTile;
         const int currentTile   = fCurrentRow/fNumRowsPerTile;
+
         fCurrentRow = rowNum;
 
@@ -188,12 +172,12 @@
             //read yet another chunk from the file
             //the size that we should read is in the catalog. we should sum up the sizes of all columns
+            const uint32_t currentCatRow = fCurrentRow/fNumRowsPerTile;
+
             int64_t sizeToRead  = 0;
-            const uint32_t currentCatRow = fCurrentRow/fNumRowsPerTile;
-
             for (uint32_t i=0;i<fCatalog[currentCatRow].size();i++)
                 sizeToRead += fCatalog[currentCatRow][i].first;
 
             //skip to the beginning of the tile
-            seekg(fHeapPtr+fCatalog[currentCatRow][0].second);
+            seekg(fCatalog[currentCatRow][0].second, ios_base::cur);
             read(fCompressedBuffer.data(), sizeToRead);
 
@@ -203,4 +187,5 @@
             //copy the tile and transpose it
             uint32_t offset=0;
+
             const uint32_t thisRoundNumRows = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
             for (uint32_t i=0;i<fTable.sortedCols.size();i++)
@@ -208,30 +193,33 @@
                 switch (fTable.sortedCols[i].comp)
                 {
-                    case UNCOMPRESSED:
-                    case SMOOTHMAN:
+                case UNCOMPRESSED:
+                case SMOOTHMAN:
+                    //regular, "semi-transposed" copy
+                    for (uint32_t k=0;k<thisRoundNumRows;k++)
+                    {
+                        memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size*fTable.sortedCols[i].num);
+                        offset += fTable.sortedCols[i].size*fTable.sortedCols[i].num;
+                    }
+                    break;
+
+                default:
+                    //transposed copy
+                    for (uint32_t j=0;j<fTable.sortedCols[i].num;j++)
                         for (uint32_t k=0;k<thisRoundNumRows;k++)
-                        {//regular, "semi-transposed" copy
-                            memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size*fTable.sortedCols[i].num);
-                            offset += fTable.sortedCols[i].size*fTable.sortedCols[i].num;
+                        {
+                            memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset + fTable.sortedCols[i].size*j, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size);
+                            offset += fTable.sortedCols[i].size;
                         }
-                    break;
-                    default :
-                        for (uint32_t j=0;j<fTable.sortedCols[i].num;j++)
-                            for (uint32_t k=0;k<thisRoundNumRows;k++)
-                            {//transposed copy
-                                memcpy(fBuffer.data()+k*fTable.bytes_per_row + fTable.sortedCols[i].offset + fTable.sortedCols[i].size*j, fTransposedBuffer.data()+offset, fTable.sortedCols[i].size);
-                                offset += fTable.sortedCols[i].size;
-                            }
                     break;
                 };
             }
         }
+
         //Data loaded and uncompressed. Copy it to destination
         memcpy(bufferToRead, fBuffer.data()+fTable.bytes_per_row*(fCurrentRow%fNumRowsPerTile), fTable.bytes_per_row);
         return good();
     }
-    /*
-     *  Read a bunch of uncompressed data
-     */
+
+    // Read a bunch of uncompressed data
     uint32_t UncompressUNCOMPRESSED(char*       dest,
                                     const char* src,
@@ -243,7 +231,6 @@
         return numRows*sizeOfElems*numRowElems;
     }
-    /*
-     *  Read a bunch of data compressed with the Huffman algorithm
-     */
+
+    // Read a bunch of data compressed with the Huffman algorithm
     uint32_t UncompressHUFFMAN(char*       dest,
                                const char* src,
@@ -257,4 +244,5 @@
             return -1;
         }
+
         vector<uint16_t> uncompressed;
 
@@ -268,5 +256,7 @@
         {
             Huffman::Decode(reinterpret_cast<const unsigned char*>(src), compressedSizes[j], uncompressed);
+
             memcpy(dest, uncompressed.data(), uncompressed.size()*sizeof(uint16_t));
+
             sizeWritten += uncompressed.size()*sizeof(uint16_t);
             dest        += uncompressed.size()*sizeof(uint16_t);
@@ -275,7 +265,6 @@
         return sizeWritten;
     }
-    /*
-     *  Read a bunch of data compressed with the smoothman algorithm
-     */
+
+    //Read a bunch of data compressed with the smoothman algorithm
     uint32_t UncompressSMOOTHMAN(int16_t*   dest,
                                  const char* src,
@@ -285,5 +274,5 @@
     {
         //call huffman transposed
-        uint32_t sizeWritten = UncompressHUFFMAN(reinterpret_cast<char*>(dest), src, numRowElems, sizeOfElems, numRows);
+        const uint32_t sizeWritten = UncompressHUFFMAN(reinterpret_cast<char*>(dest), src, numRowElems, sizeOfElems, numRows);
 
         //un-do the integer smoothing
@@ -293,27 +282,31 @@
         return sizeWritten;
     }
-    /*
-     *  Data has been read from disk. Uncompress it !
-     */
+
+    // Data has been read from disk. Uncompress it !
     void UncompressBuffer()
     {
-        uint32_t offset            = 0;
-        int64_t  compressedOffset  = 0;
         const uint32_t catalogCurrentRow = fCurrentRow/fNumRowsPerTile;
         const uint32_t thisRoundNumRows  = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
 
+        uint32_t offset = 0;
+        int64_t  compressedOffset  = 0;
+ 
         //uncompress column by column
         for (uint32_t i=0;i<fTable.sortedCols.size();i++)
         {
-            compressedOffset     = fCatalog[catalogCurrentRow][i].second - fCatalog[catalogCurrentRow][0].second;
-            uint32_t compression = fTable.sortedCols[i].comp;
-
-            if (fTable.sortedCols[i].num == 0) continue;
+            compressedOffset = fCatalog[catalogCurrentRow][i].second - fCatalog[catalogCurrentRow][0].second;
+
+            if (fTable.sortedCols[i].num == 0)
+                continue;
 
             //get the compression flag
             const char compressedFlag = fCompressedBuffer[compressedOffset++];
 
+            //#define COMPRESSED_FLAG 0x1
+            //#define UNCOMPRESSED_FLAG 0x0
+
             //if this bunch of data is not compressed, modify the compression flag
-            if (compressedFlag == UNCOMPRESSED_FLAG)
+            uint32_t compression = fTable.sortedCols[i].comp;
+            if (compressedFlag == 0)
                 compression = UNCOMPRESSED;
 
@@ -322,8 +315,10 @@
                 case UNCOMPRESSED:
                     offset += UncompressUNCOMPRESSED(fTransposedBuffer.data()+offset, fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num);
-                break;
+                    break;
+
                 case SMOOTHMAN:
                     offset += UncompressSMOOTHMAN(reinterpret_cast<int16_t*>(fTransposedBuffer.data()+offset), fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num);
-                break;
+                    break;
+
                 default:
                     ;
@@ -338,3 +333,3 @@
 #endif
 
-#endif /* ZFITS_H_ */
+#endif 
