2 *******************************************************************************
4 * Copyright (C) 2011-2012 International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
13 #include "unicode/utypes.h"
14 #include "unicode/uobject.h"
15 #include "unicode/locid.h"
18 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION
22 * \brief C++ API: Index Characters
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.
33 typedef enum UAlphabeticIndexLabelType
{
35 * Normal Label, typically the starting letter of the names
36 * in the bucket with this label.
39 U_ALPHAINDEX_NORMAL
= 0,
42 * Undeflow Label. The bucket with this label contains names
43 * in scripts that sort before any of the bucket labels in this index.
46 U_ALPHAINDEX_UNDERFLOW
= 1,
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
56 U_ALPHAINDEX_INFLOW
= 2,
59 * Overflow Label. Te bucket with this label contains names in scripts
60 * that sort after all of the bucket labels in this index.
63 U_ALPHAINDEX_OVERFLOW
= 3
64 } UAlphabeticIndexLabelType
;
72 // Forward Declarations
75 class RuleBasedCollator
;
76 class StringEnumeration
;
83 * class AlphabeticIndex supports the creation of a UI index appropriate for a given language, such as:
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>
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.
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.
109 * The AlphabeticIndex class is not intended for public subclassing.
113 * The "show..." methods below are just to illustrate usage.
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.
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);
127 * // Show index at top. We could skip or gray out empty buckets
129 * while (index->nextBucket(status)) {
130 * if (showAll || index->getBucketRecordCount() != 0) {
131 * showLabelAtTop(UI, index->getBucketLabel());
135 * // Show the buckets with their contents, skipping empty buckets
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()))
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:
150 * <b>... A-F G-N O-Z ...</b>
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".
165 class U_I18N_API AlphabeticIndex
: public UObject
{
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.
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.
181 AlphabeticIndex(const Locale
&locale
, UErrorCode
&status
);
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
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
192 * @return this, for chaining
195 virtual AlphabeticIndex
&addLabels(const UnicodeSet
&additions
, UErrorCode
&status
);
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.
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
207 * @return this, for chaining
210 virtual AlphabeticIndex
&addLabels(const Locale
&locale
, UErrorCode
&status
);
216 virtual ~AlphabeticIndex();
220 * Get the Collator that establishes the ordering of the items in this index.
221 * Ownership of the collator remains with the AlphabeticIndex instance.
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.
228 * @return The collator
231 virtual const RuleBasedCollator
&getCollator() const;
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 ... Α Β Γ.
239 * @return inflow label
242 virtual const UnicodeString
&getInflowLabel() const;
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 (...)
250 * @param inflowLabel the new Inflow label.
251 * @param status Error code, will be set with the reason if the operation fails.
255 virtual AlphabeticIndex
&setInflowLabel(const UnicodeString
&inflowLabel
, UErrorCode
&status
);
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.
263 * @return the overflow label
266 virtual const UnicodeString
&getOverflowLabel() const;
270 * Set the label used for items that sort after the last normal label,
271 * and that would not otherwise have an appropriate label.
273 * @param overflowLabel the new overflow label.
274 * @param status Error code, will be set with the reason if the operation fails.
278 virtual AlphabeticIndex
&setOverflowLabel(const UnicodeString
&overflowLabel
, UErrorCode
&status
);
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.
284 * @return underflow label
287 virtual const UnicodeString
&getUnderflowLabel() const;
290 * Set the label used for items that sort before the first normal label,
291 * and that would not otherwise have an appropriate label.
293 * @param underflowLabel the new underflow label.
294 * @param status Error code, will be set with the reason if the operation fails.
298 virtual AlphabeticIndex
&setUnderflowLabel(const UnicodeString
&underflowLabel
, UErrorCode
&status
);
302 * Get the limit on the number of labels permitted in the index.
303 * The number does not include over, under and inflow labels.
305 * @return maxLabelCount maximum number of labels.
308 virtual int32_t getMaxLabelCount() const;
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.
317 * @param maxLabelCount the maximum number of labels.
318 * @param status error code
319 * @return This, for chaining
322 virtual AlphabeticIndex
&setMaxLabelCount(int32_t maxLabelCount
, UErrorCode
&status
);
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>.
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
337 virtual const UnicodeString
&getOverflowComparisonString(const UnicodeString
&lowerLimit
,
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.
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.
357 virtual AlphabeticIndex
&addRecord(const UnicodeString
&name
, const void *data
, UErrorCode
&status
);
360 * Remove all Records from the Index. The set of Buckets, which define the headings under
361 * which records are classified, is not altered.
363 * @param status Error code, will be set with the reason if the operation fails.
364 * @return This, for chaining.
367 virtual AlphabeticIndex
&clearRecords(UErrorCode
&status
);
370 /** Get the number of labels in this index.
371 * Note: may trigger lazy index construction.
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
378 virtual int32_t getBucketCount(UErrorCode
&status
);
381 /** Get the total number of Records in this index, that is, the number
382 * of <name, data> pairs added.
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().
389 virtual int32_t getRecordCount(UErrorCode
&status
);
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.
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.
405 virtual int32_t getBucketIndex(const UnicodeString
&itemName
, UErrorCode
&status
);
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
414 virtual int32_t getBucketIndex() const;
418 * Advance the iteration over the Buckets of this index. Return FALSE if
419 * there are no more Buckets.
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.
425 * @return TRUE if success, FALSE if at end of iteration
428 virtual UBool
nextBucket(UErrorCode
&status
);
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.
435 * @return the bucket label.
438 virtual const UnicodeString
&getBucketLabel() const;
441 * Return the type of the label for the current Bucket (selected by the
442 * iteration over Buckets.)
444 * @return the label type.
447 virtual UAlphabeticIndexLabelType
getBucketLabelType() const;
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
454 * @return the number of Records.
457 virtual int32_t getBucketRecordCount() const;
461 * Reset the Bucket iteration for this index. The next call to nextBucket()
462 * will restart the iteration at the first label.
464 * @param status Error code, will be set with the reason if the operation fails.
465 * @return this, for chaining.
468 virtual AlphabeticIndex
&resetBucketIterator(UErrorCode
&status
);
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.
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.
481 virtual UBool
nextRecord(UErrorCode
&status
);
484 * Get the name of the current Record.
485 * Return an empty string if the Record iteration position is before first
488 * @return The name of the current index item.
491 virtual const UnicodeString
&getRecordName() const;
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,
499 * @return The current Record's data pointer.
502 virtual const void *getRecordData() const;
506 * Reset the Record iterator position to before the first Record in the current Bucket.
508 * @return This, for chaining.
511 virtual AlphabeticIndex
&resetRecordIterator();
514 // No ICU "poor man's RTTI" for this class nor its subclasses.
515 virtual UClassID
getDynamicClassID() const;
518 * No Copy constructor.
521 AlphabeticIndex(const AlphabeticIndex
&other
);
526 AlphabeticIndex
&operator =(const AlphabeticIndex
& /*other*/) { return *this;};
529 * No Equality operators.
532 virtual UBool
operator==(const AlphabeticIndex
& other
) const;
535 * Inequality operator.
538 virtual UBool
operator!=(const AlphabeticIndex
& other
) const;
540 // Common initialization, for use from all constructors.
541 void init(UErrorCode
&status
);
543 // Initialize & destruct static constants used by this class.
544 static void staticInit(UErrorCode
&status
);
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
);
551 #ifndef U_HIDE_INTERNAL_API
553 * Delete all shared (static) data associated with an AlphabeticIndex.
554 * Internal function, not intended for direct use.
557 static void staticCleanup();
558 #endif /* U_HIDE_INTERNAL_API */
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
);
565 UVector
*firstStringsInScript(UErrorCode
&status
);
567 static UnicodeString
separated(const UnicodeString
&item
);
569 static UnicodeSet
*getScriptSet(UnicodeSet
&dest
, const UnicodeString
&codePoint
, UErrorCode
&status
);
571 void buildIndex(UErrorCode
&status
);
572 void buildBucketList(UErrorCode
&status
);
573 void bucketRecords(UErrorCode
&status
);
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
582 #ifndef U_HIDE_INTERNAL_API
584 * A record, or item, in the index.
587 struct Record
: public UMemory
{
588 AlphabeticIndex
*alphaIndex_
;
589 const UnicodeString name_
;
590 UnicodeString sortingName_
; // Usually the same as name_; different for Pinyin.
592 int32_t serialNumber_
; // Defines sorting order for names that compare equal.
593 Record(AlphabeticIndex
*alphaIndex
, const UnicodeString
&name
, const void *data
);
596 #endif /* U_HIDE_INTERNAL_API */
599 * Holds all user records before they are distributed into buckets.
600 * Type of contents is (Record *)
603 UVector
*inputRecords_
;
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.
610 struct Bucket
: public UMemory
{
611 UnicodeString label_
;
612 UnicodeString lowerBoundary_
;
613 UAlphabeticIndexLabelType labelType_
;
614 UVector
*records_
; // Records are owned by inputRecords_ vector.
616 Bucket(const UnicodeString
&label
, // Parameter strings are copied.
617 const UnicodeString
&lowerBoundary
,
618 UAlphabeticIndexLabelType type
, UErrorCode
&status
);
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)
638 * Get the Language Type for this Index. Based on the locale.
641 static ELangType
langTypeFromLocale(const Locale
&loc
);
646 // Holds the contents of this index, buckets of user items.
647 // UVector elements are of type (Bucket *)
648 UVector
*bucketList_
;
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.
656 UBool indexBuildRequired_
; // Caller has made changes to the index that
657 // require rebuilding & bucketing before the
658 // contents can be iterated.
660 int32_t maxLabelCount_
; // Limit on # of labels permitted in the index.
662 UHashtable
*alreadyIn_
; // Key=UnicodeString, value=UnicodeSet
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.
669 UVector
*labels_
; // List of Labels, after processing, sorting.
670 // Contents are (UnicodeString *)
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.
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.
683 UVector
*firstScriptCharacters_
; // The first character from each script,
684 // in collation order.
688 Collator
*collatorPrimaryOnly_
;
690 UnicodeString inflowLabel_
;
691 UnicodeString overflowLabel_
;
692 UnicodeString underflowLabel_
;
693 UnicodeString overflowComparisonString_
;
695 ELangType langType_
; // The language type, simplified Chinese, Traditional Chinese,
696 // or not Chinese (Normal). Part of the Pinyin support
698 typedef const UChar PinyinLookup
[24][3];
699 static PinyinLookup HACK_PINYIN_LOOKUP_SHORT
;
700 static PinyinLookup HACK_PINYIN_LOOKUP_LONG
;
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
;
709 int32_t recordCounter_
; // Counts Records created. For minting record serial numbers.
711 // Constants. Lazily initialized the first time an AlphabeticIndex object is created.
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
;
726 #endif /* UCONFIG_NO_COLLATION / UCONFIG_NO_NORMALIZATION */