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

« back to all changes in this revision

Viewing changes to src/CLucene/index/Term.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*------------------------------------------------------------------------------
2
 
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3
 
4
 
* Distributable under the terms of either the Apache License (Version 2.0) or 
5
 
* the GNU Lesser General Public License, as specified in the COPYING file.
6
 
------------------------------------------------------------------------------*/
7
 
#include "CLucene/StdHeader.h"
8
 
#include "Term.h"
9
 
#include "CLucene/util/StringIntern.h"
10
 
 
11
 
CL_NS_USE(util)
12
 
CL_NS_DEF(index)
13
 
 
14
 
Term::Term(){
15
 
        //Intern fld and assign it to field
16
 
        _field = LUCENE_BLANK_STRING;
17
 
        internF = false;
18
 
        cachedHashCode = 0;
19
 
        textLen = 0;            
20
 
                
21
 
        //Duplicate txt and assign it to text
22
 
        #ifdef LUCENE_TERM_TEXT_LENGTH
23
 
                _text[0]=0;
24
 
        #else
25
 
                _text = LUCENE_BLANK_STRING;
26
 
                textLenBuf = 0;
27
 
        #endif
28
 
        textLen = 0;
29
 
}
30
 
 
31
 
Term::Term(const TCHAR* fld, const TCHAR* txt, bool internField){
32
 
//Func - Constructor.
33
 
//       Constructs a Term with the given field and text. Field and text are not copied
34
 
//       Field and text are deleted in destructor only if intern is false. 
35
 
//Pre  - fld != NULL and contains the name of the field
36
 
//       txt != NULL and contains the value of the field
37
 
//       internF is true or false and indicates if term Field is interned or not
38
 
//       internT is true or false and indicates if term Text is interned or not
39
 
//       canDelete defaults to true but can be false and indicates to the IGarbageCollector that the Term can be deleted when finalized
40
 
//Post - An instance of Term has been created.Field and txt have not been copied but assigned
41
 
        
42
 
        _field = LUCENE_BLANK_STRING;
43
 
        internF = false;
44
 
        textLen = 0;
45
 
        #ifdef LUCENE_TERM_TEXT_LENGTH
46
 
                _text[0]=0;
47
 
        #else
48
 
                _text = LUCENE_BLANK_STRING;
49
 
                textLenBuf = 0;
50
 
        #endif
51
 
 
52
 
    set(fld,txt,internField);
53
 
}
54
 
 
55
 
 
56
 
Term::Term(const Term* fieldTerm, const TCHAR* txt){
57
 
        _field = LUCENE_BLANK_STRING;
58
 
        internF = false;
59
 
        textLen = 0;
60
 
        #ifdef LUCENE_TERM_TEXT_LENGTH
61
 
                _text[0]=0;
62
 
        #else
63
 
                _text = LUCENE_BLANK_STRING;
64
 
                textLenBuf = 0;
65
 
        #endif
66
 
 
67
 
    set(fieldTerm,txt);
68
 
}
69
 
 
70
 
Term::Term(const TCHAR* fld, const TCHAR* txt){
71
 
        _field = LUCENE_BLANK_STRING;
72
 
        internF = false;
73
 
        textLen = 0;
74
 
        #ifdef LUCENE_TERM_TEXT_LENGTH
75
 
                _text[0]=0;
76
 
        #else
77
 
                _text = LUCENE_BLANK_STRING;
78
 
                textLenBuf = 0;
79
 
        #endif
80
 
 
81
 
    set(fld,txt);
82
 
}
83
 
 
84
 
Term::~Term(){
85
 
//Func - Destructor.
86
 
//Pre  - true
87
 
//Post - The instance has been destroyed. field and text have been deleted if pre(intrn) is false
88
 
 
89
 
        //Unintern field
90
 
        if ( internF )
91
 
                CLStringIntern::unintern(_field);
92
 
        _field = NULL;
93
 
 
94
 
#ifndef LUCENE_TERM_TEXT_LENGTH
95
 
        //Deletetext if it is the owner
96
 
        if ( _text != LUCENE_BLANK_STRING)
97
 
                _CLDELETE_CARRAY( _text );
98
 
#endif
99
 
}
100
 
 
101
 
const TCHAR* Term::field() const {
102
 
//Func - Returns the field of this term, an interned string. The field indicates
103
 
//       the part of a document which this term came from. 
104
 
//Pre  - true
105
 
//Post - field has been returned
106
 
 
107
 
    return _field;
108
 
}
109
 
 
110
 
const TCHAR* Term::text() const {
111
 
//Func - Returns the text of this term.  In the case of words, this is simply the
112
 
//       text of the word.  In the case of dates and other types, this is an
113
 
//       encoding of the object as a string.
114
 
//Pre  - true
115
 
//Post - text has been returned
116
 
 
117
 
    return _text;
118
 
}
119
 
 
120
 
 
121
 
void Term::set(const Term* term, const TCHAR* txt){
122
 
        set(term->field(),txt,false);
123
 
}
124
 
 
125
 
void Term::set(const TCHAR* fld, const TCHAR* txt,const bool internField){
126
 
//Func - Resets the field and text of a Term.
127
 
//Pre  - fld != NULL and contains the name of the field
128
 
//       txt != NULL and contains the value of the field
129
 
//       internF is true or false
130
 
//       internT is true or false
131
 
//Post - field and text of Term have been reset
132
 
 
133
 
    CND_PRECONDITION(fld != NULL, "fld contains NULL");
134
 
    CND_PRECONDITION(txt != NULL, "txt contains NULL");
135
 
 
136
 
        //save field for unintern later
137
 
        const TCHAR* oldField = _field;
138
 
        //bool oldInternF = internF; //Not used
139
 
        cachedHashCode = 0;
140
 
 
141
 
    textLen = _tcslen(txt);
142
 
 
143
 
        //Delete text if it is the owner
144
 
#ifdef LUCENE_TERM_TEXT_LENGTH
145
 
        if ( textLen > LUCENE_TERM_TEXT_LENGTH )
146
 
           textLen = LUCENE_TERM_TEXT_LENGTH;
147
 
        _tcsncpy(_text,txt,textLen+1);
148
 
    _text[textLen]=0;
149
 
#else
150
 
 
151
 
        //if the term text buffer is bigger than what we have
152
 
        if ( _text && textLen > textLenBuf){
153
 
                if ( _text != LUCENE_BLANK_STRING ){
154
 
                        _CLDELETE_ARRAY( _text );
155
 
                }else
156
 
                        _text = NULL;
157
 
                textLenBuf = 0;
158
 
        }
159
 
 
160
 
        if ( _text==LUCENE_BLANK_STRING )
161
 
                _text = LUCENE_BLANK_STRING;
162
 
        else if ( _text==NULL ){
163
 
                if ( txt[0] == 0 ){
164
 
                        //if the string is blank and we aren't re-using the buffer...
165
 
                        _text = LUCENE_BLANK_STRING;
166
 
                }else{
167
 
                        //duplicate the text
168
 
                        _text  = stringDuplicate(txt);
169
 
                        textLenBuf = textLen;
170
 
                }
171
 
        }else{
172
 
                //re-use the buffer
173
 
                _tcscpy(_text,txt);
174
 
        }
175
 
 
176
 
#endif
177
 
 
178
 
    //Set Term Field
179
 
        if ( internField )
180
 
                _field = CLStringIntern::intern(fld  CL_FILELINE);
181
 
        else
182
 
                _field = fld;
183
 
 
184
 
        //unintern old field after interning new one, 
185
 
        if ( internF )
186
 
                CLStringIntern::unintern(oldField);
187
 
        internF = internField;
188
 
                    
189
 
    CND_PRECONDITION(_tcscmp(fld, _field)==0,"field not equal");
190
 
}
191
 
 
192
 
/** Compares two terms, returning true iff they have the same
193
 
  field and text. */
194
 
bool Term::equals(const Term* other) const{
195
 
   if ( cachedHashCode != 0 && other->cachedHashCode != 0 &&
196
 
                other->cachedHashCode != cachedHashCode )
197
 
                return false;
198
 
 
199
 
  if ( _field==other->_field ){
200
 
     //this can be quicker than using compareTo, because checks
201
 
     //field length first
202
 
          if ( textLen == other->textLen ){
203
 
                  return (_tcscmp(_text,other->_text)==0);
204
 
          }else
205
 
        return false;
206
 
  }else
207
 
     return false;
208
 
}
209
 
 
210
 
size_t Term::hashCode(){
211
 
        if ( cachedHashCode == 0 )
212
 
                cachedHashCode = Misc::thashCode(_field) + Misc::thashCode(_text,textLen);
213
 
        return cachedHashCode;
214
 
}
215
 
 
216
 
 
217
 
int32_t Term::compareTo(const Term* other) const {
218
 
//Func - Compares two terms, to see if this term belongs before,is equal to or after
219
 
//       after the argument term.
220
 
//Pre  - other is a reference to another term
221
 
//Post - A negative integer is returned if this term belongs before the argument, 
222
 
//       zero is returned if this term is equal to the argument, and a positive integer 
223
 
//       if this term belongs after the argument.
224
 
 
225
 
        //Check ret to see if text needs to be compared
226
 
        if ( _field == other->_field ){ // fields are interned
227
 
                //Compare text with text of other and return the result
228
 
                return _tcscmp(_text,other->_text);
229
 
        }else
230
 
                return _tcscmp(_field,other->_field);
231
 
}
232
 
 
233
 
TCHAR* Term::toString() const{
234
 
//Func - Forms the contents of Field and term in some kind of tuple notation
235
 
//       <field:text>
236
 
//Pre  - true
237
 
//Post - a string formatted as <field:text> is returned if pre(field) is NULL and
238
 
//       text is NULL the returned string will be formatted as <:>
239
 
 
240
 
        return CL_NS(util)::Misc::join( _field, _T(":"), _text);
241
 
}
242
 
 
243
 
CL_NS_END