Index: /trunk/Mars/mcore/zfits.h
===================================================================
--- /trunk/Mars/mcore/zfits.h	(revision 16424)
+++ /trunk/Mars/mcore/zfits.h	(revision 16425)
@@ -146,5 +146,5 @@
                 //swap the bytes
                 int64_t tempValues[2] = {0,0};
-                revcpy<8>(reinterpret_cast<char*>(&tempValues[0]), readBuf, 2);
+                revcpy<8>(reinterpret_cast<char*>(tempValues), readBuf, 2);
 
                 //add catalog entry
@@ -157,11 +157,11 @@
 
     // Compressed versin of the read row
-    virtual bool ReadBinaryRow(uint64_t rowNum, char* bufferToRead)
+    bool ReadBinaryRow(const size_t &rowNum, char *bufferToRead)
     {
         if (rowNum >= GetNumRows())
             return false;
 
-        const int requestedTile = rowNum/fNumRowsPerTile;
-        const int currentTile   = fCurrentRow/fNumRowsPerTile;
+        const uint32_t requestedTile = rowNum/fNumRowsPerTile;
+        const uint32_t currentTile   = fCurrentRow/fNumRowsPerTile;
 
         fCurrentRow = rowNum;
@@ -182,33 +182,38 @@
             read(fCompressedBuffer.data(), sizeToRead);
 
+            const uint32_t thisRoundNumRows = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
+
             //uncompress it
-            UncompressBuffer();
-
-            //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++)
+            UncompressBuffer(currentCatRow, thisRoundNumRows);
+
+            // pointer to column (source buffer)
+            const char *src = fTransposedBuffer.data();
+
+            for (auto it=fTable.sortedCols.begin(); it!=fTable.sortedCold.end(); it++)
             {
-                switch (fTable.sortedCols[i].comp)
+                char *buffer = fBuffer.data() + col->offset; // pointer to column (destination buffer)
+
+                switch (it->comp)
                 {
                 case UNCOMPRESSED:
                 case SMOOTHMAN:
-                    //regular, "semi-transposed" copy
-                    for (uint32_t k=0;k<thisRoundNumRows;k++)
+                    // regular, "semi-transposed" copy
+                    for (char *dest=buffer; dest<buffer+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row
                     {
-                        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(dest, src, col->bytes);
+                        src += it->bytes;  // next column
                     }
                     break;
 
                 default:
-                    //transposed copy
-                    for (uint32_t j=0;j<fTable.sortedCols[i].num;j++)
-                        for (uint32_t k=0;k<thisRoundNumRows;k++)
+                    // transposed copy
+                    for (char *elem=buffer; elem<buffer+col->bytes; elem+=col->size) // element-by-element (arrays)
+                    {
+                        for (char *dest=elem; dest<elem+thisRoundNumRows*fTable.bytes_per_row; dest+=fTable.bytes_per_row) // row-by-row
                         {
-                            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;
+                            memcpy(dest, src, col->size);
+                            src += it->size; // next element
                         }
+                    }
                     break;
                 };
@@ -284,44 +289,41 @@
 
     // Data has been read from disk. Uncompress it !
-    void UncompressBuffer()
-    {
-        const uint32_t catalogCurrentRow = fCurrentRow/fNumRowsPerTile;
-        const uint32_t thisRoundNumRows  = (GetNumRows()<fCurrentRow + fNumRowsPerTile) ? GetNumRows()%fNumRowsPerTile : fNumRowsPerTile;
-
-        uint32_t offset = 0;
-        int64_t  compressedOffset  = 0;
- 
+    void UncompressBuffer(const uint32_t &catalogCurrentRow, const uint32_t &thisRoundNumRows)
+    {
+        char *dest = fTransposeBuffer.data();
+
         //uncompress column by column
-        for (uint32_t i=0;i<fTable.sortedCols.size();i++)
-        {
-            compressedOffset = fCatalog[catalogCurrentRow][i].second - fCatalog[catalogCurrentRow][0].second;
-
-            if (fTable.sortedCols[i].num == 0)
+        for (uint32_t i=0; i<fTable.sortedCols.size(); i++)
+        {
+            const Column &col = fTable.sortedCols[i];
+            if (col.num == 0)
                 continue;
 
             //get the compression flag
-            const char compressedFlag = fCompressedBuffer[compressedOffset++];
+            const int64_t compressedOffset = fCatalog[catalogCurrentRow][i].second - fCatalog[catalogCurrentRow][0].second;
+            const char    compressedFlag   = fCompressedBuffer[compressedOffset];
 
             //#define COMPRESSED_FLAG 0x1
             //#define UNCOMPRESSED_FLAG 0x0
 
+            const char *src = fCompressedBuffer.data()+compressedOffset+1;
+
             //if this bunch of data is not compressed, modify the compression flag
-            uint32_t compression = fTable.sortedCols[i].comp;
-            if (compressedFlag == 0)
-                compression = UNCOMPRESSED;
-
+            const uint32_t compression = compressedFlag==0 ? UNCOMPRESSED : col.comp;
             switch (compression)
             {
                 case UNCOMPRESSED:
-                    offset += UncompressUNCOMPRESSED(fTransposedBuffer.data()+offset, fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num);
+                    const uint32_t offset = UncompressUNCOMPRESSED(dest, src, thisRoundNumRows, col.size, col.num);
+                    dest += offset;
                     break;
 
                 case SMOOTHMAN:
-                    offset += UncompressSMOOTHMAN(reinterpret_cast<int16_t*>(fTransposedBuffer.data()+offset), fCompressedBuffer.data()+compressedOffset, thisRoundNumRows, fTable.sortedCols[i].size, fTable.sortedCols[i].num);
+                    const uint32_t offset = UncompressSMOOTHMAN(reinterpret_cast<int16_t*>(dest), src, thisRoundNumRows, col.size, col.num);
+                    dest += offset;
                     break;
 
                 default:
                     ;
-            };
+            }
         }
     }
