summaryrefslogtreecommitdiff
path: root/src/assistant/3rdparty/clucene/src/CLucene/index/Term.cpp
blob: 0d8c1c8444fcfaea361661334d973c6516a571de (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*------------------------------------------------------------------------------
* 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.
*
* Changes are Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
------------------------------------------------------------------------------*/

#include "CLucene/StdHeader.h"
#include "Term.h"
#include "CLucene/util/StringIntern.h"

CL_NS_USE(util)
CL_NS_DEF(index)

Term::Term()
{
    init();
}

Term::Term(const TCHAR* fld, const TCHAR* txt, bool internField)
{
    init();
    set(fld, txt, internField);
}

Term::Term(const Term* fieldTerm, const TCHAR* txt)
{
    init();
    set(fieldTerm, txt);
}

Term::Term(const TCHAR* fld, const TCHAR* txt)
{
    init();
    set(fld, txt);
}

Term::~Term()
{
    if (internF)
        CLStringIntern::unintern(_field);
    _field = NULL;

#ifndef LUCENE_TERM_TEXT_LENGTH
    if (_text != LUCENE_BLANK_STRING)
        _CLDELETE_CARRAY(_text);
#endif
}

const TCHAR* Term::field() const
{
    return _field;
}

const TCHAR* Term::text() const
{
    return _text;
}

void Term::set(const Term* term, const TCHAR* txt)
{
    set(term->field(), txt, false);
}

void Term::set(const TCHAR* fld, const TCHAR* txt,bool internField)
{
    CND_PRECONDITION(fld != NULL, "fld contains NULL");
    CND_PRECONDITION(txt != NULL, "txt contains NULL");

    //save field for unintern later
    const TCHAR* oldField = _field;
    cachedHashCode = 0;
    textLen = _tcslen(txt);

    //Delete text if it is the owner
#ifdef LUCENE_TERM_TEXT_LENGTH
    if (textLen > LUCENE_TERM_TEXT_LENGTH)
        textLen = LUCENE_TERM_TEXT_LENGTH;

    _tcsncpy(_text,txt,textLen+1);
    _text[textLen]=0;
#else
    //if the term text buffer is bigger than what we have
    if (_text && textLen > textLenBuf) {
        if (_text != LUCENE_BLANK_STRING) {
            _CLDELETE_ARRAY(_text);
        } else {
            _text = NULL;
        }
        textLenBuf = 0;
    }

    if (_text == LUCENE_BLANK_STRING) {
        _text = LUCENE_BLANK_STRING;
    } else if (_text == NULL) {
        if (txt[0] == 0) {
            //if the string is blank and we aren't re-using the buffer...
            _text = LUCENE_BLANK_STRING;
        } else {
            //duplicate the text
            _text  = stringDuplicate(txt);
            textLenBuf = textLen;
        }
    } else {
        //re-use the buffer
        _tcscpy(_text,txt);
    }
#endif

    //Set Term Field
    if (internField) {
        _field = CLStringIntern::intern(fld  CL_FILELINE);
    } else {
        _field = fld;
    }

    //unintern old field after interning new one, 
    if (internF)
        CLStringIntern::unintern(oldField);
    internF = internField;

    CND_PRECONDITION(_tcscmp(fld, _field) == 0, "field not equal");
}

bool Term::equals(const Term* other) const
{
    if (cachedHashCode != 0 && other->cachedHashCode != 0
        && other->cachedHashCode != cachedHashCode)
        return false;

    if (_field == other->_field) {
        if (textLen == other->textLen)
            return (_tcscmp(_text, other->_text) == 0);
        return false;
    }

    return false;
}

size_t Term::hashCode()
{
    if (cachedHashCode == 0)
        cachedHashCode = Misc::thashCode(_field) + Misc::thashCode(_text, textLen);

    return cachedHashCode;
}

int32_t Term::compareTo(const Term* other) const
{
    //Check ret to see if text needs to be compared
    if (_field == other->_field)
        return _tcscmp(_text, other->_text);

    int32_t ret = _tcscmp(_field, other->_field);
    if (ret == 0)
        ret = _tcscmp(_text, other->_text);
    return ret;
}

TCHAR* Term::toString() const
{
    return CL_NS(util)::Misc::join(_field, _T(":"), _text);
}

void Term::init()
{
    textLen = 0;
    internF = false;
    cachedHashCode = 0;
    _field = LUCENE_BLANK_STRING;    

#ifdef LUCENE_TERM_TEXT_LENGTH
    _text[0] = 0;
#else
    _text = LUCENE_BLANK_STRING;
    textLenBuf = 0;
#endif
}

CL_NS_END