summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/index/Term.h
blob: 68eefd1946d594e3394c3c1445b7385105197e73 (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
/*------------------------------------------------------------------------------
* 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) 2009 Nokia Corporation and/or its subsidiary(-ies).
------------------------------------------------------------------------------*/
#ifndef _lucene_index_Term_
#define _lucene_index_Term_

#if defined(_LUCENE_PRAGMA_ONCE)
# pragma once
#endif

#include "CLucene/util/Misc.h"
#include "CLucene/util/StringIntern.h"

CL_NS_DEF(index)

/*
A Term represents a word from text.  This is the unit of search.  It is
composed of two elements, the text of the word, as a string, and the name of
the field that the text occured in, an interned string.

Note that terms may represent more than words from text fields, but also
things like dates, email addresses, urls, etc.  

IMPORTANT NOTE:
Term inherits from the template class LUCENE_REFBASE which tries to do
some garbage collection by counting the references an instance has. As a result
of this construction you MUST use _CLDECDELETE(obj) when you want to delete an 
of Term!

ABOUT intrn 

intrn indicates if field and text are interned or not. Interning of Strings
is the process of converting duplicated strings to shared ones. 

*/
class Term : LUCENE_REFBASE
{
private:
    const TCHAR* _field;
    bool internF; // Indicates if Term Field is interned(and therefore must be uninternd).
    size_t cachedHashCode;
    size_t textLen; // a cache of text len, this allows for a preliminary comparison of text lengths

#ifdef LUCENE_TERM_TEXT_LENGTH
    TCHAR _text[LUCENE_TERM_TEXT_LENGTH + 1];
#else
    TCHAR* _text;
    size_t textLenBuf; //a cache of text len, this allows for a preliminary comparison of text lengths
#endif

    void init();
public:

    //uses the specified fieldTerm's field. this saves on intern'ing time.
    Term(const Term* fieldTerm, const TCHAR* txt);

    ///Constructs a blank term
    Term();

    // TODO: need to be private, a few other things need to be changed first...
    Term(const TCHAR* fld, const TCHAR* txt, bool internField);

    /**
    * Constructor. Constructs a Term with the given field and text. Field and
    * text are not copied Field and text are deleted in destructor only if
    * intern is false. 
    */
    Term(const TCHAR* fld, const TCHAR* txt);

    ///Destructor.
    ~Term();

    ///Returns the field of this term, an interned string. The field indicates
    ///the part of a document which this term came from. 
    const TCHAR* field() const; ///<returns reference

    ///Returns the text of this term.  In the case of words, this is simply the
    ///text of the word.  In the case of dates and other types, this is an
    ///encoding of the object as a string.
    const TCHAR* text() const; ///<returns reference

    ///Resets the field and text of a Term.
    inline void set(const TCHAR* fld, const TCHAR* txt)
    {
        set(fld, txt, true);
    }

    /**
    * Optimized set of Term by reusing same field as this Term
    * - avoids field.intern() overhead
    * @param text The text of the new term
    * (field is implicitly same as this Term instance)
    */
    void set(const Term* term, const TCHAR* txt);

    void set(const TCHAR* fld, const TCHAR* txt, bool internField);

    /** Compares two terms, returning a negative integer if this
    term belongs before the argument, zero if this term is equal to the
    argument, and a positive integer if this term belongs after the argument.

    The ordering of terms is first by field, then by text.*/
    int32_t compareTo(const Term* other) const;

    bool equals(const Term* other) const;

    size_t textLength() const { return textLen; }

    ///Forms the contents of Field and term in some kind of tuple notation
    ///<field:text>
    TCHAR* toString() const;

    size_t hashCode();

    class Equals:public CL_NS_STD(binary_function)<const Term*,const Term*,bool>
    {
    public:
        bool operator()( const Term* val1, const Term* val2 ) const
        {
            return val1->equals(val2);
        }
    };

    class Compare:LUCENE_BASE, public CL_NS(util)::Compare::_base //<Term*>
    {
    public:
        bool operator()(Term* t1, Term* t2) const
        {
            return (t1->compareTo(t2) < 0);
        }

        size_t operator()(Term* t) const
        {
            return t->hashCode();
        }
    };
};

CL_NS_END

#endif