|
1 #include "CLucene/StdHeader.h" |
|
2 #include "WeightedTerm.h" |
|
3 #include "CLucene/search/Similarity.h" |
|
4 |
|
5 CL_NS_DEF2(search,highlight) |
|
6 |
|
7 WeightedTerm::WeightedTerm (float_t weight,const TCHAR* term) |
|
8 { |
|
9 _weight=weight; |
|
10 _term = stringDuplicate(term); |
|
11 cachedHashCode = 0; |
|
12 } |
|
13 |
|
14 WeightedTerm::~WeightedTerm() |
|
15 { |
|
16 _CLDELETE_CARRAY(_term); |
|
17 } |
|
18 |
|
19 WeightedTerm::WeightedTerm(const WeightedTerm& other) |
|
20 { |
|
21 _weight = other.getWeight(); |
|
22 _term = STRDUP_TtoT(other.getTerm()); |
|
23 } |
|
24 |
|
25 WeightedTerm* WeightedTerm::clone() const{ |
|
26 return _CLNEW WeightedTerm(*this); |
|
27 } |
|
28 |
|
29 /** |
|
30 * @return the term value (stemmed) |
|
31 */ |
|
32 const TCHAR* WeightedTerm::getTerm() const |
|
33 { |
|
34 return _term; |
|
35 } |
|
36 |
|
37 /** |
|
38 * @return the weight associated with this term |
|
39 */ |
|
40 float_t WeightedTerm::getWeight() const |
|
41 { |
|
42 return _weight; |
|
43 } |
|
44 |
|
45 /** |
|
46 * @param term the term value (stemmed) |
|
47 */ |
|
48 void WeightedTerm::setTerm(TCHAR* term) |
|
49 { |
|
50 _CLDELETE_CARRAY(this->_term); |
|
51 this->_term = STRDUP_TtoT(_term); |
|
52 cachedHashCode = 0; |
|
53 } |
|
54 |
|
55 /** |
|
56 * @param weight the weight associated with this term |
|
57 */ |
|
58 void WeightedTerm::setWeight(float_t aweight) { |
|
59 this->_weight = aweight; |
|
60 cachedHashCode = 0; |
|
61 } |
|
62 |
|
63 size_t WeightedTerm::hashCode(){ |
|
64 if ( cachedHashCode == 0 ){ |
|
65 cachedHashCode = ( CL_NS(util)::Misc::thashCode(this->_term) ^ CL_NS(search)::Similarity::floatToByte(_weight) ); |
|
66 } |
|
67 |
|
68 return cachedHashCode; |
|
69 } |
|
70 |
|
71 bool WeightedTerm::Compare::operator()( WeightedTerm* t1, WeightedTerm* t2 ) const{ |
|
72 int r = _tcscmp(t1->getTerm(), t2->getTerm()); |
|
73 if ( r < 0 ) |
|
74 return true; |
|
75 else if ( r == 0 ) |
|
76 return t1->getWeight() < t2->getWeight(); |
|
77 else |
|
78 return false; |
|
79 } |
|
80 size_t WeightedTerm::Compare::operator()( WeightedTerm* t ) const{ |
|
81 return t->hashCode(); |
|
82 } |
|
83 |
|
84 CL_NS_END2 |