ICU 76.1 76.1
|
AlphabeticIndex supports the creation of a UI index appropriate for a given language. More...
#include <alphaindex.h>
Data Structures | |
class | Bucket |
An index "bucket" with a label string and type. More... | |
class | ImmutableIndex |
Immutable, thread-safe version of AlphabeticIndex. More... | |
Public Member Functions | |
AlphabeticIndex (const Locale &locale, UErrorCode &status) | |
Construct an AlphabeticIndex object for the specified locale. | |
AlphabeticIndex (RuleBasedCollator *collator, UErrorCode &status) | |
Construct an AlphabeticIndex that uses a specific collator. | |
virtual AlphabeticIndex & | addLabels (const UnicodeSet &additions, UErrorCode &status) |
Add Labels to this Index. | |
virtual AlphabeticIndex & | addLabels (const Locale &locale, UErrorCode &status) |
Add the index characters from a Locale to the index. | |
virtual | ~AlphabeticIndex () |
Destructor. | |
ImmutableIndex * | buildImmutableIndex (UErrorCode &errorCode) |
Builds an immutable, thread-safe version of this instance, without data records. | |
virtual const RuleBasedCollator & | getCollator () const |
Get the Collator that establishes the ordering of the items in this index. | |
virtual const UnicodeString & | getInflowLabel () const |
Get the default label used for abbreviated buckets between other index characters. | |
virtual AlphabeticIndex & | setInflowLabel (const UnicodeString &inflowLabel, UErrorCode &status) |
Set the default label used for abbreviated buckets between other index characters. | |
virtual const UnicodeString & | getOverflowLabel () const |
Get the special label used for items that sort after the last normal label, and that would not otherwise have an appropriate label. | |
virtual AlphabeticIndex & | setOverflowLabel (const UnicodeString &overflowLabel, UErrorCode &status) |
Set the label used for items that sort after the last normal label, and that would not otherwise have an appropriate label. | |
virtual const UnicodeString & | getUnderflowLabel () const |
Get the special label used for items that sort before the first normal label, and that would not otherwise have an appropriate label. | |
virtual AlphabeticIndex & | setUnderflowLabel (const UnicodeString &underflowLabel, UErrorCode &status) |
Set the label used for items that sort before the first normal label, and that would not otherwise have an appropriate label. | |
virtual int32_t | getMaxLabelCount () const |
Get the limit on the number of labels permitted in the index. | |
virtual AlphabeticIndex & | setMaxLabelCount (int32_t maxLabelCount, UErrorCode &status) |
Set a limit on the number of labels permitted in the index. | |
virtual AlphabeticIndex & | addRecord (const UnicodeString &name, const void *data, UErrorCode &status) |
Add a record to the index. | |
virtual AlphabeticIndex & | clearRecords (UErrorCode &status) |
Remove all Records from the Index. | |
virtual int32_t | getBucketCount (UErrorCode &status) |
Get the number of labels in this index. | |
virtual int32_t | getRecordCount (UErrorCode &status) |
Get the total number of Records in this index, that is, the number of <name, data> pairs added. | |
virtual int32_t | getBucketIndex (const UnicodeString &itemName, UErrorCode &status) |
Given the name of a record, return the zero-based index of the Bucket in which the item should appear. | |
virtual int32_t | getBucketIndex () const |
Get the zero based index of the current Bucket from an iteration over the Buckets of this index. | |
virtual UBool | nextBucket (UErrorCode &status) |
Advance the iteration over the Buckets of this index. | |
virtual const UnicodeString & | getBucketLabel () const |
Return the name of the Label of the current bucket from an iteration over the buckets. | |
virtual UAlphabeticIndexLabelType | getBucketLabelType () const |
Return the type of the label for the current Bucket (selected by the iteration over Buckets.) | |
virtual int32_t | getBucketRecordCount () const |
Get the number of <name, data> Records in the current Bucket. | |
virtual AlphabeticIndex & | resetBucketIterator (UErrorCode &status) |
Reset the Bucket iteration for this index. | |
virtual UBool | nextRecord (UErrorCode &status) |
Advance to the next record in the current Bucket. | |
virtual const UnicodeString & | getRecordName () const |
Get the name of the current Record. | |
virtual const void * | getRecordData () const |
Return the data pointer of the Record currently being iterated over. | |
virtual AlphabeticIndex & | resetRecordIterator () |
Reset the Record iterator position to before the first Record in the current Bucket. | |
![]() | |
virtual | ~UObject () |
Destructor. | |
virtual UClassID | getDynamicClassID () const |
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class. | |
AlphabeticIndex supports the creation of a UI index appropriate for a given language.
It can support either direct use, or use with a client that doesn't support localized collation. The following is an example of what an index might look like in a UI:
... 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 ... A Addison Albertson Azensky B Baker ...
The class can generate a list of labels for use as a UI "index", that is, a list of clickable characters (or character sequences) that allow the user to see a segment (bucket) of a larger "target" list. That is, each label corresponds to a bucket in the target list, where everything in the bucket is greater than or equal to the character (according to the locale's collation). Strings can be added to the index; they will be in sorted order in the right bucket.
The class also supports having buckets for strings before the first (underflow), after the last (overflow), and between scripts (inflow). For example, if the index is constructed with labels for Russian and English, Greek characters would fall into an inflow bucket between the other two scripts.
The AlphabeticIndex class is not intended for public subclassing.
Note: If you expect to have a lot of ASCII or Latin characters as well as characters from the user's language, then it is a good idea to call addLabels(Locale::getEnglish(), status).
The following shows an example of building an index directly. The "show..." methods below are just to illustrate usage.
// Create a simple index. "Item" is assumed to be an application // defined type that the application's UI and other processing knows about, // and that has a name. UErrorCode status = U_ZERO_ERROR; AlphabeticIndex index = new AlphabeticIndex(desiredLocale, status); index->addLabels(additionalLocale, status); for (Item *item in some source of Items ) { index->addRecord(item->name(), item, status); } ... // Show index at top. We could skip or gray out empty buckets while (index->nextBucket(status)) { if (showAll || index->getBucketRecordCount() != 0) { showLabelAtTop(UI, index->getBucketLabel()); } } ... // Show the buckets with their contents, skipping empty buckets index->resetBucketIterator(status); while (index->nextBucket(status)) { if (index->getBucketRecordCount() != 0) { showLabelInList(UI, index->getBucketLabel()); while (index->nextRecord(status)) { showIndexedItem(UI, static_cast<Item *>(index->getRecordData()))
The caller can build different UIs using this class. For example, an index character could be omitted or grayed-out if its bucket is empty. Small buckets could also be combined based on size, such as:
... A-F G-N O-Z ...
Callers can also use the AlphabeticIndex::ImmutableIndex, or the AlphabeticIndex itself, to support sorting on a client that doesn't support AlphabeticIndex functionality.
The ImmutableIndex is both immutable and thread-safe. The corresponding AlphabeticIndex methods are not thread-safe because they "lazily" build the index buckets.
When the client has a new name, it sends that name to the server. The server needs to call the following methods, and communicate the bucketIndex and collationKey back to the client.
int32_t bucketIndex = index.getBucketIndex(name, status); const UnicodeString &label = immutableIndex.getBucket(bucketIndex)->getLabel(); // optional int32_t skLength = collator.getSortKey(name, sk, skCapacity);
Definition at line 192 of file alphaindex.h.
icu::AlphabeticIndex::AlphabeticIndex | ( | const Locale & | locale, |
UErrorCode & | status | ||
) |
Construct an AlphabeticIndex object for the specified locale.
If the locale's data does not include index characters, a set of them will be synthesized based on the locale's exemplar characters. The locale determines the sorting order for both the index characters and the user item names appearing under each Index character.
locale | the desired locale. |
status | Error code, will be set with the reason if the construction of the AlphabeticIndex object fails. |
icu::AlphabeticIndex::AlphabeticIndex | ( | RuleBasedCollator * | collator, |
UErrorCode & | status | ||
) |
Construct an AlphabeticIndex that uses a specific collator.
The index will be created with no labels; the addLabels() function must be called after creation to add the desired labels to the index.
The index adopts the collator, and is responsible for deleting it. The caller should make no further use of the collator after creating the index.
collator | The collator to use to order the contents of this index. |
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Add the index characters from a Locale to the index.
The labels are added to those that are already in the index; they do not replace the existing index characters. The collation order for this index is not changed; it remains that of the locale that was originally specified when creating this Index.
locale | The locale whose index characters are to be added. |
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Add Labels to this Index.
The labels are additions to those that are already in the index; they do not replace the existing ones.
additions | The additional characters to add to the index, such as A-Z. |
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Add a record to the index.
Each record will be associated with an index Bucket based on the record's name. The list of records for each bucket will be sorted based on the collation ordering of the names in the index's locale. Records with duplicate names are permitted; they will be kept in the order that they were added.
name | The display name for the Record. The Record will be placed in a bucket based on this name. |
data | An optional pointer to user data associated with this item. When iterating the contents of a bucket, both the data pointer the name will be available for each Record. |
status | Error code, will be set with the reason if the operation fails. |
ImmutableIndex * icu::AlphabeticIndex::buildImmutableIndex | ( | UErrorCode & | errorCode | ) |
Builds an immutable, thread-safe version of this instance, without data records.
|
virtual |
Remove all Records from the Index.
The set of Buckets, which define the headings under which records are classified, is not altered.
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Get the number of labels in this index.
Note: may trigger lazy index construction.
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Given the name of a record, return the zero-based index of the Bucket in which the item should appear.
The name need not be in the index. A Record will not be added to the index by this function. Bucket numbers are zero-based, in Bucket iteration order.
itemName | The name whose bucket position in the index is to be determined. |
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Return the name of the Label of the current bucket from an iteration over the buckets.
If the iteration is before the first Bucket (nextBucket() has not been called), or after the last, return an empty string.
|
virtual |
|
virtual |
Get the Collator that establishes the ordering of the items in this index.
Ownership of the collator remains with the AlphabeticIndex instance.
The returned collator is a reference to the internal collator used by this index. It may be safely used to compare the names of items or to get sort keys for names. However if any settings need to be changed, or other non-const methods called, a cloned copy must be made first.
|
virtual |
Get the default label used for abbreviated buckets between other index characters.
For example, consider the labels when Latin (X Y Z) and Greek (Α Β Γ) are used:
X Y Z ... Α Β Γ.
Get the limit on the number of labels permitted in the index.
The number does not include over, under and inflow labels.
|
virtual |
Get the special label used for items that sort after the last normal label, and that would not otherwise have an appropriate label.
|
virtual |
Get the total number of Records in this index, that is, the number of <name, data> pairs added.
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Get the name of the current Record.
Return an empty string if the Record iteration position is before first or after the last.
|
virtual |
Get the special label used for items that sort before the first normal label, and that would not otherwise have an appropriate label.
|
virtual |
Advance the iteration over the Buckets of this index.
Return false if there are no more Buckets.
status | Error code, will be set with the reason if the operation fails. U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while an enumeration of its contents are in process. |
|
virtual |
Advance to the next record in the current Bucket.
When nextBucket() is called, Record iteration is reset to just before the first Record in the new Bucket.
status | Error code, will be set with the reason if the operation fails. U_ENUM_OUT_OF_SYNC_ERROR will be reported if the index is modified while an enumeration of its contents are in process. |
|
virtual |
Reset the Bucket iteration for this index.
The next call to nextBucket() will restart the iteration at the first label.
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
|
virtual |
Set the default label used for abbreviated buckets between other index characters.
An inflow label will be automatically inserted if two otherwise-adjacent label characters are from different scripts, e.g. Latin and Cyrillic, and a third script, e.g. Greek, sorts between the two. The default inflow character is an ellipsis (...)
inflowLabel | the new Inflow label. |
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Set a limit on the number of labels permitted in the index.
The number does not include over, under and inflow labels. Currently, if the number is exceeded, then every nth item is removed to bring the count down. A more sophisticated mechanism may be available in the future.
maxLabelCount | the maximum number of labels. |
status | error code |
|
virtual |
Set the label used for items that sort after the last normal label, and that would not otherwise have an appropriate label.
overflowLabel | the new overflow label. |
status | Error code, will be set with the reason if the operation fails. |
|
virtual |
Set the label used for items that sort before the first normal label, and that would not otherwise have an appropriate label.
underflowLabel | the new underflow label. |
status | Error code, will be set with the reason if the operation fails. |