Commit b177e1a1 authored by Marco Bubke's avatar Marco Bubke

Add set functions examples

parent 6f78752b
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include <iterator>
#include <QTextDocument>
#include <QTextBlock>
#include <vector>
namespace Examples {
class TextBlockIterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = QTextBlock;
using difference_type = int;
using pointer = QTextBlock*;
using reference = QTextBlock&;
TextBlockIterator(const QTextDocument *document, int blockNumber)
: m_document(document)
, m_blockNumber(blockNumber)
{}
TextBlockIterator(const QTextBlock &textBlock)
: m_document(textBlock.document())
, m_blockNumber(textBlock.blockNumber())
{}
QTextBlock operator*() { return m_document->findBlockByNumber(m_blockNumber); }
TextBlockIterator operator++()
{
++m_blockNumber;
return *this;
}
friend bool operator==(TextBlockIterator first, TextBlockIterator second)
{
return first.m_document == second.m_document && first.m_blockNumber == second.m_blockNumber;
}
friend bool operator!=(TextBlockIterator first, TextBlockIterator second)
{
return !(first == second);
}
private:
const QTextDocument *m_document;
int m_blockNumber;
};
class Range
{
public:
int start = 0;
int end = 0;
friend bool operator<(Range first, Range second)
{
return std::tie(first.start, first.end) < std::tie(second.start, second.end);
}
};
using Ranges = std::vector<Range>;
void function()
{
QTextDocument document;
Ranges ranges;
std::sort(ranges.begin(), ranges.end());
auto begin = TextBlockIterator(document.begin());
auto end = TextBlockIterator(document.end());
std::vector<QTextBlock> textBlockInsideRanges;
std::vector<QTextBlock> textBlockOutsideRanges;
class Compare
{
public:
bool operator()(QTextBlock first, Range second)
{
return first.blockNumber() < second.start;
}
bool operator()(Range first, QTextBlock second)
{
return first.end < second.blockNumber();
}
};
std::set_intersection(begin,
end,
ranges.begin(),
ranges.end(),
std::back_inserter(textBlockInsideRanges),
Compare{});
std::set_difference(begin,
end,
ranges.begin(),
ranges.end(),
std::back_inserter(textBlockOutsideRanges),
Compare{});
}
}
......@@ -3,5 +3,6 @@ include(../shared/benchmark.pri)
TARGET = set_functions
SOURCES += \
set_functions-benchmark.cpp
set_functions-benchmark.cpp \
set_functions-examples.cpp
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