xapian-core  1.4.22
mset.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2015,2016,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/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 
203  SNIPPET_CJK_NGRAM = 2048
204  };
205 
235  std::string snippet(const std::string & text,
236  size_t length = 500,
237  const Xapian::Stem & stemmer = Xapian::Stem(),
238  unsigned flags = SNIPPET_BACKGROUND_MODEL|SNIPPET_EXHAUSTIVE,
239  const std::string & hi_start = "<b>",
240  const std::string & hi_end = "</b>",
241  const std::string & omit = "...") const;
242 
253  void fetch(const MSetIterator &begin, const MSetIterator &end) const;
254 
265  void fetch(const MSetIterator &item) const;
266 
277  void fetch() const { fetch_(0, Xapian::doccount(-1)); }
278 
280  Xapian::doccount size() const;
281 
283  bool empty() const { return size() == 0; }
284 
286  void swap(MSet & o) { internal.swap(o.internal); }
287 
289  MSetIterator begin() const;
290 
292  MSetIterator end() const;
293 
295  MSetIterator operator[](Xapian::doccount i) const;
296 
298  MSetIterator back() const;
299 
301  std::string get_description() const;
302 
314  // @{
326  typedef value_type * pointer;
328  typedef const value_type * const_pointer;
330  typedef value_type & reference;
332  typedef const value_type & const_reference;
333  // @}
334  //
344  // @{
345  // The size is fixed once created.
346  Xapian::doccount max_size() const { return size(); }
347  // @}
348 };
349 
352  friend class MSet;
353 
354  MSetIterator(const Xapian::MSet & mset_, Xapian::doccount off_from_end_)
355  : mset(mset_), off_from_end(off_from_end_) { }
356 
357  public:
360 
367 
369  MSetIterator() : off_from_end(0) { }
370 
372  Xapian::docid operator*() const;
373 
376  --off_from_end;
377  return *this;
378  }
379 
382  MSetIterator retval = *this;
383  --off_from_end;
384  return retval;
385  }
386 
389  ++off_from_end;
390  return *this;
391  }
392 
395  MSetIterator retval = *this;
396  ++off_from_end;
397  return retval;
398  }
399 
410  // @{
412  typedef std::random_access_iterator_tag iterator_category;
414  typedef std::string value_type;
418  typedef std::string * pointer;
420  typedef std::string & reference;
421  // @}
422 
424  MSetIterator & operator+=(difference_type n) {
425  off_from_end -= n;
426  return *this;
427  }
428 
430  MSetIterator & operator-=(difference_type n) {
431  off_from_end += n;
432  return *this;
433  }
434 
439  MSetIterator operator+(difference_type n) const {
440  return MSetIterator(mset, off_from_end - n);
441  }
442 
447  MSetIterator operator-(difference_type n) const {
448  return MSetIterator(mset, off_from_end + n);
449  }
450 
452  difference_type operator-(const MSetIterator& o) const {
453  return difference_type(o.off_from_end) - difference_type(off_from_end);
454  }
455 
461  return mset.get_firstitem() + (mset.size() - off_from_end);
462  }
463 
465  Xapian::Document get_document() const;
466 
468  double get_weight() const;
469 
474  std::string get_collapse_key() const;
475 
493  Xapian::doccount get_collapse_count() const;
494 
501  std::string get_sort_key() const;
502 
514  int get_percent() const {
515  return mset.convert_to_percent(get_weight());
516  }
517 
519  std::string get_description() const;
520 };
521 
522 bool
523 XAPIAN_NOTHROW(operator==(const MSetIterator &a, const MSetIterator &b));
524 
526 inline bool
528 {
529  return a.off_from_end == b.off_from_end;
530 }
531 
532 inline bool
533 XAPIAN_NOTHROW(operator!=(const MSetIterator &a, const MSetIterator &b));
534 
536 inline bool
538 {
539  return !(a == b);
540 }
541 
542 bool
543 XAPIAN_NOTHROW(operator<(const MSetIterator &a, const MSetIterator &b));
544 
546 inline bool
548 {
549  return a.off_from_end > b.off_from_end;
550 }
551 
552 inline bool
553 XAPIAN_NOTHROW(operator>(const MSetIterator &a, const MSetIterator &b));
554 
556 inline bool
558 {
559  return b < a;
560 }
561 
562 inline bool
563 XAPIAN_NOTHROW(operator>=(const MSetIterator &a, const MSetIterator &b));
564 
566 inline bool
568 {
569  return !(a < b);
570 }
571 
572 inline bool
573 XAPIAN_NOTHROW(operator<=(const MSetIterator &a, const MSetIterator &b));
574 
576 inline bool
578 {
579  return !(b < a);
580 }
581 
586 inline MSetIterator
588 {
589  return it + n;
590 }
591 
592 // Inlined methods of MSet which need MSetIterator to have been defined:
593 
594 inline void
595 MSet::fetch(const MSetIterator &begin_it, const MSetIterator &end_it) const
596 {
597  fetch_(begin_it.off_from_end, end_it.off_from_end);
598 }
599 
600 inline void
601 MSet::fetch(const MSetIterator &item) const
602 {
603  fetch_(item.off_from_end, item.off_from_end);
604 }
605 
606 inline MSetIterator
607 MSet::begin() const {
608  return MSetIterator(*this, size());
609 }
610 
611 inline MSetIterator
612 MSet::end() const {
613  // Decrementing the result of end() needs to work, so we must pass in
614  // *this here.
615  return MSetIterator(*this, 0);
616 }
617 
618 inline MSetIterator
620  return MSetIterator(*this, size() - i);
621 }
622 
623 inline MSetIterator
624 MSet::back() const {
625  return MSetIterator(*this, 1);
626 }
627 
628 inline int
630  return convert_to_percent(it.get_weight());
631 }
632 
633 }
634 
635 #endif // XAPIAN_INCLUDED_MSET_H
Xapian::doccount max_size() const
Definition: mset.h:346
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:420
MSetIterator & operator-=(difference_type n)
Move the iterator back by n positions.
Definition: mset.h:430
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:394
bool empty() const
Return true if this MSet object is empty.
Definition: mset.h:283
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:375
Xapian::termcount_diff difference_type
Definition: mset.h:416
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:359
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:318
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
Xapian::MSetIterator iterator
Definition: mset.h:322
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:619
MSetIterator(const Xapian::MSet &mset_, Xapian::doccount off_from_end_)
Definition: mset.h:354
Xapian::MSetIterator value_type
Definition: mset.h:316
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:369
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:351
Define XAPIAN_VISIBILITY_* macros.
MSetIterator operator-(difference_type n) const
Return the iterator decremented by n positions.
Definition: mset.h:447
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:286
MSetIterator begin() const
Return iterator pointing to the first item in this MSet.
Definition: mset.h:607
MSetIterator end() const
Return iterator pointing to just after the last item in this MSet.
Definition: mset.h:612
std::string value_type
Definition: mset.h:414
std::string * pointer
Definition: mset.h:418
std::random_access_iterator_tag iterator_category
Definition: mset.h:412
difference_type operator-(const MSetIterator &o) const
Return the number of positions between o and this iterator.
Definition: mset.h:452
int get_percent() const
Convert the weight of the current iterator position to a percentage.
Definition: mset.h:514
value_type * pointer
Definition: mset.h:326
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:366
XAPIAN_TERMCOUNT_BASE_TYPE termcount_diff
A signed difference between two counts of terms.
Definition: types.h:79
value_type & reference
Definition: mset.h:330
void fetch() const
Prefetch hint the whole MSet.
Definition: mset.h:277
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:381
MSetIterator & operator--()
Move the iterator to the previous position.
Definition: mset.h:388
const value_type & const_reference
Definition: mset.h:332
const value_type * const_pointer
Definition: mset.h:328
Xapian::doccount_diff difference_type
Definition: mset.h:320
MSetIterator & operator+=(difference_type n)
Move the iterator forwards by n positions.
Definition: mset.h:424
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:460
MSetIterator back() const
Return iterator pointing to the last object in this MSet.
Definition: mset.h:624
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:324
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:439