| 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ******************************************************************************* |
| 5 | * |
| 6 | * Copyright (C) 2011-2014 International Business Machines |
| 7 | * Corporation and others. All Rights Reserved. |
| 8 | * |
| 9 | ******************************************************************************* |
| 10 | */ |
| 11 | |
| 12 | #ifndef INDEXCHARS_H |
| 13 | #define INDEXCHARS_H |
| 14 | |
| 15 | #include "unicode/utypes.h" |
| 16 | |
| 17 | #if U_SHOW_CPLUSPLUS_API |
| 18 | |
| 19 | #include "unicode/uobject.h" |
| 20 | #include "unicode/locid.h" |
| 21 | #include "unicode/unistr.h" |
| 22 | |
| 23 | #if !UCONFIG_NO_COLLATION |
| 24 | |
| 25 | /** |
| 26 | * \file |
| 27 | * \brief C++ API: Index Characters |
| 28 | */ |
| 29 | |
| 30 | U_CDECL_BEGIN |
| 31 | |
| 32 | /** |
| 33 | * Constants for Alphabetic Index Label Types. |
| 34 | * The form of these enum constants anticipates having a plain C API |
| 35 | * for Alphabetic Indexes that will also use them. |
| 36 | * @stable ICU 4.8 |
| 37 | */ |
| 38 | typedef enum UAlphabeticIndexLabelType { |
| 39 | /** |
| 40 | * Normal Label, typically the starting letter of the names |
| 41 | * in the bucket with this label. |
| 42 | * @stable ICU 4.8 |
| 43 | */ |
| 44 | U_ALPHAINDEX_NORMAL = 0, |
| 45 | |
| 46 | /** |
| 47 | * Undeflow Label. The bucket with this label contains names |
| 48 | * in scripts that sort before any of the bucket labels in this index. |
| 49 | * @stable ICU 4.8 |
| 50 | */ |
| 51 | U_ALPHAINDEX_UNDERFLOW = 1, |
| 52 | |
| 53 | /** |
| 54 | * Inflow Label. The bucket with this label contains names |
| 55 | * in scripts that sort between two of the bucket labels in this index. |
| 56 | * Inflow labels are created when an index contains normal labels for |
| 57 | * multiple scripts, and skips other scripts that sort between some of the |
| 58 | * included scripts. |
| 59 | * @stable ICU 4.8 |
| 60 | */ |
| 61 | U_ALPHAINDEX_INFLOW = 2, |
| 62 | |
| 63 | /** |
| 64 | * Overflow Label. Te bucket with this label contains names in scripts |
| 65 | * that sort after all of the bucket labels in this index. |
| 66 | * @stable ICU 4.8 |
| 67 | */ |
| 68 | U_ALPHAINDEX_OVERFLOW = 3 |
| 69 | } UAlphabeticIndexLabelType; |
| 70 | |
| 71 | |
| 72 | struct UHashtable; |
| 73 | U_CDECL_END |
| 74 | |
| 75 | U_NAMESPACE_BEGIN |
| 76 | |
| 77 | // Forward Declarations |
| 78 | |
| 79 | class BucketList; |
| 80 | class Collator; |
| 81 | class RuleBasedCollator; |
| 82 | class StringEnumeration; |
| 83 | class UnicodeSet; |
| 84 | class UVector; |
| 85 | |
| 86 | /** |
| 87 | * AlphabeticIndex supports the creation of a UI index appropriate for a given language. |
| 88 | * It can support either direct use, or use with a client that doesn't support localized collation. |
| 89 | * The following is an example of what an index might look like in a UI: |
| 90 | * |
| 91 | * <pre> |
| 92 | * <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> |
| 93 | * |
| 94 | * <b>A</b> |
| 95 | * Addison |
| 96 | * Albertson |
| 97 | * Azensky |
| 98 | * <b>B</b> |
| 99 | * Baker |
| 100 | * ... |
| 101 | * </pre> |
| 102 | * |
| 103 | * The class can generate a list of labels for use as a UI "index", that is, a list of |
| 104 | * clickable characters (or character sequences) that allow the user to see a segment |
| 105 | * (bucket) of a larger "target" list. That is, each label corresponds to a bucket in |
| 106 | * the target list, where everything in the bucket is greater than or equal to the character |
| 107 | * (according to the locale's collation). Strings can be added to the index; |
| 108 | * they will be in sorted order in the right bucket. |
| 109 | * <p> |
| 110 | * The class also supports having buckets for strings before the first (underflow), |
| 111 | * after the last (overflow), and between scripts (inflow). For example, if the index |
| 112 | * is constructed with labels for Russian and English, Greek characters would fall |
| 113 | * into an inflow bucket between the other two scripts. |
| 114 | * <p> |
| 115 | * The AlphabeticIndex class is not intended for public subclassing. |
| 116 | * |
| 117 | * <p><em>Note:</em> If you expect to have a lot of ASCII or Latin characters |
| 118 | * as well as characters from the user's language, |
| 119 | * then it is a good idea to call addLabels(Locale::getEnglish(), status).</p> |
| 120 | * |
| 121 | * <h2>Direct Use</h2> |
| 122 | * <p>The following shows an example of building an index directly. |
| 123 | * The "show..." methods below are just to illustrate usage. |
| 124 | * |
| 125 | * <pre> |
| 126 | * // Create a simple index. "Item" is assumed to be an application |
| 127 | * // defined type that the application's UI and other processing knows about, |
| 128 | * // and that has a name. |
| 129 | * |
| 130 | * UErrorCode status = U_ZERO_ERROR; |
| 131 | * AlphabeticIndex index = new AlphabeticIndex(desiredLocale, status); |
| 132 | * index->addLabels(additionalLocale, status); |
| 133 | * for (Item *item in some source of Items ) { |
| 134 | * index->addRecord(item->name(), item, status); |
| 135 | * } |
| 136 | * ... |
| 137 | * // Show index at top. We could skip or gray out empty buckets |
| 138 | * |
| 139 | * while (index->nextBucket(status)) { |
| 140 | * if (showAll || index->getBucketRecordCount() != 0) { |
| 141 | * showLabelAtTop(UI, index->getBucketLabel()); |
| 142 | * } |
| 143 | * } |
| 144 | * ... |
| 145 | * // Show the buckets with their contents, skipping empty buckets |
| 146 | * |
| 147 | * index->resetBucketIterator(status); |
| 148 | * while (index->nextBucket(status)) { |
| 149 | * if (index->getBucketRecordCount() != 0) { |
| 150 | * showLabelInList(UI, index->getBucketLabel()); |
| 151 | * while (index->nextRecord(status)) { |
| 152 | * showIndexedItem(UI, static_cast<Item *>(index->getRecordData())) |
| 153 | * </pre> |
| 154 | * |
| 155 | * The caller can build different UIs using this class. |
| 156 | * For example, an index character could be omitted or grayed-out |
| 157 | * if its bucket is empty. Small buckets could also be combined based on size, such as: |
| 158 | * |
| 159 | * <pre> |
| 160 | * <b>... A-F G-N O-Z ...</b> |
| 161 | * </pre> |
| 162 | * |
| 163 | * <h2>Client Support</h2> |
| 164 | * <p>Callers can also use the AlphabeticIndex::ImmutableIndex, or the AlphabeticIndex itself, |
| 165 | * to support sorting on a client that doesn't support AlphabeticIndex functionality. |
| 166 | * |
| 167 | * <p>The ImmutableIndex is both immutable and thread-safe. |
| 168 | * The corresponding AlphabeticIndex methods are not thread-safe because |
| 169 | * they "lazily" build the index buckets. |
| 170 | * <ul> |
| 171 | * <li>ImmutableIndex.getBucket(index) provides random access to all |
| 172 | * buckets and their labels and label types. |
| 173 | * <li>The AlphabeticIndex bucket iterator or ImmutableIndex.getBucket(0..getBucketCount-1) |
| 174 | * can be used to get a list of the labels, |
| 175 | * such as "...", "A", "B",..., and send that list to the client. |
| 176 | * <li>When the client has a new name, it sends that name to the server. |
| 177 | * The server needs to call the following methods, |
| 178 | * and communicate the bucketIndex and collationKey back to the client. |
| 179 | * |
| 180 | * <pre> |
| 181 | * int32_t bucketIndex = index.getBucketIndex(name, status); |
| 182 | * const UnicodeString &label = immutableIndex.getBucket(bucketIndex)->getLabel(); // optional |
| 183 | * int32_t skLength = collator.getSortKey(name, sk, skCapacity); |
| 184 | * </pre> |
| 185 | * |
| 186 | * <li>The client would put the name (and associated information) into its bucket for bucketIndex. The sort key sk is a |
| 187 | * sequence of bytes that can be compared with a binary compare, and produce the right localized result.</li> |
| 188 | * </ul> |
| 189 | * |
| 190 | * @stable ICU 4.8 |
| 191 | */ |
| 192 | class U_I18N_API AlphabeticIndex: public UObject { |
| 193 | public: |
| 194 | /** |
| 195 | * An index "bucket" with a label string and type. |
| 196 | * It is referenced by getBucketIndex(), |
| 197 | * and returned by ImmutableIndex.getBucket(). |
| 198 | * |
| 199 | * The Bucket class is not intended for public subclassing. |
| 200 | * @stable ICU 51 |
| 201 | */ |
| 202 | class U_I18N_API Bucket : public UObject { |
| 203 | public: |
| 204 | /** |
| 205 | * Destructor. |
| 206 | * @stable ICU 51 |
| 207 | */ |
| 208 | virtual ~Bucket(); |
| 209 | |
| 210 | /** |
| 211 | * Returns the label string. |
| 212 | * |
| 213 | * @return the label string for the bucket |
| 214 | * @stable ICU 51 |
| 215 | */ |
| 216 | const UnicodeString &getLabel() const { return label_; } |
| 217 | /** |
| 218 | * Returns whether this bucket is a normal, underflow, overflow, or inflow bucket. |
| 219 | * |
| 220 | * @return the bucket label type |
| 221 | * @stable ICU 51 |
| 222 | */ |
| 223 | UAlphabeticIndexLabelType getLabelType() const { return labelType_; } |
| 224 | |
| 225 | private: |
| 226 | friend class AlphabeticIndex; |
| 227 | friend class BucketList; |
| 228 | |
| 229 | UnicodeString label_; |
| 230 | UnicodeString lowerBoundary_; |
| 231 | UAlphabeticIndexLabelType labelType_; |
| 232 | Bucket *displayBucket_; |
| 233 | int32_t displayIndex_; |
| 234 | UVector *records_; // Records are owned by the inputList_ vector. |
| 235 | |
| 236 | Bucket(const UnicodeString &label, // Parameter strings are copied. |
| 237 | const UnicodeString &lowerBoundary, |
| 238 | UAlphabeticIndexLabelType type); |
| 239 | }; |
| 240 | |
| 241 | /** |
| 242 | * Immutable, thread-safe version of AlphabeticIndex. |
| 243 | * This class provides thread-safe methods for bucketing, |
| 244 | * and random access to buckets and their properties, |
| 245 | * but does not offer adding records to the index. |
| 246 | * |
| 247 | * The ImmutableIndex class is not intended for public subclassing. |
| 248 | * |
| 249 | * @stable ICU 51 |
| 250 | */ |
| 251 | class U_I18N_API ImmutableIndex : public UObject { |
| 252 | public: |
| 253 | /** |
| 254 | * Destructor. |
| 255 | * @stable ICU 51 |
| 256 | */ |
| 257 | virtual ~ImmutableIndex(); |
| 258 | |
| 259 | /** |
| 260 | * Returns the number of index buckets and labels, including underflow/inflow/overflow. |
| 261 | * |
| 262 | * @return the number of index buckets |
| 263 | * @stable ICU 51 |
| 264 | */ |
| 265 | int32_t getBucketCount() const; |
| 266 | |
| 267 | /** |
| 268 | * Finds the index bucket for the given name and returns the number of that bucket. |
| 269 | * Use getBucket() to get the bucket's properties. |
| 270 | * |
| 271 | * @param name the string to be sorted into an index bucket |
| 272 | * @param errorCode Error code, will be set with the reason if the |
| 273 | * operation fails. |
| 274 | * @return the bucket number for the name |
| 275 | * @stable ICU 51 |
| 276 | */ |
| 277 | int32_t getBucketIndex(const UnicodeString &name, UErrorCode &errorCode) const; |
| 278 | |
| 279 | /** |
| 280 | * Returns the index-th bucket. Returns NULL if the index is out of range. |
| 281 | * |
| 282 | * @param index bucket number |
| 283 | * @return the index-th bucket |
| 284 | * @stable ICU 51 |
| 285 | */ |
| 286 | const Bucket *getBucket(int32_t index) const; |
| 287 | |
| 288 | private: |
| 289 | friend class AlphabeticIndex; |
| 290 | |
| 291 | ImmutableIndex(BucketList *bucketList, Collator *collatorPrimaryOnly) |
| 292 | : buckets_(bucketList), collatorPrimaryOnly_(collatorPrimaryOnly) {} |
| 293 | |
| 294 | BucketList *buckets_; |
| 295 | Collator *collatorPrimaryOnly_; |
| 296 | }; |
| 297 | |
| 298 | /** |
| 299 | * Construct an AlphabeticIndex object for the specified locale. If the locale's |
| 300 | * data does not include index characters, a set of them will be |
| 301 | * synthesized based on the locale's exemplar characters. The locale |
| 302 | * determines the sorting order for both the index characters and the |
| 303 | * user item names appearing under each Index character. |
| 304 | * |
| 305 | * @param locale the desired locale. |
| 306 | * @param status Error code, will be set with the reason if the construction |
| 307 | * of the AlphabeticIndex object fails. |
| 308 | * @stable ICU 4.8 |
| 309 | */ |
| 310 | AlphabeticIndex(const Locale &locale, UErrorCode &status); |
| 311 | |
| 312 | /** |
| 313 | * Construct an AlphabeticIndex that uses a specific collator. |
| 314 | * |
| 315 | * The index will be created with no labels; the addLabels() function must be called |
| 316 | * after creation to add the desired labels to the index. |
| 317 | * |
| 318 | * The index adopts the collator, and is responsible for deleting it. |
| 319 | * The caller should make no further use of the collator after creating the index. |
| 320 | * |
| 321 | * @param collator The collator to use to order the contents of this index. |
| 322 | * @param status Error code, will be set with the reason if the |
| 323 | * operation fails. |
| 324 | * @stable ICU 51 |
| 325 | */ |
| 326 | AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status); |
| 327 | |
| 328 | /** |
| 329 | * Add Labels to this Index. The labels are additions to those |
| 330 | * that are already in the index; they do not replace the existing |
| 331 | * ones. |
| 332 | * @param additions The additional characters to add to the index, such as A-Z. |
| 333 | * @param status Error code, will be set with the reason if the |
| 334 | * operation fails. |
| 335 | * @return this, for chaining |
| 336 | * @stable ICU 4.8 |
| 337 | */ |
| 338 | virtual AlphabeticIndex &addLabels(const UnicodeSet &additions, UErrorCode &status); |
| 339 | |
| 340 | /** |
| 341 | * Add the index characters from a Locale to the index. The labels |
| 342 | * are added to those that are already in the index; they do not replace the |
| 343 | * existing index characters. The collation order for this index is not |
| 344 | * changed; it remains that of the locale that was originally specified |
| 345 | * when creating this Index. |
| 346 | * |
| 347 | * @param locale The locale whose index characters are to be added. |
| 348 | * @param status Error code, will be set with the reason if the |
| 349 | * operation fails. |
| 350 | * @return this, for chaining |
| 351 | * @stable ICU 4.8 |
| 352 | */ |
| 353 | virtual AlphabeticIndex &addLabels(const Locale &locale, UErrorCode &status); |
| 354 | |
| 355 | /** |
| 356 | * Destructor |
| 357 | * @stable ICU 4.8 |
| 358 | */ |
| 359 | virtual ~AlphabeticIndex(); |
| 360 | |
| 361 | /** |
| 362 | * Builds an immutable, thread-safe version of this instance, without data records. |
| 363 | * |
| 364 | * @return an immutable index instance |
| 365 | * @stable ICU 51 |
| 366 | */ |
| 367 | ImmutableIndex *buildImmutableIndex(UErrorCode &errorCode); |
| 368 | |
| 369 | /** |
| 370 | * Get the Collator that establishes the ordering of the items in this index. |
| 371 | * Ownership of the collator remains with the AlphabeticIndex instance. |
| 372 | * |
| 373 | * The returned collator is a reference to the internal collator used by this |
| 374 | * index. It may be safely used to compare the names of items or to get |
| 375 | * sort keys for names. However if any settings need to be changed, |
| 376 | * or other non-const methods called, a cloned copy must be made first. |
| 377 | * |
| 378 | * @return The collator |
| 379 | * @stable ICU 4.8 |
| 380 | */ |
| 381 | virtual const RuleBasedCollator &getCollator() const; |
| 382 | |
| 383 | |
| 384 | /** |
| 385 | * Get the default label used for abbreviated buckets *between* other index characters. |
| 386 | * For example, consider the labels when Latin (X Y Z) and Greek (Α Β Γ) are used: |
| 387 | * |
| 388 | * X Y Z ... Α Β Γ. |
| 389 | * |
| 390 | * @return inflow label |
| 391 | * @stable ICU 4.8 |
| 392 | */ |
| 393 | virtual const UnicodeString &getInflowLabel() const; |
| 394 | |
| 395 | /** |
| 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 (...) |
| 400 | * |
| 401 | * @param inflowLabel the new Inflow label. |
| 402 | * @param status Error code, will be set with the reason if the operation fails. |
| 403 | * @return this |
| 404 | * @stable ICU 4.8 |
| 405 | */ |
| 406 | virtual AlphabeticIndex &setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status); |
| 407 | |
| 408 | |
| 409 | /** |
| 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. |
| 412 | * |
| 413 | * @return the overflow label |
| 414 | * @stable ICU 4.8 |
| 415 | */ |
| 416 | virtual const UnicodeString &getOverflowLabel() const; |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Set the label used for items that sort after the last normal label, |
| 421 | * and that would not otherwise have an appropriate label. |
| 422 | * |
| 423 | * @param overflowLabel the new overflow label. |
| 424 | * @param status Error code, will be set with the reason if the operation fails. |
| 425 | * @return this |
| 426 | * @stable ICU 4.8 |
| 427 | */ |
| 428 | virtual AlphabeticIndex &setOverflowLabel(const UnicodeString &overflowLabel, UErrorCode &status); |
| 429 | |
| 430 | /** |
| 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. |
| 433 | * |
| 434 | * @return underflow label |
| 435 | * @stable ICU 4.8 |
| 436 | */ |
| 437 | virtual const UnicodeString &getUnderflowLabel() const; |
| 438 | |
| 439 | /** |
| 440 | * Set the label used for items that sort before the first normal label, |
| 441 | * and that would not otherwise have an appropriate label. |
| 442 | * |
| 443 | * @param underflowLabel the new underflow label. |
| 444 | * @param status Error code, will be set with the reason if the operation fails. |
| 445 | * @return this |
| 446 | * @stable ICU 4.8 |
| 447 | */ |
| 448 | virtual AlphabeticIndex &setUnderflowLabel(const UnicodeString &underflowLabel, UErrorCode &status); |
| 449 | |
| 450 | |
| 451 | /** |
| 452 | * Get the limit on the number of labels permitted in the index. |
| 453 | * The number does not include over, under and inflow labels. |
| 454 | * |
| 455 | * @return maxLabelCount maximum number of labels. |
| 456 | * @stable ICU 4.8 |
| 457 | */ |
| 458 | virtual int32_t getMaxLabelCount() const; |
| 459 | |
| 460 | /** |
| 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. |
| 466 | * |
| 467 | * @param maxLabelCount the maximum number of labels. |
| 468 | * @param status error code |
| 469 | * @return This, for chaining |
| 470 | * @stable ICU 4.8 |
| 471 | */ |
| 472 | virtual AlphabeticIndex &setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status); |
| 473 | |
| 474 | |
| 475 | /** |
| 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. |
| 481 | * |
| 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. |
| 489 | * @stable ICU 4.8 |
| 490 | */ |
| 491 | virtual AlphabeticIndex &addRecord(const UnicodeString &name, const void *data, UErrorCode &status); |
| 492 | |
| 493 | /** |
| 494 | * Remove all Records from the Index. The set of Buckets, which define the headings under |
| 495 | * which records are classified, is not altered. |
| 496 | * |
| 497 | * @param status Error code, will be set with the reason if the operation fails. |
| 498 | * @return This, for chaining. |
| 499 | * @stable ICU 4.8 |
| 500 | */ |
| 501 | virtual AlphabeticIndex &clearRecords(UErrorCode &status); |
| 502 | |
| 503 | |
| 504 | /** Get the number of labels in this index. |
| 505 | * Note: may trigger lazy index construction. |
| 506 | * |
| 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 |
| 509 | * in-flow labels. |
| 510 | * @stable ICU 4.8 |
| 511 | */ |
| 512 | virtual int32_t getBucketCount(UErrorCode &status); |
| 513 | |
| 514 | |
| 515 | /** Get the total number of Records in this index, that is, the number |
| 516 | * of <name, data> pairs added. |
| 517 | * |
| 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(). |
| 521 | * @stable ICU 4.8 |
| 522 | */ |
| 523 | virtual int32_t getRecordCount(UErrorCode &status); |
| 524 | |
| 525 | |
| 526 | |
| 527 | /** |
| 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. |
| 532 | * |
| 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. |
| 536 | * @stable ICU 4.8 |
| 537 | * |
| 538 | */ |
| 539 | virtual int32_t getBucketIndex(const UnicodeString &itemName, UErrorCode &status); |
| 540 | |
| 541 | |
| 542 | /** |
| 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 |
| 546 | * @stable ICU 4.8 |
| 547 | */ |
| 548 | virtual int32_t getBucketIndex() const; |
| 549 | |
| 550 | |
| 551 | /** |
| 552 | * Advance the iteration over the Buckets of this index. Return FALSE if |
| 553 | * there are no more Buckets. |
| 554 | * |
| 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. |
| 558 | * |
| 559 | * @return TRUE if success, FALSE if at end of iteration |
| 560 | * @stable ICU 4.8 |
| 561 | */ |
| 562 | virtual UBool nextBucket(UErrorCode &status); |
| 563 | |
| 564 | /** |
| 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. |
| 568 | * |
| 569 | * @return the bucket label. |
| 570 | * @stable ICU 4.8 |
| 571 | */ |
| 572 | virtual const UnicodeString &getBucketLabel() const; |
| 573 | |
| 574 | /** |
| 575 | * Return the type of the label for the current Bucket (selected by the |
| 576 | * iteration over Buckets.) |
| 577 | * |
| 578 | * @return the label type. |
| 579 | * @stable ICU 4.8 |
| 580 | */ |
| 581 | virtual UAlphabeticIndexLabelType getBucketLabelType() const; |
| 582 | |
| 583 | /** |
| 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 |
| 586 | * last, return 0. |
| 587 | * |
| 588 | * @return the number of Records. |
| 589 | * @stable ICU 4.8 |
| 590 | */ |
| 591 | virtual int32_t getBucketRecordCount() const; |
| 592 | |
| 593 | |
| 594 | /** |
| 595 | * Reset the Bucket iteration for this index. The next call to nextBucket() |
| 596 | * will restart the iteration at the first label. |
| 597 | * |
| 598 | * @param status Error code, will be set with the reason if the operation fails. |
| 599 | * @return this, for chaining. |
| 600 | * @stable ICU 4.8 |
| 601 | */ |
| 602 | virtual AlphabeticIndex &resetBucketIterator(UErrorCode &status); |
| 603 | |
| 604 | /** |
| 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. |
| 608 | * |
| 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. |
| 613 | * @stable ICU 4.8 |
| 614 | */ |
| 615 | virtual UBool nextRecord(UErrorCode &status); |
| 616 | |
| 617 | /** |
| 618 | * Get the name of the current Record. |
| 619 | * Return an empty string if the Record iteration position is before first |
| 620 | * or after the last. |
| 621 | * |
| 622 | * @return The name of the current index item. |
| 623 | * @stable ICU 4.8 |
| 624 | */ |
| 625 | virtual const UnicodeString &getRecordName() const; |
| 626 | |
| 627 | |
| 628 | /** |
| 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, |
| 631 | * or after the last. |
| 632 | * |
| 633 | * @return The current Record's data pointer. |
| 634 | * @stable ICU 4.8 |
| 635 | */ |
| 636 | virtual const void *getRecordData() const; |
| 637 | |
| 638 | |
| 639 | /** |
| 640 | * Reset the Record iterator position to before the first Record in the current Bucket. |
| 641 | * |
| 642 | * @return This, for chaining. |
| 643 | * @stable ICU 4.8 |
| 644 | */ |
| 645 | virtual AlphabeticIndex &resetRecordIterator(); |
| 646 | |
| 647 | private: |
| 648 | /** |
| 649 | * No Copy constructor. |
| 650 | * @internal |
| 651 | */ |
| 652 | AlphabeticIndex(const AlphabeticIndex &other); |
| 653 | |
| 654 | /** |
| 655 | * No assignment. |
| 656 | */ |
| 657 | AlphabeticIndex &operator =(const AlphabeticIndex & /*other*/) { return *this;} |
| 658 | |
| 659 | /** |
| 660 | * No Equality operators. |
| 661 | * @internal |
| 662 | */ |
| 663 | virtual UBool operator==(const AlphabeticIndex& other) const; |
| 664 | |
| 665 | /** |
| 666 | * Inequality operator. |
| 667 | * @internal |
| 668 | */ |
| 669 | virtual UBool operator!=(const AlphabeticIndex& other) const; |
| 670 | |
| 671 | // Common initialization, for use from all constructors. |
| 672 | void init(const Locale *locale, UErrorCode &status); |
| 673 | |
| 674 | /** |
| 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. |
| 677 | */ |
| 678 | void addIndexExemplars(const Locale &locale, UErrorCode &status); |
| 679 | /** |
| 680 | * Add Chinese index characters from the tailoring. |
| 681 | */ |
| 682 | UBool addChineseIndexCharacters(UErrorCode &errorCode); |
| 683 | |
| 684 | UVector *firstStringsInScript(UErrorCode &status); |
| 685 | |
| 686 | static UnicodeString separated(const UnicodeString &item); |
| 687 | |
| 688 | /** |
| 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. |
| 692 | */ |
| 693 | void initLabels(UVector &indexCharacters, UErrorCode &errorCode) const; |
| 694 | BucketList *createBucketList(UErrorCode &errorCode) const; |
| 695 | void initBuckets(UErrorCode &errorCode); |
| 696 | void clearBuckets(); |
| 697 | void internalResetBucketIterator(); |
| 698 | |
| 699 | public: |
| 700 | |
| 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. |
| 704 | |
| 705 | #ifndef U_HIDE_INTERNAL_API |
| 706 | /** |
| 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. |
| 709 | * \cond |
| 710 | * @internal |
| 711 | */ |
| 712 | struct Record: public UMemory { |
| 713 | const UnicodeString name_; |
| 714 | const void *data_; |
| 715 | Record(const UnicodeString &name, const void *data); |
| 716 | ~Record(); |
| 717 | }; |
| 718 | /** \endcond */ |
| 719 | #endif /* U_HIDE_INTERNAL_API */ |
| 720 | |
| 721 | private: |
| 722 | |
| 723 | /** |
| 724 | * Holds all user records before they are distributed into buckets. |
| 725 | * Type of contents is (Record *) |
| 726 | * @internal |
| 727 | */ |
| 728 | UVector *inputList_; |
| 729 | |
| 730 | int32_t labelsIterIndex_; // Index of next item to return. |
| 731 | int32_t itemsIterIndex_; |
| 732 | Bucket *currentBucket_; // While an iteration of the index in underway, |
| 733 | // point to the bucket for the current label. |
| 734 | // NULL when no iteration underway. |
| 735 | |
| 736 | int32_t maxLabelCount_; // Limit on # of labels permitted in the index. |
| 737 | |
| 738 | UnicodeSet *initialLabels_; // Initial (unprocessed) set of Labels. Union |
| 739 | // of those explicitly set by the user plus |
| 740 | // those from locales. Raw values, before |
| 741 | // crunching into bucket labels. |
| 742 | |
| 743 | UVector *firstCharsInScripts_; // The first character from each script, |
| 744 | // in collation order. |
| 745 | |
| 746 | RuleBasedCollator *collator_; |
| 747 | RuleBasedCollator *collatorPrimaryOnly_; |
| 748 | |
| 749 | // Lazy evaluated: null means that we have not built yet. |
| 750 | BucketList *buckets_; |
| 751 | |
| 752 | UnicodeString inflowLabel_; |
| 753 | UnicodeString overflowLabel_; |
| 754 | UnicodeString underflowLabel_; |
| 755 | UnicodeString overflowComparisonString_; |
| 756 | |
| 757 | UnicodeString emptyString_; |
| 758 | }; |
| 759 | |
| 760 | U_NAMESPACE_END |
| 761 | |
| 762 | #endif // !UCONFIG_NO_COLLATION |
| 763 | |
| 764 | #endif /* U_SHOW_CPLUSPLUS_API */ |
| 765 | |
| 766 | #endif |
| 767 | |