LCOV - code coverage report
Current view: top level - expand - termlistmerger.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7028d852e609 Lines: 16 19 84.2 %
Date: 2019-02-17 14:59:59 Functions: 3 3 100.0 %
Branches: 13 28 46.4 %

           Branch data     Line data    Source code
       1                 :            : /** @file termlistmerger.h
       2                 :            :  * @brief Build tree to merge TermList objects
       3                 :            :  */
       4                 :            : /* Copyright (C) 2008,2010,2011,2013,2016,2017,2018 Olly Betts
       5                 :            :  * Copyright (C) 2011 Action Without Borders
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_TERMLISTMERGER_H
      23                 :            : #define XAPIAN_INCLUDED_TERMLISTMERGER_H
      24                 :            : 
      25                 :            : #include "api/termlist.h"
      26                 :            : #include "heap.h"
      27                 :            : #include "omassert.h"
      28                 :            : #include "ortermlist.h"
      29                 :            : 
      30                 :            : struct CompareTermListSizeAscending {
      31                 :      10433 :     bool operator()(const TermList* a, const TermList* b) const {
      32                 :      10433 :         return a->get_approx_size() > b->get_approx_size();
      33                 :            :     }
      34                 :            : };
      35                 :            : 
      36                 :            : template<class ORTERMLIST = OrTermList>
      37                 :            : inline TermList*
      38                 :      44867 : make_termlist_merger(std::vector<TermList*>& termlists)
      39                 :            : {
      40 [ -  + ][ +  + ]:      44867 :     if (termlists.size() <= 1) {
      41 [ #  # ][ +  + ]:      34434 :         return termlists.size() == 1 ? termlists[0] : NULL;
      42                 :            :     }
      43                 :            : 
      44                 :            :     // Make termlists into a heap so that the longest termlist is at the
      45                 :            :     // top of the heap.
      46   [ +  -  +  - ]:      10433 :     Heap::make(termlists.begin(), termlists.end(),
      47                 :      20866 :                CompareTermListSizeAscending());
      48                 :            : 
      49                 :            :     // Now build a tree of binary TermList objects.  The algorithm used to
      50                 :            :     // build the tree is like that used to build an optimal Huffman coding
      51                 :            :     // tree.  If we called next() repeatedly, this arrangement would
      52                 :            :     // minimise the number of method calls.  Generally we don't actually do
      53                 :            :     // that, but this arrangement is still likely to be a good one, and it
      54                 :            :     // does minimise the work in the worst case.
      55                 :            :     while (true) {
      56                 :            :         AssertRel(termlists.size(), >=, 2);
      57                 :            :         // We build the tree such that at each branch:
      58                 :            :         //
      59                 :            :         //   l.get_approx_size() >= r.get_approx_size()
      60                 :            :         //
      61                 :            :         // We do this so that the OrTermList class can be optimised
      62                 :            :         // assuming that this is the case.
      63                 :      10433 :         TermList* r = termlists.front();
      64   [ +  -  +  - ]:      10433 :         Heap::pop(termlists.begin(), termlists.end(),
      65                 :      10433 :                   CompareTermListSizeAscending());
      66                 :      10433 :         termlists.pop_back();
      67                 :      10433 :         TermList* l = termlists.front();
      68                 :            : 
      69   [ +  -  +  - ]:      10433 :         TermList* tl = new ORTERMLIST(l, r);
      70                 :            : 
      71 [ +  - ][ +  - ]:      10433 :         if (termlists.size() == 1)
      72                 :      10433 :             return tl;
      73                 :            : 
      74                 :          0 :         termlists.front() = tl;
      75   [ #  #  #  # ]:          0 :         Heap::replace(termlists.begin(), termlists.end(),
      76                 :          0 :                       CompareTermListSizeAscending());
      77                 :      44867 :     }
      78                 :            : }
      79                 :            : 
      80                 :            : #endif // XAPIAN_INCLUDED_TERMLISTMERGER_H

Generated by: LCOV version 1.11