From 686c8d47f9a7979e3637048a0164758ebabb07d7 Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Sat, 9 Feb 2013 01:07:41 +0000 Subject: [PATCH] ICU-9842 AlphabeticIndex: add constructor taking a collator. X-SVN-Rev: 33148 --- icu4c/source/i18n/alphaindex.cpp | 27 +++++++++++++++++++- icu4c/source/i18n/unicode/alphaindex.h | 17 +++++++++++- icu4c/source/test/intltest/alphaindextst.cpp | 17 ++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/icu4c/source/i18n/alphaindex.cpp b/icu4c/source/i18n/alphaindex.cpp index a9c4d7a5e60..1d493745749 100644 --- a/icu4c/source/i18n/alphaindex.cpp +++ b/icu4c/source/i18n/alphaindex.cpp @@ -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); } diff --git a/icu4c/source/i18n/unicode/alphaindex.h b/icu4c/source/i18n/unicode/alphaindex.h index f493ec66c77..a4dbbbd50cd 100644 --- a/icu4c/source/i18n/unicode/alphaindex.h +++ b/icu4c/source/i18n/unicode/alphaindex.h @@ -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 diff --git a/icu4c/source/test/intltest/alphaindextst.cpp b/icu4c/source/test/intltest/alphaindextst.cpp index 427e450c44a..adb9b0fe957 100644 --- a/icu4c/source/test/intltest/alphaindextst.cpp +++ b/icu4c/source/test/intltest/alphaindextst.cpp @@ -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(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;