ICU 55.1  55.1
alphaindex.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2011-2014 International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 */
9 
10 #ifndef INDEXCHARS_H
11 #define INDEXCHARS_H
12 
13 #include "unicode/utypes.h"
14 #include "unicode/uobject.h"
15 #include "unicode/locid.h"
16 
17 #if !UCONFIG_NO_COLLATION
18 
25 
39 
46 
56 
64 
65 
66 struct UHashtable;
68 
70 
71 // Forward Declarations
72 
73 class BucketList;
74 class Collator;
75 class RuleBasedCollator;
76 class StringEnumeration;
77 class UnicodeSet;
78 class UVector;
79 
187 public:
196  class U_I18N_API Bucket : public UObject {
197  public:
202  virtual ~Bucket();
203 
210  const UnicodeString &getLabel() const { return label_; }
217  UAlphabeticIndexLabelType getLabelType() const { return labelType_; }
218 
219  private:
220  friend class AlphabeticIndex;
221  friend class BucketList;
222 
223  UnicodeString label_;
224  UnicodeString lowerBoundary_;
225  UAlphabeticIndexLabelType labelType_;
226  Bucket *displayBucket_;
227  int32_t displayIndex_;
228  UVector *records_; // Records are owned by the inputList_ vector.
229 
230  Bucket(const UnicodeString &label, // Parameter strings are copied.
231  const UnicodeString &lowerBoundary,
233  };
234 
246  public:
251  virtual ~ImmutableIndex();
252 
259  int32_t getBucketCount() const;
260 
269  int32_t getBucketIndex(const UnicodeString &name, UErrorCode &errorCode) const;
270 
278  const Bucket *getBucket(int32_t index) const;
279 
280  private:
281  friend class AlphabeticIndex;
282 
283  ImmutableIndex(BucketList *bucketList, Collator *collatorPrimaryOnly)
284  : buckets_(bucketList), collatorPrimaryOnly_(collatorPrimaryOnly) {}
285 
286  BucketList *buckets_;
287  Collator *collatorPrimaryOnly_;
288  };
289 
302  AlphabeticIndex(const Locale &locale, UErrorCode &status);
303 
319 
330  virtual AlphabeticIndex &addLabels(const UnicodeSet &additions, UErrorCode &status);
331 
345  virtual AlphabeticIndex &addLabels(const Locale &locale, UErrorCode &status);
346 
351  virtual ~AlphabeticIndex();
352 
360 
373  virtual const RuleBasedCollator &getCollator() const;
374 
375 
384  virtual const UnicodeString &getInflowLabel() const;
385 
397  virtual AlphabeticIndex &setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status);
398 
399 
407  virtual const UnicodeString &getOverflowLabel() const;
408 
409 
419  virtual AlphabeticIndex &setOverflowLabel(const UnicodeString &overflowLabel, UErrorCode &status);
420 
428  virtual const UnicodeString &getUnderflowLabel() const;
429 
439  virtual AlphabeticIndex &setUnderflowLabel(const UnicodeString &underflowLabel, UErrorCode &status);
440 
441 
449  virtual int32_t getMaxLabelCount() const;
450 
463  virtual AlphabeticIndex &setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status);
464 
465 
482  virtual AlphabeticIndex &addRecord(const UnicodeString &name, const void *data, UErrorCode &status);
483 
493 
494 
503  virtual int32_t getBucketCount(UErrorCode &status);
504 
505 
514  virtual int32_t getRecordCount(UErrorCode &status);
515 
516 
517 
530  virtual int32_t getBucketIndex(const UnicodeString &itemName, UErrorCode &status);
531 
532 
539  virtual int32_t getBucketIndex() const;
540 
541 
553  virtual UBool nextBucket(UErrorCode &status);
554 
563  virtual const UnicodeString &getBucketLabel() const;
564 
573 
582  virtual int32_t getBucketRecordCount() const;
583 
584 
594 
606  virtual UBool nextRecord(UErrorCode &status);
607 
616  virtual const UnicodeString &getRecordName() const;
617 
618 
627  virtual const void *getRecordData() const;
628 
629 
637 
638 private:
643  AlphabeticIndex(const AlphabeticIndex &other);
644 
648  AlphabeticIndex &operator =(const AlphabeticIndex & /*other*/) { return *this;};
649 
654  virtual UBool operator==(const AlphabeticIndex& other) const;
655 
660  virtual UBool operator!=(const AlphabeticIndex& other) const;
661 
662  // Common initialization, for use from all constructors.
663  void init(const Locale *locale, UErrorCode &status);
664 
669  void addIndexExemplars(const Locale &locale, UErrorCode &status);
673  UBool addChineseIndexCharacters(UErrorCode &errorCode);
674 
675  UVector *firstStringsInScript(UErrorCode &status);
676 
677  static UnicodeString separated(const UnicodeString &item);
678 
684  void initLabels(UVector &indexCharacters, UErrorCode &errorCode) const;
685  BucketList *createBucketList(UErrorCode &errorCode) const;
686  void initBuckets(UErrorCode &errorCode);
687  void clearBuckets();
688  void internalResetBucketIterator();
689 
690 public:
691 
692  // The Record is declared public only to allow access from
693  // implementation code written in plain C.
694  // It is not intended for public use.
695 
696 #ifndef U_HIDE_INTERNAL_API
697 
702  struct Record: public UMemory {
703  const UnicodeString name_;
704  const void *data_;
705  Record(const UnicodeString &name, const void *data);
706  ~Record();
707  };
708 #endif /* U_HIDE_INTERNAL_API */
709 
710 private:
711 
717  UVector *inputList_;
718 
719  int32_t labelsIterIndex_; // Index of next item to return.
720  int32_t itemsIterIndex_;
721  Bucket *currentBucket_; // While an iteration of the index in underway,
722  // point to the bucket for the current label.
723  // NULL when no iteration underway.
724 
725  int32_t maxLabelCount_; // Limit on # of labels permitted in the index.
726 
727  UnicodeSet *initialLabels_; // Initial (unprocessed) set of Labels. Union
728  // of those explicitly set by the user plus
729  // those from locales. Raw values, before
730  // crunching into bucket labels.
731 
732  UVector *firstCharsInScripts_; // The first character from each script,
733  // in collation order.
734 
735  RuleBasedCollator *collator_;
736  RuleBasedCollator *collatorPrimaryOnly_;
737 
738  // Lazy evaluated: null means that we have not built yet.
739  BucketList *buckets_;
740 
741  UnicodeString inflowLabel_;
742  UnicodeString overflowLabel_;
743  UnicodeString underflowLabel_;
744  UnicodeString overflowComparisonString_;
745 
746  UnicodeString emptyString_;
747 };
748 
750 
751 #endif // !UCONFIG_NO_COLLATION
752 #endif
icu::AlphabeticIndex::Bucket::getLabelType
UAlphabeticIndexLabelType getLabelType() const
Returns whether this bucket is a normal, underflow, overflow, or inflow bucket.
Definition: alphaindex.h:217
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::~AlphabeticIndex
virtual ~AlphabeticIndex()
Destructor.
icu::AlphabeticIndex::addLabels
virtual AlphabeticIndex & addLabels(const UnicodeSet &additions, UErrorCode &status)
Add Labels to this Index.
icu::AlphabeticIndex::Bucket::getLabel
const UnicodeString & getLabel() const
Returns the label string.
Definition: alphaindex.h:210
icu::AlphabeticIndex::getRecordName
virtual const UnicodeString & getRecordName() const
Get the name of the current Record.
icu::AlphabeticIndex::getRecordData
virtual const void * getRecordData() const
Return the data pointer of the Record currently being iterated over.
icu::AlphabeticIndex::ImmutableIndex::~ImmutableIndex
virtual ~ImmutableIndex()
Destructor.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
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::getBucketRecordCount
virtual int32_t getBucketRecordCount() const
Get the number of <name, data> Records in the current Bucket.
icu::UnicodeSet
A mutable set of Unicode characters and multicharacter strings.
Definition: uniset.h:276
icu::AlphabeticIndex::getBucketLabelType
virtual UAlphabeticIndexLabelType getBucketLabelType() const
Return the type of the label for the current Bucket (selected by the iteration over Buckets....
UAlphabeticIndexLabelType
UAlphabeticIndexLabelType
Constants for Alphabetic Index Label Types.
Definition: alphaindex.h:32
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:358
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:234
icu::AlphabeticIndex::ImmutableIndex::getBucketCount
int32_t getBucketCount() const
Returns the number of index buckets and labels, including underflow/inflow/overflow.
icu::AlphabeticIndex::getBucketCount
virtual int32_t getBucketCount(UErrorCode &status)
Get the number of labels in this index.
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::Collator
The Collator class performs locale-sensitive string comparison.
Definition: coll.h:163
icu::AlphabeticIndex::nextBucket
virtual UBool nextBucket(UErrorCode &status)
Advance the iteration over the Buckets of this index.
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::getInflowLabel
virtual const UnicodeString & getInflowLabel() const
Get the default label used for abbreviated buckets between other index characters.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:246
icu::AlphabeticIndex::resetRecordIterator
virtual AlphabeticIndex & resetRecordIterator()
Reset the Record iterator position to before the first Record in the current Bucket.
icu::AlphabeticIndex::ImmutableIndex::getBucket
const Bucket * getBucket(int32_t index) const
Returns the index-th bucket.
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::addRecord
virtual AlphabeticIndex & addRecord(const UnicodeString &name, const void *data, UErrorCode &status)
Add a record to the index.
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:221
U_ALPHAINDEX_NORMAL
@ U_ALPHAINDEX_NORMAL
Normal Label, typically the starting letter of the names in the bucket with this label.
Definition: alphaindex.h:38
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:476
icu::AlphabeticIndex::clearRecords
virtual AlphabeticIndex & clearRecords(UErrorCode &status)
Remove all Records from the Index.
icu::AlphabeticIndex::Bucket::~Bucket
virtual ~Bucket()
Destructor.
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:110
icu::RuleBasedCollator
The RuleBasedCollator class provides the implementation of Collator, using data-driven tables.
Definition: tblcoll.h:111
U_ALPHAINDEX_INFLOW
@ U_ALPHAINDEX_INFLOW
Inflow Label.
Definition: alphaindex.h:55
U_CDECL_END
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:83
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
U_CDECL_BEGIN
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:82
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::AlphabeticIndex
AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status)
Construct an AlphabeticIndex that uses a specific collator.
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
AlphabeticIndex(const Locale &locale, UErrorCode &status)
Construct an AlphabeticIndex object for the specified locale.
icu::AlphabeticIndex::buildImmutableIndex
ImmutableIndex * buildImmutableIndex(UErrorCode &errorCode)
Builds an immutable, thread-safe version of this instance, without data records.
icu::AlphabeticIndex::getMaxLabelCount
virtual int32_t getMaxLabelCount() const
Get the limit on the number of labels permitted in the index.
icu::AlphabeticIndex::resetBucketIterator
virtual AlphabeticIndex & resetBucketIterator(UErrorCode &status)
Reset the Bucket iteration for this index.
icu::AlphabeticIndex::addLabels
virtual AlphabeticIndex & addLabels(const Locale &locale, UErrorCode &status)
Add the index characters from a Locale to the index.
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::ImmutableIndex
Immutable, thread-safe version of AlphabeticIndex.
Definition: alphaindex.h:245
U_ALPHAINDEX_OVERFLOW
@ U_ALPHAINDEX_OVERFLOW
Overflow Label.
Definition: alphaindex.h:62
U_ALPHAINDEX_UNDERFLOW
@ U_ALPHAINDEX_UNDERFLOW
Undeflow Label.
Definition: alphaindex.h:45
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::getCollator
virtual const RuleBasedCollator & getCollator() const
Get the Collator that establishes the ordering of the items in 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::Record
A (name, data) pair, to be sorted by name into one of the index buckets.
Definition: alphaindex.h:702
locid.h
C++ API: Locale ID object.
uobject.h
C++ API: Common ICU base class UObject.
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
icu::AlphabeticIndex
AlphabeticIndex supports the creation of a UI index appropriate for a given language.
Definition: alphaindex.h:186
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...
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
icu::AlphabeticIndex::nextRecord
virtual UBool nextRecord(UErrorCode &status)
Advance to the next record in the current Bucket.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:185
icu::AlphabeticIndex::Bucket
An index "bucket" with a label string and type.
Definition: alphaindex.h:196
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...