Commit c531887b authored by Carlos Alves's avatar Carlos Alves Committed by Kurt Hindenburg
Browse files

Change HistoryScroll contantness

Declaring it where there is no change in content and enable compiler to
perform better optimizations.
parent 2aa61bb9
......@@ -18,7 +18,7 @@ HistoryScroll::HistoryScroll(HistoryType *t) :
HistoryScroll::~HistoryScroll() = default;
bool HistoryScroll::hasScroll()
bool HistoryScroll::hasScroll() const
{
return true;
}
......@@ -31,18 +31,18 @@ public:
explicit HistoryScroll(HistoryType *);
virtual ~HistoryScroll();
virtual bool hasScroll();
virtual bool hasScroll() const;
// access to history
virtual int getLines() = 0;
virtual int getMaxLines() = 0;
virtual int getLineLen(int lineno) = 0;
virtual void getCells(int lineno, int colno, int count, Character res[]) = 0;
virtual bool isWrappedLine(int lineNumber) = 0;
virtual LineProperty getLineProperty(int lineno) = 0;
virtual int getLines() const = 0;
virtual int getMaxLines() const = 0;
virtual int getLineLen(const int lineno) const = 0;
virtual void getCells(const int lineno, const int colno, const int count, Character res[]) const = 0;
virtual bool isWrappedLine(const int lineNumber) const = 0;
virtual LineProperty getLineProperty(const int lineno) const = 0;
// adding lines.
virtual void addCells(const Character a[], int count) = 0;
virtual void addCells(const Character a[], const int count) = 0;
// convenience method - this is virtual so that subclasses can take advantage
// of QVector's implicit copying
virtual void addCellsVector(const QVector<Character> &cells)
......@@ -50,11 +50,11 @@ public:
addCells(cells.data(), cells.size());
}
virtual void addLine(LineProperty lineProperty = 0) = 0;
virtual void addLine(const LineProperty lineProperty = 0) = 0;
// modify history
virtual void removeCells() = 0;
virtual int reflowLines(int columns) = 0;
virtual int reflowLines(const int columns) = 0;
//
// FIXME: Passing around constant references to HistoryType instances
......
......@@ -28,64 +28,58 @@ HistoryScrollFile::HistoryScrollFile() :
HistoryScrollFile::~HistoryScrollFile() = default;
int HistoryScrollFile::getLines()
int HistoryScrollFile::getLines() const
{
return _index.len() / sizeof(qint64);
}
int HistoryScrollFile::getMaxLines()
int HistoryScrollFile::getMaxLines() const
{
return getLines();
}
int HistoryScrollFile::getLineLen(int lineno)
int HistoryScrollFile::getLineLen(const int lineno) const
{
return (startOfLine(lineno + 1) - startOfLine(lineno)) / sizeof(Character);
}
bool HistoryScrollFile::isWrappedLine(int lineno)
bool HistoryScrollFile::isWrappedLine(const int lineno) const
{
if (lineno >= 0 && lineno <= getLines()) {
unsigned char flag = 0;
_lineflags.get(reinterpret_cast<char *>(&flag), sizeof(unsigned char),
(lineno)*sizeof(unsigned char));
return (flag & LINE_WRAPPED) != 0;
}
return false;
return (getLineProperty(lineno) & LINE_WRAPPED) > 0;
}
LineProperty HistoryScrollFile::getLineProperty(int lineno)
LineProperty HistoryScrollFile::getLineProperty(const int lineno) const
{
if (lineno >= 0 && lineno <= getLines()) {
LineProperty flag = 0;
_lineflags.get(reinterpret_cast<char *>(&flag), sizeof(unsigned char),
(lineno)*sizeof(unsigned char));
(lineno) * sizeof(unsigned char));
return flag;
}
return 0;
}
qint64 HistoryScrollFile::startOfLine(int lineno)
qint64 HistoryScrollFile::startOfLine(const int lineno) const
{
if (lineno <= 0) {
return 0;
}
if (lineno <= getLines()) {
qint64 res = 0;
_index.get(reinterpret_cast<char*>(&res), sizeof(qint64), (lineno - 1)*sizeof(qint64));
_index.get(reinterpret_cast<char *>(&res), sizeof(qint64), (lineno - 1) * sizeof(qint64));
return res;
}
return _cells.len();
}
void HistoryScrollFile::getCells(int lineno, int colno, int count, Character res[])
void HistoryScrollFile::getCells(const int lineno, const int colno, const int count, Character res[]) const
{
_cells.get(reinterpret_cast<char*>(res), count * sizeof(Character), startOfLine(lineno) + colno * sizeof(Character));
_cells.get(reinterpret_cast<char *>(res), count * sizeof(Character), startOfLine(lineno) + colno * sizeof(Character));
}
void HistoryScrollFile::addCells(const Character text[], int count)
void HistoryScrollFile::addCells(const Character text[], const int count)
{
_cells.add(reinterpret_cast<const char*>(text), count * sizeof(Character));
_cells.add(reinterpret_cast<const char *>(text), count * sizeof(Character));
}
void HistoryScrollFile::addLine(LineProperty lineProperty)
......@@ -109,15 +103,15 @@ void HistoryScrollFile::removeCells()
_lineflags.removeLast(res * sizeof(unsigned char));
}
int HistoryScrollFile::reflowLines(int columns)
int HistoryScrollFile::reflowLines(const int columns)
{
auto reflowFile = std::make_unique<HistoryFile>();
reflowData newLine;
auto reflowLineLen = [] (qint64 start, qint64 end) {
auto reflowLineLen = [](qint64 start, qint64 end) {
return (int)((end - start) / sizeof(Character));
};
auto setNewLine = [] (reflowData &change, qint64 index, LineProperty lineflag) {
auto setNewLine = [](reflowData &change, qint64 index, LineProperty lineflag) {
change.index = index;
change.lineFlag = lineflag;
};
......
......@@ -26,26 +26,26 @@ public:
explicit HistoryScrollFile();
~HistoryScrollFile() override;
int getLines() override;
int getMaxLines() override;
int getLineLen(int lineno) override;
void getCells(int lineno, int colno, int count, Character res[]) override;
bool isWrappedLine(int lineno) override;
LineProperty getLineProperty(int lineno) override;
void addCells(const Character text[], int count) override;
int getLines() const override;
int getMaxLines() const override;
int getLineLen(const int lineno) const override;
void getCells(const int lineno, const int colno, const int count, Character res[]) const override;
bool isWrappedLine(const int lineno) const override;
LineProperty getLineProperty(const int lineno) const override;
void addCells(const Character text[], const int count) override;
void addLine(LineProperty lineProperty = 0) override;
// Modify history
void removeCells() override;
int reflowLines(int columns) override;
int reflowLines(const int columns) override;
private:
qint64 startOfLine(int lineno);
qint64 startOfLine(const int lineno) const;
HistoryFile _index; // lines Row(qint64)
HistoryFile _cells; // text Row(Character)
HistoryFile _lineflags; // flags Row(unsigned char)
mutable HistoryFile _index; // lines Row(qint64)
mutable HistoryFile _cells; // text Row(Character)
mutable HistoryFile _lineflags; // flags Row(unsigned char)
struct reflowData { // data to reflow lines
qint64 index;
......
......@@ -20,37 +20,37 @@ HistoryScrollNone::HistoryScrollNone() :
HistoryScrollNone::~HistoryScrollNone() = default;
bool HistoryScrollNone::hasScroll()
bool HistoryScrollNone::hasScroll() const
{
return false;
}
int HistoryScrollNone::getLines()
int HistoryScrollNone::getLines() const
{
return 0;
}
int HistoryScrollNone::getMaxLines()
int HistoryScrollNone::getMaxLines() const
{
return 0;
}
int HistoryScrollNone::getLineLen(int)
int HistoryScrollNone::getLineLen(int) const
{
return 0;
}
bool HistoryScrollNone::isWrappedLine(int /*lineno*/)
bool HistoryScrollNone::isWrappedLine(int /*lineno*/) const
{
return false;
}
LineProperty HistoryScrollNone::getLineProperty(int /*lineno*/)
LineProperty HistoryScrollNone::getLineProperty(int /*lineno*/) const
{
return 0;
}
void HistoryScrollNone::getCells(int, int, int, Character [])
void HistoryScrollNone::getCells(int, int, int, Character []) const
{
}
......
......@@ -23,21 +23,21 @@ public:
HistoryScrollNone();
~HistoryScrollNone() override;
bool hasScroll() override;
bool hasScroll() const override;
int getLines() override;
int getMaxLines() override;
int getLineLen(int lineno) override;
void getCells(int lineno, int colno, int count, Character res[]) override;
bool isWrappedLine(int lineno) override;
LineProperty getLineProperty(int lineno) override;
int getLines() const override;
int getMaxLines() const override;
int getLineLen(const int lineno) const override;
void getCells(const int lineno, const int colno, const int count, Character res[]) const override;
bool isWrappedLine(const int lineno) const override;
LineProperty getLineProperty(const int lineno) const override;
void addCells(const Character a[], int count) override;
void addLine(LineProperty lineProperty = 0) override;
void addCells(const Character a[], const int count) override;
void addLine(const LineProperty lineProperty = 0) override;
// Modify history (do nothing here)
void removeCells() override;
int reflowLines(int) override;
int reflowLines(const int) override;
};
}
......
......@@ -16,7 +16,7 @@ struct reflowData { // data to reflow lines
QList<LineProperty> flags;
};
CompactHistoryScroll::CompactHistoryScroll(unsigned int maxLineCount) :
CompactHistoryScroll::CompactHistoryScroll(const unsigned int maxLineCount) :
HistoryScroll(new CompactHistoryType(maxLineCount)),
_cells(),
_index(),
......@@ -28,28 +28,28 @@ CompactHistoryScroll::CompactHistoryScroll(unsigned int maxLineCount) :
void CompactHistoryScroll::removeFirstLine()
{
_flags.pop_front();
_flags.pop_front();
auto removing = _index.first();
_index.pop_front();
std::transform(_index.begin(), _index.end(), _index.begin(), [removing](int i) { return i - removing; });
_index.pop_front();
std::transform(_index.begin(), _index.end(), _index.begin(), [removing](int i) { return i - removing; });
while (_cells.size() > _index.last()) {
_cells.pop_front();
}
}
inline int CompactHistoryScroll::lineLen(int line)
inline int CompactHistoryScroll::lineLen(const int line) const
{
return line == 0 ? _index[0] : _index[line] - _index[line - 1];
}
inline int CompactHistoryScroll::startOfLine(int line)
inline int CompactHistoryScroll::startOfLine(const int line) const
{
return line == 0 ? 0 : _index[line - 1];
}
void CompactHistoryScroll::addCells(const Character a[], int count)
void CompactHistoryScroll::addCells(const Character a[], const int count)
{
std::copy(a, a + count, std::back_inserter(_cells));
......@@ -61,23 +61,23 @@ void CompactHistoryScroll::addCells(const Character a[], int count)
}
}
void CompactHistoryScroll::addLine(LineProperty lineProperty)
void CompactHistoryScroll::addLine(const LineProperty lineProperty)
{
auto &flag = _flags.last();
flag = lineProperty;
}
int CompactHistoryScroll::getLines()
int CompactHistoryScroll::getLines() const
{
return _index.size();
}
int CompactHistoryScroll::getMaxLines()
int CompactHistoryScroll::getMaxLines() const
{
return _maxLineCount;
}
int CompactHistoryScroll::getLineLen(int lineNumber)
int CompactHistoryScroll::getLineLen(int lineNumber) const
{
if (lineNumber < 0 || lineNumber >= _index.size()) {
return 0;
......@@ -86,7 +86,7 @@ int CompactHistoryScroll::getLineLen(int lineNumber)
return lineLen(lineNumber);
}
void CompactHistoryScroll::getCells(int lineNumber, int startColumn, int count, Character buffer[])
void CompactHistoryScroll::getCells(const int lineNumber, const int startColumn, const int count, Character buffer[]) const
{
if (count == 0) {
return;
......@@ -101,7 +101,7 @@ void CompactHistoryScroll::getCells(int lineNumber, int startColumn, int count,
std::copy(startCopy, endCopy, buffer);
}
void CompactHistoryScroll::setMaxNbLines(int lineCount)
void CompactHistoryScroll::setMaxNbLines(const int lineCount)
{
Q_ASSERT(lineCount >= 0);
_maxLineCount = lineCount;
......@@ -127,19 +127,19 @@ void CompactHistoryScroll::removeCells()
}
}
bool CompactHistoryScroll::isWrappedLine(int lineNumber)
bool CompactHistoryScroll::isWrappedLine(const int lineNumber) const
{
Q_ASSERT(lineNumber < _index.size());
return _flags[lineNumber] & LINE_WRAPPED;
return (_flags[lineNumber] & LINE_WRAPPED) > 0;
}
LineProperty CompactHistoryScroll::getLineProperty(int lineNumber)
LineProperty CompactHistoryScroll::getLineProperty(const int lineNumber) const
{
Q_ASSERT(lineNumber < _index.size());
return _flags[lineNumber];
}
int CompactHistoryScroll::reflowLines(int columns)
int CompactHistoryScroll::reflowLines(const int columns)
{
reflowData newLine;
......
......@@ -22,24 +22,24 @@ class KONSOLEPRIVATE_EXPORT CompactHistoryScroll : public HistoryScroll
typedef QVector<Character> TextLine;
public:
explicit CompactHistoryScroll(unsigned int maxLineCount = 1000);
explicit CompactHistoryScroll(const unsigned int maxLineCount = 1000);
~CompactHistoryScroll() = default;
int getLines() override;
int getMaxLines() override;
int getLineLen(int lineNumber) override;
void getCells(int lineNumber, int startColumn, int count, Character buffer[]) override;
bool isWrappedLine(int lineNumber) override;
LineProperty getLineProperty(int lineNumber) override;
int getLines() const override;
int getMaxLines() const override;
int getLineLen(const int lineNumber) const override;
void getCells(const int lineNumber, const int startColumn, const int count, Character buffer[]) const override;
bool isWrappedLine(const int lineNumber) const override;
LineProperty getLineProperty(const int lineNumber) const override;
void addCells(const Character a[], int count) override;
void addLine(LineProperty lineProperty = 0) override;
void addCells(const Character a[], const int count) override;
void addLine(const LineProperty lineProperty = 0) override;
void removeCells() override;
void setMaxNbLines(int lineCount);
void setMaxNbLines(const int lineCount);
int reflowLines(int columns) override;
int reflowLines(const int columns) override;
private:
QList<Character> _cells;
......@@ -49,8 +49,8 @@ private:
int _maxLineCount;
void removeFirstLine();
inline int lineLen(const int line);
inline int startOfLine(int line);
inline int lineLen(const int line) const;
inline int startOfLine(const int line) const;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment