~ubuntu-branches/ubuntu/wily/clucene-core/wily-proposed

« back to all changes in this revision

Viewing changes to src/CLucene/search/Sort.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-02-21 08:43:54 UTC
  • mfrom: (3.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20080221084354-bzl2ied6qgvwwfr8
Tags: 0.9.20-3
* Lower compat/debhelper to 5.
* Re-enable static library build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
CL_NS_DEF(search)
13
13
 
14
14
 
15
 
                
16
 
  /** Represents sorting by document score (relevancy). */
17
15
  SortField* SortField::FIELD_SCORE = _CLNEW SortField (NULL, DOCSCORE,false);
18
 
 
19
 
  /** Represents sorting by document number (index order). */
20
16
  SortField* SortField::FIELD_DOC = _CLNEW SortField (NULL, DOC,false);
21
 
 
22
 
  
23
 
        /** Represents sorting by computed relevance. Using this sort criteria
24
 
         * returns the same results as calling {@link Searcher#search(Query) Searcher#search()}
25
 
         * without a sort criteria, only with slightly more overhead. */
26
 
   Sort* Sort::RELEVANCE = _CLNEW Sort();
27
 
 
28
 
        /** Represents sorting by index order. */
29
 
   Sort* Sort::INDEXORDER = _CLNEW Sort (SortField::FIELD_DOC);
30
 
 
31
 
 
32
 
 
33
 
 
34
 
   /** Creates a sort by terms in the given field where the type of term value
35
 
   * is determined dynamically ({@link #AUTO AUTO}).
36
 
   * @param field Name of field to sort by, cannot be <code>null</code>.
37
 
   */
 
17
  Sort* Sort::RELEVANCE = _CLNEW Sort();
 
18
  Sort* Sort::INDEXORDER = _CLNEW Sort (SortField::FIELD_DOC);
 
19
 
 
20
 
 
21
 
 
22
 
38
23
  SortField::SortField (const TCHAR* field) {
39
24
     this->type = AUTO;
40
25
     this->reverse = false;
42
27
         this->factory = NULL;
43
28
  }
44
29
 
45
 
  /** Creates a sort, possibly in reverse, by terms in the given field where
46
 
   * the type of term value is determined dynamically ({@link #AUTO AUTO}).
47
 
   * @param field Name of field to sort by, cannot be <code>null</code>.
48
 
   * @param reverse True if natural order should be reversed.
49
 
   
50
 
  SortField::SortField (const TCHAR* field, bool reverse) {
51
 
    this->field = CLStringIntern::intern(field  CL_FILELINE);
52
 
    this->reverse = reverse;
53
 
     this->type = AUTO;
54
 
         this->factory = NULL;
55
 
  }*/
56
 
 
57
 
 
58
 
  /** Creates a sort, possibly in reverse, by terms in the given field with the
59
 
   * type of term values explicitly given.
60
 
   * @param field  Name of field to sort by.  Can be <code>null</code> if
61
 
   *               <code>type</code> is SCORE or DOC.
62
 
   * @param type   Type of values in the terms.
63
 
   * @param reverse True if natural order should be reversed (default=false).
64
 
   */
65
30
  SortField::SortField (const TCHAR* field, int32_t type, bool reverse) {
66
31
    this->field = (field != NULL) ? CLStringIntern::intern(field  CL_FILELINE) : field;
67
32
    this->type = type;
103
68
  }*/
104
69
 
105
70
 
106
 
  /** Creates a sort, possibly in reverse, with a custom comparison function.
107
 
   * @param field Name of field to sort by; cannot be <code>null</code>.
108
 
   * @param comparator Returns a comparator for sorting hits.
109
 
   * @param reverse True if natural order should be reversed (default=false).
110
 
   */
111
71
  SortField::SortField (const TCHAR* field, SortComparatorSource* comparator, bool reverse) {
112
72
    this->field = (field != NULL) ? CLStringIntern::intern(field  CL_FILELINE): field;
113
73
    this->type = CUSTOM;
119
79
          CLStringIntern::unintern(field);
120
80
  }
121
81
  
122
 
  const TCHAR* SortField::toString() const {
 
82
  TCHAR* SortField::toString() const {
123
83
        CL_NS(util)::StringBuffer buffer;
124
84
    switch (type) {
125
85
      case DOCSCORE: buffer.append(_T("<score>"));
158
118
 
159
119
 
160
120
 
161
 
 
162
 
   /** Sorts by computed relevance.  This is the same sort criteria as
163
 
         * calling {@link Searcher#search(Query) Searcher#search()} without a sort criteria, only with
164
 
         * slightly more overhead. */
165
121
        Sort::Sort() {
166
122
                fields=NULL;
167
123
                SortField** fields=_CL_NEWARRAY(SortField*,3);
188
144
                        _CLDELETE_ARRAY(fields);
189
145
                }
190
146
        }
191
 
 
192
 
        /** Sorts possibly in reverse by the terms in <code>field</code> then by
193
 
         * index order (document number). The type of value in <code>field</code> is determined
194
 
         * automatically.
195
 
         * @see SortField#AUTO
196
 
         */
 
147
        
197
148
        Sort::Sort (const TCHAR* field, bool reverse) {
198
149
                this->fields=NULL;
199
150
                setSort (field, reverse);
200
151
        }
201
152
 
202
 
 
203
 
        /** Sorts in succession by the terms in each field.
204
 
         * The type of value in <code>field</code> is determined
205
 
         * automatically.
206
 
         * @see SortField#AUTO
207
 
         */
208
153
        Sort::Sort (const TCHAR** fields) {
209
154
                this->fields=NULL;
210
155
                setSort (fields);
211
156
        }
212
 
 
213
 
 
214
 
        /** Sorts by the criteria in the given SortField. */
215
157
        Sort::Sort (SortField* field) {
216
158
                this->fields=NULL;
217
159
                setSort (field);
218
160
        }
219
161
 
220
 
 
221
 
        /** Sorts in succession by the criteria in each SortField. */
222
162
        Sort::Sort (SortField** fields) {
223
163
                this->fields=NULL;
224
164
                setSort (fields);
225
165
        }
226
166
 
227
 
 
228
 
        /** Sets the sort to the terms in <code>field</code> possibly in reverse,
229
 
         * then by index order (document number). */
230
167
        void Sort::setSort (const TCHAR* field, bool reverse) {
231
168
                clear();
232
169
                fields = _CL_NEWARRAY(SortField*,3);
235
172
                fields[2] = NULL;
236
173
        }
237
174
 
238
 
 
239
 
        /** Sets the sort to the terms in each field in succession. */
240
175
        void Sort::setSort (const TCHAR** fieldnames) {
241
176
                clear();
242
177
 
252
187
        }
253
188
 
254
189
 
255
 
        /** Sets the sort to the given criteria. */
256
190
        void Sort::setSort (SortField* field) {
257
191
                clear();
258
192
 
261
195
                this->fields[1] = NULL;
262
196
        }
263
197
 
264
 
 
265
 
        /** Sets the sort to the given criteria in succession. */
266
198
        void Sort::setSort (SortField** fields) {
267
199
                clear();
268
200
        
274
206
            this->fields[i]=fields[i];
275
207
        }
276
208
 
277
 
        const TCHAR* Sort::toString() const {
 
209
        TCHAR* Sort::toString() const {
278
210
                CL_NS(util)::StringBuffer buffer;
279
211
 
280
212
                int32_t i = 0;
281
213
                while ( fields[i] != NULL ){
282
 
                if (i>0)
283
 
                        buffer.appendChar(',');
 
214
                        if (i>0)
 
215
                                buffer.appendChar(',');
284
216
 
285
 
                const TCHAR* p = fields[i]->toString();
286
 
                buffer.append(p);
287
 
                _CLDELETE_CARRAY(p);
288
 
              
289
 
                i++;
 
217
                        const TCHAR* p = fields[i]->toString();
 
218
                        buffer.append(p);
 
219
                        _CLDELETE_CARRAY(p);
 
220
                          
 
221
                        i++;
290
222
                }
291
223
 
292
224
                return buffer.toString();
301
233
 
302
234
  ScoreDocComparator::~ScoreDocComparator(){
303
235
  }
304
 
        
305
 
  // inherit javadocs
306
 
  /*ScoreDocComparator* SortComparator::newComparator (CL_NS(index)::IndexReader* reader, TCHAR* fieldname){
307
 
    TCHAR* field = CLStringIntern::intern(fieldname);
 
236
 
 
237
 
 
238
class ScoreDocComparatorImpl: public ScoreDocComparator{
 
239
    Comparable** cachedValues;
 
240
        FieldCacheAuto* fca;
 
241
        int32_t cachedValuesLen;
 
242
public:
 
243
        ScoreDocComparatorImpl(FieldCacheAuto* fca){
 
244
                this->fca = fca;
 
245
                if ( fca->contentType != FieldCacheAuto::COMPARABLE_ARRAY )
 
246
                _CLTHROWA(CL_ERR_InvalidCast,"Invalid field cache auto type");
 
247
                this->cachedValues = fca->comparableArray;
 
248
                this->cachedValuesLen = fca->contentLen;
 
249
        }
 
250
        ~ScoreDocComparatorImpl(){
 
251
        }
 
252
        int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j){
 
253
                CND_PRECONDITION(i->doc >= 0 && i->doc < cachedValuesLen, "i->doc out of range")
 
254
                CND_PRECONDITION(j->doc >= 0 && j->doc < cachedValuesLen, "j->doc out of range")
 
255
                return cachedValues[i->doc]->compareTo (cachedValues[j->doc]);
 
256
        }
 
257
 
 
258
        CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i){
 
259
                CND_PRECONDITION(i->doc >= 0 && i->doc < cachedValuesLen, "i->doc out of range")
 
260
                return cachedValues[i->doc];
 
261
        }
 
262
 
 
263
        int32_t sortType(){
 
264
                return SortField::CUSTOM;
 
265
        }
 
266
};
308
267
    
309
 
        Comparable** cachedValues = FieldCache::DEFAULT().getCustom (reader, field, _this);
310
 
    return _CLNEW ScoreDocComparator(cachedValues);
311
 
  }*/
312
 
 
313
 
 
314
 
        SortComparator::SortComparator(ScoreDocComparator* _this):SortComparatorSource(){
315
 
                this->_this = _this;
316
 
        }
317
 
        SortComparator::~SortComparator(){
318
 
        }
 
268
ScoreDocComparator* SortComparator::newComparator (CL_NS(index)::IndexReader* reader, const TCHAR* fieldname){
 
269
        return _CLNEW ScoreDocComparatorImpl(FieldCache::DEFAULT->getCustom (reader, fieldname, this));
 
270
}
 
271
SortComparator::SortComparator(){
 
272
}
 
273
SortComparator::~SortComparator(){
 
274
}
319
275
 
320
276
 
321
277
CL_NS_END