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