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.

VSUtil.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 VSUTIL_H
  9. #define VSUTIL_H
  10. #include "modules/search_engine/VisitedSearch.h"
  11. struct WordRank
  12. {
  13. uni_char *word;
  14. float rank;
  15. WordRank(void) {word = NULL; rank = 0.0F;}
  16. WordRank(const uni_char *w, float r)
  17. {
  18. word = uni_strdup(w);
  19. rank = r;
  20. }
  21. BOOL operator<(const WordRank &r) const
  22. {
  23. return this->word < r.word; // anything is fine here
  24. }
  25. static void Destruct(void *wr)
  26. {
  27. if (((WordRank *)wr)->word != NULL)
  28. op_free(((WordRank *)wr)->word);
  29. }
  30. void Destroy()
  31. {
  32. op_free(word);
  33. }
  34. };
  35. class RecordHandleRec : public BSCursor
  36. {
  37. public:
  38. RecordHandleRec(RankIndex *index) : BSCursor(&(index->m_metadata)), m_word_list(&DefDescriptor<WordRank>::Compare, &WordRank::Destruct)
  39. {
  40. m_index = index;
  41. m_word_count = 0;
  42. m_plaintext.SetExpansionPolicy(TempBuffer::AGGRESSIVE);
  43. m_plaintext.SetCachedLengthPolicy(TempBuffer::TRUSTED);
  44. }
  45. ~RecordHandleRec(void)
  46. {
  47. m_word_list.Clear();
  48. }
  49. friend class VisitedSearch;
  50. friend struct FileWord;
  51. protected:
  52. void Invalidate() { m_index = NULL; table = NULL; }
  53. BOOL IsInvalid() { return m_index == NULL; }
  54. RankIndex *m_index;
  55. int m_word_count;
  56. TempBuffer m_plaintext;
  57. TVector<WordRank> m_word_list;
  58. };
  59. struct FileWord : public NonCopyable
  60. {
  61. struct RankRec
  62. {
  63. VisitedSearch::RecordHandle h;
  64. float rank;
  65. RankRec(void)
  66. {
  67. h = NULL;
  68. rank = 0;
  69. }
  70. RankRec(float rank, VisitedSearch::RecordHandle h)
  71. {
  72. this->h = h;
  73. this->rank = rank;
  74. }
  75. BOOL operator<(const RankRec &right) const {return h < right.h;}
  76. };
  77. uni_char *word; // the caches are sorted by the word
  78. TVector<RankRec> *file_ids; // in-memory list of id/rank
  79. UINT32 file_pos; // position where to update file_ids
  80. RankIndex *index; // one of the N indexes to save the data into
  81. FileWord(void)
  82. {
  83. word = NULL;
  84. file_ids = NULL;
  85. file_pos = 0;
  86. index = NULL;
  87. }
  88. ~FileWord(void)
  89. {
  90. if (word != NULL)
  91. OP_DELETEA(word); // op_free(word);
  92. if (file_ids != NULL)
  93. OP_DELETE(file_ids);
  94. }
  95. CHECK_RESULT(OP_STATUS Add(float rank, VisitedSearch::RecordHandle h));
  96. static FileWord *Create(const uni_char *word, float rank, VisitedSearch::RecordHandle h);
  97. static BOOL LessThan(const void *left, const void *right);
  98. };
  99. class CacheIterator : public SearchIterator<VisitedSearch::Result>
  100. {
  101. public:
  102. CacheIterator(TypeDescriptor::ComparePtr sort);
  103. CHECK_RESULT(OP_STATUS Init(const TVector<FileWord *> &cache, const TVector<uni_char *> *words, BOOL prefix_search = FALSE));
  104. CHECK_RESULT(OP_STATUS Handle2Result(VisitedSearch::Result &result, VisitedSearch::RecordHandle handle));
  105. virtual BOOL Next(void) {return (UINT32)++m_pos < m_results.GetCount();}
  106. virtual BOOL Prev(void) {return --m_pos >= 0;}
  107. virtual const VisitedSearch::Result &Get(void) {return m_results.Get(m_pos);}
  108. CHECK_RESULT(virtual OP_STATUS Error(void) const) {return OpStatus::OK;}
  109. virtual int Count(void) const {return m_results.GetCount();}
  110. virtual BOOL End(void) const {return (UINT32)m_pos >= m_results.GetCount();}
  111. virtual BOOL Beginning(void) const {return m_pos < 0;}
  112. protected:
  113. TVector<VisitedSearch::Result> m_results;
  114. int m_pos;
  115. };
  116. #endif // VSUTIL_H