ddc
QueryResult.h
Go to the documentation of this file.
1 //*-*- Mode: C++ -*- */
2 //
3 // DDC originally by Alexey Sokirko
4 // Changes and modifications 2011-2016 by Bryan Jurish (formerly in ConcHolder.h)
5 //
6 // This file is part of DDC.
7 //
8 // DDC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // DDC is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with DDC. If not, see <http://www.gnu.org/licenses/>.
20 //
21 #ifndef DDC_QUERY_RESULT_H
22 #define DDC_QUERY_RESULT_H
23 
24 #include "ConcCommon.h"
25 
26 //==============================================================================
27 // Typedefs
28 
30 typedef map<string,size_t> CCountMap;
31 
32 //==============================================================================
33 // Class Definitions
34 
35 //----------------------------------------------------------------------
43 struct CQueryResult
44 {
46  vector<CHit> m_Hits;
47 
49  vector<CTokenNo> m_HighlightOccurs;
50 
52  vector<BYTE> m_HighlightIds;
53 
56 
59 
61  bool m_bSortByString : 1;
62 
64  bool m_bPrune : 1;
65 
68 
70  mutable vector<BYTE> m_DebugInfo;
71 
73  void ClearQueryResults();
74 
78  template <typename HitIndexLessThanT>
79  inline void SortResultsByIndex(HitIndexLessThanT& HitIndexLess);
80 
84  template <typename HitLessThanT>
85  inline void SortResultsByHit(HitLessThanT& HitLess);
86 
90  template <typename HitIndexLessThanT>
91  inline void SortResultsByIndexP(HitIndexLessThanT HitIndexLess)
92  { SortResultsByIndex(HitIndexLess); };
93 
97  template <typename HitLessThanT>
98  inline void SortResultsByHitP(HitLessThanT HitLess)
99  { SortResultsByHit(HitLess); };
100 };
101 
102 //----------------------------------------------------------------------
106 template <typename HitLessThanT> struct CResultHitLess
107 {
110 
112  HitLessThanT& m_HitLess;
113 
115  CResultHitLess(const CQueryResult& Result, HitLessThanT& HitLess)
116  : m_Result(Result), m_HitLess(HitLess)
117  {};
118 
120  inline bool operator() (size_t i1, size_t i2) const
121  { return m_HitLess(m_Result.m_Hits[i1], m_Result.m_Hits[i2]); }
122 };
123 
124 
125 //----------------------------------------------------------------------
130 {
133 
135  vector<size_t>* m_pHitIndex;
136 
139 
142  : m_Result(Result), m_pHitIndex(NULL), m_bLocalIndex(false)
143  { Reset(); }
144 
147  CQueryResultIndex(const CQueryResult& Result, vector<size_t>* HitIndex)
148  : m_Result(Result), m_pHitIndex(HitIndex), m_bLocalIndex(false)
149  {};
150 
153  CQueryResultIndex(const CQueryResult& Result, vector<size_t>& HitIndex)
154  : m_Result(Result), m_pHitIndex(&HitIndex), m_bLocalIndex(false)
155  {};
156 
159  { if (m_bLocalIndex) delete m_pHitIndex; };
160 
162  void Reset(void);
163 
165  inline size_t size(void) const
166  { return m_pHitIndex==NULL ? 0 : m_pHitIndex->size(); };
167 
171  template <typename HitIndexLessThanT>
172  inline void SortByIndex(HitIndexLessThanT& HitIndexLess)
173  { stable_sort(m_pHitIndex->begin(), m_pHitIndex->end(), HitIndexLess); };
174 
178  template <typename HitLessThanT>
179  inline void SortByHit(HitLessThanT& HitLess)
180  {
181  CResultHitLess<HitLessThanT> ResultLess(m_Result, HitLess);
182  SortByIndex(ResultLess);
183  };
184 
189  void Apply(CQueryResult &Dst, size_t Begin=0, size_t End=(size_t)-1);
190 };
191 
192 //==============================================================================
193 // Template instantiations
194 
195 //----------------------------------------------------------------------
196 // CQueryResult
197 
198 //-- hit-sorting: result: by index: guts
199 template <typename HitIndexLessThanT>
200 inline void CQueryResult::SortResultsByIndex(HitIndexLessThanT& HitIndexLess)
201 {
202  CQueryResultIndex ResultIndex(*this);
203  ResultIndex.SortByIndex(HitIndexLess);
204  ResultIndex.Apply(*this);
205 };
206 
207 //-- hit-sorting: result: by hit: guts
208 template <typename HitLessThanT>
209 inline void CQueryResult::SortResultsByHit(HitLessThanT& HitLess)
210 {
211  CQueryResultIndex ResultIndex(*this);
212  ResultIndex.SortByHit(HitLess);
213  ResultIndex.Apply(*this);
214 };
215 
216 
217 
218 #endif /* DDC_QUERY_RESULT_H */
219 
220 /*--- emacs style variables ---
221  * Local Variables:
222  * mode: C++
223  * c-file-style: "ellemtel"
224  * c-basic-offset: 4
225  * tab-width: 8
226  * indent-tabs-mode: nil
227  * End:
228  */
size_t size(void) const
Wrapper for m_pHitIndex->size()
Definition: QueryResult.h:165
HitLessThanT & m_HitLess
hit comparison operator
Definition: QueryResult.h:112
A file for globally defined constants and classes.
HitSortOrderEnum
Definition: ConcCommon.h:172
CQueryResultIndex(const CQueryResult &Result)
Default constructor creates new trivial hit index.
Definition: QueryResult.h:141
void SortResultsByHit(HitLessThanT &HitLess)
Definition: QueryResult.h:209
bool m_bSortByString
whether to sort by string-value
Definition: QueryResult.h:61
~CQueryResultIndex(void)
Destructor.
Definition: QueryResult.h:158
CQueryResult: query results, possibly aggregated by "break".
Definition: QueryResult.h:43
void SortResultsByIndexP(HitIndexLessThanT HitIndexLess)
Definition: QueryResult.h:91
vector< CTokenNo > m_HighlightOccurs
words that should be highlighted in hits; m_HighlightOccurs is the concatenation of CQueryNode::m_Occ...
Definition: QueryResult.h:49
bool m_bPrune
whether primary sort is a prune-sort
Definition: QueryResult.h:64
CResultHitLess(const CQueryResult &Result, HitLessThanT &HitLess)
default constructor
Definition: QueryResult.h:115
void SortResultsByIndex(HitIndexLessThanT &HitIndexLess)
Definition: QueryResult.h:200
size_t m_AllHitsCount
the number of all found hits (if m_AllHitsCount < m_pConcordance->m_MaxCachedHitsCount, then m_Hits.size() == m_AllHitsCount)
Definition: QueryResult.h:55
vector< CHit > m_Hits
found hits (not more than m_pConcordance->m_MaxCachedHitsCount).
Definition: QueryResult.h:46
HitSortOrderEnum m_SortOrder
hit sort order
Definition: QueryResult.h:67
void SortByHit(HitLessThanT &HitLess)
Definition: QueryResult.h:179
CQueryResultIndex(const CQueryResult &Result, vector< size_t > *HitIndex)
Definition: QueryResult.h:147
size_t m_RelevantDocumentCount
the number of documents, where at least one hit is found
Definition: QueryResult.h:58
void SortByIndex(HitIndexLessThanT &HitIndexLess)
Definition: QueryResult.h:172
vector< BYTE > m_HighlightIds
highlighting match-ids for m_HighlightOccurs
Definition: QueryResult.h:52
vector< BYTE > m_DebugInfo
?
Definition: QueryResult.h:70
map< string, size_t > CCountMap
type for count-query maps
Definition: QueryResult.h:30
void ClearQueryResults()
clears CQueryResult fields
Definition: QueryResult.cpp:27
const CQueryResult & m_Result
result-set containing hits to be indexed/sorted
Definition: QueryResult.h:132
CQueryResultIndex(const CQueryResult &Result, vector< size_t > &HitIndex)
Definition: QueryResult.h:153
template for converting hit-comparison functions to index-comparisons for CQueryResult sorting ...
Definition: QueryResult.h:106
vector< size_t > * m_pHitIndex
hit index: elements are indices into m_Result.m_Hits, sorted in "logical" order
Definition: QueryResult.h:135
temporary type for (re-)sorting CQueryResult hits
Definition: QueryResult.h:129
void SortResultsByHitP(HitLessThanT HitLess)
Definition: QueryResult.h:98
bool m_bLocalIndex
whether m_pHitIndex was created locally (if true, it will be destroyed with this object) ...
Definition: QueryResult.h:138
const CQueryResult & m_Result
result hit-set with respect to which comparisons will be executed
Definition: QueryResult.h:109
void Apply(CQueryResult &Dst, size_t Begin=0, size_t End=(size_t) -1)
Definition: QueryResult.cpp:57