1/*
2 *******************************************************************************
3 * Copyright (C) 1996-2006, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7
8#ifndef CANITER_H
9#define CANITER_H
10
11#include "unicode/utypes.h"
12
13#if !UCONFIG_NO_NORMALIZATION
14
15#include "unicode/uobject.h"
16#include "unicode/unistr.h"
17
18/**
19 * \file
20 * \brief C++ API: Canonical Iterator
21 */
22
23/** Should permutation skip characters with combining class zero
24 *  Should be either TRUE or FALSE. This is a compile time option
25 *  @stable ICU 2.4
26 */
27#ifndef CANITER_SKIP_ZEROES
28#define CANITER_SKIP_ZEROES TRUE
29#endif
30
31U_NAMESPACE_BEGIN
32
33class Hashtable;
34
35/**
36 * This class allows one to iterate through all the strings that are canonically equivalent to a given
37 * string. For example, here are some sample results:
38Results for: {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
391: \\u0041\\u030A\\u0064\\u0307\\u0327
40 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
412: \\u0041\\u030A\\u0064\\u0327\\u0307
42 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
433: \\u0041\\u030A\\u1E0B\\u0327
44 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
454: \\u0041\\u030A\\u1E11\\u0307
46 = {LATIN CAPITAL LETTER A}{COMBINING RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
475: \\u00C5\\u0064\\u0307\\u0327
48 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
496: \\u00C5\\u0064\\u0327\\u0307
50 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
517: \\u00C5\\u1E0B\\u0327
52 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
538: \\u00C5\\u1E11\\u0307
54 = {LATIN CAPITAL LETTER A WITH RING ABOVE}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
559: \\u212B\\u0064\\u0307\\u0327
56 = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING DOT ABOVE}{COMBINING CEDILLA}
5710: \\u212B\\u0064\\u0327\\u0307
58 = {ANGSTROM SIGN}{LATIN SMALL LETTER D}{COMBINING CEDILLA}{COMBINING DOT ABOVE}
5911: \\u212B\\u1E0B\\u0327
60 = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH DOT ABOVE}{COMBINING CEDILLA}
6112: \\u212B\\u1E11\\u0307
62 = {ANGSTROM SIGN}{LATIN SMALL LETTER D WITH CEDILLA}{COMBINING DOT ABOVE}
63 *<br>Note: the code is intended for use with small strings, and is not suitable for larger ones,
64 * since it has not been optimized for that situation.
65 * Note, CanonicalIterator is not intended to be subclassed.
66 * @author M. Davis
67 * @author C++ port by V. Weinstein
68 * @stable ICU 2.4
69 */
70class U_COMMON_API CanonicalIterator : public UObject {
71public:
72    /**
73     * Construct a CanonicalIterator object
74     * @param source    string to get results for
75     * @param status    Fill-in parameter which receives the status of this operation.
76     * @stable ICU 2.4
77     */
78    CanonicalIterator(const UnicodeString &source, UErrorCode &status);
79
80    /** Destructor
81     *  Cleans pieces
82     * @stable ICU 2.4
83     */
84    virtual ~CanonicalIterator();
85
86    /**
87     * Gets the NFD form of the current source we are iterating over.
88     * @return gets the source: NOTE: it is the NFD form of source
89     * @stable ICU 2.4
90     */
91    UnicodeString getSource();
92
93    /**
94     * Resets the iterator so that one can start again from the beginning.
95     * @stable ICU 2.4
96     */
97    void reset();
98
99    /**
100     * Get the next canonically equivalent string.
101     * <br><b>Warning: The strings are not guaranteed to be in any particular order.</b>
102     * @return the next string that is canonically equivalent. A bogus string is returned when
103     * the iteration is done.
104     * @stable ICU 2.4
105     */
106    UnicodeString next();
107
108    /**
109     * Set a new source for this iterator. Allows object reuse.
110     * @param newSource     the source string to iterate against. This allows the same iterator to be used
111     *                     while changing the source string, saving object creation.
112     * @param status        Fill-in parameter which receives the status of this operation.
113     * @stable ICU 2.4
114     */
115    void setSource(const UnicodeString &newSource, UErrorCode &status);
116
117    /**
118     * Dumb recursive implementation of permutation.
119     * TODO: optimize
120     * @param source     the string to find permutations for
121     * @param skipZeros  determine if skip zeros
122     * @param result     the results in a set.
123     * @param status       Fill-in parameter which receives the status of this operation.
124     * @internal
125     */
126    static void U_EXPORT2 permute(UnicodeString &source, UBool skipZeros, Hashtable *result, UErrorCode &status);
127
128    /**
129     * ICU "poor man's RTTI", returns a UClassID for this class.
130     *
131     * @stable ICU 2.2
132     */
133    static UClassID U_EXPORT2 getStaticClassID();
134
135    /**
136     * ICU "poor man's RTTI", returns a UClassID for the actual class.
137     *
138     * @stable ICU 2.2
139     */
140    virtual UClassID getDynamicClassID() const;
141
142private:
143    // ===================== PRIVATES ==============================
144    // private default constructor
145    CanonicalIterator();
146
147
148    /**
149     * Copy constructor. Private for now.
150     * @internal
151     */
152    CanonicalIterator(const CanonicalIterator& other);
153
154    /**
155     * Assignment operator. Private for now.
156     * @internal
157     */
158    CanonicalIterator& operator=(const CanonicalIterator& other);
159
160    // fields
161    UnicodeString source;
162    UBool done;
163
164    // 2 dimensional array holds the pieces of the string with
165    // their different canonically equivalent representations
166    UnicodeString **pieces;
167    int32_t pieces_length;
168    int32_t *pieces_lengths;
169
170    // current is used in iterating to combine pieces
171    int32_t *current;
172    int32_t current_length;
173
174    // transient fields
175    UnicodeString buffer;
176
177    // we have a segment, in NFD. Find all the strings that are canonically equivalent to it.
178    UnicodeString *getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status); //private String[] getEquivalents(String segment)
179
180    //Set getEquivalents2(String segment);
181    Hashtable *getEquivalents2(Hashtable *fillinResult, const UChar *segment, int32_t segLen, UErrorCode &status);
182    //Hashtable *getEquivalents2(const UnicodeString &segment, int32_t segLen, UErrorCode &status);
183
184    /**
185     * See if the decomposition of cp2 is at segment starting at segmentPos
186     * (with canonical rearrangment!)
187     * If so, take the remainder, and return the equivalents
188     */
189    //Set extract(int comp, String segment, int segmentPos, StringBuffer buffer);
190    Hashtable *extract(Hashtable *fillinResult, UChar32 comp, const UChar *segment, int32_t segLen, int32_t segmentPos, UErrorCode &status);
191    //Hashtable *extract(UChar32 comp, const UnicodeString &segment, int32_t segLen, int32_t segmentPos, UErrorCode &status);
192
193    void cleanPieces();
194
195};
196
197U_NAMESPACE_END
198
199#endif /* #if !UCONFIG_NO_NORMALIZATION */
200
201#endif
202