os/textandloc/fontservices/textshaperplugin/IcuSource/common/unicode/dbbi.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
**********************************************************************
sl@0
     3
*   Copyright (C) 1999-2005 IBM Corp. All rights reserved.
sl@0
     4
**********************************************************************
sl@0
     5
*   Date        Name        Description
sl@0
     6
*   12/1/99    rgillam     Complete port from Java.
sl@0
     7
*   01/13/2000 helena      Added UErrorCode to ctors.
sl@0
     8
**********************************************************************
sl@0
     9
*/
sl@0
    10
sl@0
    11
#ifndef DBBI_H
sl@0
    12
#define DBBI_H
sl@0
    13
sl@0
    14
#include "unicode/rbbi.h"
sl@0
    15
sl@0
    16
#if !UCONFIG_NO_BREAK_ITERATION
sl@0
    17
sl@0
    18
/**
sl@0
    19
 * \file
sl@0
    20
 * \brief C++ API: Dictionary Based Break Iterator
sl@0
    21
 */
sl@0
    22
 
sl@0
    23
U_NAMESPACE_BEGIN
sl@0
    24
sl@0
    25
/* forward declaration */
sl@0
    26
class DictionaryBasedBreakIteratorTables;
sl@0
    27
sl@0
    28
/**
sl@0
    29
 * A subclass of RuleBasedBreakIterator that adds the ability to use a dictionary
sl@0
    30
 * to further subdivide ranges of text beyond what is possible using just the
sl@0
    31
 * state-table-based algorithm.  This is necessary, for example, to handle
sl@0
    32
 * word and line breaking in Thai, which doesn't use spaces between words.  The
sl@0
    33
 * state-table-based algorithm used by RuleBasedBreakIterator is used to divide
sl@0
    34
 * up text as far as possible, and then contiguous ranges of letters are
sl@0
    35
 * repeatedly compared against a list of known words (i.e., the dictionary)
sl@0
    36
 * to divide them up into words.
sl@0
    37
 *
sl@0
    38
 * <p>Applications do not normally need to include this header.</p>
sl@0
    39
 *
sl@0
    40
 * <p>This class will probably be deprecated in a future release of ICU, and replaced
sl@0
    41
 *  with a more flexible and capable dictionary based break iterator.  This change
sl@0
    42
 *  should be invisible to applications, because creation and use of instances of
sl@0
    43
 *  DictionaryBasedBreakIterator is through the factories and abstract
sl@0
    44
 *  API on class BreakIterator, which will remain stable.</p>
sl@0
    45
 *
sl@0
    46
 * <p>This class is not intended to be subclassed.</p>
sl@0
    47
 *
sl@0
    48
 *
sl@0
    49
 * DictionaryBasedBreakIterator uses the same rule language as RuleBasedBreakIterator,
sl@0
    50
 * but adds one more special substitution name: &lt;dictionary&gt;.  This substitution
sl@0
    51
 * name is used to identify characters in words in the dictionary.  The idea is that
sl@0
    52
 * if the iterator passes over a chunk of text that includes two or more characters
sl@0
    53
 * in a row that are included in &lt;dictionary&gt;, it goes back through that range and
sl@0
    54
 * derives additional break positions (if possible) using the dictionary.
sl@0
    55
 *
sl@0
    56
 * DictionaryBasedBreakIterator is also constructed with the filename of a dictionary
sl@0
    57
 * file.  It follows a prescribed search path to locate the dictionary (right now,
sl@0
    58
 * it looks for it in /com/ibm/text/resources in each directory in the classpath,
sl@0
    59
 * and won't find it in JAR files, but this location is likely to change).  The
sl@0
    60
 * dictionary file is in a serialized binary format.  We have a very primitive (and
sl@0
    61
 * slow) BuildDictionaryFile utility for creating dictionary files, but aren't
sl@0
    62
 * currently making it public.  Contact us for help.
sl@0
    63
 * <p>
sl@0
    64
 * <b> NOTE </b>  The DictionaryBasedIterator class is still under development.  The
sl@0
    65
 * APIs are not in stable condition yet.
sl@0
    66
 */
sl@0
    67
class U_COMMON_API DictionaryBasedBreakIterator : public RuleBasedBreakIterator {
sl@0
    68
sl@0
    69
private:
sl@0
    70
sl@0
    71
    /**
sl@0
    72
     * when a range of characters is divided up using the dictionary, the break
sl@0
    73
     * positions that are discovered are stored here, preventing us from having
sl@0
    74
     * to use either the dictionary or the state table again until the iterator
sl@0
    75
     * leaves this range of text
sl@0
    76
     */
sl@0
    77
    int32_t* cachedBreakPositions;
sl@0
    78
sl@0
    79
    /**
sl@0
    80
     * The number of elements in cachedBreakPositions
sl@0
    81
     */
sl@0
    82
    int32_t numCachedBreakPositions;
sl@0
    83
sl@0
    84
    /**
sl@0
    85
     * if cachedBreakPositions is not null, this indicates which item in the
sl@0
    86
     * cache the current iteration position refers to
sl@0
    87
     */
sl@0
    88
    int32_t positionInCache;
sl@0
    89
sl@0
    90
    DictionaryBasedBreakIteratorTables  *fTables;
sl@0
    91
sl@0
    92
    /**=======================================================================
sl@0
    93
     * Create a dictionary based break boundary detection iterator.
sl@0
    94
     * @param tablesImage The location for the dictionary to be loaded into memory
sl@0
    95
     * @param dictionaryFilename The name of the dictionary file
sl@0
    96
     * @param status the error code status
sl@0
    97
     * @return A dictionary based break detection iterator.  The UErrorCode& status
sl@0
    98
     * parameter is used to return status information to the user.
sl@0
    99
     * To check whether the construction succeeded or not, you should check
sl@0
   100
     * the value of U_SUCCESS(err).  If you wish more detailed information, you
sl@0
   101
     * can check for informational error results which still indicate success.  For example,
sl@0
   102
     * U_FILE_ACCESS_ERROR will be returned if the file does not exist.
sl@0
   103
     * The caller owns the returned object and is responsible for deleting it.
sl@0
   104
     ======================================================================= */
sl@0
   105
    DictionaryBasedBreakIterator(UDataMemory* tablesImage, const char* dictionaryFilename, UErrorCode& status);
sl@0
   106
sl@0
   107
public:
sl@0
   108
    //=======================================================================
sl@0
   109
    // boilerplate
sl@0
   110
    //=======================================================================
sl@0
   111
sl@0
   112
    /**
sl@0
   113
     * Destructor
sl@0
   114
     * @stable ICU 2.0
sl@0
   115
     */
sl@0
   116
    virtual ~DictionaryBasedBreakIterator();
sl@0
   117
sl@0
   118
    /**
sl@0
   119
     * Default constructor.  Creates an "empty" break iterator.
sl@0
   120
     * Such an iterator can subsequently be assigned to.
sl@0
   121
     * @return the newly created DictionaryBaseBreakIterator.
sl@0
   122
     * @stable ICU 2.0
sl@0
   123
     */
sl@0
   124
     DictionaryBasedBreakIterator();
sl@0
   125
sl@0
   126
     /**
sl@0
   127
      * Copy constructor.
sl@0
   128
      * @param other The DictionaryBasedBreakIterator to be copied.
sl@0
   129
      * @return the newly created DictionaryBasedBreakIterator.
sl@0
   130
      * @stable ICU 2.0
sl@0
   131
      */
sl@0
   132
     DictionaryBasedBreakIterator(const DictionaryBasedBreakIterator &other);
sl@0
   133
sl@0
   134
    /**
sl@0
   135
     * Assignment operator.
sl@0
   136
     * @param that The object to be copied.
sl@0
   137
     * @return the newly set DictionaryBasedBreakIterator.
sl@0
   138
     * @stable ICU 2.0
sl@0
   139
     */
sl@0
   140
    DictionaryBasedBreakIterator& operator=(const DictionaryBasedBreakIterator& that);
sl@0
   141
sl@0
   142
    /**
sl@0
   143
     * Returns a newly-constructed RuleBasedBreakIterator with the same
sl@0
   144
     * behavior, and iterating over the same text, as this one.
sl@0
   145
     * @return Returns a newly-constructed RuleBasedBreakIterator.
sl@0
   146
     * @stable ICU 2.0
sl@0
   147
     */
sl@0
   148
    virtual BreakIterator* clone(void) const;
sl@0
   149
sl@0
   150
    //=======================================================================
sl@0
   151
    // BreakIterator overrides
sl@0
   152
    //=======================================================================
sl@0
   153
    /**
sl@0
   154
     * Advances the iterator backwards, to the last boundary preceding this one.
sl@0
   155
     * @return The position of the last boundary position preceding this one.
sl@0
   156
     * @stable ICU 2.0
sl@0
   157
     */
sl@0
   158
    virtual int32_t previous(void);
sl@0
   159
sl@0
   160
    /**
sl@0
   161
     * Sets the iterator to refer to the first boundary position following
sl@0
   162
     * the specified position.
sl@0
   163
     * @param offset The position from which to begin searching for a break position.
sl@0
   164
     * @return The position of the first break after the current position.
sl@0
   165
     * @stable ICU 2.0
sl@0
   166
     */
sl@0
   167
    virtual int32_t following(int32_t offset);
sl@0
   168
sl@0
   169
    /**
sl@0
   170
     * Sets the iterator to refer to the last boundary position before the
sl@0
   171
     * specified position.
sl@0
   172
     * @param offset The position to begin searching for a break from.
sl@0
   173
     * @return The position of the last boundary before the starting position.
sl@0
   174
     * @stable ICU 2.0
sl@0
   175
     */
sl@0
   176
    virtual int32_t preceding(int32_t offset);
sl@0
   177
sl@0
   178
    /**
sl@0
   179
     * Returns the class ID for this class.  This is useful only for
sl@0
   180
     * comparing to a return value from getDynamicClassID().  For example:
sl@0
   181
     *
sl@0
   182
     *      Base* polymorphic_pointer = createPolymorphicObject();
sl@0
   183
     *      if (polymorphic_pointer->getDynamicClassID() ==
sl@0
   184
     *          Derived::getStaticClassID()) ...
sl@0
   185
     *
sl@0
   186
     * @return          The class ID for all objects of this class.
sl@0
   187
     * @stable ICU 2.0
sl@0
   188
     */
sl@0
   189
    static UClassID U_EXPORT2 getStaticClassID(void);
sl@0
   190
sl@0
   191
    /**
sl@0
   192
     * Returns a unique class ID POLYMORPHICALLY.  Pure virtual override.
sl@0
   193
     * This method is to implement a simple version of RTTI, since not all
sl@0
   194
     * C++ compilers support genuine RTTI.  Polymorphic operator==() and
sl@0
   195
     * clone() methods call this method.
sl@0
   196
     *
sl@0
   197
     * @return          The class ID for this object. All objects of a
sl@0
   198
     *                  given class have the same class ID.  Objects of
sl@0
   199
     *                  other classes have different class IDs.
sl@0
   200
     * @stable ICU 2.0
sl@0
   201
     */
sl@0
   202
    virtual UClassID getDynamicClassID(void) const;
sl@0
   203
sl@0
   204
protected:
sl@0
   205
    //=======================================================================
sl@0
   206
    // implementation
sl@0
   207
    //=======================================================================
sl@0
   208
    /**
sl@0
   209
     * This method is the actual implementation of the next() method.  All iteration
sl@0
   210
     * vectors through here.  This method initializes the state machine to state 1
sl@0
   211
     * and advances through the text character by character until we reach the end
sl@0
   212
     * of the text or the state machine transitions to state 0.  We update our return
sl@0
   213
     * value every time the state machine passes through a possible end state.
sl@0
   214
     * @internal
sl@0
   215
     */
sl@0
   216
    virtual int32_t handleNext(void);
sl@0
   217
sl@0
   218
    /**
sl@0
   219
     * removes the cache of break positions (usually in response to a change in
sl@0
   220
     * position of some sort)
sl@0
   221
     * @internal
sl@0
   222
     */
sl@0
   223
    virtual void reset(void);
sl@0
   224
sl@0
   225
    /**
sl@0
   226
     *  init    Initialize a dbbi.  Common routine for use by constructors.
sl@0
   227
     *  @internal
sl@0
   228
     */
sl@0
   229
    void init();
sl@0
   230
sl@0
   231
    /**
sl@0
   232
     * @param stackBuffer user allocated space for the new clone. If NULL new memory will be allocated.
sl@0
   233
     * If buffer is not large enough, new memory will be allocated.
sl@0
   234
     * @param BufferSize reference to size of allocated space.
sl@0
   235
     * If BufferSize == 0, a sufficient size for use in cloning will
sl@0
   236
     * be returned ('pre-flighting')
sl@0
   237
     * If BufferSize is not enough for a stack-based safe clone,
sl@0
   238
     * new memory will be allocated.
sl@0
   239
     * @param status to indicate whether the operation went on smoothly or there were errors
sl@0
   240
     *  An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were
sl@0
   241
     *  necessary.
sl@0
   242
     * @return pointer to the new clone
sl@0
   243
     * @internal
sl@0
   244
     */
sl@0
   245
    virtual BreakIterator *  createBufferClone(void *stackBuffer,
sl@0
   246
                                               int32_t &BufferSize,
sl@0
   247
                                               UErrorCode &status);
sl@0
   248
sl@0
   249
sl@0
   250
private:
sl@0
   251
    /**
sl@0
   252
     * This is the function that actually implements the dictionary-based
sl@0
   253
     * algorithm.  Given the endpoints of a range of text, it uses the
sl@0
   254
     * dictionary to determine the positions of any boundaries in this
sl@0
   255
     * range.  It stores all the boundary positions it discovers in
sl@0
   256
     * cachedBreakPositions so that we only have to do this work once
sl@0
   257
     * for each time we enter the range.
sl@0
   258
     * @param startPos The start position of a range of text
sl@0
   259
     * @param endPos The end position of a range of text
sl@0
   260
     * @param status The error code status
sl@0
   261
     */
sl@0
   262
    void divideUpDictionaryRange(int32_t startPos, int32_t endPos, UErrorCode &status);
sl@0
   263
sl@0
   264
sl@0
   265
    /*
sl@0
   266
     * HSYS : Please revisit with Rich, the ctors of the DBBI class is currently
sl@0
   267
     * marked as private.
sl@0
   268
     */
sl@0
   269
    friend class DictionaryBasedBreakIteratorTables;
sl@0
   270
    friend class BreakIterator;
sl@0
   271
};
sl@0
   272
sl@0
   273
U_NAMESPACE_END
sl@0
   274
sl@0
   275
#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
sl@0
   276
sl@0
   277
#endif