QCodeEdit  2.2
lib/document/qdocumentbuffer.h
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
00004 **
00005 ** This file is part of the Edyuk project <http://edyuk.org>
00006 ** 
00007 ** This file may be used under the terms of the GNU General Public License
00008 ** version 3 as published by the Free Software Foundation and appearing in the
00009 ** file GPL.txt included in the packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ****************************************************************************/
00015 
00016 #ifndef _QDOCUMENT_BUFFER_H_
00017 #define _QDOCUMENT_BUFFER_H_
00018 
00019 #include "qce-config.h"
00020 
00021 #include <QVector>
00022 
00023 #include "qdocumentline_p.h"
00024 
00025 class QDocumentLineHandle;
00026 
00027 class QCE_EXPORT QDocumentBuffer
00028 {
00029     friend class QDocumentLineHandle;
00030     
00031     public:
00032         class iterator
00033         {
00034             public:
00035                 iterator(const iterator& i);
00036                 
00037                 bool atEnd() const;
00038                 
00039                 int lineNumber() const;
00040                 QDocumentLineHandle* lineHandle() const;
00041                 
00042                 void move(int numLines);
00043                 
00044             protected:
00045                 iterator(QDocumentBuffer *buffer, int block, int line);
00046                 
00047             private:
00048                 int m_block;
00049                 int m_line;
00050                 QDocumentBuffer *m_buffer;
00051         };
00052         
00053         QDocumentBuffer();
00054         ~QDocumentBuffer();
00055         
00056         QDocumentLineHandle* at(int index) const;
00057         
00058         void appendLine(QDocumentLineHandle *l);
00059         
00060         void insertLine(int index, QDocumentLineHandle *l);
00061         void removeLine(int index);
00062         
00063         void insertLines(int after, const QVector<QDocumentLineHandle*>& l);
00064         void removeLines(int after, int n);
00065         
00066     private:
00067         static void cleanHelper(QVector<QDocumentLineHandle*>& l)
00068         {
00069             foreach ( QDocumentLineHandle *h, l )
00070                 h->deref();
00071         }
00072         
00073         struct Block
00074         {
00075             inline Block() : start(-1), end(-1) {}
00076             inline Block(int line) : start(line), end(line) {}
00077             ~Block() { cleanHelper(lines); }
00078             
00079             inline void move(int numLines) { start += numLines; end += numLines; }
00080             
00081             inline int size() const { return lines.count(); }
00082             inline QDocumentLineHandle* at(int index) const { return lines.at(index - start); }
00083             
00084             inline void append(QDocumentLineHandle *h) { lines.append(h); }
00085             inline void prepend(QDocumentLineHandle *h) { lines.prepend(h); }
00086             inline void insert(int index, QDocumentLineHandle *h) { lines.insert(index - start, h); }
00087             inline void insert(int index, const QDocumentLineHandle* const* l, int n)
00088             {
00089                 QDocumentLineHandle **d = const_cast<QDocumentLineHandle**>(l);
00090                 
00091                 int i = index - start;
00092                 lines.insert(i, n, 0);
00093                 
00094                 while ( n )
00095                 {
00096                     lines[i++] = *d;
00097                     ++d;
00098                     --n;
00099                 }
00100             }
00101             
00102             inline void append(const QDocumentLineHandle* const* l, int n)
00103             {
00104                 QDocumentLineHandle **d = const_cast<QDocumentLineHandle**>(l);
00105                 
00106                 int i = lines.count();
00107                 lines.insert(i, n, 0);
00108                 
00109                 while ( n )
00110                 {
00111                     lines[i++] = *d;
00112                     ++d;
00113                     --n;
00114                 }
00115             }
00116             
00117             inline void prepend(const QDocumentLineHandle* const* l, int n)
00118             {
00119                 QDocumentLineHandle **d = const_cast<QDocumentLineHandle**>(l);
00120                 
00121                 int i = 0;
00122                 lines.insert(i, n, 0);
00123                 
00124                 while ( n )
00125                 {
00126                     lines[i++] = *d;
00127                     ++d;
00128                     --n;
00129                 }
00130             }
00131             
00132             inline void remove(int index) { lines.remove(index - start); }
00133             inline void remove(int index, int count) { lines.remove(index - start, qMin(count, end - index)); }
00134             
00135             int start, end;
00136             QVector<QDocumentLineHandle*> lines;
00137         };
00138         
00139         int blockForLine(int index) const;
00140         
00141         int m_safetyRoom;
00142         int m_optimalSize;
00143         int m_forkThresold;
00144         int m_mergeThresold;
00145         
00146         QVector<Block*> m_blocks;
00147 };
00148 
00149 #endif // !_QDOCUMENT_BUFFER_H_