From 9dbe7cde3cbdbebccfa89ff6ae0daa8143591fab Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Sun, 11 Feb 2024 12:50:03 +0000 Subject: Make separate make/push/pop heap function to improve readability --- .../src/vespa/searchvisitor/hitcollector.cpp | 49 ++++++++++++++-------- .../src/vespa/searchvisitor/hitcollector.h | 3 ++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/streamingvisitors/src/vespa/searchvisitor/hitcollector.cpp b/streamingvisitors/src/vespa/searchvisitor/hitcollector.cpp index 8efcf73738e..4058826f1d1 100644 --- a/streamingvisitors/src/vespa/searchvisitor/hitcollector.cpp +++ b/streamingvisitors/src/vespa/searchvisitor/hitcollector.cpp @@ -85,11 +85,38 @@ HitCollector::addHitToHeap(const Hit & hit) const : (hit.cmpSort(_hits[0]) < 0); } +void +HitCollector::make_heap(bool useSortBlob) { + if (useSortBlob) { + std::make_heap(_hits.begin(), _hits.end(), Hit::SortComparator()); + } else { + std::make_heap(_hits.begin(), _hits.end(), Hit::RankComparator()); + } +} + +void +HitCollector::pop_heap(bool useSortBlob) { + if (useSortBlob) { + std::pop_heap(_hits.begin(), _hits.end(), Hit::SortComparator()); + } else { + std::pop_heap(_hits.begin(), _hits.end(), Hit::RankComparator()); + } +} + +void +HitCollector::push_heap(bool useSortBlob) { + if (useSortBlob) { + std::push_heap(_hits.begin(), _hits.end(), Hit::SortComparator()); + } else { + std::push_heap(_hits.begin(), _hits.end(), Hit::RankComparator()); + } +} + bool HitCollector::addHit(Hit && hit) { bool amongTheBest(false); - ssize_t avail = (_hits.capacity() - _hits.size()); + size_t avail = (_hits.capacity() - _hits.size()); bool useSortBlob( ! hit.getSortBlob().empty() ); if (avail > 1) { // No heap yet. @@ -99,28 +126,14 @@ HitCollector::addHit(Hit && hit) // this happens when wantedHitCount = 0 // in this case we shall not put anything on the heap (which is empty) } else if ( avail == 0 && addHitToHeap(hit)) { // already a heap - if (useSortBlob) { - std::pop_heap(_hits.begin(), _hits.end(), Hit::SortComparator()); - } else { - std::pop_heap(_hits.begin(), _hits.end(), Hit::RankComparator()); - } - + pop_heap(useSortBlob); _hits.back() = std::move(hit); amongTheBest = true; - - if (useSortBlob) { - std::push_heap(_hits.begin(), _hits.end(), Hit::SortComparator()); - } else { - std::push_heap(_hits.begin(), _hits.end(), Hit::RankComparator()); - } + push_heap(useSortBlob); } else if (avail == 1) { // make a heap of the hit vector _hits.emplace_back(std::move(hit)); amongTheBest = true; - if (useSortBlob) { - std::make_heap(_hits.begin(), _hits.end(), Hit::SortComparator()); - } else { - std::make_heap(_hits.begin(), _hits.end(), Hit::RankComparator()); - } + make_heap(useSortBlob); _sortedByDocId = false; // the hit vector is no longer sorted by docId } return amongTheBest; diff --git a/streamingvisitors/src/vespa/searchvisitor/hitcollector.h b/streamingvisitors/src/vespa/searchvisitor/hitcollector.h index 76c94840c7b..803e539d93b 100644 --- a/streamingvisitors/src/vespa/searchvisitor/hitcollector.h +++ b/streamingvisitors/src/vespa/searchvisitor/hitcollector.h @@ -77,6 +77,9 @@ private: void sortByDocId(); bool addHitToHeap(const Hit & hit) const; bool addHit(Hit && hit); + void make_heap(bool useSortBlob); + void pop_heap(bool useSortBlob); + void push_heap(bool useSortBlob); public: using UP = std::unique_ptr; -- cgit v1.2.3