ICU 73.2  73.2
alphaindex.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 *******************************************************************************
00005 *
00006 *   Copyright (C) 2011-2014 International Business Machines
00007 *   Corporation and others.  All Rights Reserved.
00008 *
00009 *******************************************************************************
00010 */
00011 
00012 #ifndef INDEXCHARS_H
00013 #define INDEXCHARS_H
00014 
00015 #include "unicode/utypes.h"
00016 
00017 #if U_SHOW_CPLUSPLUS_API
00018 
00019 #include "unicode/uobject.h"
00020 #include "unicode/locid.h"
00021 #include "unicode/unistr.h"
00022 
00023 #if !UCONFIG_NO_COLLATION
00024 
00030 U_CDECL_BEGIN
00031 
00038 typedef enum UAlphabeticIndexLabelType {
00044     U_ALPHAINDEX_NORMAL    = 0,
00045 
00051     U_ALPHAINDEX_UNDERFLOW = 1,
00052 
00061     U_ALPHAINDEX_INFLOW    = 2,
00062 
00068     U_ALPHAINDEX_OVERFLOW  = 3
00069 } UAlphabeticIndexLabelType;
00070 
00071 
00072 struct UHashtable;
00073 U_CDECL_END
00074 
00075 U_NAMESPACE_BEGIN
00076 
00077 // Forward Declarations
00078 
00079 class BucketList;
00080 class Collator;
00081 class RuleBasedCollator;
00082 class StringEnumeration;
00083 class UnicodeSet;
00084 class UVector;
00085 
00192 class U_I18N_API AlphabeticIndex: public UObject {
00193 public:
00202      class U_I18N_API Bucket : public UObject {
00203      public:
00208         virtual ~Bucket();
00209 
00216         const UnicodeString &getLabel() const { return label_; }
00223         UAlphabeticIndexLabelType getLabelType() const { return labelType_; }
00224 
00225      private:
00226         friend class AlphabeticIndex;
00227         friend class BucketList;
00228 
00229         UnicodeString label_;
00230         UnicodeString lowerBoundary_;
00231         UAlphabeticIndexLabelType labelType_;
00232         Bucket *displayBucket_;
00233         int32_t displayIndex_;
00234         UVector *records_;  // Records are owned by the inputList_ vector.
00235 
00236         Bucket(const UnicodeString &label,   // Parameter strings are copied.
00237                const UnicodeString &lowerBoundary,
00238                UAlphabeticIndexLabelType type);
00239      };
00240 
00251     class U_I18N_API ImmutableIndex : public UObject {
00252     public:
00257         virtual ~ImmutableIndex();
00258 
00265         int32_t getBucketCount() const;
00266 
00277         int32_t getBucketIndex(const UnicodeString &name, UErrorCode &errorCode) const;
00278 
00286         const Bucket *getBucket(int32_t index) const;
00287 
00288     private:
00289         friend class AlphabeticIndex;
00290 
00291         ImmutableIndex(BucketList *bucketList, Collator *collatorPrimaryOnly)
00292                 : buckets_(bucketList), collatorPrimaryOnly_(collatorPrimaryOnly) {}
00293 
00294         BucketList *buckets_;
00295         Collator *collatorPrimaryOnly_;
00296     };
00297 
00310      AlphabeticIndex(const Locale &locale, UErrorCode &status);
00311 
00326     AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status);
00327 
00338     virtual AlphabeticIndex &addLabels(const UnicodeSet &additions, UErrorCode &status);
00339 
00353     virtual AlphabeticIndex &addLabels(const Locale &locale, UErrorCode &status);
00354 
00359     virtual ~AlphabeticIndex();
00360 
00367     ImmutableIndex *buildImmutableIndex(UErrorCode &errorCode);
00368 
00381     virtual const RuleBasedCollator &getCollator() const;
00382 
00383 
00393     virtual const UnicodeString &getInflowLabel() const;
00394 
00406     virtual AlphabeticIndex &setInflowLabel(const UnicodeString &inflowLabel, UErrorCode &status);
00407 
00408 
00416     virtual const UnicodeString &getOverflowLabel() const;
00417 
00418 
00428     virtual AlphabeticIndex &setOverflowLabel(const UnicodeString &overflowLabel, UErrorCode &status);
00429 
00437     virtual const UnicodeString &getUnderflowLabel() const;
00438 
00448     virtual AlphabeticIndex &setUnderflowLabel(const UnicodeString &underflowLabel, UErrorCode &status);
00449 
00450 
00458     virtual int32_t getMaxLabelCount() const;
00459 
00472     virtual AlphabeticIndex &setMaxLabelCount(int32_t maxLabelCount, UErrorCode &status);
00473 
00474 
00491     virtual AlphabeticIndex &addRecord(const UnicodeString &name, const void *data, UErrorCode &status);
00492 
00501     virtual AlphabeticIndex &clearRecords(UErrorCode &status);
00502 
00503 
00512     virtual int32_t  getBucketCount(UErrorCode &status);
00513 
00514 
00523     virtual int32_t  getRecordCount(UErrorCode &status);
00524 
00525 
00526 
00539     virtual int32_t  getBucketIndex(const UnicodeString &itemName, UErrorCode &status);
00540 
00541 
00548     virtual int32_t  getBucketIndex() const;
00549 
00550 
00562     virtual UBool nextBucket(UErrorCode &status);
00563 
00572     virtual const UnicodeString &getBucketLabel() const;
00573 
00581     virtual UAlphabeticIndexLabelType getBucketLabelType() const;
00582 
00591     virtual int32_t getBucketRecordCount() const;
00592 
00593 
00602     virtual AlphabeticIndex &resetBucketIterator(UErrorCode &status);
00603 
00615     virtual UBool nextRecord(UErrorCode &status);
00616 
00625     virtual const UnicodeString &getRecordName() const;
00626 
00627 
00636     virtual const void *getRecordData() const;
00637 
00638 
00645     virtual AlphabeticIndex &resetRecordIterator();
00646 
00647 private:
00652      AlphabeticIndex(const AlphabeticIndex &other) = delete;
00653 
00657      AlphabeticIndex &operator =(const AlphabeticIndex & /*other*/) { return *this;}
00658 
00663      virtual bool operator==(const AlphabeticIndex& other) const;
00664 
00669      virtual bool operator!=(const AlphabeticIndex& other) const;
00670 
00671      // Common initialization, for use from all constructors.
00672      void init(const Locale *locale, UErrorCode &status);
00673 
00678     void addIndexExemplars(const Locale &locale, UErrorCode &status);
00682     UBool addChineseIndexCharacters(UErrorCode &errorCode);
00683 
00684     UVector *firstStringsInScript(UErrorCode &status);
00685 
00686     static UnicodeString separated(const UnicodeString &item);
00687 
00693     void initLabels(UVector &indexCharacters, UErrorCode &errorCode) const;
00694     BucketList *createBucketList(UErrorCode &errorCode) const;
00695     void initBuckets(UErrorCode &errorCode);
00696     void clearBuckets();
00697     void internalResetBucketIterator();
00698 
00699 public:
00700 
00701     //  The Record is declared public only to allow access from
00702     //  implementation code written in plain C.
00703     //  It is not intended for public use.
00704 
00705 #ifndef U_HIDE_INTERNAL_API
00706 
00712     struct Record: public UMemory {
00713         const UnicodeString  name_;
00714         const void           *data_;
00715         Record(const UnicodeString &name, const void *data);
00716         ~Record();
00717     };
00719 #endif  /* U_HIDE_INTERNAL_API */
00720 
00721 private:
00722 
00728     UVector  *inputList_;
00729 
00730     int32_t  labelsIterIndex_;        // Index of next item to return.
00731     int32_t  itemsIterIndex_;
00732     Bucket   *currentBucket_;         // While an iteration of the index in underway,
00733                                       //   point to the bucket for the current label.
00734                                       // nullptr when no iteration underway.
00735 
00736     int32_t    maxLabelCount_;        // Limit on # of labels permitted in the index.
00737 
00738     UnicodeSet *initialLabels_;       // Initial (unprocessed) set of Labels.  Union
00739                                       //   of those explicitly set by the user plus
00740                                       //   those from locales.  Raw values, before
00741                                       //   crunching into bucket labels.
00742 
00743     UVector *firstCharsInScripts_;    // The first character from each script,
00744                                       //   in collation order.
00745 
00746     RuleBasedCollator *collator_;
00747     RuleBasedCollator *collatorPrimaryOnly_;
00748 
00749     // Lazy evaluated: null means that we have not built yet.
00750     BucketList *buckets_;
00751 
00752     UnicodeString  inflowLabel_;
00753     UnicodeString  overflowLabel_;
00754     UnicodeString  underflowLabel_;
00755     UnicodeString  overflowComparisonString_;
00756 
00757     UnicodeString emptyString_;
00758 };
00759 
00760 U_NAMESPACE_END
00761 
00762 #endif  // !UCONFIG_NO_COLLATION
00763 
00764 #endif /* U_SHOW_CPLUSPLUS_API */
00765 
00766 #endif