summaryrefslogtreecommitdiffstats
path: root/3rdparty/clucene/src/CLucene/search/Compare.h
blob: ab38b17f104fe181bc6ba158f12840bbeb4cf2a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
* 
* Distributable under the terms of either the Apache License (Version 2.0) or 
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#ifndef _lucene_search_Compare_
#define _lucene_search_Compare_

#if defined(_LUCENE_PRAGMA_ONCE)
# pragma once
#endif

#include "FieldSortedHitQueue.h"

CL_NS_DEF(search)


class ScoreDocComparators:LUCENE_BASE {
protected:
	ScoreDocComparators(){}
public:
    ~ScoreDocComparators(){
    }

	class Relevance:public ScoreDocComparator {
	public:
		int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) {
			if (i->score > j->score) return -1;
			if (i->score < j->score) return 1;
			return 0;
		}
		CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) {
			return _CLNEW CL_NS(util)::Compare::Float (i->score);
		}
		int32_t sortType() {
			return SortField::DOCSCORE;
		}
	};

	class IndexOrder:public ScoreDocComparator{
	public:
		IndexOrder():
			ScoreDocComparator()
		{

		}
		int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) {
			if (i->doc < j->doc) return -1;
			if (i->doc > j->doc) return 1;
			return 0;
		}
		CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) {
			return _CLNEW CL_NS(util)::Compare::Int32(i->doc);
		}
		int32_t sortType() {
			return SortField::DOC;
		}
	};


	class String: public ScoreDocComparator {
		FieldCache::StringIndex* index;
#ifdef _CL__CND_DEBUG
		int32_t length;
#endif
	public:
		String(FieldCache::StringIndex* index, int32_t len)
		{
#ifdef _CL__CND_DEBUG
			this->length = len;
#endif
			this->index = index;
		}

		int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) {
			CND_PRECONDITION(i->doc<length, "i->doc>=length")
			CND_PRECONDITION(j->doc<length, "j->doc>=length")
			if (index->order[i->doc] < index->order[j->doc]) return -1;
			if (index->order[i->doc] > index->order[j->doc]) return 1;
			return 0;
		}

		CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) {
			return _CLNEW CL_NS(util)::Compare::TChar(index->lookup[index->order[i->doc]]);
		}

		int32_t sortType() {
			return SortField::STRING;
		}
	};

	class Int32:public ScoreDocComparator{
		int32_t* fieldOrder;
#ifdef _CL__CND_DEBUG
		int32_t length;
#endif
	public:
		Int32(int32_t* fieldOrder, int32_t len)
		{
			this->fieldOrder = fieldOrder;
#ifdef _CL__CND_DEBUG
			this->length = len;
#endif
		}


		int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) {
			CND_PRECONDITION(i->doc<length, "i->doc>=length")
			CND_PRECONDITION(j->doc<length, "j->doc>=length")
			if (fieldOrder[i->doc] < fieldOrder[j->doc]) return -1;
			if (fieldOrder[i->doc] > fieldOrder[j->doc]) return 1;
			return 0;
		}

		CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) {
			CND_PRECONDITION(i->doc<length, "i->doc>=length")
			return _CLNEW CL_NS(util)::Compare::Int32(fieldOrder[i->doc]);
		}

		int32_t sortType() {
			return SortField::INT;
		}
	};

	class Float:public ScoreDocComparator {
		qreal* fieldOrder;
#ifdef _CL__CND_DEBUG
		int32_t length;
#endif
	public:
		Float(qreal* fieldOrder, int32_t len)
		{
			this->fieldOrder = fieldOrder;
#ifdef _CL__CND_DEBUG
			this->length = len;
#endif
		}

		int32_t compare (struct ScoreDoc* i, struct ScoreDoc* j) {
			CND_PRECONDITION(i->doc<length, "i->doc>=length")
			CND_PRECONDITION(j->doc<length, "j->doc>=length")
			if (fieldOrder[i->doc] < fieldOrder[j->doc]) return -1;
			if (fieldOrder[i->doc] > fieldOrder[j->doc]) return 1;
			return 0;
		}

		CL_NS(util)::Comparable* sortValue (struct ScoreDoc* i) {
			CND_PRECONDITION(i->doc<length, "i->doc>=length")
			return _CLNEW CL_NS(util)::Compare::Float(fieldOrder[i->doc]);
		}

		int32_t sortType() {
			return SortField::FLOAT;
		}
	};
};


CL_NS_END
#endif