xapian-core  1.4.27
mset.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2015,2016,2019,2023 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/intrusive_ptr.h>
35 #include <xapian/stem.h>
36 #include <xapian/types.h>
37 #include <xapian/visibility.h>
38 
39 namespace Xapian {
40 
41 class MSetIterator;
42 
45  friend class MSetIterator;
46 
47  // Helper function for fetch() methods.
48  void fetch_(Xapian::doccount first, Xapian::doccount last) const;
49 
50  public:
52  class Internal;
55 
60  MSet(const MSet & o);
61 
66  MSet & operator=(const MSet & o);
67 
68 #ifdef XAPIAN_MOVE_SEMANTICS
69  MSet(MSet && o);
71 
73  MSet & operator=(MSet && o);
74 #endif
75 
80  MSet();
81 
83  ~MSet();
84 
96  int convert_to_percent(double weight) const;
97 
109  int convert_to_percent(const MSetIterator & it) const;
110 
119  Xapian::doccount get_termfreq(const std::string & term) const;
120 
126  double get_termweight(const std::string & term) const;
127 
132  Xapian::doccount get_firstitem() const;
133 
135  Xapian::doccount get_matches_lower_bound() const;
137  Xapian::doccount get_matches_estimated() const;
139  Xapian::doccount get_matches_upper_bound() const;
140 
146  Xapian::doccount get_uncollapsed_matches_lower_bound() const;
152  Xapian::doccount get_uncollapsed_matches_estimated() const;
158  Xapian::doccount get_uncollapsed_matches_upper_bound() const;
159 
161  double get_max_attained() const;
163  double get_max_possible() const;
164 
165  enum {
172  SNIPPET_BACKGROUND_MODEL = 1,
179  SNIPPET_EXHAUSTIVE = 2,
186  SNIPPET_EMPTY_WITHOUT_MATCH = 4,
187 
211  SNIPPET_NGRAMS = 2048,
212 
220  SNIPPET_CJK_NGRAM = SNIPPET_NGRAMS
221  };
222 
252  std::string snippet(const std::string & text,
253  size_t length = 500,
254  const Xapian::Stem & stemmer = Xapian::Stem(),
255  unsigned flags = SNIPPET_BACKGROUND_MODEL|SNIPPET_EXHAUSTIVE,
256  const std::string & hi_start = "<b>",
257  const std::string & hi_end = "</b>",
258  const std::string & omit = "...") const;
259 
270  void fetch(const MSetIterator &begin, const MSetIterator &end) const;
271 
282  void fetch(const MSetIterator &item) const;
283 
294  void fetch() const { fetch_(0, Xapian::doccount(-1)); }
295 
297  Xapian::doccount size() const;
298 
300  bool empty() const { return size() == 0; }
301 
303  void swap(MSet & o) { internal.swap(o.internal); }
304 
306  MSetIterator begin() const;
307 
309  MSetIterator end() const;
310 
312  MSetIterator operator[](Xapian::doccount i) const;
313 
315  MSetIterator back() const;
316 
318  std::string get_description() const;
319 
331  // @{
343  typedef value_type * pointer;
345  typedef const value_type * const_pointer;
347  typedef value_type & reference;
349  typedef const value_type & const_reference;
350  // @}
351  //
361  // @{
362  // The size is fixed once created.
363  Xapian::doccount max_size() const { return size(); }
364  // @}
365 };
366 
369  friend class MSet;
370 
371  MSetIterator(const Xapian::MSet & mset_, Xapian::doccount off_from_end_)
372  : mset(mset_), off_from_end(off_from_end_) { }
373 
374  public:
377 
384 
386  MSetIterator() : off_from_end(0) { }
387 
389  Xapian::docid operator*() const;
390 
393  --off_from_end;
394  return *this;
395  }
396 
399  MSetIterator retval = *this;
400  --off_from_end;
401  return retval;
402  }
403 
406  ++off_from_end;
407  return *this;
408  }
409 
412  MSetIterator retval = *this;
413  ++off_from_end;
414  return retval;
415  }
416 
427  // @{
429  typedef std::random_access_iterator_tag iterator_category;
431  typedef std::string value_type;
435  typedef std::string * pointer;
437  typedef std::string & reference;
438  // @}
439 
441  MSetIterator & operator+=(difference_type n) {
442  off_from_end -= n;
443  return *this;
444  }
445 
447  MSetIterator & operator-=(difference_type n) {
448  off_from_end += n;
449  return *this;
450  }
451 
456  MSetIterator operator+(difference_type n) const {
457  return MSetIterator(mset, off_from_end - n);
458  }
459 
464  MSetIterator operator-(difference_type n) const {
465  return MSetIterator(mset, off_from_end + n);
466  }
467 
469  difference_type operator-(const MSetIterator& o) const {
470  return difference_type(o.off_from_end) - difference_type(off_from_end);
471  }
472 
478  return mset.get_firstitem() + (mset.size() - off_from_end);
479  }
480 
482  Xapian::Document get_document() const;
483 
485  double get_weight() const;
486 
491  std::string get_collapse_key() const;
492 
510  Xapian::doccount get_collapse_count() const;
511 
518  std::string get_sort_key() const;
519 
531  int get_percent() const {
532  return mset.convert_to_percent(get_weight());
533  }
534 
536  std::string get_description() const;
537 };
538 
539 bool
540 XAPIAN_NOTHROW(operator==(const MSetIterator &a, const MSetIterator &b));
541 
543 inline bool
545 {
546  return a.off_from_end == b.off_from_end;
547 }
548 
549 inline bool
550 XAPIAN_NOTHROW(operator!=(const MSetIterator &a, const MSetIterator &b));
551 
553 inline bool
555 {
556  return !(a == b);
557 }
558 
559 bool
560 XAPIAN_NOTHROW(operator<(const MSetIterator &a, const MSetIterator &b));
561 
563 inline bool
565 {
566  return a.off_from_end > b.off_from_end;
567 }
568 
569 inline bool
570 XAPIAN_NOTHROW(operator>(const MSetIterator &a, const MSetIterator &b));
571 
573 inline bool
575 {
576  return b < a;
577 }
578 
579 inline bool
580 XAPIAN_NOTHROW(operator>=(const MSetIterator &a, const MSetIterator &b));
581 
583 inline bool
585 {
586  return !(a < b);
587 }
588 
589 inline bool
590 XAPIAN_NOTHROW(operator<=(const MSetIterator &a, const MSetIterator &b));
591 
593 inline bool
595 {
596  return !(b < a);
597 }
598 
603 inline MSetIterator
605 {
606  return it + n;
607 }
608 
609 // Inlined methods of MSet which need MSetIterator to have been defined:
610 
611 inline void
612 MSet::fetch(const MSetIterator &begin_it, const MSetIterator &end_it) const
613 {
614  fetch_(begin_it.off_from_end, end_it.off_from_end);
615 }
616 
617 inline void
618 MSet::fetch(const MSetIterator &item) const
619 {
620  fetch_(item.off_from_end, item.off_from_end);
621 }
622 
623 inline MSetIterator
624 MSet::begin() const {
625  return MSetIterator(*this, size());
626 }
627 
628 inline MSetIterator
629 MSet::end() const {
630  // Decrementing the result of end() needs to work, so we must pass in
631  // *this here.
632  return MSetIterator(*this, 0);
633 }
634 
635 inline MSetIterator
637  return MSetIterator(*this, size() - i);
638 }
639 
640 inline MSetIterator
641 MSet::back() const {
642  return MSetIterator(*this, 1);
643 }
644 
645 inline int
647  return convert_to_percent(it.get_weight());
648 }
649 
650 }
651 
652 #endif // XAPIAN_INCLUDED_MSET_H
Xapian::doccount max_size() const
Definition: mset.h:363
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
Xapian::doccount size() const
Return number of items in this MSet object.
Definition: omenquire.cc:318
std::string & reference
Definition: mset.h:437
MSetIterator & operator-=(difference_type n)
Move the iterator back by n positions.
Definition: mset.h:447
bool operator>=(const ESetIterator &a, const ESetIterator &b)
Inequality test for ESetIterator objects.
Definition: eset.h:317
typedefs for Xapian
int convert_to_percent(double weight) const
Convert a weight to a percentage.
Definition: omenquire.cc:198
Class representing a stemming algorithm.
Definition: stem.h:62
double weight
The weight of a document or term.
Definition: types.h:122
MSetIterator operator--(int)
Move the iterator to the previous position (postfix version).
Definition: mset.h:411
bool empty() const
Return true if this MSet object is empty.
Definition: mset.h:300
Xapian::Internal::intrusive_ptr< Internal > internal
Definition: mset.h:52
Compiler attribute macros.
MSetIterator & operator++()
Advance the iterator to the next position.
Definition: mset.h:392
Xapian::termcount_diff difference_type
Definition: mset.h:433
bool operator!=(const ESetIterator &a, const ESetIterator &b)
Inequality test for ESetIterator objects.
Definition: eset.h:287
Class representing a list of search results.
Definition: mset.h:44
Xapian::MSet mset
Definition: mset.h:376
bool operator==(const ESetIterator &a, const ESetIterator &b)
Equality test for ESetIterator objects.
Definition: eset.h:277
static Xapian::Stem stemmer
Definition: stemtest.cc:41
Xapian::doccount size_type
Definition: mset.h:335
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
Xapian::MSetIterator iterator
Definition: mset.h:339
bool operator<(const ESetIterator &a, const ESetIterator &b)
Inequality test for ESetIterator objects.
Definition: eset.h:297
MSetIterator operator[](Xapian::doccount i) const
Return iterator pointing to the i-th object in this MSet.
Definition: mset.h:636
MSetIterator(const Xapian::MSet &mset_, Xapian::doccount off_from_end_)
Definition: mset.h:371
Xapian::MSetIterator value_type
Definition: mset.h:333
Xapian::doccount get_firstitem() const
Rank of first item in this MSet.
Definition: omenquire.cc:239
bool operator<=(const ESetIterator &a, const ESetIterator &b)
Inequality test for ESetIterator objects.
Definition: eset.h:327
MSetIterator()
Create an unpositioned MSetIterator.
Definition: mset.h:386
XAPIAN_DOCID_BASE_TYPE doccount_diff
A signed difference between two counts of documents.
Definition: types.h:45
Iterator over a Xapian::MSet.
Definition: mset.h:368
Define XAPIAN_VISIBILITY_* macros.
MSetIterator operator-(difference_type n) const
Return the iterator decremented by n positions.
Definition: mset.h:464
bool operator>(const ESetIterator &a, const ESetIterator &b)
Inequality test for ESetIterator objects.
Definition: eset.h:307
void swap(MSet &o)
Efficiently swap this MSet object with another.
Definition: mset.h:303
MSetIterator begin() const
Return iterator pointing to the first item in this MSet.
Definition: mset.h:624
MSetIterator end() const
Return iterator pointing to just after the last item in this MSet.
Definition: mset.h:629
std::string value_type
Definition: mset.h:431
std::string * pointer
Definition: mset.h:435
std::random_access_iterator_tag iterator_category
Definition: mset.h:429
difference_type operator-(const MSetIterator &o) const
Return the number of positions between o and this iterator.
Definition: mset.h:469
int get_percent() const
Convert the weight of the current iterator position to a percentage.
Definition: mset.h:531
value_type * pointer
Definition: mset.h:343
double get_weight() const
Get the weight for the current position.
Definition: omenquire.cc:460
Xapian::MSet::size_type off_from_end
Definition: mset.h:383
XAPIAN_TERMCOUNT_BASE_TYPE termcount_diff
A signed difference between two counts of terms.
Definition: types.h:79
value_type & reference
Definition: mset.h:347
void fetch() const
Prefetch hint the whole MSet.
Definition: mset.h:294
const Query operator*(double factor, const Query &q)
Scale a Xapian::Query object using OP_SCALE_WEIGHT.
Definition: query.h:670
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
ESetIterator operator+(ESetIterator::difference_type n, const ESetIterator &it)
Return ESetIterator it incremented by n positions.
Definition: eset.h:337
MSetIterator operator++(int)
Advance the iterator to the next position (postfix version).
Definition: mset.h:398
MSetIterator & operator--()
Move the iterator to the previous position.
Definition: mset.h:405
const value_type & const_reference
Definition: mset.h:349
const value_type * const_pointer
Definition: mset.h:345
Xapian::doccount_diff difference_type
Definition: mset.h:337
MSetIterator & operator+=(difference_type n)
Move the iterator forwards by n positions.
Definition: mset.h:441
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
API for working with documents.
Xapian::doccount get_rank() const
Return the MSet rank for the current position.
Definition: mset.h:477
MSetIterator back() const
Return iterator pointing to the last object in this MSet.
Definition: mset.h:641
A smart pointer that uses intrusive reference counting.
Definition: intrusive_ptr.h:81
stemming algorithms
#define XAPIAN_NOEXCEPT
Definition: attributes.h:39
Xapian::MSetIterator const_iterator
Definition: mset.h:341
A handle representing a document in a Xapian database.
Definition: document.h:61
MSetIterator operator+(difference_type n) const
Return the iterator incremented by n positions.
Definition: mset.h:456