Warning, file /utilities/okteta/core/piecetable/piecelist.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of the Okteta Core library, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #ifndef KPIECETABLE_PIECELIST_HPP 0010 #define KPIECETABLE_PIECELIST_HPP 0011 0012 // lib 0013 #include "piece.hpp" 0014 // Qt 0015 #include <QVector> 0016 0017 namespace KPieceTable { 0018 0019 class PieceList 0020 { 0021 public: 0022 PieceList(); 0023 explicit PieceList(const Piece& piece); 0024 PieceList(const PieceList&); 0025 0026 ~PieceList(); 0027 0028 PieceList& operator=(const PieceList&) = default; 0029 0030 public: 0031 int size() const; 0032 bool isEmpty() const; 0033 Size totalLength() const; 0034 const Piece& at(int i) const; 0035 0036 public: 0037 void append(const PieceList& other); 0038 void append(const Piece& piece); 0039 void prepend(const PieceList& other); 0040 0041 private: 0042 QVector<Piece> mList; 0043 Size mTotalLength = 0; 0044 }; 0045 0046 inline PieceList::PieceList() = default; 0047 inline PieceList::PieceList(const PieceList&) = default; 0048 inline PieceList::PieceList(const Piece& piece) 0049 { 0050 append(piece); 0051 } 0052 0053 inline PieceList::~PieceList() = default; 0054 0055 inline int PieceList::size() const { return mList.size(); } 0056 inline bool PieceList::isEmpty() const { return mList.isEmpty(); } 0057 inline Size PieceList::totalLength() const { return mTotalLength; } 0058 inline const Piece& PieceList::at(int i) const { return mList.at(i); } 0059 0060 inline void PieceList::append(const Piece& piece) 0061 { 0062 bool isMerged = false; 0063 if (!mList.isEmpty()) { 0064 isMerged = mList.last().append(piece); 0065 } 0066 if (!isMerged) { 0067 mList.append(piece); 0068 } 0069 mTotalLength += piece.width(); 0070 } 0071 inline void PieceList::append(const PieceList& other) 0072 { 0073 QVector<Piece>::ConstIterator it = other.mList.begin(); 0074 0075 // see if the ones at the border can be merged 0076 bool isMerged = false; 0077 if (!mList.isEmpty() && !other.mList.isEmpty()) { 0078 isMerged = mList.last().append(other.mList.first()); 0079 } 0080 if (isMerged) { 0081 ++it; 0082 } 0083 0084 std::for_each(it, other.mList.end(), [this](const Piece& piece) { 0085 mList.append(piece); 0086 }); 0087 // was: mList += other.mList; 0088 0089 mTotalLength += other.mTotalLength; 0090 } 0091 inline void PieceList::prepend(const PieceList& other) 0092 { 0093 QVector<Piece> otherCopy = other.mList; 0094 QVector<Piece>::Iterator it = mList.begin(); 0095 0096 // see if the ones at the border can be merged 0097 bool isMerged = false; 0098 if (!otherCopy.isEmpty() && !mList.isEmpty()) { 0099 isMerged = otherCopy.last().append(mList.first()); 0100 } 0101 if (isMerged) { 0102 ++it; 0103 } 0104 0105 std::for_each(it, mList.end(), [&otherCopy](const Piece& piece) mutable { 0106 otherCopy.append(piece); 0107 }); 0108 0109 mList = otherCopy; 0110 // was: mList = other.mList + mList; 0111 0112 mTotalLength += other.mTotalLength; 0113 } 0114 0115 } 0116 0117 #endif