FirteX-高性能全文索引和检索平台API Documentation |
00001 // 00002 // Copyright(C) 2005--2006 Institute of Computing Tech, Chinese Academy of Sciences. 00003 // All rights reserved. 00004 // This file is part of FirteX (www.firtex.org) 00005 // 00006 // Use of the FirteX is subject to the terms of the software license set forth in 00007 // the LICENSE file included with this software, and also available at 00008 // http://www.firtex.org/license.html 00009 // 00010 // Author : 郭瑞杰(GuoRuijie) 00011 // Email : ruijieguo@software.ict.ac.cn,ruijieguo@gmail.com 00012 // Created : 2006/7/12 00013 // 00014 #ifndef _SORTER_H 00015 #define _SORTER_H 00016 00017 #if _MSC_VER > 1000 00018 #pragma once 00019 #endif // _MSC_VER > 1000 00020 #pragma once 00021 00022 #include <assert.h> 00023 #include "../utility/StdHeader.h" 00024 #include "../utility/FXString.h" 00025 #include "../search/SortFieldComparator.h" 00026 #include "../index/IndexReader.h" 00027 using namespace firtex::utility; 00028 using namespace firtex::index; 00029 00030 namespace firtex 00031 { 00032 namespace search 00033 { 00034 class CSortField 00035 { 00036 public: 00037 enum SortFieldType 00038 { 00039 DOCSCORE = 1, //文档打分 00040 DOC = 2, //文档编号 00041 AUTO = 3, //自动判断字段类型 00042 CUSTOM = 4, //自定义 00043 }; 00044 public: 00045 CSortField(); 00046 CSortField(const CSortField& src); 00047 CSortField(const tchar* field); 00048 CSortField(const tchar* field,bool bReverse); 00049 CSortField(const tchar* field,SortFieldType type,bool bReverse = false); 00050 CSortField(const tchar* field,CSortFieldComparator* pComparator,bool bReverse = false); 00051 ~CSortField(); 00052 00053 static CSortField* FIELD_SCORE; 00054 static CSortField* FIELD_DOC; 00055 static int32_t NUMSORTFIELDS; 00056 public: 00057 00058 public: 00059 const tchar* getField(){return m_field.c_str();} 00060 SortFieldType getType(){return m_type;} 00061 bool getReverse(){return m_bReverse;} 00062 CSortFieldComparator* getComparator(){return m_comparator;} 00063 protected: 00064 static void init(); 00065 static void uninit(); 00066 protected: 00067 CFXString m_field; //排序字段名 00068 SortFieldType m_type; //排序字段类型 00069 bool m_bReverse; //比较顺序是否需要颠倒 00070 CSortFieldComparator* m_comparator; //文档字段比较器 00071 00072 friend class CSort; 00073 }; 00074 00075 class CSortFields 00076 { 00077 public: 00078 CSortFields(int32_t numFields); 00079 CSortFields(const CSortFields& src); 00080 ~CSortFields(); 00081 public: 00082 CSortField*& operator[](int32_t order); 00083 protected: 00084 CSortField** m_sortFields; 00085 int32_t m_numFields; 00086 00087 friend class CSort; 00088 }; 00089 00090 class CSortFieldCache; 00091 class CSort 00092 { 00093 public: 00094 CSort(void); 00095 CSort(const tchar* field,bool reverse); 00096 CSort(const tchar** field,int32_t numFields); 00097 CSort(CSortField* pSortField); 00098 CSort(CSortFields* pSortFields); 00099 ~CSort(void); 00100 00101 static CSort* RELEVANCE; //按相关性排序,和默认情况下无Sort提供时排序一致 00102 static CSort* DOCORDER; //按文档ID排序 00103 static int32_t NUMSORTS; 00104 public: 00110 void setSortField(const tchar* field,bool bReverse); 00111 00118 void setSortField(const tchar** field,int32_t numFields); 00119 00124 void setSortField(CSortField* pSortField); 00125 00130 void setSortField(CSortFields* pSortFields); 00131 00135 void clear(); 00136 public: 00137 bool lessThan(CScoreDoc* pDoc1,CScoreDoc* pDoc2); 00138 00139 void getComparators(CIndexReader* pReader,CSortFieldCache* pFieldCache); 00140 protected: 00141 static void init(); 00142 static void uninit(); 00143 protected: 00144 CSortFields* m_pSortFields; 00145 }; 00147 // 00148 inline CSortField*& CSortFields::operator[](int32_t order) 00149 { 00150 assert(order >=0 && order < m_numFields); 00151 return m_sortFields[order]; 00152 } 00154 // 00155 inline bool CSort::lessThan(CScoreDoc* pDoc1,CScoreDoc* pDoc2) 00156 { 00157 int32_t c = 0; 00158 int32_t i=0; 00159 int32_t numFields = m_pSortFields->m_numFields; 00160 CSortField** pSortFields = m_pSortFields->m_sortFields; 00161 while((i<numFields) &&(c==0)) 00162 { 00163 c = (pSortFields[i]->getReverse()) ? pSortFields[i]->m_comparator->compare (pDoc2, pDoc1) 00164 : pSortFields[i]->m_comparator->compare (pDoc1, pDoc2); 00165 i++; 00166 } 00167 00168 if (c == 0) 00169 return pDoc1->m_docID > pDoc2->m_docID; 00170 return c > 0; 00171 } 00172 } 00173 } 00174 00175 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex