Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VSIterator.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. **
  3. ** Copyright (C) 1995-2011 Opera Software ASA. All rights reserved.
  4. **
  5. ** This file is part of the Opera web browser.
  6. ** It may not be distributed under any circumstances.
  7. */
  8. #ifndef VSITERATOR_H
  9. #define VSITERATOR_H
  10. #include "modules/search_engine/VisitedSearch.h"
  11. /**
  12. * Returns all documents sorted by time.
  13. */
  14. class AllDocIterator : public SearchIterator<VisitedSearch::Result>
  15. {
  16. public:
  17. AllDocIterator(RankIndex *index) : m_result(&VisitedSearch::Result::DeleteResult)
  18. {
  19. m_index = index;
  20. m_result_pos = -1;
  21. m_status = OpStatus::ERR;
  22. }
  23. CHECK_RESULT(OP_STATUS Init(void));
  24. virtual BOOL Next(void);
  25. virtual BOOL Prev(void);
  26. virtual const VisitedSearch::Result &Get(void)
  27. {
  28. return m_result.Get(m_result_pos);
  29. }
  30. CHECK_RESULT(virtual OP_STATUS Error(void) const) {return m_status;}
  31. virtual int Count(void) const {return End() ? (int)m_result.GetCount() : -1;}
  32. virtual BOOL End(void) const
  33. {
  34. return (unsigned)m_result_pos >= m_result.GetCount();
  35. }
  36. virtual BOOL Beginning(void) const
  37. {
  38. return m_result_pos < 0 && m_result.GetCount() > 0;
  39. }
  40. protected:
  41. RankIndex *m_index;
  42. TVector<VisitedSearch::Result> m_result;
  43. int m_result_pos;
  44. OP_STATUS m_status;
  45. };
  46. class QueryIteratorBase : public AllDocIterator
  47. {
  48. public:
  49. QueryIteratorBase(RankIndex *index) : AllDocIterator(index) {}
  50. CHECK_RESULT(virtual OP_STATUS AddWord(UINT32 pos)) = 0;
  51. };
  52. /**
  53. * Returns documents sorted by ranking.
  54. */
  55. class RankIterator : public QueryIteratorBase
  56. {
  57. public:
  58. RankIterator(RankIndex *index) : QueryIteratorBase(index),
  59. m_rank_vec(PtrDescriptor< TVector<RankId> >()), m_id_vec(PtrDescriptor< TVector<RankId> >())
  60. {
  61. m_line = 0;
  62. m_max_line = (unsigned)-1;
  63. }
  64. CHECK_RESULT(virtual OP_STATUS AddWord(UINT32 pos));
  65. virtual BOOL Next(void);
  66. virtual int Count(void) const
  67. {
  68. if (m_rank_vec.GetCount() == 0)
  69. return AllDocIterator::Count();
  70. if (End())
  71. return (int)m_result.GetCount();
  72. return m_max_line;
  73. }
  74. virtual BOOL End(void) const
  75. {
  76. if (m_rank_vec.GetCount() == 0)
  77. return AllDocIterator::End();
  78. return m_line >= m_max_line && (unsigned)m_result_pos >= m_result.GetCount();
  79. }
  80. protected:
  81. TVector<TVector<RankId> *> m_rank_vec;
  82. TVector<TVector<RankId> *> m_id_vec;
  83. unsigned m_line;
  84. unsigned m_max_line;
  85. };
  86. /**
  87. * Returns documents sorted by time of being seen.
  88. */
  89. class TimeIterator : public QueryIteratorBase
  90. {
  91. public:
  92. TimeIterator(RankIndex *index) : QueryIteratorBase(index), m_id_vec(PtrDescriptor< TVector<RankId> >()) {}
  93. CHECK_RESULT(virtual OP_STATUS AddWord(UINT32 pos));
  94. virtual BOOL Next(void);
  95. protected:
  96. TVector<TVector<RankId> *> m_id_vec;
  97. };
  98. /**
  99. * Join results from all subindexes.
  100. * I would have to think to tell if calling Prev() after Next() will lead to the same position if multiple items had the same ranking.
  101. */
  102. class MultiOrIterator : public SearchIterator<VisitedSearch::Result>
  103. {
  104. public:
  105. MultiOrIterator(void) :
  106. subindex(&PtrDescriptor<SearchIterator<VisitedSearch::Result> >::Destruct), Compare(NULL)
  107. {}
  108. MultiOrIterator(TVector<SearchIterator<VisitedSearch::Result> *> &feeder, TypeDescriptor::ComparePtr compare) :
  109. subindex(&PtrDescriptor<SearchIterator<VisitedSearch::Result> >::Destruct)
  110. {
  111. Set(feeder, compare);
  112. }
  113. void Set(TVector<SearchIterator<VisitedSearch::Result> *> &feeder, TypeDescriptor::ComparePtr compare)
  114. {
  115. subindex.TakeOver(feeder);
  116. Compare = compare;
  117. }
  118. virtual BOOL Next(void);
  119. virtual BOOL Prev(void);
  120. virtual const VisitedSearch::Result &Get(void);
  121. CHECK_RESULT(virtual OP_STATUS Error(void) const);
  122. virtual int Count(void) const;
  123. virtual BOOL End(void) const;
  124. virtual BOOL Beginning(void) const;
  125. protected:
  126. TVector<SearchIterator<VisitedSearch::Result> *> subindex;
  127. TypeDescriptor::ComparePtr Compare;
  128. };
  129. /**
  130. * ranking is not taken into account for performance reasons
  131. */
  132. class FastPrefixIterator : public SearchIterator<VisitedSearch::Result>
  133. {
  134. public:
  135. FastPrefixIterator(RankIndex *index) :
  136. m_full_words(PtrDescriptor< TVector<RankId> >()),
  137. m_result(TypeDescriptor(sizeof(VisitedSearch::Result),
  138. &VisitedSearch::Result::Assign,
  139. &VisitedSearch::Result::CompareId,
  140. &VisitedSearch::Result::DeleteResult
  141. #ifdef ESTIMATE_MEMORY_USED_AVAILABLE
  142. , &VisitedSearch::Result::EstimateMemoryUsed
  143. #endif
  144. )),
  145. m_current_prefix(&RankId::CompareId)
  146. {
  147. m_index = index;
  148. m_prefixes = NULL;
  149. m_prefix_data_size = -1;
  150. m_result_pos = -1;
  151. m_status = OpStatus::ERR;
  152. }
  153. virtual ~FastPrefixIterator(void);
  154. CHECK_RESULT(OP_STATUS AddWord(UINT32 pos));
  155. void SetPrefix(SearchIterator<ACT::PrefixResult> *prefix_iterator);
  156. CHECK_RESULT(OP_STATUS Init(void)) {Next(); return Error();}
  157. virtual BOOL Next(void);
  158. virtual BOOL Prev(void);
  159. CHECK_RESULT(virtual OP_STATUS Error(void) const);
  160. virtual int Count(void) const;
  161. virtual BOOL End(void) const;
  162. virtual BOOL Beginning(void) const;
  163. virtual const VisitedSearch::Result &Get(void);
  164. protected:
  165. RankIndex *m_index;
  166. TVector<TVector<RankId> *> m_full_words;
  167. TVector<VisitedSearch::Result> m_result;
  168. TVector<UINT32> m_rejects;
  169. SearchIterator<ACT::PrefixResult> *m_prefixes;
  170. TVector<RankId> m_current_prefix;
  171. int m_prefix_data_size;
  172. int m_result_pos;
  173. OP_STATUS m_status;
  174. };
  175. class ChainIterator : public SearchIterator<VisitedSearch::Result>
  176. {
  177. public:
  178. ChainIterator(void) :
  179. m_words(&PtrDescriptor<uni_char>::DestructArray), m_chain(&PtrDescriptor<FastPrefixIterator>::Destruct)
  180. {
  181. m_current_iterator = 0;
  182. }
  183. CHECK_RESULT(OP_STATUS Init(TVector<uni_char *> &words, const TVector<RankIndex *> &indexes, SearchIterator<VisitedSearch::Result> *cache_iterator, BOOL prefix_search));
  184. virtual BOOL Next(void);
  185. virtual BOOL Prev(void);
  186. CHECK_RESULT(virtual OP_STATUS Error(void) const);
  187. virtual int Count(void) const;
  188. virtual BOOL End(void) const;
  189. virtual BOOL Beginning(void) const;
  190. virtual const VisitedSearch::Result &Get(void);
  191. protected:
  192. TVector<uni_char *> m_words;
  193. TVector<SearchIterator<VisitedSearch::Result> *> m_chain;
  194. int m_current_iterator;
  195. };
  196. #endif // VSITERATOR_H