LCOV - code coverage report
Current view: top level - include/xapian - mset.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 53 53 100.0 %
Date: 2019-06-30 05:20:33 Functions: 28 28 100.0 %
Branches: 10 14 71.4 %

           Branch data     Line data    Source code
       1                 :            : /** @file  mset.h
       2                 :            :  *  @brief Class representing a list of search results
       3                 :            :  */
       4                 :            : /* Copyright (C) 2015,2016,2017,2019 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_MSET_H
      23                 :            : #define XAPIAN_INCLUDED_MSET_H
      24                 :            : 
      25                 :            : #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
      26                 :            : # error "Never use <xapian/mset.h> directly; include <xapian.h> instead."
      27                 :            : #endif
      28                 :            : 
      29                 :            : #include <iterator>
      30                 :            : #include <string>
      31                 :            : 
      32                 :            : #include <xapian/attributes.h>
      33                 :            : #include <xapian/document.h>
      34                 :            : #include <xapian/error.h>
      35                 :            : #include <xapian/intrusive_ptr.h>
      36                 :            : #include <xapian/stem.h>
      37                 :            : #include <xapian/types.h>
      38                 :            : #include <xapian/visibility.h>
      39                 :            : 
      40                 :            : namespace Xapian {
      41                 :            : 
      42                 :            : class MSetIterator;
      43                 :            : 
      44                 :            : /// Class representing a list of search results.
      45                 :   66919772 : class XAPIAN_VISIBILITY_DEFAULT MSet {
      46                 :            :     friend class MSetIterator;
      47                 :            : 
      48                 :            :     // Helper function for fetch() methods.
      49                 :            :     void fetch_(Xapian::doccount first, Xapian::doccount last) const;
      50                 :            : 
      51                 :            :     /** Update the weight corresponding to the document indexed at
      52                 :            :      *  position i with wt.
      53                 :            :      *
      54                 :            :      *  The MSet's max_possible and max_attained are also updated.
      55                 :            :      *
      56                 :            :      *  This method must be called to update the weight of every document in
      57                 :            :      *  the MSet for i = 0 to mset.size() - 1 in ascending order to avoid
      58                 :            :      *  miscalculation of max_attained and max_possible.
      59                 :            :      *
      60                 :            :      *  @param i        MSet index to update
      61                 :            :      *  @param wt       new weight to assign to the document at index @a i
      62                 :            :      */
      63                 :            :     void set_item_weight(Xapian::doccount i, double wt);
      64                 :            : 
      65                 :            :   public:
      66                 :            :     /// Class representing the MSet internals.
      67                 :            :     class Internal;
      68                 :            :     /// @private @internal Reference counted internals.
      69                 :            :     Xapian::Internal::intrusive_ptr_nonnull<Internal> internal;
      70                 :            : 
      71                 :            :     /** Copying is allowed.
      72                 :            :      *
      73                 :            :      *  The internals are reference counted, so copying is cheap.
      74                 :            :      */
      75                 :            :     MSet(const MSet & o);
      76                 :            : 
      77                 :            :     /** Copying is allowed.
      78                 :            :      *
      79                 :            :      *  The internals are reference counted, so assignment is cheap.
      80                 :            :      */
      81                 :            :     MSet & operator=(const MSet & o);
      82                 :            : 
      83                 :            :     /// Move constructor.
      84                 :            :     MSet(MSet && o);
      85                 :            : 
      86                 :            :     /// Move assignment operator.
      87                 :            :     MSet & operator=(MSet && o);
      88                 :            : 
      89                 :            :     /** Default constructor.
      90                 :            :      *
      91                 :            :      *  Creates an empty MSet, mostly useful as a placeholder.
      92                 :            :      */
      93                 :            :     MSet();
      94                 :            : 
      95                 :            :     /** @private @internal Wrap an existing Internal. */
      96                 :            :     XAPIAN_VISIBILITY_INTERNAL
      97                 :            :     explicit MSet(Internal* internal_);
      98                 :            : 
      99                 :            :     /// Destructor.
     100                 :            :     ~MSet();
     101                 :            : 
     102                 :            :     /** Assigns new weights and updates MSet.
     103                 :            :      *
     104                 :            :      *  Dereferencing the Iterator should return a double.
     105                 :            :      *
     106                 :            :      *  The weights returned by the iterator are assigned to elements of
     107                 :            :      *  the MSet in rank order.
     108                 :            :      *
     109                 :            :      *  @param begin    Begin iterator.
     110                 :            :      *  @param end      End iterator.
     111                 :            :      *
     112                 :            :      *  @exception Xapian::InvalidArgument is thrown if the total number of
     113                 :            :      *             elements in the input doesn't match the total number of
     114                 :            :      *             documents in MSet.
     115                 :            :      */
     116                 :            :     template<typename Iterator>
     117                 :         21 :     void replace_weights(Iterator first, Iterator last)
     118                 :            :     {
     119                 :         21 :         auto distance = last - first;
     120                 :            :         // Take care to compare signed and unsigned types both safely and
     121                 :            :         // without triggering compiler warnings.
     122   [ +  -  +  + ]:         42 :         if (distance < 0 ||
                 [ +  + ]
     123                 :            :             (sizeof(distance) <= sizeof(Xapian::doccount) ?
     124                 :            :                 Xapian::doccount(distance) != size() :
     125                 :         21 :                 distance != static_cast<decltype(distance)>(size()))) {
     126 [ +  - ][ +  - ]:          7 :             throw Xapian::InvalidArgumentError("Number of weights assigned "
                 [ +  - ]
     127                 :            :                                                "doesn't match the number of "
     128                 :          7 :                                                "items");
     129                 :            :         }
     130                 :         14 :         Xapian::doccount i = 0;
     131         [ +  + ]:         35 :         while (first != last) {
     132                 :         21 :             set_item_weight(i, *first);
     133                 :         21 :             ++i;
     134                 :         21 :             ++first;
     135                 :            :         }
     136                 :         14 :     }
     137                 :            : 
     138                 :            :     /** Sorts the list of documents in MSet according to their weights.
     139                 :            :      *
     140                 :            :      *  Use after calling MSet::replace_weights.
     141                 :            :      */
     142                 :            :     void sort_by_relevance();
     143                 :            : 
     144                 :            :     /** Convert a weight to a percentage.
     145                 :            :      *
     146                 :            :      *  The matching document with the highest weight will get 100% if it
     147                 :            :      *  matches all the weighted query terms, and proportionally less if it
     148                 :            :      *  only matches some, and other weights are scaled by the same factor.
     149                 :            :      *
     150                 :            :      *  Documents with a non-zero score will always score at least 1%.
     151                 :            :      *
     152                 :            :      *  Note that these generally aren't percentages of anything meaningful
     153                 :            :      *  (unless you use a custom weighting formula where they are!)
     154                 :            :      */
     155                 :            :     int convert_to_percent(double weight) const;
     156                 :            : 
     157                 :            :     /** Convert the weight of the current iterator position to a percentage.
     158                 :            :      *
     159                 :            :      *  The matching document with the highest weight will get 100% if it
     160                 :            :      *  matches all the weighted query terms, and proportionally less if it
     161                 :            :      *  only matches some, and other weights are scaled by the same factor.
     162                 :            :      *
     163                 :            :      *  Documents with a non-zero score will always score at least 1%.
     164                 :            :      *
     165                 :            :      *  Note that these generally aren't percentages of anything meaningful
     166                 :            :      *  (unless you use a custom weighting formula where they are!)
     167                 :            :      */
     168                 :            :     int convert_to_percent(const MSetIterator & it) const;
     169                 :            : 
     170                 :            :     /** Get the termfreq of a term.
     171                 :            :      *
     172                 :            :      *  @return The number of documents @a term occurs in.
     173                 :            :      *
     174                 :            :      *  Since 1.5.0, this method returns 0 if called on an MSet which is
     175                 :            :      *  not associated with a database (which is consistent with
     176                 :            :      *  Database::get_termfreq() returning 0 when called on a Database
     177                 :            :      *  with no sub-databases); in earlier versions,
     178                 :            :      *  Xapian::InvalidOperationError was thrown in this case.
     179                 :            :      */
     180                 :            :     Xapian::doccount get_termfreq(const std::string & term) const;
     181                 :            : 
     182                 :            :     /** Get the term weight of a term.
     183                 :            :      *
     184                 :            :      *  @return The maximum weight that @a term could have contributed to a
     185                 :            :      *          document.
     186                 :            :      *
     187                 :            :      *  Since 1.5.0, this method returns 0.0 if called on an MSet which is
     188                 :            :      *  not associated with a database, or with a term which wasn't present
     189                 :            :      *  in the query (since in both cases the term contributes no weight to any
     190                 :            :      *  matching documents); in earlier versions, Xapian::InvalidOperationError
     191                 :            :      *  was thrown for the first case, and Xapian::InvalidArgumentError for the
     192                 :            :      *  second.
     193                 :            :      */
     194                 :            :     double get_termweight(const std::string & term) const;
     195                 :            : 
     196                 :            :     /** Rank of first item in this MSet.
     197                 :            :      *
     198                 :            :      *  This is the parameter `first` passed to Xapian::Enquire::get_mset().
     199                 :            :      */
     200                 :            :     Xapian::doccount get_firstitem() const;
     201                 :            : 
     202                 :            :     /** Lower bound on the total number of matching documents. */
     203                 :            :     Xapian::doccount get_matches_lower_bound() const;
     204                 :            :     /** Estimate of the total number of matching documents. */
     205                 :            :     Xapian::doccount get_matches_estimated() const;
     206                 :            :     /** Upper bound on the total number of matching documents. */
     207                 :            :     Xapian::doccount get_matches_upper_bound() const;
     208                 :            : 
     209                 :            :     /** Lower bound on the total number of matching documents before collapsing.
     210                 :            :      *
     211                 :            :      *  Conceptually the same as get_matches_lower_bound() for the same query
     212                 :            :      *  without any collapse part (though the actual value may differ).
     213                 :            :      */
     214                 :            :     Xapian::doccount get_uncollapsed_matches_lower_bound() const;
     215                 :            :     /** Estimate of the total number of matching documents before collapsing.
     216                 :            :      *
     217                 :            :      *  Conceptually the same as get_matches_estimated() for the same query
     218                 :            :      *  without any collapse part (though the actual value may differ).
     219                 :            :      */
     220                 :            :     Xapian::doccount get_uncollapsed_matches_estimated() const;
     221                 :            :     /** Upper bound on the total number of matching documents before collapsing.
     222                 :            :      *
     223                 :            :      *  Conceptually the same as get_matches_upper_bound() for the same query
     224                 :            :      *  without any collapse part (though the actual value may differ).
     225                 :            :      */
     226                 :            :     Xapian::doccount get_uncollapsed_matches_upper_bound() const;
     227                 :            : 
     228                 :            :     /** The maximum weight attained by any document. */
     229                 :            :     double get_max_attained() const;
     230                 :            :     /** The maximum possible weight any document could achieve. */
     231                 :            :     double get_max_possible() const;
     232                 :            : 
     233                 :            :     enum {
     234                 :            :         /** Model the relevancy of non-query terms in MSet::snippet().
     235                 :            :          *
     236                 :            :          *  Non-query terms will be assigned a small weight, and the snippet
     237                 :            :          *  will tend to prefer snippets which contain a more interesting
     238                 :            :          *  background (where the query term content is equivalent).
     239                 :            :          */
     240                 :            :         SNIPPET_BACKGROUND_MODEL = 1,
     241                 :            :         /** Exhaustively evaluate candidate snippets in MSet::snippet().
     242                 :            :          *
     243                 :            :          *  Without this flag, snippet generation will stop once it thinks
     244                 :            :          *  it has found a "good enough" snippet, which will generally reduce
     245                 :            :          *  the time taken to generate a snippet.
     246                 :            :          */
     247                 :            :         SNIPPET_EXHAUSTIVE = 2,
     248                 :            :         /** Return the empty string if no term got matched.
     249                 :            :          *
     250                 :            :          *  If enabled, snippet() returns an empty string if not a single match
     251                 :            :          *  was found in text. If not enabled, snippet() returns a (sub)string
     252                 :            :          *  of text without any highlighted terms.
     253                 :            :          */
     254                 :            :         SNIPPET_EMPTY_WITHOUT_MATCH = 4,
     255                 :            : 
     256                 :            :         /** Enable generation of n-grams from CJK text.
     257                 :            :          *
     258                 :            :          *  This option highlights CJK searches made using the QueryParser
     259                 :            :          *  FLAG_CJK_NGRAM flag.  Non-CJK characters are split into words as
     260                 :            :          *  normal.
     261                 :            :          *
     262                 :            :          *  The TermGenerator FLAG_CJK_NGRAM flag needs to have been used at
     263                 :            :          *  index time.
     264                 :            :          *
     265                 :            :          *  This mode can also be enabled by setting environment variable
     266                 :            :          *  XAPIAN_CJK_NGRAM to a non-empty value (but doing so was deprecated
     267                 :            :          *  in 1.4.11).
     268                 :            :          *
     269                 :            :          *  @since Added in Xapian 1.4.11.
     270                 :            :          */
     271                 :            :         SNIPPET_CJK_NGRAM = 2048,
     272                 :            : 
     273                 :            :         /** Enable generation of words from CJK text.
     274                 :            :          *
     275                 :            :          *  This option highlights CJK searches made using the QueryParser
     276                 :            :          *  FLAG_CJK_WORDS flag.  Spans of CJK characters are split into CJK
     277                 :            :          *  words using text boundary heuristics.  Non-CJK characters are
     278                 :            :          *  split into words as normal.
     279                 :            :          *
     280                 :            :          *  The TermGenerator FLAG_CJK_WORDS flag needs to have been used at
     281                 :            :          *  index time.
     282                 :            :          *
     283                 :            :          *  @since Added in Xapian 1.5.0.
     284                 :            :          */
     285                 :            :         SNIPPET_CJK_WORDS = 4096
     286                 :            :     };
     287                 :            : 
     288                 :            :     /** Generate a snippet.
     289                 :            :      *
     290                 :            :      *  This method selects a continuous run of words from @a text, based
     291                 :            :      *  mainly on where the query matches (currently terms, exact phrases and
     292                 :            :      *  wildcards are taken into account).  If flag SNIPPET_BACKGROUND_MODEL is
     293                 :            :      *  used (which it is by default) then the selection algorithm also
     294                 :            :      *  considers the non-query terms in the text with the aim of showing
     295                 :            :      *  a context which provides more useful information.
     296                 :            :      *
     297                 :            :      *  The size of the text selected can be controlled by the @a length
     298                 :            :      *  parameter, which specifies a number of bytes of text to aim to select.
     299                 :            :      *  However slightly more text may be selected.  Also the size of any
     300                 :            :      *  escaping, highlighting or omission markers is not considered.
     301                 :            :      *
     302                 :            :      *  The returned text is escaped to make it suitable for use in HTML
     303                 :            :      *  (though beware that in upstream releases 1.4.5 and earlier this
     304                 :            :      *  escaping was sometimes incomplete), and matches with the query will be
     305                 :            :      *  highlighted using @a hi_start and @a hi_end.
     306                 :            :      *
     307                 :            :      *  If the snippet seems to start or end mid-sentence, then @a omit is
     308                 :            :      *  prepended or append (respectively) to indicate this.
     309                 :            :      *
     310                 :            :      *  The same stemming algorithm which was used to build the query should be
     311                 :            :      *  specified in @a stemmer.
     312                 :            :      *
     313                 :            :      *  And @a flags contains flags controlling behaviour.
     314                 :            :      *
     315                 :            :      *  Added in 1.3.5.
     316                 :            :      */
     317                 :            :     std::string snippet(const std::string & text,
     318                 :            :                         size_t length = 500,
     319                 :            :                         const Xapian::Stem & stemmer = Xapian::Stem(),
     320                 :            :                         unsigned flags = SNIPPET_BACKGROUND_MODEL|SNIPPET_EXHAUSTIVE,
     321                 :            :                         const std::string & hi_start = "<b>",
     322                 :            :                         const std::string & hi_end = "</b>",
     323                 :            :                         const std::string & omit = "...") const;
     324                 :            : 
     325                 :            :     /** Prefetch hint a range of items.
     326                 :            :      *
     327                 :            :      *  For a remote database, this may start a pipelined fetch of the
     328                 :            :      *  requested documents from the remote server.
     329                 :            :      *
     330                 :            :      *  For a disk-based database, this may send prefetch hints to the
     331                 :            :      *  operating system such that the disk blocks the requested documents
     332                 :            :      *  are stored in are more likely to be in the cache when we come to
     333                 :            :      *  actually read them.
     334                 :            :      */
     335                 :            :     void fetch(const MSetIterator &begin, const MSetIterator &end) const;
     336                 :            : 
     337                 :            :     /** Prefetch hint a single MSet item.
     338                 :            :      *
     339                 :            :      *  For a remote database, this may start a pipelined fetch of the
     340                 :            :      *  requested documents from the remote server.
     341                 :            :      *
     342                 :            :      *  For a disk-based database, this may send prefetch hints to the
     343                 :            :      *  operating system such that the disk blocks the requested documents
     344                 :            :      *  are stored in are more likely to be in the cache when we come to
     345                 :            :      *  actually read them.
     346                 :            :      */
     347                 :            :     void fetch(const MSetIterator &item) const;
     348                 :            : 
     349                 :            :     /** Prefetch hint the whole MSet.
     350                 :            :      *
     351                 :            :      *  For a remote database, this may start a pipelined fetch of the
     352                 :            :      *  requested documents from the remote server.
     353                 :            :      *
     354                 :            :      *  For a disk-based database, this may send prefetch hints to the
     355                 :            :      *  operating system such that the disk blocks the requested documents
     356                 :            :      *  are stored in are more likely to be in the cache when we come to
     357                 :            :      *  actually read them.
     358                 :            :      */
     359                 :         14 :     void fetch() const { fetch_(0, Xapian::doccount(-1)); }
     360                 :            : 
     361                 :            :     /** Return number of items in this MSet object. */
     362                 :            :     Xapian::doccount size() const;
     363                 :            : 
     364                 :            :     /** Return true if this MSet object is empty. */
     365                 :        480 :     bool empty() const { return size() == 0; }
     366                 :            : 
     367                 :            :     /** Efficiently swap this MSet object with another. */
     368                 :            :     void swap(MSet & o) { internal.swap(o.internal); }
     369                 :            : 
     370                 :            :     /** Return iterator pointing to the first item in this MSet. */
     371                 :            :     MSetIterator begin() const;
     372                 :            : 
     373                 :            :     /** Return iterator pointing to just after the last item in this MSet. */
     374                 :            :     MSetIterator end() const;
     375                 :            : 
     376                 :            :     /** Return iterator pointing to the i-th object in this MSet. */
     377                 :            :     MSetIterator operator[](Xapian::doccount i) const;
     378                 :            : 
     379                 :            :     /** Return iterator pointing to the last object in this MSet. */
     380                 :            :     MSetIterator back() const;
     381                 :            : 
     382                 :            :     /// Return a string describing this object.
     383                 :            :     std::string get_description() const;
     384                 :            : 
     385                 :            :     /** @private @internal MSet is what the C++ STL calls a container.
     386                 :            :      *
     387                 :            :      *  The following typedefs allow the class to be used in templates in the
     388                 :            :      *  same way the standard containers can be.
     389                 :            :      *
     390                 :            :      *  These are deliberately hidden from the Doxygen-generated docs, as the
     391                 :            :      *  machinery here isn't interesting to API users.  They just need to know
     392                 :            :      *  that Xapian container classes are compatible with the STL.
     393                 :            :      *
     394                 :            :      *  See "The C++ Programming Language", 3rd ed. section 16.3.1:
     395                 :            :      */
     396                 :            :     // @{
     397                 :            :     /// @private
     398                 :            :     typedef Xapian::MSetIterator value_type;
     399                 :            :     /// @private
     400                 :            :     typedef Xapian::doccount size_type;
     401                 :            :     /// @private
     402                 :            :     typedef Xapian::doccount_diff difference_type;
     403                 :            :     /// @private
     404                 :            :     typedef Xapian::MSetIterator iterator;
     405                 :            :     /// @private
     406                 :            :     typedef Xapian::MSetIterator const_iterator;
     407                 :            :     /// @private
     408                 :            :     typedef value_type * pointer;
     409                 :            :     /// @private
     410                 :            :     typedef const value_type * const_pointer;
     411                 :            :     /// @private
     412                 :            :     typedef value_type & reference;
     413                 :            :     /// @private
     414                 :            :     typedef const value_type & const_reference;
     415                 :            :     // @}
     416                 :            :     //
     417                 :            :     /** @private @internal MSet is what the C++ STL calls a container.
     418                 :            :      *
     419                 :            :      *  The following methods allow the class to be used in templates in the
     420                 :            :      *  same way the standard containers can be.
     421                 :            :      *
     422                 :            :      *  These are deliberately hidden from the Doxygen-generated docs, as the
     423                 :            :      *  machinery here isn't interesting to API users.  They just need to know
     424                 :            :      *  that Xapian container classes are compatible with the STL.
     425                 :            :      */
     426                 :            :     // @{
     427                 :            :     // The size is fixed once created.
     428                 :            :     Xapian::doccount max_size() const { return size(); }
     429                 :            :     // @}
     430                 :            : };
     431                 :            : 
     432                 :            : /// Iterator over a Xapian::MSet.
     433                 :   66005946 : class XAPIAN_VISIBILITY_DEFAULT MSetIterator {
     434                 :            :     friend class MSet;
     435                 :            : 
     436                 :   32993492 :     MSetIterator(const Xapian::MSet & mset_, Xapian::doccount off_from_end_)
     437                 :   32993492 :         : mset(mset_), off_from_end(off_from_end_) { }
     438                 :            : 
     439                 :            :   public:
     440                 :            :     /** @private @internal The MSet we are iterating over. */
     441                 :            :     Xapian::MSet mset;
     442                 :            : 
     443                 :            :     /** @private @internal The current position of the iterator.
     444                 :            :      *
     445                 :            :      *  We store the offset from the end of @a mset, since that means
     446                 :            :      *  MSet::end() just needs to set this member to 0.
     447                 :            :      */
     448                 :            :     Xapian::MSet::size_type off_from_end;
     449                 :            : 
     450                 :            :     /** Create an unpositioned MSetIterator. */
     451                 :       3898 :     MSetIterator() : off_from_end(0) { }
     452                 :            : 
     453                 :            :     /** Get the numeric document id for the current position. */
     454                 :            :     Xapian::docid operator*() const;
     455                 :            : 
     456                 :            :     /// Advance the iterator to the next position.
     457                 :      76501 :     MSetIterator & operator++() {
     458                 :      76501 :         --off_from_end;
     459                 :      76501 :         return *this;
     460                 :            :     }
     461                 :            : 
     462                 :            :     /// Advance the iterator to the next position (postfix version).
     463                 :       2684 :     MSetIterator operator++(int) {
     464                 :       2684 :         MSetIterator retval = *this;
     465                 :       2684 :         --off_from_end;
     466                 :       2684 :         return retval;
     467                 :            :     }
     468                 :            : 
     469                 :            :     /// Move the iterator to the previous position.
     470                 :         63 :     MSetIterator & operator--() {
     471                 :         63 :         ++off_from_end;
     472                 :         63 :         return *this;
     473                 :            :     }
     474                 :            : 
     475                 :            :     /// Move the iterator to the previous position (postfix version).
     476                 :            :     MSetIterator operator--(int) {
     477                 :            :         MSetIterator retval = *this;
     478                 :            :         ++off_from_end;
     479                 :            :         return retval;
     480                 :            :     }
     481                 :            : 
     482                 :            :     /** @private @internal MSetIterator is what the C++ STL calls an
     483                 :            :      *  random_access_iterator.
     484                 :            :      *
     485                 :            :      *  The following typedefs allow std::iterator_traits<> to work so that
     486                 :            :      *  this iterator can be used with the STL.
     487                 :            :      *
     488                 :            :      *  These are deliberately hidden from the Doxygen-generated docs, as the
     489                 :            :      *  machinery here isn't interesting to API users.  They just need to know
     490                 :            :      *  that Xapian iterator classes are compatible with the STL.
     491                 :            :      */
     492                 :            :     // @{
     493                 :            :     /// @private
     494                 :            :     typedef std::random_access_iterator_tag iterator_category;
     495                 :            :     /// @private
     496                 :            :     typedef std::string value_type;
     497                 :            :     /// @private
     498                 :            :     typedef Xapian::termcount_diff difference_type;
     499                 :            :     /// @private
     500                 :            :     typedef std::string * pointer;
     501                 :            :     /// @private
     502                 :            :     typedef std::string & reference;
     503                 :            :     // @}
     504                 :            : 
     505                 :            :     /// Move the iterator forwards by n positions.
     506                 :            :     MSetIterator & operator+=(difference_type n) {
     507                 :            :         off_from_end -= n;
     508                 :            :         return *this;
     509                 :            :     }
     510                 :            : 
     511                 :            :     /// Move the iterator back by n positions.
     512                 :            :     MSetIterator & operator-=(difference_type n) {
     513                 :            :         off_from_end += n;
     514                 :            :         return *this;
     515                 :            :     }
     516                 :            : 
     517                 :            :     /** Return the iterator incremented by @a n positions.
     518                 :            :      *
     519                 :            :      *  If @a n is negative, decrements by (-n) positions.
     520                 :            :      */
     521                 :            :     MSetIterator operator+(difference_type n) const {
     522                 :            :         return MSetIterator(mset, off_from_end - n);
     523                 :            :     }
     524                 :            : 
     525                 :            :     /** Return the iterator decremented by @a n positions.
     526                 :            :      *
     527                 :            :      *  If @a n is negative, increments by (-n) positions.
     528                 :            :      */
     529                 :            :     MSetIterator operator-(difference_type n) const {
     530                 :            :         return MSetIterator(mset, off_from_end + n);
     531                 :            :     }
     532                 :            : 
     533                 :            :     /** Return the number of positions between @a o and this iterator. */
     534                 :            :     difference_type operator-(const MSetIterator& o) const {
     535                 :            :         return difference_type(o.off_from_end) - difference_type(off_from_end);
     536                 :            :     }
     537                 :            : 
     538                 :            :     /** Return the MSet rank for the current position.
     539                 :            :      *
     540                 :            :      *  The rank of mset[0] is mset.get_firstitem().
     541                 :            :      */
     542                 :         33 :     Xapian::doccount get_rank() const {
     543                 :         33 :         return mset.get_firstitem() + (mset.size() - off_from_end);
     544                 :            :     }
     545                 :            : 
     546                 :            :     /** Get the Document object for the current position. */
     547                 :            :     Xapian::Document get_document() const;
     548                 :            : 
     549                 :            :     /** Get the weight for the current position. */
     550                 :            :     double get_weight() const;
     551                 :            : 
     552                 :            :     /** Return the collapse key for the current position.
     553                 :            :      *
     554                 :            :      *  If collapsing isn't in use, an empty string will be returned.
     555                 :            :      */
     556                 :            :     std::string get_collapse_key() const;
     557                 :            : 
     558                 :            :     /** Return a count of the number of collapses done onto the current key.
     559                 :            :      *
     560                 :            :      *  This starts at 0, and is incremented each time an item is eliminated
     561                 :            :      *  because its key is the same as that of the current item (as returned
     562                 :            :      *  by get_collapse_key()).
     563                 :            :      *
     564                 :            :      *  Note that this is NOT necessarily one less than the total number of
     565                 :            :      *  matching documents with this collapse key due to various optimisations
     566                 :            :      *  implemented in the matcher - for example, it can skip documents
     567                 :            :      *  completely if it can prove their weight wouldn't be enough to make the
     568                 :            :      *  result set.
     569                 :            :      *
     570                 :            :      *  You can say is that if get_collapse_count() > 0 then there are
     571                 :            :      *  >= get_collapse_count() other documents with the current collapse
     572                 :            :      *  key.  But if get_collapse_count() == 0 then there may or may not be
     573                 :            :      *  other such documents.
     574                 :            :      */
     575                 :            :     Xapian::doccount get_collapse_count() const;
     576                 :            : 
     577                 :            :     /** Return the sort key for the current position.
     578                 :            :      *
     579                 :            :      *  If sorting didn't use a key then an empty string will be returned.
     580                 :            :      *
     581                 :            :      *  @since Added in Xapian 1.4.6.
     582                 :            :      */
     583                 :            :     std::string get_sort_key() const;
     584                 :            : 
     585                 :            :     /** Convert the weight of the current iterator position to a percentage.
     586                 :            :      *
     587                 :            :      *  The matching document with the highest weight will get 100% if it
     588                 :            :      *  matches all the weighted query terms, and proportionally less if it
     589                 :            :      *  only matches some, and other weights are scaled by the same factor.
     590                 :            :      *
     591                 :            :      *  Documents with a non-zero score will always score at least 1%.
     592                 :            :      *
     593                 :            :      *  Note that these generally aren't percentages of anything meaningful
     594                 :            :      *  (unless you use a custom weighting formula where they are!)
     595                 :            :      */
     596                 :      48182 :     int get_percent() const {
     597                 :      48182 :         return mset.convert_to_percent(get_weight());
     598                 :            :     }
     599                 :            : 
     600                 :            :     /// Return a string describing this object.
     601                 :            :     std::string get_description() const;
     602                 :            : };
     603                 :            : 
     604                 :            : bool
     605                 :            : XAPIAN_NOTHROW(operator==(const MSetIterator &a, const MSetIterator &b));
     606                 :            : 
     607                 :            : /// Equality test for MSetIterator objects.
     608                 :            : inline bool
     609                 :      84966 : operator==(const MSetIterator &a, const MSetIterator &b) XAPIAN_NOEXCEPT
     610                 :            : {
     611                 :      84966 :     return a.off_from_end == b.off_from_end;
     612                 :            : }
     613                 :            : 
     614                 :            : inline bool
     615                 :            : XAPIAN_NOTHROW(operator!=(const MSetIterator &a, const MSetIterator &b));
     616                 :            : 
     617                 :            : /// Inequality test for MSetIterator objects.
     618                 :            : inline bool
     619                 :      84742 : operator!=(const MSetIterator &a, const MSetIterator &b) XAPIAN_NOEXCEPT
     620                 :            : {
     621                 :      84742 :     return !(a == b);
     622                 :            : }
     623                 :            : 
     624                 :            : bool
     625                 :            : XAPIAN_NOTHROW(operator<(const MSetIterator &a, const MSetIterator &b));
     626                 :            : 
     627                 :            : /// Inequality test for MSetIterator objects.
     628                 :            : inline bool
     629                 :            : operator<(const MSetIterator &a, const MSetIterator &b) XAPIAN_NOEXCEPT
     630                 :            : {
     631                 :            :     return a.off_from_end > b.off_from_end;
     632                 :            : }
     633                 :            : 
     634                 :            : inline bool
     635                 :            : XAPIAN_NOTHROW(operator>(const MSetIterator &a, const MSetIterator &b));
     636                 :            : 
     637                 :            : /// Inequality test for MSetIterator objects.
     638                 :            : inline bool
     639                 :            : operator>(const MSetIterator &a, const MSetIterator &b) XAPIAN_NOEXCEPT
     640                 :            : {
     641                 :            :     return b < a;
     642                 :            : }
     643                 :            : 
     644                 :            : inline bool
     645                 :            : XAPIAN_NOTHROW(operator>=(const MSetIterator &a, const MSetIterator &b));
     646                 :            : 
     647                 :            : /// Inequality test for MSetIterator objects.
     648                 :            : inline bool
     649                 :            : operator>=(const MSetIterator &a, const MSetIterator &b) XAPIAN_NOEXCEPT
     650                 :            : {
     651                 :            :     return !(a < b);
     652                 :            : }
     653                 :            : 
     654                 :            : inline bool
     655                 :            : XAPIAN_NOTHROW(operator<=(const MSetIterator &a, const MSetIterator &b));
     656                 :            : 
     657                 :            : /// Inequality test for MSetIterator objects.
     658                 :            : inline bool
     659                 :            : operator<=(const MSetIterator &a, const MSetIterator &b) XAPIAN_NOEXCEPT
     660                 :            : {
     661                 :            :     return !(b < a);
     662                 :            : }
     663                 :            : 
     664                 :            : /** Return MSetIterator @a it incremented by @a n positions.
     665                 :            :  *
     666                 :            :  *  If @a n is negative, decrements by (-n) positions.
     667                 :            :  */
     668                 :            : inline MSetIterator
     669                 :            : operator+(MSetIterator::difference_type n, const MSetIterator& it)
     670                 :            : {
     671                 :            :     return it + n;
     672                 :            : }
     673                 :            : 
     674                 :            : // Inlined methods of MSet which need MSetIterator to have been defined:
     675                 :            : 
     676                 :            : inline void
     677                 :         14 : MSet::fetch(const MSetIterator &begin_it, const MSetIterator &end_it) const
     678                 :            : {
     679                 :         14 :     fetch_(begin_it.off_from_end, end_it.off_from_end);
     680                 :         14 : }
     681                 :            : 
     682                 :            : inline void
     683                 :          7 : MSet::fetch(const MSetIterator &item) const
     684                 :            : {
     685                 :          7 :     fetch_(item.off_from_end, item.off_from_end);
     686                 :          7 : }
     687                 :            : 
     688                 :            : inline MSetIterator
     689                 :      21297 : MSet::begin() const {
     690                 :      21297 :     return MSetIterator(*this, size());
     691                 :            : }
     692                 :            : 
     693                 :            : inline MSetIterator
     694                 :      84819 : MSet::end() const {
     695                 :            :     // Decrementing the result of end() needs to work, so we must pass in
     696                 :            :     // *this here.
     697                 :      84819 :     return MSetIterator(*this, 0);
     698                 :            : }
     699                 :            : 
     700                 :            : inline MSetIterator
     701                 :   32887341 : MSet::operator[](Xapian::doccount i) const {
     702                 :   32887341 :     return MSetIterator(*this, size() - i);
     703                 :            : }
     704                 :            : 
     705                 :            : inline MSetIterator
     706                 :         35 : MSet::back() const {
     707                 :         35 :     return MSetIterator(*this, 1);
     708                 :            : }
     709                 :            : 
     710                 :            : inline int
     711                 :        307 : MSet::convert_to_percent(const MSetIterator & it) const {
     712                 :        307 :     return convert_to_percent(it.get_weight());
     713                 :            : }
     714                 :            : 
     715                 :            : }
     716                 :            : 
     717                 :            : #endif // XAPIAN_INCLUDED_MSET_H

Generated by: LCOV version 1.11