]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
4388f060 A |
3 | /* |
4 | ******************************************************************************* | |
5 | * | |
57a6839d | 6 | * Copyright (C) 2011-2014 International Business Machines |
4388f060 A |
7 | * Corporation and others. All Rights Reserved. |
8 | * | |
9 | ******************************************************************************* | |
10 | */ | |
11 | ||
12 | #ifndef INDEXCHARS_H | |
13 | #define INDEXCHARS_H | |
14 | ||
15 | #include "unicode/utypes.h" | |
16 | #include "unicode/uobject.h" | |
17 | #include "unicode/locid.h" | |
f3c0d7a5 | 18 | #include "unicode/unistr.h" |
4388f060 | 19 | |
57a6839d | 20 | #if !UCONFIG_NO_COLLATION |
4388f060 A |
21 | |
22 | /** | |
23 | * \file | |
24 | * \brief C++ API: Index Characters | |
25 | */ | |
26 | ||
27 | U_CDECL_BEGIN | |
28 | ||
29 | /** | |
30 | * Constants for Alphabetic Index Label Types. | |
31 | * The form of these enum constants anticipates having a plain C API | |
32 | * for Alphabetic Indexes that will also use them. | |
33 | * @stable ICU 4.8 | |
34 | */ | |
35 | typedef enum UAlphabeticIndexLabelType { | |
36 | /** | |
37 | * Normal Label, typically the starting letter of the names | |
38 | * in the bucket with this label. | |
39 | * @stable ICU 4.8 | |
40 | */ | |
41 | U_ALPHAINDEX_NORMAL = 0, | |
42 | ||
43 | /** | |
44 | * Undeflow Label. The bucket with this label contains names | |
45 | * in scripts that sort before any of the bucket labels in this index. | |
46 | * @stable ICU 4.8 | |
47 | */ | |
48 | U_ALPHAINDEX_UNDERFLOW = 1, | |
49 | ||
50 | /** | |
51 | * Inflow Label. The bucket with this label contains names | |
52 | * in scripts that sort between two of the bucket labels in this index. | |
53 | * Inflow labels are created when an index contains normal labels for | |
54 | * multiple scripts, and skips other scripts that sort between some of the | |
55 | * included scripts. | |
56 | * @stable ICU 4.8 | |
57 | */ | |
58 | U_ALPHAINDEX_INFLOW = 2, | |
59 | ||
60 | /** | |
61 | * Overflow Label. Te bucket with this label contains names in scripts | |
62 | * that sort after all of the bucket labels in this index. | |
63 | * @stable ICU 4.8 | |
64 | */ | |
65 | U_ALPHAINDEX_OVERFLOW = 3 | |
66 | } UAlphabeticIndexLabelType; | |
67 | ||
68 | ||
69 | struct UHashtable; | |
70 | U_CDECL_END | |
71 | ||
f3c0d7a5 | 72 | #if U_SHOW_CPLUSPLUS_API |
4388f060 A |
73 | U_NAMESPACE_BEGIN |
74 | ||
75 | // Forward Declarations | |
76 | ||
51004dcb | 77 | class BucketList; |
4388f060 A |
78 | class Collator; |
79 | class RuleBasedCollator; | |
80 | class StringEnumeration; | |
81 | class UnicodeSet; | |
82 | class UVector; | |
83 | ||
4388f060 | 84 | /** |
51004dcb A |
85 | * AlphabeticIndex supports the creation of a UI index appropriate for a given language. |
86 | * It can support either direct use, or use with a client that doesn't support localized collation. | |
87 | * The following is an example of what an index might look like in a UI: | |
88 | * | |
4388f060 | 89 | * <pre> |
51004dcb | 90 | * <b>... A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ...</b> |
4388f060 A |
91 | * |
92 | * <b>A</b> | |
93 | * Addison | |
94 | * Albertson | |
95 | * Azensky | |
96 | * <b>B</b> | |
97 | * Baker | |
98 | * ... | |
99 | * </pre> | |
100 | * | |
101 | * The class can generate a list of labels for use as a UI "index", that is, a list of | |
102 | * clickable characters (or character sequences) that allow the user to see a segment | |
103 | * (bucket) of a larger "target" list. That is, each label corresponds to a bucket in | |
104 | * the target list, where everything in the bucket is greater than or equal to the character | |
105 | * (according to the locale's collation). Strings can be added to the index; | |
106 | * they will be in sorted order in the right bucket. | |
107 | * <p> | |
108 | * The class also supports having buckets for strings before the first (underflow), | |
109 | * after the last (overflow), and between scripts (inflow). For example, if the index | |
110 | * is constructed with labels for Russian and English, Greek characters would fall | |
111 | * into an inflow bucket between the other two scripts. | |
112 | * <p> | |
113 | * The AlphabeticIndex class is not intended for public subclassing. | |
51004dcb A |
114 | * |
115 | * <p><em>Note:</em> If you expect to have a lot of ASCII or Latin characters | |
116 | * as well as characters from the user's language, | |
117 | * then it is a good idea to call addLabels(Locale::getEnglish(), status).</p> | |
118 | * | |
119 | * <h2>Direct Use</h2> | |
120 | * <p>The following shows an example of building an index directly. | |
121 | * The "show..." methods below are just to illustrate usage. | |
4388f060 A |
122 | * |
123 | * <pre> | |
124 | * // Create a simple index. "Item" is assumed to be an application | |
125 | * // defined type that the application's UI and other processing knows about, | |
126 | * // and that has a name. | |
127 | * | |
128 | * UErrorCode status = U_ZERO_ERROR; | |
129 | * AlphabeticIndex index = new AlphabeticIndex(desiredLocale, status); | |
130 | * index->addLabels(additionalLocale, status); | |
131 | * for (Item *item in some source of Items ) { | |
132 | * index->addRecord(item->name(), item, status); | |
133 | * } | |
134 | * ... | |
135 | * // Show index at top. We could skip or gray out empty buckets | |
136 | * | |
137 | * while (index->nextBucket(status)) { | |
138 | * if (showAll || index->getBucketRecordCount() != 0) { | |
139 | * showLabelAtTop(UI, index->getBucketLabel()); | |
140 | * } | |
141 | * } | |
142 | * ... | |
143 | * // Show the buckets with their contents, skipping empty buckets | |
144 | * | |
145 | * index->resetBucketIterator(status); | |
146 | * while (index->nextBucket(status)) { | |
147 | * if (index->getBucketRecordCount() != 0) { | |
148 | * showLabelInList(UI, index->getBucketLabel()); | |
149 | * while (index->nextRecord(status)) { | |
150 | * showIndexedItem(UI, static_cast<Item *>(index->getRecordData())) | |
151 | * </pre> | |
152 | * | |
153 | * The caller can build different UIs using this class. | |
154 | * For example, an index character could be omitted or grayed-out | |
155 | * if its bucket is empty. Small buckets could also be combined based on size, such as: | |
156 | * | |
157 | * <pre> | |
158 | * <b>... A-F G-N O-Z ...</b> | |
159 | * </pre> | |
160 | * | |
51004dcb A |
161 | * <h2>Client Support</h2> |
162 | * <p>Callers can also use the AlphabeticIndex::ImmutableIndex, or the AlphabeticIndex itself, | |
163 | * to support sorting on a client that doesn't support AlphabeticIndex functionality. | |
164 | * | |
165 | * <p>The ImmutableIndex is both immutable and thread-safe. | |
166 | * The corresponding AlphabeticIndex methods are not thread-safe because | |
167 | * they "lazily" build the index buckets. | |
4388f060 | 168 | * <ul> |
51004dcb A |
169 | * <li>ImmutableIndex.getBucket(index) provides random access to all |
170 | * buckets and their labels and label types. | |
171 | * <li>The AlphabeticIndex bucket iterator or ImmutableIndex.getBucket(0..getBucketCount-1) | |
172 | * can be used to get a list of the labels, | |
173 | * such as "...", "A", "B",..., and send that list to the client. | |
174 | * <li>When the client has a new name, it sends that name to the server. | |
175 | * The server needs to call the following methods, | |
176 | * and communicate the bucketIndex and collationKey back to the client. | |
177 | * | |
178 | * <pre> | |
179 | * int32_t bucketIndex = index.getBucketIndex(name, status); | |
180 | * const UnicodeString &label = immutableIndex.getBucket(bucketIndex)->getLabel(); // optional | |
181 | * int32_t skLength = collator.getSortKey(name, sk, skCapacity); | |
182 | * </pre> | |
183 | * | |
184 | * <li>The client would put the name (and associated information) into its bucket for bucketIndex. The sort key sk is a | |
185 | * sequence of bytes that can be compared with a binary compare, and produce the right localized result.</li> | |
4388f060 A |
186 | * </ul> |
187 | * | |
188 | * @stable ICU 4.8 | |
189 | */ | |
4388f060 | 190 | class U_I18N_API AlphabeticIndex: public UObject { |
51004dcb | 191 | public: |
51004dcb A |
192 | /** |
193 | * An index "bucket" with a label string and type. | |
194 | * It is referenced by getBucketIndex(), | |
195 | * and returned by ImmutableIndex.getBucket(). | |
196 | * | |
197 | * The Bucket class is not intended for public subclassing. | |
57a6839d | 198 | * @stable ICU 51 |
51004dcb A |
199 | */ |
200 | class U_I18N_API Bucket : public UObject { | |
201 | public: | |
202 | /** | |
203 | * Destructor. | |
57a6839d | 204 | * @stable ICU 51 |
51004dcb A |
205 | */ |
206 | virtual ~Bucket(); | |
207 | ||
208 | /** | |
209 | * Returns the label string. | |
210 | * | |
211 | * @return the label string for the bucket | |
57a6839d | 212 | * @stable ICU 51 |
51004dcb A |
213 | */ |
214 | const UnicodeString &getLabel() const { return label_; } | |
215 | /** | |
216 | * Returns whether this bucket is a normal, underflow, overflow, or inflow bucket. | |
217 | * | |
218 | * @return the bucket label type | |
57a6839d | 219 | * @stable ICU 51 |
51004dcb A |
220 | */ |
221 | UAlphabeticIndexLabelType getLabelType() const { return labelType_; } | |
222 | ||
223 | private: | |
224 | friend class AlphabeticIndex; | |
225 | friend class BucketList; | |
226 | ||
227 | UnicodeString label_; | |
228 | UnicodeString lowerBoundary_; | |
229 | UAlphabeticIndexLabelType labelType_; | |
230 | Bucket *displayBucket_; | |
231 | int32_t displayIndex_; | |
232 | UVector *records_; // Records are owned by the inputList_ vector. | |
233 | ||
234 | Bucket(const UnicodeString &label, // Parameter strings are copied. | |
235 | const UnicodeString &lowerBoundary, | |
236 | UAlphabeticIndexLabelType type); | |
237 | }; | |
4388f060 | 238 | |
51004dcb A |
239 | /** |
240 | * Immutable, thread-safe version of AlphabeticIndex. | |
241 | * This class provides thread-safe methods for bucketing, | |
242 | * and random access to buckets and their properties, | |
243 | * but does not offer adding records to the index. | |
244 | * | |
245 | * The ImmutableIndex class is not intended for public subclassing. | |
246 | * | |
57a6839d | 247 | * @stable ICU 51 |
51004dcb A |
248 | */ |
249 | class U_I18N_API ImmutableIndex : public UObject { | |
250 | public: | |
251 | /** | |
252 | * Destructor. | |
57a6839d | 253 | * @stable ICU 51 |
51004dcb A |
254 | */ |
255 | virtual ~ImmutableIndex(); | |
256 | ||
257 | /** | |
258 | * Returns the number of index buckets and labels, including underflow/inflow/overflow. | |
259 | * | |
260 | * @return the number of index buckets | |
57a6839d | 261 | * @stable ICU 51 |
51004dcb A |
262 | */ |
263 | int32_t getBucketCount() const; | |
264 | ||
265 | /** | |
266 | * Finds the index bucket for the given name and returns the number of that bucket. | |
267 | * Use getBucket() to get the bucket's properties. | |
268 | * | |
269 | * @param name the string to be sorted into an index bucket | |
270 | * @return the bucket number for the name | |
57a6839d | 271 | * @stable ICU 51 |
51004dcb A |
272 | */ |
273 | int32_t getBucketIndex(const UnicodeString &name, UErrorCode &errorCode) const; | |
274 | ||
275 | /** | |
276 | * Returns the index-th bucket. Returns NULL if the index is out of range. | |
277 | * | |
278 | * @param index bucket number | |
279 | * @return the index-th bucket | |
57a6839d | 280 | * @stable ICU 51 |
51004dcb A |
281 | */ |
282 | const Bucket *getBucket(int32_t index) const; | |
283 | ||
284 | private: | |
285 | friend class AlphabeticIndex; | |
286 | ||
287 | ImmutableIndex(BucketList *bucketList, Collator *collatorPrimaryOnly) | |
288 | : buckets_(bucketList), collatorPrimaryOnly_(collatorPrimaryOnly) {} | |
289 | ||
290 | BucketList *buckets_; | |
291 | Collator *collatorPrimaryOnly_; | |
292 | }; | |
4388f060 A |
293 | |
294 | /** | |
295 | * Construct an AlphabeticIndex object for the specified locale. If the locale's | |
296 | * data does not include index characters, a set of them will be | |
297 | * synthesized based on the locale's exemplar characters. The locale | |
298 | * determines the sorting order for both the index characters and the | |
299 | * user item names appearing under each Index character. | |
300 | * | |
301 | * @param locale the desired locale. | |
302 | * @param status Error code, will be set with the reason if the construction | |
303 | * of the AlphabeticIndex object fails. | |
304 | * @stable ICU 4.8 | |
305 | */ | |
306 | AlphabeticIndex(const Locale &locale, UErrorCode &status); | |
307 | ||
51004dcb A |
308 | /** |
309 | * Construct an AlphabeticIndex that uses a specific collator. | |
310 | * | |
311 | * The index will be created with no labels; the addLabels() function must be called | |
312 | * after creation to add the desired labels to the index. | |
313 | * | |
314 | * The index adopts the collator, and is responsible for deleting it. | |
315 | * The caller should make no further use of the collator after creating the index. | |
316 | * | |
317 | * @param collator The collator to use to order the contents of this index. | |
318 | * @param status Error code, will be set with the reason if the | |
319 | * operation fails. | |
57a6839d | 320 | * @stable ICU 51 |
51004dcb A |
321 | */ |
322 | AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status); | |
4388f060 A |
323 | |
324 | /** | |
325 | * Add Labels to this Index. The labels are additions to those | |
326 | * that are already in the index; they do not replace the existing | |
327 | * ones. | |
328 | * @param additions The additional characters to add to the index, such as A-Z. | |
329 | * @param status Error code, will be set with the reason if the | |
330 | * operation fails. | |
331 | * @return this, for chaining | |
332 | * @stable ICU 4.8 | |
333 | */ | |
51004dcb | 334 | virtual AlphabeticIndex &addLabels(const UnicodeSet &additions, UErrorCode &status); |
4388f060 A |
335 | |
336 | /** | |
337 | * Add the index characters from a Locale to the index. The labels | |
338 | * are added to those that are already in the index; they do not replace the | |
339 | * existing index characters. The collation order for this index is not | |
340 | * changed; it remains that of the locale that was originally specified | |
341 | * when creating this Index. | |
342 | * | |
343 | * @param locale The locale whose index characters are to be added. | |
344 | * @param status Error code, will be set with the reason if the | |
345 | * operation fails. | |
346 | * @return this, for chaining | |
347 | * @stable ICU 4.8 | |
348 | */ | |
51004dcb | 349 | virtual AlphabeticIndex &addLabels(const Locale &locale, UErrorCode &status); |
4388f060 A |
350 | |
351 | /** | |
352 | * Destructor | |
353 | * @stable ICU 4.8 | |
354 | */ | |
51004dcb | 355 | virtual ~AlphabeticIndex(); |
4388f060 | 356 | |
51004dcb A |
357 | /** |
358 | * Builds an immutable, thread-safe version of this instance, without data records. | |
359 | * | |
360 | * @return an immutable index instance | |
57a6839d | 361 | * @stable ICU 51 |
51004dcb A |
362 | */ |
363 | ImmutableIndex *buildImmutableIndex(UErrorCode &errorCode); | |
4388f060 A |
364 | |
365 | /** | |
366 | * Get the Collator that establishes the ordering of the items in this index. | |
367 | * Ownership of the collator remains with the AlphabeticIndex instance. | |
368 | * | |
369 | * The returned collator is a reference to the internal collator used by this | |
370 | * index. It may be safely used to compare the names of items or to get | |
371 | * sort keys for names. However if any settings need to be changed, | |
372 | * or other non-const methods called, a cloned copy must be made first. | |
373 | * | |
374 | * @return The collator | |
375 | * @stable ICU 4.8 | |
376 | */ | |
377 | virtual const RuleBasedCollator &getCollator() const; | |
378 | ||
379 | ||
380 | /** | |
381 | * Get the default label used for abbreviated buckets <i>between</i> other index characters. | |
382 | * For example, consider the labels when Latin and Greek are used: | |
383 | * X Y Z ... Α Β Γ. | |
384 | * | |
385 | * @return inflow label | |
386 | * @stable ICU 4.8 | |
387 | */ | |
388 | virtual const UnicodeString &getInflowLabel() const; | |
389 | ||
390 | /** | |
391 | * Set the default label used for abbreviated buckets <i>between</i> other index characters. | |
392 | * An inflow label will be automatically inserted if two otherwise-adjacent label characters | |
393 | * are from different scripts, e.g. Latin and Cyrillic, and a third script, e.g. Greek, | |
394 | * sorts between the two. The default inflow character is an ellipsis (...) | |
395 | * | |
396 | * @param inflowLabel the new Inflow label. | |
397 | * @param status Error code, will be set with the reason if the operation fails. | |
398 | * @return this | |
399 | * @stable ICU 4.8 | |
400 | */ | |
401 | virtual AlphabeticIndex &setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status); | |
402 | ||
403 | ||
4388f060 A |
404 | /** |
405 | * Get the special label used for items that sort after the last normal label, | |
406 | * and that would not otherwise have an appropriate label. | |
407 | * | |
408 | * @return the overflow label | |
409 | * @stable ICU 4.8 | |
410 | */ | |
411 | virtual const UnicodeString &getOverflowLabel() const; | |
412 | ||
413 | ||
414 | /** | |
415 | * Set the label used for items that sort after the last normal label, | |
416 | * and that would not otherwise have an appropriate label. | |
417 | * | |
418 | * @param overflowLabel the new overflow label. | |
419 | * @param status Error code, will be set with the reason if the operation fails. | |
420 | * @return this | |
421 | * @stable ICU 4.8 | |
422 | */ | |
423 | virtual AlphabeticIndex &setOverflowLabel(const UnicodeString &overflowLabel, UErrorCode &status); | |
424 | ||
425 | /** | |
426 | * Get the special label used for items that sort before the first normal label, | |
427 | * and that would not otherwise have an appropriate label. | |
428 | * | |
429 | * @return underflow label | |
430 | * @stable ICU 4.8 | |
431 | */ | |
432 | virtual const UnicodeString &getUnderflowLabel() const; | |
433 | ||
434 | /** | |
435 | * Set the label used for items that sort before the first normal label, | |
436 | * and that would not otherwise have an appropriate label. | |
437 | * | |
438 | * @param underflowLabel the new underflow label. | |
439 | * @param status Error code, will be set with the reason if the operation fails. | |
440 | * @return this | |
441 | * @stable ICU 4.8 | |
442 | */ | |
443 | virtual AlphabeticIndex &setUnderflowLabel(const UnicodeString &underflowLabel, UErrorCode &status); | |
444 | ||
445 | ||
446 | /** | |
447 | * Get the limit on the number of labels permitted in the index. | |
448 | * The number does not include over, under and inflow labels. | |
449 | * | |
450 | * @return maxLabelCount maximum number of labels. | |
451 | * @stable ICU 4.8 | |
452 | */ | |
453 | virtual int32_t getMaxLabelCount() const; | |
454 | ||
455 | /** | |
456 | * Set a limit on the number of labels permitted in the index. | |
457 | * The number does not include over, under and inflow labels. | |
458 | * Currently, if the number is exceeded, then every | |
459 | * nth item is removed to bring the count down. | |
460 | * A more sophisticated mechanism may be available in the future. | |
461 | * | |
462 | * @param maxLabelCount the maximum number of labels. | |
463 | * @param status error code | |
464 | * @return This, for chaining | |
465 | * @stable ICU 4.8 | |
466 | */ | |
467 | virtual AlphabeticIndex &setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status); | |
468 | ||
469 | ||
4388f060 A |
470 | /** |
471 | * Add a record to the index. Each record will be associated with an index Bucket | |
472 | * based on the record's name. The list of records for each bucket will be sorted | |
473 | * based on the collation ordering of the names in the index's locale. | |
474 | * Records with duplicate names are permitted; they will be kept in the order | |
475 | * that they were added. | |
476 | * | |
477 | * @param name The display name for the Record. The Record will be placed in | |
478 | * a bucket based on this name. | |
479 | * @param data An optional pointer to user data associated with this | |
480 | * item. When iterating the contents of a bucket, both the | |
481 | * data pointer the name will be available for each Record. | |
482 | * @param status Error code, will be set with the reason if the operation fails. | |
483 | * @return This, for chaining. | |
484 | * @stable ICU 4.8 | |
485 | */ | |
486 | virtual AlphabeticIndex &addRecord(const UnicodeString &name, const void *data, UErrorCode &status); | |
487 | ||
488 | /** | |
489 | * Remove all Records from the Index. The set of Buckets, which define the headings under | |
490 | * which records are classified, is not altered. | |
491 | * | |
492 | * @param status Error code, will be set with the reason if the operation fails. | |
493 | * @return This, for chaining. | |
494 | * @stable ICU 4.8 | |
495 | */ | |
496 | virtual AlphabeticIndex &clearRecords(UErrorCode &status); | |
497 | ||
498 | ||
499 | /** Get the number of labels in this index. | |
500 | * Note: may trigger lazy index construction. | |
501 | * | |
502 | * @param status Error code, will be set with the reason if the operation fails. | |
503 | * @return The number of labels in this index, including any under, over or | |
504 | * in-flow labels. | |
505 | * @stable ICU 4.8 | |
506 | */ | |
507 | virtual int32_t getBucketCount(UErrorCode &status); | |
508 | ||
509 | ||
510 | /** Get the total number of Records in this index, that is, the number | |
511 | * of <name, data> pairs added. | |
512 | * | |
513 | * @param status Error code, will be set with the reason if the operation fails. | |
514 | * @return The number of records in this index, that is, the total number | |
515 | * of (name, data) items added with addRecord(). | |
516 | * @stable ICU 4.8 | |
517 | */ | |
518 | virtual int32_t getRecordCount(UErrorCode &status); | |
519 | ||
520 | ||
521 | ||
522 | /** | |
523 | * Given the name of a record, return the zero-based index of the Bucket | |
524 | * in which the item should appear. The name need not be in the index. | |
525 | * A Record will not be added to the index by this function. | |
526 | * Bucket numbers are zero-based, in Bucket iteration order. | |
527 | * | |
528 | * @param itemName The name whose bucket position in the index is to be determined. | |
529 | * @param status Error code, will be set with the reason if the operation fails. | |
530 | * @return The bucket number for this name. | |
531 | * @stable ICU 4.8 | |
532 | * | |
533 | */ | |
534 | virtual int32_t getBucketIndex(const UnicodeString &itemName, UErrorCode &status); | |
535 | ||
536 | ||
537 | /** | |
538 | * Get the zero based index of the current Bucket from an iteration | |
539 | * over the Buckets of this index. Return -1 if no iteration is in process. | |
540 | * @return the index of the current Bucket | |
541 | * @stable ICU 4.8 | |
542 | */ | |
543 | virtual int32_t getBucketIndex() const; | |
544 | ||
545 | ||
546 | /** | |
547 | * Advance the iteration over the Buckets of this index. Return FALSE if | |
548 | * there are no more Buckets. | |
549 | * | |
550 | * @param status Error code, will be set with the reason if the operation fails. | |
551 | * U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while | |
552 | * an enumeration of its contents are in process. | |
553 | * | |
554 | * @return TRUE if success, FALSE if at end of iteration | |
555 | * @stable ICU 4.8 | |
556 | */ | |
557 | virtual UBool nextBucket(UErrorCode &status); | |
558 | ||
559 | /** | |
560 | * Return the name of the Label of the current bucket from an iteration over the buckets. | |
561 | * If the iteration is before the first Bucket (nextBucket() has not been called), | |
562 | * or after the last, return an empty string. | |
563 | * | |
564 | * @return the bucket label. | |
565 | * @stable ICU 4.8 | |
566 | */ | |
567 | virtual const UnicodeString &getBucketLabel() const; | |
568 | ||
569 | /** | |
570 | * Return the type of the label for the current Bucket (selected by the | |
571 | * iteration over Buckets.) | |
572 | * | |
573 | * @return the label type. | |
574 | * @stable ICU 4.8 | |
575 | */ | |
576 | virtual UAlphabeticIndexLabelType getBucketLabelType() const; | |
577 | ||
578 | /** | |
579 | * Get the number of <name, data> Records in the current Bucket. | |
580 | * If the current bucket iteration position is before the first label or after the | |
581 | * last, return 0. | |
582 | * | |
583 | * @return the number of Records. | |
584 | * @stable ICU 4.8 | |
585 | */ | |
586 | virtual int32_t getBucketRecordCount() const; | |
587 | ||
588 | ||
589 | /** | |
590 | * Reset the Bucket iteration for this index. The next call to nextBucket() | |
591 | * will restart the iteration at the first label. | |
592 | * | |
593 | * @param status Error code, will be set with the reason if the operation fails. | |
594 | * @return this, for chaining. | |
595 | * @stable ICU 4.8 | |
596 | */ | |
597 | virtual AlphabeticIndex &resetBucketIterator(UErrorCode &status); | |
598 | ||
599 | /** | |
600 | * Advance to the next record in the current Bucket. | |
601 | * When nextBucket() is called, Record iteration is reset to just before the | |
602 | * first Record in the new Bucket. | |
603 | * | |
604 | * @param status Error code, will be set with the reason if the operation fails. | |
605 | * U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while | |
606 | * an enumeration of its contents are in process. | |
607 | * @return TRUE if successful, FALSE when the iteration advances past the last item. | |
608 | * @stable ICU 4.8 | |
609 | */ | |
610 | virtual UBool nextRecord(UErrorCode &status); | |
611 | ||
612 | /** | |
613 | * Get the name of the current Record. | |
614 | * Return an empty string if the Record iteration position is before first | |
615 | * or after the last. | |
616 | * | |
617 | * @return The name of the current index item. | |
618 | * @stable ICU 4.8 | |
619 | */ | |
620 | virtual const UnicodeString &getRecordName() const; | |
621 | ||
622 | ||
623 | /** | |
624 | * Return the data pointer of the Record currently being iterated over. | |
625 | * Return NULL if the current iteration position before the first item in this Bucket, | |
626 | * or after the last. | |
627 | * | |
628 | * @return The current Record's data pointer. | |
629 | * @stable ICU 4.8 | |
630 | */ | |
631 | virtual const void *getRecordData() const; | |
632 | ||
633 | ||
634 | /** | |
635 | * Reset the Record iterator position to before the first Record in the current Bucket. | |
636 | * | |
637 | * @return This, for chaining. | |
638 | * @stable ICU 4.8 | |
639 | */ | |
640 | virtual AlphabeticIndex &resetRecordIterator(); | |
641 | ||
642 | private: | |
4388f060 A |
643 | /** |
644 | * No Copy constructor. | |
645 | * @internal | |
646 | */ | |
647 | AlphabeticIndex(const AlphabeticIndex &other); | |
648 | ||
649 | /** | |
650 | * No assignment. | |
651 | */ | |
652 | AlphabeticIndex &operator =(const AlphabeticIndex & /*other*/) { return *this;}; | |
653 | ||
654 | /** | |
655 | * No Equality operators. | |
656 | * @internal | |
657 | */ | |
658 | virtual UBool operator==(const AlphabeticIndex& other) const; | |
659 | ||
660 | /** | |
661 | * Inequality operator. | |
662 | * @internal | |
663 | */ | |
664 | virtual UBool operator!=(const AlphabeticIndex& other) const; | |
665 | ||
666 | // Common initialization, for use from all constructors. | |
51004dcb | 667 | void init(const Locale *locale, UErrorCode &status); |
4388f060 | 668 | |
51004dcb A |
669 | /** |
670 | * This method is called to get the index exemplars. Normally these come from the locale directly, | |
671 | * but if they aren't available, we have to synthesize them. | |
672 | */ | |
673 | void addIndexExemplars(const Locale &locale, UErrorCode &status); | |
674 | /** | |
675 | * Add Chinese index characters from the tailoring. | |
676 | */ | |
677 | UBool addChineseIndexCharacters(UErrorCode &errorCode); | |
4388f060 | 678 | |
51004dcb | 679 | UVector *firstStringsInScript(UErrorCode &status); |
4388f060 | 680 | |
51004dcb | 681 | static UnicodeString separated(const UnicodeString &item); |
4388f060 | 682 | |
51004dcb A |
683 | /** |
684 | * Determine the best labels to use. | |
685 | * This is based on the exemplars, but we also process to make sure that they are unique, | |
686 | * and sort differently, and that the overall list is small enough. | |
687 | */ | |
688 | void initLabels(UVector &indexCharacters, UErrorCode &errorCode) const; | |
689 | BucketList *createBucketList(UErrorCode &errorCode) const; | |
690 | void initBuckets(UErrorCode &errorCode); | |
691 | void clearBuckets(); | |
692 | void internalResetBucketIterator(); | |
4388f060 | 693 | |
51004dcb | 694 | public: |
4388f060 | 695 | |
51004dcb A |
696 | // The Record is declared public only to allow access from |
697 | // implementation code written in plain C. | |
698 | // It is not intended for public use. | |
4388f060 A |
699 | |
700 | #ifndef U_HIDE_INTERNAL_API | |
701 | /** | |
51004dcb A |
702 | * A (name, data) pair, to be sorted by name into one of the index buckets. |
703 | * The user data is not used by the index implementation. | |
4388f060 A |
704 | * @internal |
705 | */ | |
51004dcb A |
706 | struct Record: public UMemory { |
707 | const UnicodeString name_; | |
708 | const void *data_; | |
709 | Record(const UnicodeString &name, const void *data); | |
710 | ~Record(); | |
711 | }; | |
4388f060 A |
712 | #endif /* U_HIDE_INTERNAL_API */ |
713 | ||
51004dcb | 714 | private: |
4388f060 A |
715 | |
716 | /** | |
51004dcb A |
717 | * Holds all user records before they are distributed into buckets. |
718 | * Type of contents is (Record *) | |
719 | * @internal | |
720 | */ | |
721 | UVector *inputList_; | |
4388f060 | 722 | |
51004dcb A |
723 | int32_t labelsIterIndex_; // Index of next item to return. |
724 | int32_t itemsIterIndex_; | |
725 | Bucket *currentBucket_; // While an iteration of the index in underway, | |
726 | // point to the bucket for the current label. | |
727 | // NULL when no iteration underway. | |
4388f060 | 728 | |
51004dcb | 729 | int32_t maxLabelCount_; // Limit on # of labels permitted in the index. |
4388f060 | 730 | |
51004dcb A |
731 | UnicodeSet *initialLabels_; // Initial (unprocessed) set of Labels. Union |
732 | // of those explicitly set by the user plus | |
733 | // those from locales. Raw values, before | |
734 | // crunching into bucket labels. | |
4388f060 | 735 | |
51004dcb A |
736 | UVector *firstCharsInScripts_; // The first character from each script, |
737 | // in collation order. | |
4388f060 | 738 | |
51004dcb A |
739 | RuleBasedCollator *collator_; |
740 | RuleBasedCollator *collatorPrimaryOnly_; | |
4388f060 | 741 | |
51004dcb A |
742 | // Lazy evaluated: null means that we have not built yet. |
743 | BucketList *buckets_; | |
4388f060 | 744 | |
51004dcb A |
745 | UnicodeString inflowLabel_; |
746 | UnicodeString overflowLabel_; | |
747 | UnicodeString underflowLabel_; | |
748 | UnicodeString overflowComparisonString_; | |
4388f060 | 749 | |
51004dcb | 750 | UnicodeString emptyString_; |
4388f060 A |
751 | }; |
752 | ||
753 | U_NAMESPACE_END | |
f3c0d7a5 | 754 | #endif // U_SHOW_CPLUSPLUS_API |
4388f060 | 755 | |
57a6839d | 756 | #endif // !UCONFIG_NO_COLLATION |
4388f060 | 757 | #endif |