[Kst] branches/work/kst/portto4/kst/src/datasources/ascii

Peter Kümmel syntheticpp at gmx.net
Sat Oct 13 17:41:16 UTC 2012


SVN commit 1320355 by kuemmel:

split out AsciiDataReader step by step

make _rowIndex a AsciiDataReader member

 M  +14 -14    asciidatareader.cpp  
 M  +25 -20    asciidatareader.h  
 M  +8 -9      asciisource.cpp  
 M  +1 -2      asciisource.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.cpp #1320354:1320355
@@ -143,7 +143,7 @@
 }
 
 
-int AsciiDataReader::readField(const RowIndex& _rowIndex, FileBuffer* _fileBuffer, int col, int bufstart, int bufread,
+int AsciiDataReader::readField(FileBuffer* _fileBuffer, int col, int bufstart, int bufread,
                                double *v, const QString& field, int s, int n, bool& re_alloc) 
   {
 
@@ -162,23 +162,23 @@
     if (_config._columnDelimiter.value().size() == 1) {
       MeasureTime t("AsciiSource::readField: 1 custom column delimiter");
       const AsciiDataReader::IsCharacter column_del(_config._columnDelimiter.value()[0].toLatin1());
-      return readColumns(_rowIndex, v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
+      return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
     } if (_config._columnDelimiter.value().size() > 1) {
       MeasureTime t(QString("AsciiSource::readField: %1 custom column delimiters").arg(_config._columnDelimiter.value().size()));
       const AsciiDataReader::IsInString column_del(_config._columnDelimiter.value());
-      return readColumns(_rowIndex, v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
+      return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
     }
   } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
     MeasureTime t("AsciiSource::readField: whitespace separated columns");
     const AsciiDataReader::IsWhiteSpace column_del;
-    return readColumns(_rowIndex, v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
+    return readColumns(v, _fileBuffer->constData(), bufstart, bufread, col, s, n, _lineending, column_del);
   }
 
   return 0;
 }
 
 
-bool AsciiDataReader::findDataRows(RowIndex& rowIndex, int& numFrames, bool read_completely, QFile& file, int _byteLength)
+bool AsciiDataReader::findDataRows(int& numFrames, bool read_completely, QFile& file, int _byteLength)
 {
   AsciiDataReader::LineEndingType lineending = detectLineEndingType(file);
 
@@ -191,40 +191,40 @@
     buf.resize(buf.capacity());
 
     //bufstart += bufread;
-    buf._bufferedS = rowIndex[numFrames]; // always read from the start of a line
+    buf._bufferedS = _rowIndex[numFrames]; // always read from the start of a line
     buf._bufferedN = readFromFile(file, buf, buf._bufferedS, _byteLength - buf._bufferedS, AsciiDataReader::FileBuffer::Prealloc - 1);
 
     if (_config._delimiters.value().size() == 0) {
       const AsciiDataReader::NoDelimiter comment_del;
       if (lineending.isLF()) {
-        new_data = findDataRows(rowIndex, numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
       } else {
-        new_data = findDataRows(rowIndex, numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
       }
     } else if (_config._delimiters.value().size() == 1) {
       const AsciiDataReader::IsCharacter comment_del(_config._delimiters.value()[0].toLatin1());
       if (lineending.isLF()) {
-        new_data = findDataRows(rowIndex, numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
       } else {
-        new_data = findDataRows(rowIndex, numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
       }
     } else if (_config._delimiters.value().size() > 1) {
       const AsciiDataReader::IsInString comment_del(_config._delimiters.value());
       if (lineending.isLF()) {
-        new_data = findDataRows(rowIndex, numFrames, buf.constData(), buf._bufferedS, buf._bufferedS, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedS, AsciiDataReader::IsLineBreakLF(lineending), comment_del);
       } else {
-        new_data = findDataRows(rowIndex, numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
+        new_data = findDataRows(numFrames, buf.constData(), buf._bufferedS, buf._bufferedN, AsciiDataReader::IsLineBreakCR(lineending), comment_del);
       }
     }
   } while (buf._bufferedN == AsciiDataReader::FileBuffer::Prealloc - 1  && read_completely);
 
-  rowIndex.resize(numFrames + 1);
+  _rowIndex.resize(numFrames + 1);
 
   return new_data;
 }
 
 template<class Buffer, typename IsLineBreak, typename CommentDelimiter>
-bool AsciiDataReader::findDataRows(RowIndex& _rowIndex, int& _numFrames, const Buffer& buffer, int bufstart, int bufread, const IsLineBreak& isLineBreak, const CommentDelimiter& comment_del)
+bool AsciiDataReader::findDataRows(int& _numFrames, const Buffer& buffer, int bufstart, int bufread, const IsLineBreak& isLineBreak, const CommentDelimiter& comment_del)
 {
   const AsciiDataReader::IsWhiteSpace isWhiteSpace;
 
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.h #1320354:1320355
@@ -56,6 +56,7 @@
       };
 
       typedef QVarLengthArray<char, Prealloc> Array;
+      typedef QVarLengthArray<int, Prealloc> RowIndex;
 
       inline FileBuffer() : _bufferedS(-10), _bufferedN(-10), _array(new Array) {}
       inline ~FileBuffer() { delete _array; }
@@ -74,12 +75,16 @@
 
       void clearFileBuffer(bool forceDelete = false);
 
+
     private:
+
       Array* _array;
     };
 
-    typedef QVarLengthArray<int, AsciiDataReader::FileBuffer::Prealloc> RowIndex;
+    FileBuffer::RowIndex _rowIndex;
 
+    inline FileBuffer::RowIndex& rowIndex() { return _rowIndex; }
+
     void clearFileBuffer(bool forceDelete = false);
 
     template<class T>
@@ -88,7 +93,7 @@
     
     int readFromFile(QFile&, AsciiDataReader::FileBuffer&, int start, int numberOfBytes, int maximalBytes = -1); 
     
-    int readField(const RowIndex& _rowIndex, FileBuffer* _fileBuffer, int col, int bufstart, int bufread,
+    int readField(FileBuffer* _fileBuffer, int col, int bufstart, int bufread,
                   double *v, const QString& field, int s, int n, bool& re_alloc);
 
 
@@ -196,21 +201,21 @@
 
 
     template<class Buffer, typename ColumnDelimiter>
-    int readColumns(const RowIndex& rowIndex, double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
+    int readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
                     const LineEndingType&, const ColumnDelimiter&);
 
     template<class Buffer, typename ColumnDelimiter, typename CommentDelimiter>
-    int readColumns(const RowIndex& rowIndex, double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
+    int readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
                     const LineEndingType&, const ColumnDelimiter&, const CommentDelimiter&);
 
     template<class Buffer, typename IsLineBreak, typename ColumnDelimiter, typename CommentDelimiter, typename ColumnWidthsAreConst>
-    int readColumns(const RowIndex& rowIndex, double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
+    int readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
                     const IsLineBreak&, const ColumnDelimiter&, const CommentDelimiter&, const ColumnWidthsAreConst&);
 
-    bool findDataRows(RowIndex &rowIndex, int& _numFrames, bool read_completely, QFile& file, int _byteLength);
+    bool findDataRows(int& _numFrames, bool read_completely, QFile& file, int _byteLength);
 
     template<class Buffer, typename IsLineBreak, typename CommentDelimiter>
-    bool findDataRows(RowIndex &rowIndex, int& _numFrames, const Buffer& buffer, int bufstart, int bufread, const IsLineBreak&, const CommentDelimiter&);
+    bool findDataRows(int& _numFrames, const Buffer& buffer, int bufstart, int bufread, const IsLineBreak&, const CommentDelimiter&);
 
     void toDouble(const LexicalCast& lexc, const char* buffer, int bufread, int ch, double* v, int row);
 
@@ -222,47 +227,47 @@
 
 //-------------------------------------------------------------------------------------------
 template<class Buffer, typename ColumnDelimiter>
-int AsciiDataReader::readColumns(const RowIndex& rowIndex, double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
+int AsciiDataReader::readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
                               const LineEndingType& lineending, const ColumnDelimiter& column_del)
 {
   if (_config._delimiters.value().size() == 0) {
     const NoDelimiter comment_del;
-    return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
+    return readColumns(v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
   } else if (_config._delimiters.value().size() == 1) {
     const IsCharacter comment_del(_config._delimiters.value()[0].toLatin1());
-    return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
+    return readColumns(v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
   } else if (_config._delimiters.value().size() > 1) {
     const IsInString comment_del(_config._delimiters.value());
-    return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
+    return readColumns(v, buffer, bufstart, bufread, col, s, n, lineending, column_del, comment_del);
   }
 
   return 0;
 }
 
 template<class Buffer, typename ColumnDelimiter, typename CommentDelimiter>
-int AsciiDataReader::readColumns(const RowIndex& rowIndex, double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
+int AsciiDataReader::readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
                               const LineEndingType& lineending, const ColumnDelimiter& column_del, const CommentDelimiter& comment_del)
 {
   if (_config._columnWidthIsConst) {
     const AlwaysTrue column_withs_const;
     if (lineending.isLF()) {
-      return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, IsLineBreakLF(lineending), column_del, comment_del, column_withs_const);
+      return readColumns(v, buffer, bufstart, bufread, col, s, n, IsLineBreakLF(lineending), column_del, comment_del, column_withs_const);
     } else {
-      return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, IsLineBreakCR(lineending), column_del, comment_del, column_withs_const);
+      return readColumns(v, buffer, bufstart, bufread, col, s, n, IsLineBreakCR(lineending), column_del, comment_del, column_withs_const);
     }
   } else {
     const AlwaysFalse column_withs_const;
     if (lineending.isLF()) {
-      return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, IsLineBreakLF(lineending), column_del, comment_del, column_withs_const);
+      return readColumns(v, buffer, bufstart, bufread, col, s, n, IsLineBreakLF(lineending), column_del, comment_del, column_withs_const);
     } else {
-      return readColumns(rowIndex, v, buffer, bufstart, bufread, col, s, n, IsLineBreakCR(lineending), column_del, comment_del, column_withs_const);
+      return readColumns(v, buffer, bufstart, bufread, col, s, n, IsLineBreakCR(lineending), column_del, comment_del, column_withs_const);
     }
   }
 }
 
 
 template<class Buffer, typename IsLineBreak, typename ColumnDelimiter, typename CommentDelimiter, typename ColumnWidthsAreConst>
-int AsciiDataReader::readColumns(const RowIndex& rowIndex, double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
+int AsciiDataReader::readColumns(double* v, const Buffer& buffer, int bufstart, int bufread, int col, int s, int n,
                               const IsLineBreak& isLineBreak,
                               const ColumnDelimiter& column_del, const CommentDelimiter& comment_del,
                               const ColumnWidthsAreConst& are_column_widths_const)
@@ -280,13 +285,13 @@
 
     if (are_column_widths_const()) {
       if (col_start != -1) {
-        v[i] = lexc.toDouble(&buffer[0] + rowIndex[s] + col_start);
+        v[i] = lexc.toDouble(&buffer[0] + _rowIndex[s] + col_start);
         continue;
       }
     }
 
     v[i] = Kst::NOPOINT;
-    for (int ch = rowIndex[s] - bufstart; ch < bufread; ++ch) {
+    for (int ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
       if (isLineBreak(buffer[ch])) {
         break;
       } else if (column_del(buffer[ch])) { //<- check for column start
@@ -307,7 +312,7 @@
             toDouble(lexc, &buffer[0], bufread, ch, &v[i], i);
             if (are_column_widths_const()) {
               if (col_start == -1) {
-                col_start = ch - rowIndex[s];
+                col_start = ch - _rowIndex[s];
               }
             }
             break;
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1320354:1320355
@@ -61,7 +61,6 @@
   Kst::DataSource(store, cfg, filename, type),
   r(_config),
   _fileBuffer(new AsciiDataReader::FileBuffer),
-  _rowIndex(),
   is(new DataInterfaceAsciiString(*this)),
   iv(new DataInterfaceAsciiVector(*this))
 {
@@ -106,7 +105,7 @@
 {
   // forget about cached data
   _fileBuffer->clearFileBuffer();
-  _rowIndex.clear();
+  r.rowIndex().clear();
 
   _valid = false;
   _byteLength = 0;
@@ -145,9 +144,9 @@
 bool AsciiSource::initRowIndex() 
 {
   // capacity is at least the pre-allocated memory
-  _rowIndex.resize(_rowIndex.capacity());
+  r.rowIndex().resize(r.rowIndex().capacity());
 
-  _rowIndex[0] = 0;
+  r.rowIndex()[0] = 0;
   _byteLength = 0;
   _numFrames = 0;
 
@@ -171,7 +170,7 @@
       }
       header_row++;
     }
-    _rowIndex[0] = didRead;
+    r.rowIndex()[0] = didRead;
   }
 
   return true;
@@ -230,7 +229,7 @@
   }
   _byteLength = file.size();
 
-  bool new_data = r.findDataRows(_rowIndex, _numFrames, read_completely, file, _byteLength);
+  bool new_data = r.findDataRows(_numFrames, read_completely, file, _byteLength);
 
   return (!new_data && !force_update ? NoChange : Updated);
 }
@@ -322,8 +321,8 @@
     return 0;
   }
 
-  int bufstart = _rowIndex[s];
-  int bufread = _rowIndex[s + n] - bufstart;
+  int bufstart = r.rowIndex()[s];
+  int bufread = r.rowIndex()[s + n] - bufstart;
   if (bufread <= 0) {
     return 0;
   }
@@ -346,7 +345,7 @@
     _fileBuffer->_bufferedN = n;
   }
 
- return r.readField(_rowIndex, _fileBuffer, col, bufstart, bufread, v, field, s, n, re_alloc);
+ return r.readField(_fileBuffer, col, bufstart, bufread, v, field, s, n, re_alloc);
 }
 
 
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1320354:1320355
@@ -80,10 +80,9 @@
 
   private:
     AsciiDataReader r;
-
     AsciiDataReader::FileBuffer* _fileBuffer;
-    AsciiDataReader::RowIndex _rowIndex;
 
+
     friend class ConfigWidgetAscii;
     mutable AsciiSourceConfig _config;
        


More information about the Kst mailing list