ICU 73.2 73.2

alphaindex.h
Go to the documentation of this file.
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
30 U_CDECL_BEGIN
31
38 typedef enum UAlphabeticIndexLabelType {
44 U_ALPHAINDEX_NORMAL = 0,
45
51 U_ALPHAINDEX_UNDERFLOW = 1,
52
61 U_ALPHAINDEX_INFLOW = 2,
62
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
192 class U_I18N_API AlphabeticIndex: public UObject {
193 public:
202 class U_I18N_API Bucket : public UObject {
203 public:
208 virtual ~Bucket();
209
216 const UnicodeString &getLabel() const { return label_; }
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
251 class U_I18N_API ImmutableIndex : public UObject {
252 public:
257 virtual ~ImmutableIndex();
258
265 int32_t getBucketCount() const;
266
277 int32_t getBucketIndex(const UnicodeString &name, UErrorCode &errorCode) const;
278
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
310 AlphabeticIndex(const Locale &locale, UErrorCode &status);
311
326 AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status);
327
338 virtual AlphabeticIndex &addLabels(const UnicodeSet &additions, UErrorCode &status);
339
353 virtual AlphabeticIndex &addLabels(const Locale &locale, UErrorCode &status);
354
359 virtual ~AlphabeticIndex();
360
367 ImmutableIndex *buildImmutableIndex(UErrorCode &errorCode);
368
381 virtual const RuleBasedCollator &getCollator() const;
382
383
393 virtual const UnicodeString &getInflowLabel() const;
394
406 virtual AlphabeticIndex &setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status);
407
408
416 virtual const UnicodeString &getOverflowLabel() const;
417
418
428 virtual AlphabeticIndex &setOverflowLabel(const UnicodeString &overflowLabel, UErrorCode &status);
429
437 virtual const UnicodeString &getUnderflowLabel() const;
438
448 virtual AlphabeticIndex &setUnderflowLabel(const UnicodeString &underflowLabel, UErrorCode &status);
449
450
458 virtual int32_t getMaxLabelCount() const;
459
472 virtual AlphabeticIndex &setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status);
473
474
491 virtual AlphabeticIndex &addRecord(const UnicodeString &name, const void *data, UErrorCode &status);
492
501 virtual AlphabeticIndex &clearRecords(UErrorCode &status);
502
503
512 virtual int32_t getBucketCount(UErrorCode &status);
513
514
523 virtual int32_t getRecordCount(UErrorCode &status);
524
525
526
539 virtual int32_t getBucketIndex(const UnicodeString &itemName, UErrorCode &status);
540
541
548 virtual int32_t getBucketIndex() const;
549
550
562 virtual UBool nextBucket(UErrorCode &status);
563
572 virtual const UnicodeString &getBucketLabel() const;
573
581 virtual UAlphabeticIndexLabelType getBucketLabelType() const;
582
591 virtual int32_t getBucketRecordCount() const;
592
593
602 virtual AlphabeticIndex &resetBucketIterator(UErrorCode &status);
603
615 virtual UBool nextRecord(UErrorCode &status);
616
625 virtual const UnicodeString &getRecordName() const;
626
627
636 virtual const void *getRecordData() const;
637
638
645 virtual AlphabeticIndex &resetRecordIterator();
646
647 private:
652 AlphabeticIndex(const AlphabeticIndex &other) = delete;
653
657 AlphabeticIndex &operator =(const AlphabeticIndex & /*other*/) { return *this;}
658
663 virtual bool operator==(const AlphabeticIndex& other) const;
664
669 virtual bool operator!=(const AlphabeticIndex& other) const;
670
671 // Common initialization, for use from all constructors.
672 void init(const Locale *locale, UErrorCode &status);
673
678 void addIndexExemplars(const Locale &locale, UErrorCode &status);
682 UBool addChineseIndexCharacters(UErrorCode &errorCode);
683
684 UVector *firstStringsInScript(UErrorCode &status);
685
686 static UnicodeString separated(const UnicodeString &item);
687
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
712 struct Record: public UMemory {
713 const UnicodeString name_;
714 const void *data_;
715 Record(const UnicodeString &name, const void *data);
716 ~Record();
717 };
719 #endif /* U_HIDE_INTERNAL_API */
720
721 private:
722
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 // nullptr 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
UAlphabeticIndexLabelType
UAlphabeticIndexLabelType
Constants for Alphabetic Index Label Types.
Definition: alphaindex.h:38
U_ALPHAINDEX_OVERFLOW
@ U_ALPHAINDEX_OVERFLOW
Overflow Label.
Definition: alphaindex.h:68
U_ALPHAINDEX_UNDERFLOW
@ U_ALPHAINDEX_UNDERFLOW
Underflow Label.
Definition: alphaindex.h:51
U_ALPHAINDEX_NORMAL
@ U_ALPHAINDEX_NORMAL
Normal Label, typically the starting letter of the names in the bucket with this label.
Definition: alphaindex.h:44
U_ALPHAINDEX_INFLOW
@ U_ALPHAINDEX_INFLOW
Inflow Label.
Definition: alphaindex.h:61
icu::AlphabeticIndex::Bucket
An index "bucket" with a label string and type.
Definition: alphaindex.h:202
icu::AlphabeticIndex::Bucket::getLabelType
UAlphabeticIndexLabelType getLabelType() const
Returns whether this bucket is a normal, underflow, overflow, or inflow bucket.
Definition: alphaindex.h:223
icu::AlphabeticIndex::Bucket::~Bucket
virtual ~Bucket()
Destructor.
icu::AlphabeticIndex::Bucket::getLabel
const UnicodeString & getLabel() const
Returns the label string.
Definition: alphaindex.h:216
icu::AlphabeticIndex::ImmutableIndex
Immutable, thread-safe version of AlphabeticIndex.
Definition: alphaindex.h:251
icu::AlphabeticIndex::ImmutableIndex::getBucketCount
int32_t getBucketCount() const
Returns the number of index buckets and labels, including underflow/inflow/overflow.
icu::AlphabeticIndex::ImmutableIndex::getBucket
const Bucket * getBucket(int32_t index) const
Returns the index-th bucket.
icu::AlphabeticIndex::ImmutableIndex::getBucketIndex
int32_t getBucketIndex(const UnicodeString &name, UErrorCode &errorCode) const
Finds the index bucket for the given name and returns the number of that bucket.
icu::AlphabeticIndex::ImmutableIndex::~ImmutableIndex
virtual ~ImmutableIndex()
Destructor.
icu::AlphabeticIndex
AlphabeticIndex supports the creation of a UI index appropriate for a given language.
Definition: alphaindex.h:192
icu::AlphabeticIndex::getCollator
virtual const RuleBasedCollator & getCollator() const
Get the Collator that establishes the ordering of the items in this index.
icu::AlphabeticIndex::nextRecord
virtual UBool nextRecord(UErrorCode &status)
Advance to the next record in the current Bucket.
icu::AlphabeticIndex::clearRecords
virtual AlphabeticIndex & clearRecords(UErrorCode &status)
Remove all Records from the Index.
icu::AlphabeticIndex::resetBucketIterator
virtual AlphabeticIndex & resetBucketIterator(UErrorCode &status)
Reset the Bucket iteration for this index.
icu::AlphabeticIndex::buildImmutableIndex
ImmutableIndex * buildImmutableIndex(UErrorCode &errorCode)
Builds an immutable, thread-safe version of this instance, without data records.
icu::AlphabeticIndex::AlphabeticIndex
AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status)
Construct an AlphabeticIndex that uses a specific collator.
icu::AlphabeticIndex::getOverflowLabel
virtual const UnicodeString & getOverflowLabel() const
Get the special label used for items that sort after the last normal label, and that would not otherw...
icu::AlphabeticIndex::getRecordCount
virtual int32_t getRecordCount(UErrorCode &status)
Get the total number of Records in this index, that is, the number of <name, data> pairs added.
icu::AlphabeticIndex::~AlphabeticIndex
virtual ~AlphabeticIndex()
Destructor.
icu::AlphabeticIndex::resetRecordIterator
virtual AlphabeticIndex & resetRecordIterator()
Reset the Record iterator position to before the first Record in the current Bucket.
icu::AlphabeticIndex::setUnderflowLabel
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 ha...
icu::AlphabeticIndex::nextBucket
virtual UBool nextBucket(UErrorCode &status)
Advance the iteration over the Buckets of this index.
icu::AlphabeticIndex::getBucketLabel
virtual const UnicodeString & getBucketLabel() const
Return the name of the Label of the current bucket from an iteration over the buckets.
icu::AlphabeticIndex::setOverflowLabel
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...
icu::AlphabeticIndex::addLabels
virtual AlphabeticIndex & addLabels(const Locale &locale, UErrorCode &status)
Add the index characters from a Locale to the index.
icu::AlphabeticIndex::AlphabeticIndex
AlphabeticIndex(const Locale &locale, UErrorCode &status)
Construct an AlphabeticIndex object for the specified locale.
icu::AlphabeticIndex::setMaxLabelCount
virtual AlphabeticIndex & setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status)
Set a limit on the number of labels permitted in the index.
icu::AlphabeticIndex::addRecord
virtual AlphabeticIndex & addRecord(const UnicodeString &name, const void *data, UErrorCode &status)
Add a record to the index.
icu::AlphabeticIndex::setInflowLabel
virtual AlphabeticIndex & setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status)
Set the default label used for abbreviated buckets between other index characters.
icu::AlphabeticIndex::getBucketIndex
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...
icu::AlphabeticIndex::getBucketIndex
virtual int32_t getBucketIndex() const
Get the zero based index of the current Bucket from an iteration over the Buckets of this index.
icu::AlphabeticIndex::getRecordData
virtual const void * getRecordData() const
Return the data pointer of the Record currently being iterated over.
icu::AlphabeticIndex::addLabels
virtual AlphabeticIndex & addLabels(const UnicodeSet &additions, UErrorCode &status)
Add Labels to this Index.
icu::AlphabeticIndex::getRecordName
virtual const UnicodeString & getRecordName() const
Get the name of the current Record.
icu::AlphabeticIndex::getBucketRecordCount
virtual int32_t getBucketRecordCount() const
Get the number of <name, data> Records in the current Bucket.
icu::AlphabeticIndex::getInflowLabel
virtual const UnicodeString & getInflowLabel() const
Get the default label used for abbreviated buckets between other index characters.
icu::AlphabeticIndex::getUnderflowLabel
virtual const UnicodeString & getUnderflowLabel() const
Get the special label used for items that sort before the first normal label, and that would not othe...
icu::AlphabeticIndex::getBucketCount
virtual int32_t getBucketCount(UErrorCode &status)
Get the number of labels in this index.
icu::AlphabeticIndex::getBucketLabelType
virtual UAlphabeticIndexLabelType getBucketLabelType() const
Return the type of the label for the current Bucket (selected by the iteration over Buckets....
icu::AlphabeticIndex::getMaxLabelCount
virtual int32_t getMaxLabelCount() const
Get the limit on the number of labels permitted in the index.
icu::Collator
The Collator class performs locale-sensitive string comparison.
Definition: coll.h:167
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::RuleBasedCollator
The RuleBasedCollator class provides the implementation of Collator, using data-driven tables.
Definition: tblcoll.h:115
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
icu::UnicodeSet
A mutable set of Unicode characters and multicharacter strings.
Definition: uniset.h:285
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
locid.h
C++ API: Locale ID object.
UHashtable
struct UHashtable UHashtable
Definition: msgfmt.h:43
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
U_CDECL_END
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:86
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
U_CDECL_BEGIN
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:85
unistr.h
C++ API: Unicode String.
uobject.h
C++ API: Common ICU base class UObject.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301