1 /* 2 * Copyright (C) 2015 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef LINEPOSITIONARRAY_H 21 #define LINEPOSITIONARRAY_H 22 23 #include <QtGlobal> 24 #include <QVector> 25 26 #include "data/compressedlinestorage.h" 27 28 typedef QVector<qint64> SimpleLinePositionStorage; 29 30 // This class is a list of end of lines position, 31 // in addition to a list of qint64 (positions within the files) 32 // it can keep track of whether the final LF was added (for non-LF terminated 33 // files) and remove it when more data are added. 34 template <typename Storage> 35 class LinePosition 36 { 37 public: 38 // Default constructor 39 LinePosition() : array() 40 { fakeFinalLF_ = false; } 41 // Copy constructor (slow: deleted) 42 LinePosition( const LinePosition& orig ) = delete; 43 // Move assignement 44 LinePosition& operator=( LinePosition&& orig ) 45 { array = std::move( orig.array ); 46 fakeFinalLF_ = orig.fakeFinalLF_; 47 return *this; } 48 49 // Add a new line position at the given position 50 // Invariant: pos must be greater than the previous one 51 // (this is NOT checked!) 52 inline void append( qint64 pos ) 53 { 54 if ( fakeFinalLF_ ) 55 array.pop_back(); 56 57 array.append( pos ); 58 } 59 // Size of the array 60 inline int size() const 61 { return array.size(); } 62 // Extract an element 63 inline qint64 at( int i ) const 64 { return array.at( i ); } 65 inline qint64 operator[]( int i ) const 66 { return array.at( i ); } 67 // Set the presence of a fake final LF 68 // Must be used after 'append'-ing a fake LF at the end. 69 void setFakeFinalLF( bool finalLF=true ) 70 { fakeFinalLF_ = finalLF; } 71 72 // Add another list to this one, removing any fake LF on this list. 73 // Invariant: all pos in other must be greater than any pos in this 74 // (this is NOT checked!) 75 void append_list( const LinePosition<Storage>& other ) 76 { 77 // If our final LF is fake, we remove it 78 if ( fakeFinalLF_ ) 79 this->array.pop_back(); 80 81 // Append the arrays 82 this->array.append_list( other.array ); 83 84 // In case the 'other' object has a fake LF 85 this->fakeFinalLF_ = other.fakeFinalLF_; 86 } 87 88 private: 89 Storage array; 90 bool fakeFinalLF_; 91 }; 92 93 // Use the non-optimised storage 94 // typedef LinePosition<SimpleLinePositionStorage> FastLinePositionArray; 95 96 typedef LinePosition<CompressedLinePositionStorage> LinePositionArray; 97 98 #endif 99