xref: /glogg/src/data/linepositionarray.h (revision 7a2f3a57e884000357471fe3462e6bb0262e96af)
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
42     inline LinePosition( const LinePosition& orig )
43         : array(orig.array)
44     { fakeFinalLF_ = orig.fakeFinalLF_; }
45 
46     // Add a new line position at the given position
47     // Invariant: pos must be greater than the previous one
48     // (this is NOT checked!)
49     inline void append( qint64 pos )
50     {
51         if ( fakeFinalLF_ )
52             array.pop_back();
53 
54         array.append( pos );
55     }
56     // Size of the array
57     inline int size() const
58     { return array.size(); }
59     // Extract an element
60     inline qint64 at( int i ) const
61     { return array.at( i ); }
62     inline qint64 operator[]( int i ) const
63     { return array.at( i ); }
64     // Set the presence of a fake final LF
65     // Must be used after 'append'-ing a fake LF at the end.
66     void setFakeFinalLF( bool finalLF=true )
67     { fakeFinalLF_ = finalLF; }
68 
69     // Add another list to this one, removing any fake LF on this list.
70     // Invariant: all pos in other must be greater than any pos in this
71     // (this is NOT checked!)
72     void append_list( const LinePosition<Storage>& other )
73     {
74         // If our final LF is fake, we remove it
75         if ( fakeFinalLF_ )
76             this->array.pop_back();
77 
78         // Append the arrays
79         this->array.append_list( other.array );
80 
81         // In case the 'other' object has a fake LF
82         this->fakeFinalLF_ = other.fakeFinalLF_;
83     }
84 
85   private:
86     Storage array;
87     bool fakeFinalLF_;
88 };
89 
90 // Use the non-optimised storage
91 // typedef LinePosition<SimpleLinePositionStorage> FastLinePositionArray;
92 
93 typedef LinePosition<CompressedLinePositionStorage> LinePositionArray;
94 
95 #endif
96