CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_search_Compare_ 00008 #define _lucene_search_Compare_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "FieldSortedHitQueue.h" 00015 00016 CL_NS_DEF(search) 00017 00018 00019 class ScoreDocComparators:LUCENE_BASE { 00020 protected: 00021 ScoreDocComparators(){} 00022 public: 00023 ~ScoreDocComparators(){ 00024 } 00025 00026 class Relevance:public ScoreDocComparator { 00027 public: 00028 int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) { 00029 if (i->score > j->score) return -1; 00030 if (i->score < j->score) return 1; 00031 return 0; 00032 } 00033 CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) { 00034 return _CLNEW CL_NS(util)::Compare::Float (i->score); 00035 } 00036 int32_t sortType() { 00037 return SortField::DOCSCORE; 00038 } 00039 }; 00040 00041 class IndexOrder:public ScoreDocComparator{ 00042 public: 00043 IndexOrder(): 00044 ScoreDocComparator() 00045 { 00046 00047 } 00048 int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) { 00049 if (i->doc < j->doc) return -1; 00050 if (i->doc > j->doc) return 1; 00051 return 0; 00052 } 00053 CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) { 00054 return _CLNEW CL_NS(util)::Compare::Int32(i->doc); 00055 } 00056 int32_t sortType() { 00057 return SortField::DOC; 00058 } 00059 }; 00060 00061 00062 class String: public ScoreDocComparator { 00063 FieldCache::StringIndex* index; 00064 #ifdef _CL__CND_DEBUG 00065 int32_t length; 00066 #endif 00067 public: 00068 String(FieldCache::StringIndex* index, int32_t len) 00069 { 00070 #ifdef _CL__CND_DEBUG 00071 this->length = len; 00072 #endif 00073 this->index = index; 00074 } 00075 00076 int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) { 00077 CND_PRECONDITION(i->doc<length, "i->doc>=length") 00078 CND_PRECONDITION(j->doc<length, "j->doc>=length") 00079 if (index->order[i->doc] < index->order[j->doc]) return -1; 00080 if (index->order[i->doc] > index->order[j->doc]) return 1; 00081 return 0; 00082 } 00083 00084 CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) { 00085 return _CLNEW CL_NS(util)::Compare::TChar(index->lookup[index->order[i->doc]]); 00086 } 00087 00088 int32_t sortType() { 00089 return SortField::STRING; 00090 } 00091 }; 00092 00093 class Int32:public ScoreDocComparator{ 00094 int32_t* fieldOrder; 00095 #ifdef _CL__CND_DEBUG 00096 int32_t length; 00097 #endif 00098 public: 00099 Int32(int32_t* fieldOrder, int32_t len) 00100 { 00101 this->fieldOrder = fieldOrder; 00102 #ifdef _CL__CND_DEBUG 00103 this->length = len; 00104 #endif 00105 } 00106 00107 00108 int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) { 00109 CND_PRECONDITION(i->doc<length, "i->doc>=length") 00110 CND_PRECONDITION(j->doc<length, "j->doc>=length") 00111 if (fieldOrder[i->doc] < fieldOrder[j->doc]) return -1; 00112 if (fieldOrder[i->doc] > fieldOrder[j->doc]) return 1; 00113 return 0; 00114 } 00115 00116 CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) { 00117 CND_PRECONDITION(i->doc<length, "i->doc>=length") 00118 return _CLNEW CL_NS(util)::Compare::Int32(fieldOrder[i->doc]); 00119 } 00120 00121 int32_t sortType() { 00122 return SortField::INT; 00123 } 00124 }; 00125 00126 class Float:public ScoreDocComparator { 00127 float_t* fieldOrder; 00128 #ifdef _CL__CND_DEBUG 00129 int32_t length; 00130 #endif 00131 public: 00132 Float(float_t* fieldOrder, int32_t len) 00133 { 00134 this->fieldOrder = fieldOrder; 00135 #ifdef _CL__CND_DEBUG 00136 this->length = len; 00137 #endif 00138 } 00139 00140 int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) { 00141 CND_PRECONDITION(i->doc<length, "i->doc>=length") 00142 CND_PRECONDITION(j->doc<length, "j->doc>=length") 00143 if (fieldOrder[i->doc] < fieldOrder[j->doc]) return -1; 00144 if (fieldOrder[i->doc] > fieldOrder[j->doc]) return 1; 00145 return 0; 00146 } 00147 00148 CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) { 00149 CND_PRECONDITION(i->doc<length, "i->doc>=length") 00150 return _CLNEW CL_NS(util)::Compare::Float(fieldOrder[i->doc]); 00151 } 00152 00153 int32_t sortType() { 00154 return SortField::FLOAT; 00155 } 00156 }; 00157 }; 00158 00159 00160 CL_NS_END 00161 #endif