]> granicus.if.org Git - icu/commitdiff
ICU-9842 AlphabeticIndex: add constructor taking a collator.
authorAndy Heninger <andy.heninger@gmail.com>
Sat, 9 Feb 2013 01:07:41 +0000 (01:07 +0000)
committerAndy Heninger <andy.heninger@gmail.com>
Sat, 9 Feb 2013 01:07:41 +0000 (01:07 +0000)
X-SVN-Rev: 33148

icu4c/source/i18n/alphaindex.cpp
icu4c/source/i18n/unicode/alphaindex.h
icu4c/source/test/intltest/alphaindextst.cpp

index a9c4d7a5e60ca7d67456913f835b7c166b023d25..1d4937457490816578e3ebf844d2917b8057b66d 100644 (file)
@@ -1,6 +1,6 @@
 /*
 *******************************************************************************
-* Copyright (C) 2009-2012, International Business Machines Corporation and    *
+* Copyright (C) 2009-2013, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
@@ -102,6 +102,29 @@ AlphabeticIndex::AlphabeticIndex(const Locale &locale, UErrorCode &status) {
 }
 
 
+AlphabeticIndex::AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status) {
+    init(status);
+    if (U_FAILURE(status)) {
+        return;
+    }
+    if (collator == NULL) {
+        status = U_ILLEGAL_ARGUMENT_ERROR;
+        return;
+    }
+    collator_ = collator;
+    collatorPrimaryOnly_ = collator_->clone();
+    if (collatorPrimaryOnly_ == NULL) {
+        status = U_MEMORY_ALLOCATION_ERROR;
+        return;
+    }
+    collatorPrimaryOnly_->setStrength(Collator::PRIMARY);
+    // Note: initialLabels_ is set to an empty UnicodeSet by init().
+    indexBuildRequired_ = TRUE;
+    firstScriptCharacters_ = firstStringsInScript(status);
+}
+
+
+
 AlphabeticIndex::~AlphabeticIndex() {
     uhash_close(alreadyIn_);
     delete bucketList_;
@@ -219,6 +242,8 @@ void AlphabeticIndex::buildIndex(UErrorCode &status) {
     // If we have no labels, hard-code a fallback default set of [A-Z]
     // This case can occur with locales that don't have exemplar character data, including root.
     // A no-labels situation will cause other problems; it needs to be avoided.
+    //
+    // TODO: This case should be handled by having an underflow label only.
     if (labelSet.isEmpty()) {
         labelSet.add((UChar32)0x41, (UChar32)0x5A);
     }
index f493ec66c773040ce9fb182388578271c98ccd3b..a4dbbbd50cd2d920002e64666add34bb992b501c 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *******************************************************************************
 *
-*   Copyright (C) 2011-2012 International Business Machines
+*   Copyright (C) 2011-2013 International Business Machines
 *   Corporation and others.  All Rights Reserved.
 *
 *******************************************************************************
@@ -181,6 +181,21 @@ class U_I18N_API AlphabeticIndex: public UObject {
      AlphabeticIndex(const Locale &locale, 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.
+     * 
+     * @param collator The collator to use to order the contents of this index.
+     * @param status Error code, will be set with the reason if the 
+     *               operation fails.
+     * @draft ICU 51
+     */
+    AlphabeticIndex(RuleBasedCollator *collator, UErrorCode &status);
 
     /**
      * Add Labels to this Index.  The labels are additions to those
index 427e450c44a557bea0c823fcb340b3cd8d95994d..adb9b0fe95705b997246b951e721933c2fa6dd27 100644 (file)
@@ -76,6 +76,23 @@ void AlphabeticIndexTest::APITest() {
     //printf("getBucketCount() == %d\n", lc);
     delete index;
 
+    // Constructor from a Collator
+    //
+    status = U_ZERO_ERROR;
+    RuleBasedCollator *coll = dynamic_cast<RuleBasedCollator *>(Collator::createInstance(Locale::getChinese(), status));
+    TEST_CHECK_STATUS;
+    TEST_ASSERT(coll != NULL);
+    index = new AlphabeticIndex(coll, status);
+    TEST_CHECK_STATUS;
+    TEST_ASSERT(coll == &index->getCollator());
+    // TODO: The bucket count for an index built from a collator should be one, the underflow label.
+    //       The current implementation adds A-Z if the index is otherwise empty.
+    // TEST_ASSERT(1 == index->getBucketCount(status));
+    TEST_ASSERT(28 == index->getBucketCount(status));
+    TEST_CHECK_STATUS;
+    delete index;
+    
+
     // addLabels()
 
     status = U_ZERO_ERROR;