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 --- svl/Library_svl.mk | 2 +- svl/qa/unit/svl.cxx | 6 +- svl/source/misc/sharedstringpool.cxx | 145 +++++++++++++++++++++++++++++++++++ svl/source/misc/stringpool.cxx | 145 ----------------------------------- 4 files changed, 149 insertions(+), 149 deletions(-) create mode 100644 svl/source/misc/sharedstringpool.cxx delete mode 100644 svl/source/misc/stringpool.cxx (limited to 'svl') 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 v1.2.3