| 1 | /* |
|---|---|
| 2 | This file is part of the KDE Baloo project. |
| 3 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "andpostingiterator.h" |
| 9 | |
| 10 | using namespace Baloo; |
| 11 | |
| 12 | AndPostingIterator::AndPostingIterator(const QVector<PostingIterator*>& iterators) |
| 13 | : m_iterators(iterators) |
| 14 | , m_docId(0) |
| 15 | { |
| 16 | if (m_iterators.contains(t: nullptr)) { |
| 17 | qDeleteAll(c: m_iterators); |
| 18 | m_iterators.clear(); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | AndPostingIterator::~AndPostingIterator() |
| 23 | { |
| 24 | qDeleteAll(c: m_iterators); |
| 25 | } |
| 26 | |
| 27 | quint64 AndPostingIterator::docId() const |
| 28 | { |
| 29 | return m_docId; |
| 30 | } |
| 31 | |
| 32 | quint64 AndPostingIterator::skipTo(quint64 id) |
| 33 | { |
| 34 | if (m_iterators.isEmpty()) { |
| 35 | m_docId = 0; |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | while (true) { |
| 40 | quint64 lower_bound = id; |
| 41 | for (PostingIterator* iter : std::as_const(t&: m_iterators)) { |
| 42 | lower_bound = iter->skipTo(docId: lower_bound); |
| 43 | |
| 44 | if (lower_bound == 0) { |
| 45 | m_docId = 0; |
| 46 | return 0; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (lower_bound == id) { |
| 51 | m_docId = lower_bound; |
| 52 | return lower_bound; |
| 53 | } |
| 54 | id = lower_bound; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | quint64 AndPostingIterator::next() |
| 59 | { |
| 60 | if (m_iterators.isEmpty()) { |
| 61 | m_docId = 0; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | m_docId = m_iterators[0]->next(); |
| 66 | m_docId = skipTo(id: m_docId); |
| 67 | |
| 68 | return m_docId; |
| 69 | } |
| 70 |
