From 3ff650d5659ed050e33074355ee2384f1569b21e Mon Sep 17 00:00:00 2001 From: Kohei Yoshida Date: Fri, 4 Oct 2013 19:13:26 -0400 Subject: Rename StringPool to SharedStringPool because that's what it is. Change-Id: I2fc3ce4f0c2291d402cb470346d5561373fb51e7 --- editeng/source/editeng/editobj.cxx | 20 ++--- editeng/source/editeng/editobj2.hxx | 14 ++-- include/editeng/editobj.hxx | 8 +- include/svl/sharedstringpool.hxx | 94 +++++++++++++++++++++ include/svl/stringpool.hxx | 94 --------------------- sc/inc/document.hxx | 8 +- sc/inc/formulagroup.hxx | 4 +- sc/qa/unit/ucalc.cxx | 4 +- sc/source/core/data/column3.cxx | 2 +- sc/source/core/data/documen2.cxx | 8 +- sc/source/core/data/documentimport.cxx | 2 +- sc/source/core/tool/interpr1.cxx | 2 +- svl/Library_svl.mk | 2 +- svl/qa/unit/svl.cxx | 6 +- svl/source/misc/sharedstringpool.cxx | 145 +++++++++++++++++++++++++++++++++ svl/source/misc/stringpool.cxx | 145 --------------------------------- 16 files changed, 279 insertions(+), 279 deletions(-) create mode 100644 include/svl/sharedstringpool.hxx delete mode 100644 include/svl/stringpool.hxx create mode 100644 svl/source/misc/sharedstringpool.cxx delete mode 100644 svl/source/misc/stringpool.cxx diff --git a/editeng/source/editeng/editobj.cxx b/editeng/source/editeng/editobj.cxx index 4e6af473b38d..3a5d41c47257 100644 --- a/editeng/source/editeng/editobj.cxx +++ b/editeng/source/editeng/editobj.cxx @@ -44,7 +44,7 @@ #include #include -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include #include @@ -149,17 +149,17 @@ ContentInfo::~ContentInfo() aAttribs.clear(); } -void ContentInfo::NormalizeString( svl::StringPool& rPool ) +void ContentInfo::NormalizeString( svl::SharedStringPool& rPool ) { aText = OUString(rPool.intern(aText)); } -sal_uIntPtr ContentInfo::GetStringID( const svl::StringPool& rPool ) const +sal_uIntPtr ContentInfo::GetStringID( const svl::SharedStringPool& rPool ) const { return rPool.getIdentifier(aText); } -sal_uIntPtr ContentInfo::GetStringIDIgnoreCase( const svl::StringPool& rPool ) const +sal_uIntPtr ContentInfo::GetStringIDIgnoreCase( const svl::SharedStringPool& rPool ) const { return rPool.getIdentifierIgnoreCase(aText); } @@ -332,17 +332,17 @@ editeng::FieldUpdater EditTextObject::GetFieldUpdater() return mpImpl->GetFieldUpdater(); } -void EditTextObject::NormalizeString( svl::StringPool& rPool ) +void EditTextObject::NormalizeString( svl::SharedStringPool& rPool ) { mpImpl->NormalizeString(rPool); } -bool EditTextObject::GetStringIDs( const svl::StringPool& rPool, std::vector& rIDs ) const +bool EditTextObject::GetStringIDs( const svl::SharedStringPool& rPool, std::vector& rIDs ) const { return mpImpl->GetStringIDs(rPool, rIDs); } -bool EditTextObject::GetStringIDsIgnoreCase( const svl::StringPool& rPool, std::vector& rIDs ) const +bool EditTextObject::GetStringIDsIgnoreCase( const svl::SharedStringPool& rPool, std::vector& rIDs ) const { return mpImpl->GetStringIDsIgnoreCase(rPool, rIDs); } @@ -633,7 +633,7 @@ void EditTextObjectImpl::SetUserType( sal_uInt16 n ) nUserType = n; } -void EditTextObjectImpl::NormalizeString( svl::StringPool& rPool ) +void EditTextObjectImpl::NormalizeString( svl::SharedStringPool& rPool ) { ContentInfosType::iterator it = aContents.begin(), itEnd = aContents.end(); for (; it != itEnd; ++it) @@ -643,7 +643,7 @@ void EditTextObjectImpl::NormalizeString( svl::StringPool& rPool ) } } -bool EditTextObjectImpl::GetStringIDs( const svl::StringPool& rPool, std::vector& rIDs ) const +bool EditTextObjectImpl::GetStringIDs( const svl::SharedStringPool& rPool, std::vector& rIDs ) const { std::vector aIDs; aIDs.reserve(aContents.size()); @@ -662,7 +662,7 @@ bool EditTextObjectImpl::GetStringIDs( const svl::StringPool& rPool, std::vector return true; } -bool EditTextObjectImpl::GetStringIDsIgnoreCase( const svl::StringPool& rPool, std::vector& rIDs ) const +bool EditTextObjectImpl::GetStringIDsIgnoreCase( const svl::SharedStringPool& rPool, std::vector& rIDs ) const { std::vector aIDs; aIDs.reserve(aContents.size()); diff --git a/editeng/source/editeng/editobj2.hxx b/editeng/source/editeng/editobj2.hxx index d2118d79c318..20dafe929db5 100644 --- a/editeng/source/editeng/editobj2.hxx +++ b/editeng/source/editeng/editobj2.hxx @@ -37,7 +37,7 @@ struct Section; namespace svl { -class StringPool; +class SharedStringPool; } @@ -142,9 +142,9 @@ private: public: ~ContentInfo(); - void NormalizeString( svl::StringPool& rPool ); - sal_uIntPtr GetStringID( const svl::StringPool& rPool ) const; - sal_uIntPtr GetStringIDIgnoreCase( const svl::StringPool& rPool ) const; + void NormalizeString( svl::SharedStringPool& rPool ); + sal_uIntPtr GetStringID( const svl::SharedStringPool& rPool ) const; + sal_uIntPtr GetStringIDIgnoreCase( const svl::SharedStringPool& rPool ) const; const XEditAttributesType& GetAttribs() const { return aAttribs; } XEditAttributesType& GetAttribs() { return aAttribs; } @@ -208,9 +208,9 @@ public: sal_uInt16 GetUserType() const; void SetUserType( sal_uInt16 n ); - void NormalizeString( svl::StringPool& rPool ); - bool GetStringIDs( const svl::StringPool& rPool, std::vector& rIDs ) const; - bool GetStringIDsIgnoreCase( const svl::StringPool& rPool, std::vector& rIDs ) const; + void NormalizeString( svl::SharedStringPool& rPool ); + bool GetStringIDs( const svl::SharedStringPool& rPool, std::vector& rIDs ) const; + bool GetStringIDsIgnoreCase( const svl::SharedStringPool& rPool, std::vector& rIDs ) const; bool IsVertical() const; void SetVertical( bool b ); diff --git a/include/editeng/editobj.hxx b/include/editeng/editobj.hxx index 4751469c68ec..3d1cccaed7b0 100644 --- a/include/editeng/editobj.hxx +++ b/include/editeng/editobj.hxx @@ -51,7 +51,7 @@ struct Section; namespace svl { -class StringPool; +class SharedStringPool; } @@ -83,10 +83,10 @@ public: * * @param rPool shared string pool. */ - void NormalizeString( svl::StringPool& rPool ); + void NormalizeString( svl::SharedStringPool& rPool ); - bool GetStringIDs( const svl::StringPool& rPool, std::vector& rIDs ) const; - bool GetStringIDsIgnoreCase( const svl::StringPool& rPool, std::vector& rIDs ) const; + bool GetStringIDs( const svl::SharedStringPool& rPool, std::vector& rIDs ) const; + bool GetStringIDsIgnoreCase( const svl::SharedStringPool& rPool, std::vector& rIDs ) const; const SfxItemPool* GetPool() const; sal_uInt16 GetUserType() const; // For OutlinerMode, it can however not save in compatible format diff --git a/include/svl/sharedstringpool.hxx b/include/svl/sharedstringpool.hxx new file mode 100644 index 000000000000..6793162059f8 --- /dev/null +++ b/include/svl/sharedstringpool.hxx @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef SVL_STRINGPOOL_HXX +#define SVL_STRINGPOOL_HXX + +#include "svl/svldllapi.h" +#include "rtl/ustring.hxx" + +#include +#include + +class CharClass; + +namespace svl { + +/** + * Storage for pool of shared strings. It also provides mapping from + * original-cased strings to upper-cased strings for case insensitive + * operations. + */ +class SVL_DLLPUBLIC SharedStringPool +{ + typedef boost::unordered_set StrHashType; + typedef std::pair InsertResultType; + typedef boost::unordered_map StrIdMapType; + + StrHashType maStrPool; + StrHashType maStrPoolUpper; + StrIdMapType maToUpperMap; + const CharClass* mpCharClass; + +public: + + SharedStringPool(); + SharedStringPool( const CharClass* pCharClass ); + + /** + * Intern a string object into the shared string pool. + * + * @param rStr string object to intern. + * + * @return a pointer to the string object stored inside the pool, or NULL + * if the insertion fails. + */ + rtl_uString* intern( const OUString& rStr ); + + /** + * Get a unique ID of string object that's expected to be in the shared + * string pool. If the string is not in the pool, NULL is returned. The + * ID obtained by this method can be used for case sensitive comparison. + * + * @param rStr string object to get the ID of. + * + * @return unique ID of the string object. + */ + sal_uIntPtr getIdentifier( const OUString& rStr ) const; + + /** + * Get a unique ID of string object for case insensitive comparison. The + * string object is expected to be in the pool. + * + * @param rStr string object to get the ID of. + * + * @return unique ID of the string object usable for case insensitive + * comparison. + */ + sal_uIntPtr getIdentifierIgnoreCase( const OUString& rStr ) const; + + /** + * Go through all string objects in the pool, and clear those that are no + * longer used outside of the pool. + */ + void purge(); + + size_t getCount() const; + + size_t getCountIgnoreCase() const; + +private: + InsertResultType findOrInsert( StrHashType& rPool, const OUString& rStr ) const; +}; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/svl/stringpool.hxx b/include/svl/stringpool.hxx deleted file mode 100644 index fbcff1e58b7c..000000000000 --- a/include/svl/stringpool.hxx +++ /dev/null @@ -1,94 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#ifndef SVL_STRINGPOOL_HXX -#define SVL_STRINGPOOL_HXX - -#include "svl/svldllapi.h" -#include "rtl/ustring.hxx" - -#include -#include - -class CharClass; - -namespace svl { - -/** - * Storage for pool of shared strings. It also provides mapping from - * original-cased strings to upper-cased strings for case insensitive - * operations. - */ -class SVL_DLLPUBLIC StringPool -{ - typedef boost::unordered_set StrHashType; - typedef std::pair InsertResultType; - typedef boost::unordered_map StrIdMapType; - - StrHashType maStrPool; - StrHashType maStrPoolUpper; - StrIdMapType maToUpperMap; - const CharClass* mpCharClass; - -public: - - StringPool(); - StringPool( const CharClass* pCharClass ); - - /** - * Intern a string object into the shared string pool. - * - * @param rStr string object to intern. - * - * @return a pointer to the string object stored inside the pool, or NULL - * if the insertion fails. - */ - rtl_uString* intern( const OUString& rStr ); - - /** - * Get a unique ID of string object that's expected to be in the shared - * string pool. If the string is not in the pool, NULL is returned. The - * ID obtained by this method can be used for case sensitive comparison. - * - * @param rStr string object to get the ID of. - * - * @return unique ID of the string object. - */ - sal_uIntPtr getIdentifier( const OUString& rStr ) const; - - /** - * Get a unique ID of string object for case insensitive comparison. The - * string object is expected to be in the pool. - * - * @param rStr string object to get the ID of. - * - * @return unique ID of the string object usable for case insensitive - * comparison. - */ - sal_uIntPtr getIdentifierIgnoreCase( const OUString& rStr ) const; - - /** - * Go through all string objects in the pool, and clear those that are no - * longer used outside of the pool. - */ - void purge(); - - size_t getCount() const; - - size_t getCountIgnoreCase() const; - -private: - InsertResultType findOrInsert( StrHashType& rPool, const OUString& rStr ) const; -}; - -} - -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx index 2ef6073a49b9..550f7b7dab2b 100644 --- a/sc/inc/document.hxx +++ b/sc/inc/document.hxx @@ -50,7 +50,7 @@ namespace editeng { class SvxBorderLine; } namespace formula { struct VectorRefArray; } -namespace svl { class StringPool; } +namespace svl { class SharedStringPool; } namespace sc { struct FormulaGroupContext; @@ -245,7 +245,7 @@ private: rtl::Reference xPoolHelper; - boost::scoped_ptr mpCellStringPool; + boost::scoped_ptr mpCellStringPool; SfxUndoManager* mpUndoManager; ScFieldEditEngine* pEditEngine; // uses pEditPool from xPoolHelper @@ -859,8 +859,8 @@ public: */ double* GetValueCell( const ScAddress& rPos ); - svl::StringPool& GetCellStringPool(); - const svl::StringPool& GetCellStringPool() const; + svl::SharedStringPool& GetCellStringPool(); + const svl::SharedStringPool& GetCellStringPool() const; sal_uIntPtr GetCellStringID( const ScAddress& rPos ) const; sal_uIntPtr GetCellStringIDIgnoreCase( const ScAddress& rPos ) const; diff --git a/sc/inc/formulagroup.hxx b/sc/inc/formulagroup.hxx index a8d20bb9890a..fc3af2a543aa 100644 --- a/sc/inc/formulagroup.hxx +++ b/sc/inc/formulagroup.hxx @@ -14,7 +14,7 @@ #include "types.hxx" #include "platforminfo.hxx" -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include #include @@ -33,7 +33,7 @@ struct FormulaGroupContext : boost::noncopyable typedef boost::ptr_vector NumArrayStoreType; typedef boost::ptr_vector StrArrayStoreType; - svl::StringPool maStrPool; + svl::SharedStringPool maStrPool; NumArrayStoreType maNumArrays; StrArrayStoreType maStrArrays; }; diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx index 3e7239efa870..eb61b906b657 100644 --- a/sc/qa/unit/ucalc.cxx +++ b/sc/qa/unit/ucalc.cxx @@ -65,7 +65,7 @@ #include #include #include "svl/srchitem.hxx" -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include @@ -502,7 +502,7 @@ void Test::testCellStringPool() CPPUNIT_ASSERT_MESSAGE("They must be equal when cases are ignored.", nId1 == nId2); // Check the string counts after purging. Purging shouldn't remove any strings in this case. - svl::StringPool& rPool = m_pDoc->GetCellStringPool(); + svl::SharedStringPool& rPool = m_pDoc->GetCellStringPool(); rPool.purge(); CPPUNIT_ASSERT_EQUAL(static_cast(4), rPool.getCount()); CPPUNIT_ASSERT_EQUAL(static_cast(2), rPool.getCountIgnoreCase()); diff --git a/sc/source/core/data/column3.cxx b/sc/source/core/data/column3.cxx index 6c964ca060c6..c651cca987f7 100644 --- a/sc/source/core/data/column3.cxx +++ b/sc/source/core/data/column3.cxx @@ -57,7 +57,7 @@ #include #include #include -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include "editeng/editstat.hxx" #include diff --git a/sc/source/core/data/documen2.cxx b/sc/source/core/data/documen2.cxx index ea00606ea08d..0fb675584cc9 100644 --- a/sc/source/core/data/documen2.cxx +++ b/sc/source/core/data/documen2.cxx @@ -35,7 +35,7 @@ #include #include #include -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include #include #include @@ -119,7 +119,7 @@ private: // STATIC DATA ----------------------------------------------------------- ScDocument::ScDocument( ScDocumentMode eMode, SfxObjectShell* pDocShell ) : - mpCellStringPool(new svl::StringPool(ScGlobal::pCharClass)), + mpCellStringPool(new svl::SharedStringPool(ScGlobal::pCharClass)), mpUndoManager( NULL ), pEditEngine( NULL ), pNoteEngine( NULL ), @@ -600,12 +600,12 @@ ScRefCellValue ScDocument::GetRefCellValue( const ScAddress& rPos ) return maTabs[rPos.Tab()]->GetRefCellValue(rPos.Col(), rPos.Row()); } -svl::StringPool& ScDocument::GetCellStringPool() +svl::SharedStringPool& ScDocument::GetCellStringPool() { return *mpCellStringPool; } -const svl::StringPool& ScDocument::GetCellStringPool() const +const svl::SharedStringPool& ScDocument::GetCellStringPool() const { return *mpCellStringPool; } diff --git a/sc/source/core/data/documentimport.cxx b/sc/source/core/data/documentimport.cxx index 4c9157d5a52b..4822557794f5 100644 --- a/sc/source/core/data/documentimport.cxx +++ b/sc/source/core/data/documentimport.cxx @@ -17,7 +17,7 @@ #include "mtvelements.hxx" #include "tokenarray.hxx" -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" struct ScDocumentImportImpl { diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx index 7026a4656179..6bf009321801 100644 --- a/sc/source/core/tool/interpr1.cxx +++ b/sc/source/core/tool/interpr1.cxx @@ -60,7 +60,7 @@ #include #include -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include #include diff --git a/svl/Library_svl.mk b/svl/Library_svl.mk index eddabf0a259d..dc2bf4134780 100644 --- a/svl/Library_svl.mk +++ b/svl/Library_svl.mk @@ -110,7 +110,7 @@ $(eval $(call gb_Library_add_exception_objects,svl,\ svl/source/misc/lockfilecommon \ svl/source/misc/ownlist \ svl/source/misc/sharecontrolfile \ - svl/source/misc/stringpool \ + svl/source/misc/sharedstringpool \ svl/source/misc/strmadpt \ svl/source/misc/urihelper \ svl/source/notify/brdcst \ diff --git a/svl/qa/unit/svl.cxx b/svl/qa/unit/svl.cxx index a34bb47caec7..fe987ec2d5e9 100644 --- a/svl/qa/unit/svl.cxx +++ b/svl/qa/unit/svl.cxx @@ -33,7 +33,7 @@ #include "svl/zforlist.hxx" #include "svl/zformat.hxx" -#include "svl/stringpool.hxx" +#include "svl/sharedstringpool.hxx" #include "unotools/syslocale.hxx" #include @@ -299,7 +299,7 @@ void Test::testNumberFormat() void Test::testStringPool() { SvtSysLocale aSysLocale; - svl::StringPool aPool(aSysLocale.GetCharClassPtr()); + svl::SharedStringPool aPool(aSysLocale.GetCharClassPtr()); const rtl_uString* p1 = aPool.intern("Andy"); const rtl_uString* p2 = aPool.intern("Andy"); @@ -342,7 +342,7 @@ void Test::testStringPool() void Test::testStringPoolPurge() { SvtSysLocale aSysLocale; - svl::StringPool aPool(aSysLocale.GetCharClassPtr()); + svl::SharedStringPool aPool(aSysLocale.GetCharClassPtr()); aPool.intern("Andy"); aPool.intern("andy"); aPool.intern("ANDY"); diff --git a/svl/source/misc/sharedstringpool.cxx b/svl/source/misc/sharedstringpool.cxx new file mode 100644 index 000000000000..805a6fc75576 --- /dev/null +++ b/svl/source/misc/sharedstringpool.cxx @@ -0,0 +1,145 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "svl/sharedstringpool.hxx" +#include "unotools/charclass.hxx" + +namespace svl { + +SharedStringPool::SharedStringPool() : mpCharClass(NULL) {} +SharedStringPool::SharedStringPool( const CharClass* pCharClass ) : mpCharClass(pCharClass) {} + +rtl_uString* SharedStringPool::intern( const OUString& rStr ) +{ + InsertResultType aRes = findOrInsert(maStrPool, rStr); + if (aRes.first == maStrPool.end()) + // Insertion failed. + return NULL; + + rtl_uString* pOrig = aRes.first->pData; + + if (!aRes.second) + // No new string has been inserted. Return the existing string in the pool. + return pOrig; + + if (!mpCharClass) + return pOrig; + + // This is a new string insertion. Establish mapping to upper-case variant. + + OUString aUpper = mpCharClass->uppercase(rStr); + aRes = findOrInsert(maStrPoolUpper, aUpper); + if (aRes.first == maStrPoolUpper.end()) + // Failed to insert or fetch upper-case variant. Should never happen. + return pOrig; + + // Set mapping. + maToUpperMap.insert(StrIdMapType::value_type(pOrig, *aRes.first)); + + return pOrig; +} + +sal_uIntPtr SharedStringPool::getIdentifier( const OUString& rStr ) const +{ + StrHashType::const_iterator it = maStrPool.find(rStr); + return (it == maStrPool.end()) ? 0 : reinterpret_cast(it->pData); +} + +sal_uIntPtr SharedStringPool::getIdentifierIgnoreCase( const OUString& rStr ) const +{ + StrHashType::const_iterator itOrig = maStrPool.find(rStr); + if (itOrig == maStrPool.end()) + // Not in the pool. + return 0; + + StrIdMapType::const_iterator itUpper = maToUpperMap.find(itOrig->pData); + if (itUpper == maToUpperMap.end()) + // Passed string is not in the pool. + return 0; + + const rtl_uString* pUpper = itUpper->second.pData; + return reinterpret_cast(pUpper); +} + +namespace { + +inline sal_Int32 getRefCount( const rtl_uString* p ) +{ + return (p->refCount & 0x3FFFFFFF); +} + +} + +void SharedStringPool::purge() +{ + StrHashType aNewStrPool; + StrHashType::iterator it = maStrPool.begin(), itEnd = maStrPool.end(); + for (; it != itEnd; ++it) + { + const rtl_uString* p = it->pData; + if (getRefCount(p) == 1) + { + // Remove it from the upper string map. This should unref the + // upper string linked to this original string. + maToUpperMap.erase(p); + } + else + // Still referenced outside the pool. Keep it. + aNewStrPool.insert(*it); + } + + maStrPool.swap(aNewStrPool); + + aNewStrPool.clear(); // for re-use. + + // Purge the upper string pool as well. + it = maStrPoolUpper.begin(); + itEnd = maStrPoolUpper.end(); + for (; it != itEnd; ++it) + { + const rtl_uString* p = it->pData; + if (getRefCount(p) > 1) + aNewStrPool.insert(*it); + } + + maStrPoolUpper.swap(aNewStrPool); +} + +size_t SharedStringPool::getCount() const +{ + return maStrPool.size(); +} + +size_t SharedStringPool::getCountIgnoreCase() const +{ + return maStrPoolUpper.size(); +} + +SharedStringPool::InsertResultType SharedStringPool::findOrInsert( StrHashType& rPool, const OUString& rStr ) const +{ + StrHashType::iterator it = rPool.find(rStr); + bool bInserted = false; + if (it == rPool.end()) + { + // Not yet in the pool. + std::pair r = rPool.insert(rStr); + if (!r.second) + // Insertion failed. + return InsertResultType(rPool.end(), false); + + it = r.first; + bInserted = true; + } + + return InsertResultType(it, bInserted); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svl/source/misc/stringpool.cxx b/svl/source/misc/stringpool.cxx deleted file mode 100644 index 7ebc207b2907..000000000000 --- a/svl/source/misc/stringpool.cxx +++ /dev/null @@ -1,145 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#include "svl/stringpool.hxx" -#include "unotools/charclass.hxx" - -namespace svl { - -StringPool::StringPool() : mpCharClass(NULL) {} -StringPool::StringPool( const CharClass* pCharClass ) : mpCharClass(pCharClass) {} - -rtl_uString* StringPool::intern( const OUString& rStr ) -{ - InsertResultType aRes = findOrInsert(maStrPool, rStr); - if (aRes.first == maStrPool.end()) - // Insertion failed. - return NULL; - - rtl_uString* pOrig = aRes.first->pData; - - if (!aRes.second) - // No new string has been inserted. Return the existing string in the pool. - return pOrig; - - if (!mpCharClass) - return pOrig; - - // This is a new string insertion. Establish mapping to upper-case variant. - - OUString aUpper = mpCharClass->uppercase(rStr); - aRes = findOrInsert(maStrPoolUpper, aUpper); - if (aRes.first == maStrPoolUpper.end()) - // Failed to insert or fetch upper-case variant. Should never happen. - return pOrig; - - // Set mapping. - maToUpperMap.insert(StrIdMapType::value_type(pOrig, *aRes.first)); - - return pOrig; -} - -sal_uIntPtr StringPool::getIdentifier( const OUString& rStr ) const -{ - StrHashType::const_iterator it = maStrPool.find(rStr); - return (it == maStrPool.end()) ? 0 : reinterpret_cast(it->pData); -} - -sal_uIntPtr StringPool::getIdentifierIgnoreCase( const OUString& rStr ) const -{ - StrHashType::const_iterator itOrig = maStrPool.find(rStr); - if (itOrig == maStrPool.end()) - // Not in the pool. - return 0; - - StrIdMapType::const_iterator itUpper = maToUpperMap.find(itOrig->pData); - if (itUpper == maToUpperMap.end()) - // Passed string is not in the pool. - return 0; - - const rtl_uString* pUpper = itUpper->second.pData; - return reinterpret_cast(pUpper); -} - -namespace { - -inline sal_Int32 getRefCount( const rtl_uString* p ) -{ - return (p->refCount & 0x3FFFFFFF); -} - -} - -void StringPool::purge() -{ - StrHashType aNewStrPool; - StrHashType::iterator it = maStrPool.begin(), itEnd = maStrPool.end(); - for (; it != itEnd; ++it) - { - const rtl_uString* p = it->pData; - if (getRefCount(p) == 1) - { - // Remove it from the upper string map. This should unref the - // upper string linked to this original string. - maToUpperMap.erase(p); - } - else - // Still referenced outside the pool. Keep it. - aNewStrPool.insert(*it); - } - - maStrPool.swap(aNewStrPool); - - aNewStrPool.clear(); // for re-use. - - // Purge the upper string pool as well. - it = maStrPoolUpper.begin(); - itEnd = maStrPoolUpper.end(); - for (; it != itEnd; ++it) - { - const rtl_uString* p = it->pData; - if (getRefCount(p) > 1) - aNewStrPool.insert(*it); - } - - maStrPoolUpper.swap(aNewStrPool); -} - -size_t StringPool::getCount() const -{ - return maStrPool.size(); -} - -size_t StringPool::getCountIgnoreCase() const -{ - return maStrPoolUpper.size(); -} - -StringPool::InsertResultType StringPool::findOrInsert( StrHashType& rPool, const OUString& rStr ) const -{ - StrHashType::iterator it = rPool.find(rStr); - bool bInserted = false; - if (it == rPool.end()) - { - // Not yet in the pool. - std::pair r = rPool.insert(rStr); - if (!r.second) - // Insertion failed. - return InsertResultType(rPool.end(), false); - - it = r.first; - bInserted = true; - } - - return InsertResultType(it, bInserted); -} - -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit