ICU 55.1 55.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- Data Structures
- Data Structure Index
- Class Hierarchy
- Data Fields

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Public Member Functions
icu::AlphabeticIndex Class Reference

AlphabeticIndex supports the creation of a UI index appropriate for a given language. More...

`#include <alphaindex.h>`

Inheritance diagram for icu::AlphabeticIndex:

## Data Structures
class | Bucket
| An index "bucket" with a label string and type. More...
class | ImmutableIndex
| Immutable, thread-safe version of AlphabeticIndex. More...
struct | Record
| A (name, data) pair, to be sorted by name into one of the index buckets. 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.
Public Member Functions inherited from icu::UObject
virtual | ~UObject ()
| Destructor.
virtual UClassID | getDynamicClassID () const
| ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.

## Detailed Description

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).

## Direct Use

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 ...

## Client Support

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.

- ImmutableIndex.getBucket(index) provides random access to all buckets and their labels and label types.
- The AlphabeticIndex bucket iterator or ImmutableIndex.getBucket(0..getBucketCount-1) can be used to get a list of the labels, such as "...", "A", "B",..., and send that list to the client.

- 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);

- The client would put the name (and associated information) into its bucket for bucketIndex. The sort key sk is a sequence of bytes that can be compared with a binary compare, and produce the right localized result.

Stable:
  ICU 4.8

Definition at line 186 of file alphaindex.h.

## Constructor & Destructor Documentation

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.

Parameters

  locale | the desired locale.
  status | Error code, will be set with the reason if the construction of the AlphabeticIndex object fails.

Stable:
  ICU 4.8

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.

Parameters

  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.

Stable:
  ICU 51

virtual icu::AlphabeticIndex::~AlphabeticIndex | ( |  | ) |
| virtual

Destructor.

Stable:
  ICU 4.8

## Member Function Documentation

virtual AlphabeticIndex& icu::AlphabeticIndex::addLabels | ( | const UnicodeSet & | additions,
|  | UErrorCode & | status
| ) |  |
| 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.

Parameters

  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.

Returns
  this, for chaining

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::addLabels | ( | const Locale & | locale,
|  | UErrorCode & | status
| ) |  |
| 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.

Parameters

  locale | The locale whose index characters are to be added.
  status | Error code, will be set with the reason if the operation fails.

Returns
  this, for chaining

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::addRecord | ( | const UnicodeString & | name,
|  | const void * | data,
|  | UErrorCode & | status
| ) |  |
| 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.

Parameters

  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.

Returns
  This, for chaining.

Stable:
  ICU 4.8

ImmutableIndex* icu::AlphabeticIndex::buildImmutableIndex | ( | UErrorCode & | errorCode | ) |

Builds an immutable, thread-safe version of this instance, without data records.

Returns
  an immutable index instance

Stable:
  ICU 51

virtual AlphabeticIndex& icu::AlphabeticIndex::clearRecords | ( | UErrorCode & | status | ) |
| virtual

Remove all Records from the Index.

The set of Buckets, which define the headings under which records are classified, is not altered.

Parameters

  status | Error code, will be set with the reason if the operation fails.

Returns
  This, for chaining.

Stable:
  ICU 4.8

virtual int32_t icu::AlphabeticIndex::getBucketCount | ( | UErrorCode & | status | ) |
| virtual

Get the number of labels in this index.

     Note: may trigger lazy index construction.

Parameters

  status | Error code, will be set with the reason if the operation fails.

Returns
  The number of labels in this index, including any under, over or in-flow labels.

Stable:
  ICU 4.8

virtual int32_t icu::AlphabeticIndex::getBucketIndex | ( | const UnicodeString & | itemName,
|  | UErrorCode & | status
| ) |  |
| 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.

Parameters

  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.

Returns
  The bucket number for this name.

Stable:
  ICU 4.8

virtual int32_t icu::AlphabeticIndex::getBucketIndex | ( |  | ) | const
| virtual

Get the zero based index of the current Bucket from an iteration over the Buckets of this index.

Return -1 if no iteration is in process.

Returns
  the index of the current Bucket

Stable:
  ICU 4.8

virtual const UnicodeString& icu::AlphabeticIndex::getBucketLabel | ( |  | ) | const
| 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.

Returns
  the bucket label.

Stable:
  ICU 4.8

virtual UAlphabeticIndexLabelType icu::AlphabeticIndex::getBucketLabelType | ( |  | ) | const
| virtual

Return the type of the label for the current Bucket (selected by the iteration over Buckets.)

Returns
  the label type.

Stable:
  ICU 4.8

virtual int32_t icu::AlphabeticIndex::getBucketRecordCount | ( |  | ) | const
| virtual

Get the number of <name, data> Records in the current Bucket.

If the current bucket iteration position is before the first label or after the last, return 0.

Returns
  the number of Records.

Stable:
  ICU 4.8

virtual const RuleBasedCollator& icu::AlphabeticIndex::getCollator | ( |  | ) | const
| 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.

Returns
  The collator

Stable:
  ICU 4.8

virtual const UnicodeString& icu::AlphabeticIndex::getInflowLabel | ( |  | ) | const
| virtual

Get the default label used for abbreviated buckets between other index characters.

For example, consider the labels when Latin and Greek are used: X Y Z ... &#x0391; &#x0392; &#x0393;.

Returns
  inflow label

Stable:
  ICU 4.8

virtual int32_t icu::AlphabeticIndex::getMaxLabelCount | ( |  | ) | const
| virtual

Get the limit on the number of labels permitted in the index.

The number does not include over, under and inflow labels.

Returns
  maxLabelCount maximum number of labels.

Stable:
  ICU 4.8

virtual const UnicodeString& icu::AlphabeticIndex::getOverflowLabel | ( |  | ) | const
| virtual

Get the special label used for items that sort after the last normal label, and that would not otherwise have an appropriate label.

Returns
  the overflow label

Stable:
  ICU 4.8

virtual int32_t icu::AlphabeticIndex::getRecordCount | ( | UErrorCode & | status | ) |
| virtual

Get the total number of Records in this index, that is, the number of <name, data> pairs added.

Parameters

  status | Error code, will be set with the reason if the operation fails.

Returns
  The number of records in this index, that is, the total number of (name, data) items added with addRecord().

Stable:
  ICU 4.8

virtual const void* icu::AlphabeticIndex::getRecordData | ( |  | ) | const
| virtual

Return the data pointer of the Record currently being iterated over.

Return NULL if the current iteration position before the first item in this Bucket, or after the last.

Returns
  The current Record's data pointer.

Stable:
  ICU 4.8

virtual const UnicodeString& icu::AlphabeticIndex::getRecordName | ( |  | ) | const
| virtual

Get the name of the current Record.

Return an empty string if the Record iteration position is before first or after the last.

Returns
  The name of the current index item.

Stable:
  ICU 4.8

virtual const UnicodeString& icu::AlphabeticIndex::getUnderflowLabel | ( |  | ) | const
| virtual

Get the special label used for items that sort before the first normal label, and that would not otherwise have an appropriate label.

Returns
  underflow label

Stable:
  ICU 4.8

virtual UBool icu::AlphabeticIndex::nextBucket | ( | UErrorCode & | status | ) |
| virtual

Advance the iteration over the Buckets of this index.

Return FALSE if there are no more Buckets.

Parameters

  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.

Returns
  TRUE if success, FALSE if at end of iteration

Stable:
  ICU 4.8

virtual UBool icu::AlphabeticIndex::nextRecord | ( | UErrorCode & | status | ) |
| 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.

Parameters

  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.

Returns
  TRUE if successful, FALSE when the iteration advances past the last item.

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::resetBucketIterator | ( | UErrorCode & | status | ) |
| virtual

Reset the Bucket iteration for this index.

The next call to nextBucket() will restart the iteration at the first label.

Parameters

  status | Error code, will be set with the reason if the operation fails.

Returns
  this, for chaining.

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::resetRecordIterator | ( |  | ) |
| virtual

Reset the Record iterator position to before the first Record in the current Bucket.

Returns
  This, for chaining.

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::setInflowLabel | ( | const UnicodeString & | inflowLabel,
|  | UErrorCode & | status
| ) |  |
| 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 (...)

Parameters

  inflowLabel | the new Inflow label.
  status | Error code, will be set with the reason if the operation fails.

Returns
  this

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::setMaxLabelCount | ( | int32_t | maxLabelCount,
|  | UErrorCode & | status
| ) |  |
| 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.

Parameters

  maxLabelCount | the maximum number of labels.
  status | error code

Returns
  This, for chaining

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::setOverflowLabel | ( | const UnicodeString & | overflowLabel,
|  | UErrorCode & | status
| ) |  |
| virtual

Set the label used for items that sort after the last normal label, and that would not otherwise have an appropriate label.

Parameters

  overflowLabel | the new overflow label.
  status | Error code, will be set with the reason if the operation fails.

Returns
  this

Stable:
  ICU 4.8

virtual AlphabeticIndex& icu::AlphabeticIndex::setUnderflowLabel | ( | const UnicodeString & | underflowLabel,
|  | UErrorCode & | status
| ) |  |
| virtual

Set the label used for items that sort before the first normal label, and that would not otherwise have an appropriate label.

Parameters

  underflowLabel | the new underflow label.
  status | Error code, will be set with the reason if the operation fails.

Returns
  this

Stable:
  ICU 4.8

The documentation for this class was generated from the following file:

- alphaindex.h