2 *******************************************************************************
4 * Copyright (C) 2011-2013 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
76 class RuleBasedCollator
;
77 class StringEnumeration
;
82 * AlphabeticIndex supports the creation of a UI index appropriate for a given language.
83 * It can support either direct use, or use with a client that doesn't support localized collation.
84 * The following is an example of what an index might look like in a UI:
87 * <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>
98 * The class can generate a list of labels for use as a UI "index", that is, a list of
99 * clickable characters (or character sequences) that allow the user to see a segment
100 * (bucket) of a larger "target" list. That is, each label corresponds to a bucket in
101 * the target list, where everything in the bucket is greater than or equal to the character
102 * (according to the locale's collation). Strings can be added to the index;
103 * they will be in sorted order in the right bucket.
105 * The class also supports having buckets for strings before the first (underflow),
106 * after the last (overflow), and between scripts (inflow). For example, if the index
107 * is constructed with labels for Russian and English, Greek characters would fall
108 * into an inflow bucket between the other two scripts.
110 * The AlphabeticIndex class is not intended for public subclassing.
112 * <p><em>Note:</em> If you expect to have a lot of ASCII or Latin characters
113 * as well as characters from the user's language,
114 * then it is a good idea to call addLabels(Locale::getEnglish(), status).</p>
116 * <h2>Direct Use</h2>
117 * <p>The following shows an example of building an index directly.
118 * The "show..." methods below are just to illustrate usage.
121 * // Create a simple index. "Item" is assumed to be an application
122 * // defined type that the application's UI and other processing knows about,
123 * // and that has a name.
125 * UErrorCode status = U_ZERO_ERROR;
126 * AlphabeticIndex index = new AlphabeticIndex(desiredLocale, status);
127 * index->addLabels(additionalLocale, status);
128 * for (Item *item in some source of Items ) {
129 * index->addRecord(item->name(), item, status);
132 * // Show index at top. We could skip or gray out empty buckets
134 * while (index->nextBucket(status)) {
135 * if (showAll || index->getBucketRecordCount() != 0) {
136 * showLabelAtTop(UI, index->getBucketLabel());
140 * // Show the buckets with their contents, skipping empty buckets
142 * index->resetBucketIterator(status);
143 * while (index->nextBucket(status)) {
144 * if (index->getBucketRecordCount() != 0) {
145 * showLabelInList(UI, index->getBucketLabel());
146 * while (index->nextRecord(status)) {
147 * showIndexedItem(UI, static_cast<Item *>(index->getRecordData()))
150 * The caller can build different UIs using this class.
151 * For example, an index character could be omitted or grayed-out
152 * if its bucket is empty. Small buckets could also be combined based on size, such as:
155 * <b>... A-F G-N O-Z ...</b>
158 * <h2>Client Support</h2>
159 * <p>Callers can also use the AlphabeticIndex::ImmutableIndex, or the AlphabeticIndex itself,
160 * to support sorting on a client that doesn't support AlphabeticIndex functionality.
162 * <p>The ImmutableIndex is both immutable and thread-safe.
163 * The corresponding AlphabeticIndex methods are not thread-safe because
164 * they "lazily" build the index buckets.
166 * <li>ImmutableIndex.getBucket(index) provides random access to all
167 * buckets and their labels and label types.
168 * <li>The AlphabeticIndex bucket iterator or ImmutableIndex.getBucket(0..getBucketCount-1)
169 * can be used to get a list of the labels,
170 * such as "...", "A", "B",..., and send that list to the client.
171 * <li>When the client has a new name, it sends that name to the server.
172 * The server needs to call the following methods,
173 * and communicate the bucketIndex and collationKey back to the client.
176 * int32_t bucketIndex = index.getBucketIndex(name, status);
177 * const UnicodeString &label = immutableIndex.getBucket(bucketIndex)->getLabel(); // optional
178 * int32_t skLength = collator.getSortKey(name, sk, skCapacity);
181 * <li>The client would put the name (and associated information) into its bucket for bucketIndex. The sort key sk is a
182 * sequence of bytes that can be compared with a binary compare, and produce the right localized result.</li>
187 class U_I18N_API AlphabeticIndex
: public UObject
{
189 #ifdef U_HIDE_DRAFT_API
193 * An index "bucket" with a label string and type.
194 * It is referenced by getBucketIndex(),
195 * and returned by ImmutableIndex.getBucket().
197 * The Bucket class is not intended for public subclassing.
200 class U_I18N_API Bucket
: public UObject
{
209 * Returns the label string.
211 * @return the label string for the bucket
214 const UnicodeString
&getLabel() const { return label_
; }
216 * Returns whether this bucket is a normal, underflow, overflow, or inflow bucket.
218 * @return the bucket label type
221 UAlphabeticIndexLabelType
getLabelType() const { return labelType_
; }
224 friend class AlphabeticIndex
;
225 friend class BucketList
;
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.
234 Bucket(const UnicodeString
&label
, // Parameter strings are copied.
235 const UnicodeString
&lowerBoundary
,
236 UAlphabeticIndexLabelType type
);
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.
245 * The ImmutableIndex class is not intended for public subclassing.
249 class U_I18N_API ImmutableIndex
: public UObject
{
255 virtual ~ImmutableIndex();
258 * Returns the number of index buckets and labels, including underflow/inflow/overflow.
260 * @return the number of index buckets
263 int32_t getBucketCount() const;
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.
269 * @param name the string to be sorted into an index bucket
270 * @return the bucket number for the name
273 int32_t getBucketIndex(const UnicodeString
&name
, UErrorCode
&errorCode
) const;
276 * Returns the index-th bucket. Returns NULL if the index is out of range.
278 * @param index bucket number
279 * @return the index-th bucket
282 const Bucket
*getBucket(int32_t index
) const;
285 friend class AlphabeticIndex
;
287 ImmutableIndex(BucketList
*bucketList
, Collator
*collatorPrimaryOnly
)
288 : buckets_(bucketList
), collatorPrimaryOnly_(collatorPrimaryOnly
) {}
290 BucketList
*buckets_
;
291 Collator
*collatorPrimaryOnly_
;
293 #endif /* U_HIDE_DRAFT_API */
296 * Construct an AlphabeticIndex object for the specified locale. If the locale's
297 * data does not include index characters, a set of them will be
298 * synthesized based on the locale's exemplar characters. The locale
299 * determines the sorting order for both the index characters and the
300 * user item names appearing under each Index character.
302 * @param locale the desired locale.
303 * @param status Error code, will be set with the reason if the construction
304 * of the AlphabeticIndex object fails.
307 AlphabeticIndex(const Locale
&locale
, UErrorCode
&status
);
309 #ifndef U_HIDE_DRAFT_API
311 * Construct an AlphabeticIndex that uses a specific collator.
313 * The index will be created with no labels; the addLabels() function must be called
314 * after creation to add the desired labels to the index.
316 * The index adopts the collator, and is responsible for deleting it.
317 * The caller should make no further use of the collator after creating the index.
319 * @param collator The collator to use to order the contents of this index.
320 * @param status Error code, will be set with the reason if the
324 AlphabeticIndex(RuleBasedCollator
*collator
, UErrorCode
&status
);
325 #endif /* U_HIDE_DRAFT_API */
328 * Add Labels to this Index. The labels are additions to those
329 * that are already in the index; they do not replace the existing
331 * @param additions The additional characters to add to the index, such as A-Z.
332 * @param status Error code, will be set with the reason if the
334 * @return this, for chaining
337 virtual AlphabeticIndex
&addLabels(const UnicodeSet
&additions
, UErrorCode
&status
);
340 * Add the index characters from a Locale to the index. The labels
341 * are added to those that are already in the index; they do not replace the
342 * existing index characters. The collation order for this index is not
343 * changed; it remains that of the locale that was originally specified
344 * when creating this Index.
346 * @param locale The locale whose index characters are to be added.
347 * @param status Error code, will be set with the reason if the
349 * @return this, for chaining
352 virtual AlphabeticIndex
&addLabels(const Locale
&locale
, UErrorCode
&status
);
358 virtual ~AlphabeticIndex();
360 #ifndef U_HIDE_DRAFT_API
362 * Builds an immutable, thread-safe version of this instance, without data records.
364 * @return an immutable index instance
367 ImmutableIndex
*buildImmutableIndex(UErrorCode
&errorCode
);
368 #endif /* U_HIDE_DRAFT_API */
371 * Get the Collator that establishes the ordering of the items in this index.
372 * Ownership of the collator remains with the AlphabeticIndex instance.
374 * The returned collator is a reference to the internal collator used by this
375 * index. It may be safely used to compare the names of items or to get
376 * sort keys for names. However if any settings need to be changed,
377 * or other non-const methods called, a cloned copy must be made first.
379 * @return The collator
382 virtual const RuleBasedCollator
&getCollator() const;
386 * Get the default label used for abbreviated buckets <i>between</i> other index characters.
387 * For example, consider the labels when Latin and Greek are used:
388 * X Y Z ... Α Β Γ.
390 * @return inflow label
393 virtual const UnicodeString
&getInflowLabel() const;
396 * Set the default label used for abbreviated buckets <i>between</i> other index characters.
397 * An inflow label will be automatically inserted if two otherwise-adjacent label characters
398 * are from different scripts, e.g. Latin and Cyrillic, and a third script, e.g. Greek,
399 * sorts between the two. The default inflow character is an ellipsis (...)
401 * @param inflowLabel the new Inflow label.
402 * @param status Error code, will be set with the reason if the operation fails.
406 virtual AlphabeticIndex
&setInflowLabel(const UnicodeString
&inflowLabel
, UErrorCode
&status
);
410 * Get the special label used for items that sort after the last normal label,
411 * and that would not otherwise have an appropriate label.
413 * @return the overflow label
416 virtual const UnicodeString
&getOverflowLabel() const;
420 * Set the label used for items that sort after the last normal label,
421 * and that would not otherwise have an appropriate label.
423 * @param overflowLabel the new overflow label.
424 * @param status Error code, will be set with the reason if the operation fails.
428 virtual AlphabeticIndex
&setOverflowLabel(const UnicodeString
&overflowLabel
, UErrorCode
&status
);
431 * Get the special label used for items that sort before the first normal label,
432 * and that would not otherwise have an appropriate label.
434 * @return underflow label
437 virtual const UnicodeString
&getUnderflowLabel() const;
440 * Set the label used for items that sort before the first normal label,
441 * and that would not otherwise have an appropriate label.
443 * @param underflowLabel the new underflow label.
444 * @param status Error code, will be set with the reason if the operation fails.
448 virtual AlphabeticIndex
&setUnderflowLabel(const UnicodeString
&underflowLabel
, UErrorCode
&status
);
452 * Get the limit on the number of labels permitted in the index.
453 * The number does not include over, under and inflow labels.
455 * @return maxLabelCount maximum number of labels.
458 virtual int32_t getMaxLabelCount() const;
461 * Set a limit on the number of labels permitted in the index.
462 * The number does not include over, under and inflow labels.
463 * Currently, if the number is exceeded, then every
464 * nth item is removed to bring the count down.
465 * A more sophisticated mechanism may be available in the future.
467 * @param maxLabelCount the maximum number of labels.
468 * @param status error code
469 * @return This, for chaining
472 virtual AlphabeticIndex
&setMaxLabelCount(int32_t maxLabelCount
, UErrorCode
&status
);
476 * Add a record to the index. Each record will be associated with an index Bucket
477 * based on the record's name. The list of records for each bucket will be sorted
478 * based on the collation ordering of the names in the index's locale.
479 * Records with duplicate names are permitted; they will be kept in the order
480 * that they were added.
482 * @param name The display name for the Record. The Record will be placed in
483 * a bucket based on this name.
484 * @param data An optional pointer to user data associated with this
485 * item. When iterating the contents of a bucket, both the
486 * data pointer the name will be available for each Record.
487 * @param status Error code, will be set with the reason if the operation fails.
488 * @return This, for chaining.
491 virtual AlphabeticIndex
&addRecord(const UnicodeString
&name
, const void *data
, UErrorCode
&status
);
494 * Remove all Records from the Index. The set of Buckets, which define the headings under
495 * which records are classified, is not altered.
497 * @param status Error code, will be set with the reason if the operation fails.
498 * @return This, for chaining.
501 virtual AlphabeticIndex
&clearRecords(UErrorCode
&status
);
504 /** Get the number of labels in this index.
505 * Note: may trigger lazy index construction.
507 * @param status Error code, will be set with the reason if the operation fails.
508 * @return The number of labels in this index, including any under, over or
512 virtual int32_t getBucketCount(UErrorCode
&status
);
515 /** Get the total number of Records in this index, that is, the number
516 * of <name, data> pairs added.
518 * @param status Error code, will be set with the reason if the operation fails.
519 * @return The number of records in this index, that is, the total number
520 * of (name, data) items added with addRecord().
523 virtual int32_t getRecordCount(UErrorCode
&status
);
528 * Given the name of a record, return the zero-based index of the Bucket
529 * in which the item should appear. The name need not be in the index.
530 * A Record will not be added to the index by this function.
531 * Bucket numbers are zero-based, in Bucket iteration order.
533 * @param itemName The name whose bucket position in the index is to be determined.
534 * @param status Error code, will be set with the reason if the operation fails.
535 * @return The bucket number for this name.
539 virtual int32_t getBucketIndex(const UnicodeString
&itemName
, UErrorCode
&status
);
543 * Get the zero based index of the current Bucket from an iteration
544 * over the Buckets of this index. Return -1 if no iteration is in process.
545 * @return the index of the current Bucket
548 virtual int32_t getBucketIndex() const;
552 * Advance the iteration over the Buckets of this index. Return FALSE if
553 * there are no more Buckets.
555 * @param status Error code, will be set with the reason if the operation fails.
556 * U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while
557 * an enumeration of its contents are in process.
559 * @return TRUE if success, FALSE if at end of iteration
562 virtual UBool
nextBucket(UErrorCode
&status
);
565 * Return the name of the Label of the current bucket from an iteration over the buckets.
566 * If the iteration is before the first Bucket (nextBucket() has not been called),
567 * or after the last, return an empty string.
569 * @return the bucket label.
572 virtual const UnicodeString
&getBucketLabel() const;
575 * Return the type of the label for the current Bucket (selected by the
576 * iteration over Buckets.)
578 * @return the label type.
581 virtual UAlphabeticIndexLabelType
getBucketLabelType() const;
584 * Get the number of <name, data> Records in the current Bucket.
585 * If the current bucket iteration position is before the first label or after the
588 * @return the number of Records.
591 virtual int32_t getBucketRecordCount() const;
595 * Reset the Bucket iteration for this index. The next call to nextBucket()
596 * will restart the iteration at the first label.
598 * @param status Error code, will be set with the reason if the operation fails.
599 * @return this, for chaining.
602 virtual AlphabeticIndex
&resetBucketIterator(UErrorCode
&status
);
605 * Advance to the next record in the current Bucket.
606 * When nextBucket() is called, Record iteration is reset to just before the
607 * first Record in the new Bucket.
609 * @param status Error code, will be set with the reason if the operation fails.
610 * U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while
611 * an enumeration of its contents are in process.
612 * @return TRUE if successful, FALSE when the iteration advances past the last item.
615 virtual UBool
nextRecord(UErrorCode
&status
);
618 * Get the name of the current Record.
619 * Return an empty string if the Record iteration position is before first
622 * @return The name of the current index item.
625 virtual const UnicodeString
&getRecordName() const;
629 * Return the data pointer of the Record currently being iterated over.
630 * Return NULL if the current iteration position before the first item in this Bucket,
633 * @return The current Record's data pointer.
636 virtual const void *getRecordData() const;
640 * Reset the Record iterator position to before the first Record in the current Bucket.
642 * @return This, for chaining.
645 virtual AlphabeticIndex
&resetRecordIterator();
649 * No Copy constructor.
652 AlphabeticIndex(const AlphabeticIndex
&other
);
657 AlphabeticIndex
&operator =(const AlphabeticIndex
& /*other*/) { return *this;};
660 * No Equality operators.
663 virtual UBool
operator==(const AlphabeticIndex
& other
) const;
666 * Inequality operator.
669 virtual UBool
operator!=(const AlphabeticIndex
& other
) const;
671 // Common initialization, for use from all constructors.
672 void init(const Locale
*locale
, UErrorCode
&status
);
675 * This method is called to get the index exemplars. Normally these come from the locale directly,
676 * but if they aren't available, we have to synthesize them.
678 void addIndexExemplars(const Locale
&locale
, UErrorCode
&status
);
680 * Add Chinese index characters from the tailoring.
682 UBool
addChineseIndexCharacters(UErrorCode
&errorCode
);
684 UVector
*firstStringsInScript(UErrorCode
&status
);
686 static UnicodeString
separated(const UnicodeString
&item
);
689 * Determine the best labels to use.
690 * This is based on the exemplars, but we also process to make sure that they are unique,
691 * and sort differently, and that the overall list is small enough.
693 void initLabels(UVector
&indexCharacters
, UErrorCode
&errorCode
) const;
694 BucketList
*createBucketList(UErrorCode
&errorCode
) const;
695 void initBuckets(UErrorCode
&errorCode
);
697 void internalResetBucketIterator();
701 // The Record is declared public only to allow access from
702 // implementation code written in plain C.
703 // It is not intended for public use.
705 #ifndef U_HIDE_INTERNAL_API
707 * A (name, data) pair, to be sorted by name into one of the index buckets.
708 * The user data is not used by the index implementation.
711 struct Record
: public UMemory
{
712 const UnicodeString name_
;
714 Record(const UnicodeString
&name
, const void *data
);
717 #endif /* U_HIDE_INTERNAL_API */
722 * Holds all user records before they are distributed into buckets.
723 * Type of contents is (Record *)
728 int32_t labelsIterIndex_
; // Index of next item to return.
729 int32_t itemsIterIndex_
;
730 Bucket
*currentBucket_
; // While an iteration of the index in underway,
731 // point to the bucket for the current label.
732 // NULL when no iteration underway.
734 int32_t maxLabelCount_
; // Limit on # of labels permitted in the index.
736 UnicodeSet
*initialLabels_
; // Initial (unprocessed) set of Labels. Union
737 // of those explicitly set by the user plus
738 // those from locales. Raw values, before
739 // crunching into bucket labels.
741 UVector
*firstCharsInScripts_
; // The first character from each script,
742 // in collation order.
744 RuleBasedCollator
*collator_
;
745 RuleBasedCollator
*collatorPrimaryOnly_
;
747 // Lazy evaluated: null means that we have not built yet.
748 BucketList
*buckets_
;
750 UnicodeString inflowLabel_
;
751 UnicodeString overflowLabel_
;
752 UnicodeString underflowLabel_
;
753 UnicodeString overflowComparisonString_
;
755 UnicodeString emptyString_
;
760 #endif /* UCONFIG_NO_COLLATION / UCONFIG_NO_NORMALIZATION */