]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/alphaindex.h
ICU-491.11.2.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / alphaindex.h
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2011-2012 International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 */
9
10 #ifndef INDEXCHARS_H
11 #define INDEXCHARS_H
12
13 #include "unicode/utypes.h"
14 #include "unicode/uobject.h"
15 #include "unicode/locid.h"
16
17
18 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION
19
20 /**
21 * \file
22 * \brief C++ API: Index Characters
23 */
24
25 U_CDECL_BEGIN
26
27 /**
28 * Constants for Alphabetic Index Label Types.
29 * The form of these enum constants anticipates having a plain C API
30 * for Alphabetic Indexes that will also use them.
31 * @stable ICU 4.8
32 */
33 typedef enum UAlphabeticIndexLabelType {
34 /**
35 * Normal Label, typically the starting letter of the names
36 * in the bucket with this label.
37 * @stable ICU 4.8
38 */
39 U_ALPHAINDEX_NORMAL = 0,
40
41 /**
42 * Undeflow Label. The bucket with this label contains names
43 * in scripts that sort before any of the bucket labels in this index.
44 * @stable ICU 4.8
45 */
46 U_ALPHAINDEX_UNDERFLOW = 1,
47
48 /**
49 * Inflow Label. The bucket with this label contains names
50 * in scripts that sort between two of the bucket labels in this index.
51 * Inflow labels are created when an index contains normal labels for
52 * multiple scripts, and skips other scripts that sort between some of the
53 * included scripts.
54 * @stable ICU 4.8
55 */
56 U_ALPHAINDEX_INFLOW = 2,
57
58 /**
59 * Overflow Label. Te bucket with this label contains names in scripts
60 * that sort after all of the bucket labels in this index.
61 * @stable ICU 4.8
62 */
63 U_ALPHAINDEX_OVERFLOW = 3
64 } UAlphabeticIndexLabelType;
65
66
67 struct UHashtable;
68 U_CDECL_END
69
70 U_NAMESPACE_BEGIN
71
72 // Forward Declarations
73
74 class Collator;
75 class RuleBasedCollator;
76 class StringEnumeration;
77 class UnicodeSet;
78 class UVector;
79
80
81
82 /**
83 * class AlphabeticIndex supports the creation of a UI index appropriate for a given language, such as:
84 *
85 * <pre>
86 * <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 \\u00C6 \\u00D8 \\u00C5 ...</b>
87 *
88 * <b>A</b>
89 * Addison
90 * Albertson
91 * Azensky
92 * <b>B</b>
93 * Baker
94 * ...
95 * </pre>
96 *
97 * The class can generate a list of labels for use as a UI "index", that is, a list of
98 * clickable characters (or character sequences) that allow the user to see a segment
99 * (bucket) of a larger "target" list. That is, each label corresponds to a bucket in
100 * the target list, where everything in the bucket is greater than or equal to the character
101 * (according to the locale's collation). Strings can be added to the index;
102 * they will be in sorted order in the right bucket.
103 * <p>
104 * The class also supports having buckets for strings before the first (underflow),
105 * after the last (overflow), and between scripts (inflow). For example, if the index
106 * is constructed with labels for Russian and English, Greek characters would fall
107 * into an inflow bucket between the other two scripts.
108 * <p>
109 * The AlphabeticIndex class is not intended for public subclassing.
110 * <p>
111 * <i>Example</i>
112 * <p>
113 * The "show..." methods below are just to illustrate usage.
114 *
115 * <pre>
116 * // Create a simple index. "Item" is assumed to be an application
117 * // defined type that the application's UI and other processing knows about,
118 * // and that has a name.
119 *
120 * UErrorCode status = U_ZERO_ERROR;
121 * AlphabeticIndex index = new AlphabeticIndex(desiredLocale, status);
122 * index->addLabels(additionalLocale, status);
123 * for (Item *item in some source of Items ) {
124 * index->addRecord(item->name(), item, status);
125 * }
126 * ...
127 * // Show index at top. We could skip or gray out empty buckets
128 *
129 * while (index->nextBucket(status)) {
130 * if (showAll || index->getBucketRecordCount() != 0) {
131 * showLabelAtTop(UI, index->getBucketLabel());
132 * }
133 * }
134 * ...
135 * // Show the buckets with their contents, skipping empty buckets
136 *
137 * index->resetBucketIterator(status);
138 * while (index->nextBucket(status)) {
139 * if (index->getBucketRecordCount() != 0) {
140 * showLabelInList(UI, index->getBucketLabel());
141 * while (index->nextRecord(status)) {
142 * showIndexedItem(UI, static_cast<Item *>(index->getRecordData()))
143 * </pre>
144 *
145 * The caller can build different UIs using this class.
146 * For example, an index character could be omitted or grayed-out
147 * if its bucket is empty. Small buckets could also be combined based on size, such as:
148 *
149 * <pre>
150 * <b>... A-F G-N O-Z ...</b>
151 * </pre>
152 *
153 * <p>
154 * <b>Notes:</b>
155 * <ul>
156 * <li>Additional collation parameters can be passed in as part of the locale name.
157 * For example, German plus numeric
158 * sorting would be "de@kn-true".
159 * </ul>
160 *
161 * @stable ICU 4.8
162 */
163
164
165 class U_I18N_API AlphabeticIndex: public UObject {
166
167 public:
168
169 /**
170 * Construct an AlphabeticIndex object for the specified locale. If the locale's
171 * data does not include index characters, a set of them will be
172 * synthesized based on the locale's exemplar characters. The locale
173 * determines the sorting order for both the index characters and the
174 * user item names appearing under each Index character.
175 *
176 * @param locale the desired locale.
177 * @param status Error code, will be set with the reason if the construction
178 * of the AlphabeticIndex object fails.
179 * @stable ICU 4.8
180 */
181 AlphabeticIndex(const Locale &locale, UErrorCode &status);
182
183
184
185 /**
186 * Add Labels to this Index. The labels are additions to those
187 * that are already in the index; they do not replace the existing
188 * ones.
189 * @param additions The additional characters to add to the index, such as A-Z.
190 * @param status Error code, will be set with the reason if the
191 * operation fails.
192 * @return this, for chaining
193 * @stable ICU 4.8
194 */
195 virtual AlphabeticIndex &addLabels(const UnicodeSet &additions, UErrorCode &status);
196
197 /**
198 * Add the index characters from a Locale to the index. The labels
199 * are added to those that are already in the index; they do not replace the
200 * existing index characters. The collation order for this index is not
201 * changed; it remains that of the locale that was originally specified
202 * when creating this Index.
203 *
204 * @param locale The locale whose index characters are to be added.
205 * @param status Error code, will be set with the reason if the
206 * operation fails.
207 * @return this, for chaining
208 * @stable ICU 4.8
209 */
210 virtual AlphabeticIndex &addLabels(const Locale &locale, UErrorCode &status);
211
212 /**
213 * Destructor
214 * @stable ICU 4.8
215 */
216 virtual ~AlphabeticIndex();
217
218
219 /**
220 * Get the Collator that establishes the ordering of the items in this index.
221 * Ownership of the collator remains with the AlphabeticIndex instance.
222 *
223 * The returned collator is a reference to the internal collator used by this
224 * index. It may be safely used to compare the names of items or to get
225 * sort keys for names. However if any settings need to be changed,
226 * or other non-const methods called, a cloned copy must be made first.
227 *
228 * @return The collator
229 * @stable ICU 4.8
230 */
231 virtual const RuleBasedCollator &getCollator() const;
232
233
234 /**
235 * Get the default label used for abbreviated buckets <i>between</i> other index characters.
236 * For example, consider the labels when Latin and Greek are used:
237 * X Y Z ... &#x0391; &#x0392; &#x0393;.
238 *
239 * @return inflow label
240 * @stable ICU 4.8
241 */
242 virtual const UnicodeString &getInflowLabel() const;
243
244 /**
245 * Set the default label used for abbreviated buckets <i>between</i> other index characters.
246 * An inflow label will be automatically inserted if two otherwise-adjacent label characters
247 * are from different scripts, e.g. Latin and Cyrillic, and a third script, e.g. Greek,
248 * sorts between the two. The default inflow character is an ellipsis (...)
249 *
250 * @param inflowLabel the new Inflow label.
251 * @param status Error code, will be set with the reason if the operation fails.
252 * @return this
253 * @stable ICU 4.8
254 */
255 virtual AlphabeticIndex &setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status);
256
257
258
259 /**
260 * Get the special label used for items that sort after the last normal label,
261 * and that would not otherwise have an appropriate label.
262 *
263 * @return the overflow label
264 * @stable ICU 4.8
265 */
266 virtual const UnicodeString &getOverflowLabel() const;
267
268
269 /**
270 * Set the label used for items that sort after the last normal label,
271 * and that would not otherwise have an appropriate label.
272 *
273 * @param overflowLabel the new overflow label.
274 * @param status Error code, will be set with the reason if the operation fails.
275 * @return this
276 * @stable ICU 4.8
277 */
278 virtual AlphabeticIndex &setOverflowLabel(const UnicodeString &overflowLabel, UErrorCode &status);
279
280 /**
281 * Get the special label used for items that sort before the first normal label,
282 * and that would not otherwise have an appropriate label.
283 *
284 * @return underflow label
285 * @stable ICU 4.8
286 */
287 virtual const UnicodeString &getUnderflowLabel() const;
288
289 /**
290 * Set the label used for items that sort before the first normal label,
291 * and that would not otherwise have an appropriate label.
292 *
293 * @param underflowLabel the new underflow label.
294 * @param status Error code, will be set with the reason if the operation fails.
295 * @return this
296 * @stable ICU 4.8
297 */
298 virtual AlphabeticIndex &setUnderflowLabel(const UnicodeString &underflowLabel, UErrorCode &status);
299
300
301 /**
302 * Get the limit on the number of labels permitted in the index.
303 * The number does not include over, under and inflow labels.
304 *
305 * @return maxLabelCount maximum number of labels.
306 * @stable ICU 4.8
307 */
308 virtual int32_t getMaxLabelCount() const;
309
310 /**
311 * Set a limit on the number of labels permitted in the index.
312 * The number does not include over, under and inflow labels.
313 * Currently, if the number is exceeded, then every
314 * nth item is removed to bring the count down.
315 * A more sophisticated mechanism may be available in the future.
316 *
317 * @param maxLabelCount the maximum number of labels.
318 * @param status error code
319 * @return This, for chaining
320 * @stable ICU 4.8
321 */
322 virtual AlphabeticIndex &setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status);
323
324
325 /**
326 * Get the Unicode character (or tailored string) that defines an overflow bucket;
327 * that is anything greater than or equal to that string should go in that bucket,
328 * instead of with the last character. Normally that is the first character of the script
329 * after lowerLimit. Thus in X Y Z ... <i>Devanagari-ka</i>, the overflow character for Z
330 * would be the <i>Greek-alpha</i>.
331 *
332 * @param lowerLimit The character below the overflow (or inflow) bucket
333 * @param status error code
334 * @return string that defines top of the overflow buck for lowerLimit, or an empty string if there is none
335 * @internal
336 */
337 virtual const UnicodeString &getOverflowComparisonString(const UnicodeString &lowerLimit,
338 UErrorCode &status);
339
340
341 /**
342 * Add a record to the index. Each record will be associated with an index Bucket
343 * based on the record's name. The list of records for each bucket will be sorted
344 * based on the collation ordering of the names in the index's locale.
345 * Records with duplicate names are permitted; they will be kept in the order
346 * that they were added.
347 *
348 * @param name The display name for the Record. The Record will be placed in
349 * a bucket based on this name.
350 * @param data An optional pointer to user data associated with this
351 * item. When iterating the contents of a bucket, both the
352 * data pointer the name will be available for each Record.
353 * @param status Error code, will be set with the reason if the operation fails.
354 * @return This, for chaining.
355 * @stable ICU 4.8
356 */
357 virtual AlphabeticIndex &addRecord(const UnicodeString &name, const void *data, UErrorCode &status);
358
359 /**
360 * Remove all Records from the Index. The set of Buckets, which define the headings under
361 * which records are classified, is not altered.
362 *
363 * @param status Error code, will be set with the reason if the operation fails.
364 * @return This, for chaining.
365 * @stable ICU 4.8
366 */
367 virtual AlphabeticIndex &clearRecords(UErrorCode &status);
368
369
370 /** Get the number of labels in this index.
371 * Note: may trigger lazy index construction.
372 *
373 * @param status Error code, will be set with the reason if the operation fails.
374 * @return The number of labels in this index, including any under, over or
375 * in-flow labels.
376 * @stable ICU 4.8
377 */
378 virtual int32_t getBucketCount(UErrorCode &status);
379
380
381 /** Get the total number of Records in this index, that is, the number
382 * of <name, data> pairs added.
383 *
384 * @param status Error code, will be set with the reason if the operation fails.
385 * @return The number of records in this index, that is, the total number
386 * of (name, data) items added with addRecord().
387 * @stable ICU 4.8
388 */
389 virtual int32_t getRecordCount(UErrorCode &status);
390
391
392
393 /**
394 * Given the name of a record, return the zero-based index of the Bucket
395 * in which the item should appear. The name need not be in the index.
396 * A Record will not be added to the index by this function.
397 * Bucket numbers are zero-based, in Bucket iteration order.
398 *
399 * @param itemName The name whose bucket position in the index is to be determined.
400 * @param status Error code, will be set with the reason if the operation fails.
401 * @return The bucket number for this name.
402 * @stable ICU 4.8
403 *
404 */
405 virtual int32_t getBucketIndex(const UnicodeString &itemName, UErrorCode &status);
406
407
408 /**
409 * Get the zero based index of the current Bucket from an iteration
410 * over the Buckets of this index. Return -1 if no iteration is in process.
411 * @return the index of the current Bucket
412 * @stable ICU 4.8
413 */
414 virtual int32_t getBucketIndex() const;
415
416
417 /**
418 * Advance the iteration over the Buckets of this index. Return FALSE if
419 * there are no more Buckets.
420 *
421 * @param status Error code, will be set with the reason if the operation fails.
422 * U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while
423 * an enumeration of its contents are in process.
424 *
425 * @return TRUE if success, FALSE if at end of iteration
426 * @stable ICU 4.8
427 */
428 virtual UBool nextBucket(UErrorCode &status);
429
430 /**
431 * Return the name of the Label of the current bucket from an iteration over the buckets.
432 * If the iteration is before the first Bucket (nextBucket() has not been called),
433 * or after the last, return an empty string.
434 *
435 * @return the bucket label.
436 * @stable ICU 4.8
437 */
438 virtual const UnicodeString &getBucketLabel() const;
439
440 /**
441 * Return the type of the label for the current Bucket (selected by the
442 * iteration over Buckets.)
443 *
444 * @return the label type.
445 * @stable ICU 4.8
446 */
447 virtual UAlphabeticIndexLabelType getBucketLabelType() const;
448
449 /**
450 * Get the number of <name, data> Records in the current Bucket.
451 * If the current bucket iteration position is before the first label or after the
452 * last, return 0.
453 *
454 * @return the number of Records.
455 * @stable ICU 4.8
456 */
457 virtual int32_t getBucketRecordCount() const;
458
459
460 /**
461 * Reset the Bucket iteration for this index. The next call to nextBucket()
462 * will restart the iteration at the first label.
463 *
464 * @param status Error code, will be set with the reason if the operation fails.
465 * @return this, for chaining.
466 * @stable ICU 4.8
467 */
468 virtual AlphabeticIndex &resetBucketIterator(UErrorCode &status);
469
470 /**
471 * Advance to the next record in the current Bucket.
472 * When nextBucket() is called, Record iteration is reset to just before the
473 * first Record in the new Bucket.
474 *
475 * @param status Error code, will be set with the reason if the operation fails.
476 * U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while
477 * an enumeration of its contents are in process.
478 * @return TRUE if successful, FALSE when the iteration advances past the last item.
479 * @stable ICU 4.8
480 */
481 virtual UBool nextRecord(UErrorCode &status);
482
483 /**
484 * Get the name of the current Record.
485 * Return an empty string if the Record iteration position is before first
486 * or after the last.
487 *
488 * @return The name of the current index item.
489 * @stable ICU 4.8
490 */
491 virtual const UnicodeString &getRecordName() const;
492
493
494 /**
495 * Return the data pointer of the Record currently being iterated over.
496 * Return NULL if the current iteration position before the first item in this Bucket,
497 * or after the last.
498 *
499 * @return The current Record's data pointer.
500 * @stable ICU 4.8
501 */
502 virtual const void *getRecordData() const;
503
504
505 /**
506 * Reset the Record iterator position to before the first Record in the current Bucket.
507 *
508 * @return This, for chaining.
509 * @stable ICU 4.8
510 */
511 virtual AlphabeticIndex &resetRecordIterator();
512
513 private:
514 // No ICU "poor man's RTTI" for this class nor its subclasses.
515 virtual UClassID getDynamicClassID() const;
516
517 /**
518 * No Copy constructor.
519 * @internal
520 */
521 AlphabeticIndex(const AlphabeticIndex &other);
522
523 /**
524 * No assignment.
525 */
526 AlphabeticIndex &operator =(const AlphabeticIndex & /*other*/) { return *this;};
527
528 /**
529 * No Equality operators.
530 * @internal
531 */
532 virtual UBool operator==(const AlphabeticIndex& other) const;
533
534 /**
535 * Inequality operator.
536 * @internal
537 */
538 virtual UBool operator!=(const AlphabeticIndex& other) const;
539
540 // Common initialization, for use from all constructors.
541 void init(UErrorCode &status);
542
543 // Initialize & destruct static constants used by this class.
544 static void staticInit(UErrorCode &status);
545
546 // Pinyin stuff. If the input name is Chinese, add the Pinyin prefix to the dest string.
547 void hackName(UnicodeString &dest, const UnicodeString &name, const Collator *coll);
548 void initPinyinBounds(const Collator *coll, UErrorCode &status);
549
550 public:
551 #ifndef U_HIDE_INTERNAL_API
552 /**
553 * Delete all shared (static) data associated with an AlphabeticIndex.
554 * Internal function, not intended for direct use.
555 * @internal.
556 */
557 static void staticCleanup();
558 #endif /* U_HIDE_INTERNAL_API */
559 private:
560
561 // Add index characters from the specified locale to the dest set.
562 // Does not remove any previous contents from dest.
563 static void getIndexExemplars(UnicodeSet &dest, const Locale &locale, UErrorCode &status);
564
565 UVector *firstStringsInScript(UErrorCode &status);
566
567 static UnicodeString separated(const UnicodeString &item);
568
569 static UnicodeSet *getScriptSet(UnicodeSet &dest, const UnicodeString &codePoint, UErrorCode &status);
570
571 void buildIndex(UErrorCode &status);
572 void buildBucketList(UErrorCode &status);
573 void bucketRecords(UErrorCode &status);
574
575
576 public:
577
578 // The following internal items are declared public only to allow access from
579 // implementation code written in plain C. They are not intended for
580 // public use.
581
582 #ifndef U_HIDE_INTERNAL_API
583 /**
584 * A record, or item, in the index.
585 * @internal
586 */
587 struct Record: public UMemory {
588 AlphabeticIndex *alphaIndex_;
589 const UnicodeString name_;
590 UnicodeString sortingName_; // Usually the same as name_; different for Pinyin.
591 const void *data_;
592 int32_t serialNumber_; // Defines sorting order for names that compare equal.
593 Record(AlphabeticIndex *alphaIndex, const UnicodeString &name, const void *data);
594 ~Record();
595 };
596 #endif /* U_HIDE_INTERNAL_API */
597
598 /**
599 * Holds all user records before they are distributed into buckets.
600 * Type of contents is (Record *)
601 * @internal
602 */
603 UVector *inputRecords_;
604
605 /**
606 * A Bucket holds an index label and references to everything belonging to that label.
607 * For implementation use only. Declared public because pure C implementation code needs access.
608 * @internal
609 */
610 struct Bucket: public UMemory {
611 UnicodeString label_;
612 UnicodeString lowerBoundary_;
613 UAlphabeticIndexLabelType labelType_;
614 UVector *records_; // Records are owned by inputRecords_ vector.
615
616 Bucket(const UnicodeString &label, // Parameter strings are copied.
617 const UnicodeString &lowerBoundary,
618 UAlphabeticIndexLabelType type, UErrorCode &status);
619 ~Bucket();
620 };
621
622 public:
623
624 /**
625 * Language Types. For internal ICU use only.
626 * @internal (but not hidden with U_HIDE_INTERNAL_API because it is used in public API)
627 */
628 enum ELangType {
629 /** @internal */
630 kNormal,
631 /** @internal */
632 kSimplified,
633 /** @internal */
634 kTraditional
635 };
636
637 /**
638 * Get the Language Type for this Index. Based on the locale.
639 * @internal
640 */
641 static ELangType langTypeFromLocale(const Locale &loc);
642
643
644 private:
645
646 // Holds the contents of this index, buckets of user items.
647 // UVector elements are of type (Bucket *)
648 UVector *bucketList_;
649
650 int32_t labelsIterIndex_; // Index of next item to return.
651 int32_t itemsIterIndex_;
652 Bucket *currentBucket_; // While an iteration of the index in underway,
653 // point to the bucket for the current label.
654 // NULL when no iteration underway.
655
656 UBool indexBuildRequired_; // Caller has made changes to the index that
657 // require rebuilding & bucketing before the
658 // contents can be iterated.
659
660 int32_t maxLabelCount_; // Limit on # of labels permitted in the index.
661
662 UHashtable *alreadyIn_; // Key=UnicodeString, value=UnicodeSet
663
664 UnicodeSet *initialLabels_; // Initial (unprocessed) set of Labels. Union
665 // of those explicitly set by the user plus
666 // those from locales. Raw values, before
667 // crunching into bucket labels.
668
669 UVector *labels_; // List of Labels, after processing, sorting.
670 // Contents are (UnicodeString *)
671
672 UnicodeSet *noDistinctSorting_; // As the set of labels is built, strings may
673 // be discarded from the exemplars. This contains
674 // some of the discards, and is
675 // intended for debugging.
676
677 UnicodeSet *notAlphabetic_; // As the set of labels is built, strings may
678 // be discarded from the exemplars. This contains
679 // some of the discards, and is
680 // intended for debugging.
681
682
683 UVector *firstScriptCharacters_; // The first character from each script,
684 // in collation order.
685
686 Locale locale_;
687 Collator *collator_;
688 Collator *collatorPrimaryOnly_;
689
690 UnicodeString inflowLabel_;
691 UnicodeString overflowLabel_;
692 UnicodeString underflowLabel_;
693 UnicodeString overflowComparisonString_;
694
695 ELangType langType_; // The language type, simplified Chinese, Traditional Chinese,
696 // or not Chinese (Normal). Part of the Pinyin support
697
698 typedef const UChar PinyinLookup[24][3];
699 static PinyinLookup HACK_PINYIN_LOOKUP_SHORT;
700 static PinyinLookup HACK_PINYIN_LOOKUP_LONG;
701
702 // These will be lazily set to the short or long tables based on which
703 // Chinese collation has been configured into the ICU library.
704 static PinyinLookup *HACK_PINYIN_LOOKUP;
705 static const UChar *PINYIN_LOWER_BOUNDS;
706
707
708
709 int32_t recordCounter_; // Counts Records created. For minting record serial numbers.
710
711 // Constants. Lazily initialized the first time an AlphabeticIndex object is created.
712
713 static UnicodeSet *ALPHABETIC;
714 static UnicodeSet *CORE_LATIN;
715 static UnicodeSet *ETHIOPIC;
716 static UnicodeSet *HANGUL;
717 static UnicodeSet *IGNORE_SCRIPTS;
718 static UnicodeSet *TO_TRY;
719 static UnicodeSet *UNIHAN;
720 static const UnicodeString *EMPTY_STRING;
721
722 };
723
724 U_NAMESPACE_END
725
726 #endif /* UCONFIG_NO_COLLATION / UCONFIG_NO_NORMALIZATION */
727 #endif