1 // Copyright (C) 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_SCORING_RANKER_H_ 16 #define ICING_SCORING_RANKER_H_ 17 18 #include <vector> 19 20 #include "icing/index/term-metadata.h" 21 #include "icing/scoring/scored-document-hit.h" 22 23 // Provides functionality to get the top N results from an unsorted vector. 24 namespace icing { 25 namespace lib { 26 27 // Builds a heap of scored document hits. The same vector is used to store the 28 // heap structure. 29 // 30 // REQUIRED: scored_document_hits is not null. 31 void BuildHeapInPlace( 32 std::vector<ScoredDocumentHit>* scored_document_hits, 33 const ScoredDocumentHitComparator& scored_document_hit_comparator); 34 35 // Returns the top num_results results from the given heap and remove those 36 // results from the heap. An empty vector will be returned if heap is empty. 37 // 38 // REQUIRED: scored_document_hits_heap is not null. 39 std::vector<ScoredDocumentHit> PopTopResultsFromHeap( 40 std::vector<ScoredDocumentHit>* scored_document_hits_heap, int num_results, 41 const ScoredDocumentHitComparator& scored_document_hit_comparator); 42 43 // The heap is a min-heap. So that we can avoid some push operations by 44 // comparing to the root term, and only pushing if greater than root. The time 45 // complexity for a single push is O(lgK) which K is the number_to_return. 46 // REQUIRED: scored_terms_heap is not null. 47 void PushToTermHeap(TermMetadata term, int number_to_return, 48 std::vector<TermMetadata>& scored_terms_heap); 49 50 // Return all terms from the given terms heap. And since the heap is a min-heap, 51 // the output vector will be increasing order. 52 // REQUIRED: scored_terms_heap is not null. 53 std::vector<TermMetadata> PopAllTermsFromHeap( 54 std::vector<TermMetadata>& scored_terms_heap); 55 } // namespace lib 56 } // namespace icing 57 58 #endif // ICING_SCORING_RANKER_H_ 59