1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <svl/sharedstringpool.hxx>
11 #include <svl/sharedstring.hxx>
12 #include <unotools/charclass.hxx>
13 #include <osl/mutex.hxx>
15 #include <unordered_map>
16 #include <unordered_set>
22 sal_Int32
getRefCount(const rtl_uString
* p
) { return (p
->refCount
& 0x3FFFFFFF); }
25 struct SharedStringPool::Impl
27 mutable osl::Mutex maMutex
;
28 // We use this map for two purposes - to store lower->upper case mappings
29 // and to retrieve a shared uppercase object, so the management logic
31 std::unordered_map
<OUString
, OUString
> maStrMap
;
32 const CharClass
& mrCharClass
;
34 explicit Impl(const CharClass
& rCharClass
)
35 : mrCharClass(rCharClass
)
40 SharedStringPool::SharedStringPool(const CharClass
& rCharClass
)
41 : mpImpl(new Impl(rCharClass
))
45 SharedStringPool::~SharedStringPool() {}
47 SharedString
SharedStringPool::intern(const OUString
& rStr
)
49 osl::MutexGuard
aGuard(&mpImpl
->maMutex
);
51 auto[mapIt
, bInserted
] = mpImpl
->maStrMap
.emplace(rStr
, rStr
);
53 // there is already a mapping
54 return SharedString(mapIt
->first
.pData
, mapIt
->second
.pData
);
56 // This is a new string insertion. Establish mapping to upper-case variant.
57 OUString aUpper
= mpImpl
->mrCharClass
.uppercase(rStr
);
59 // no need to do anything more, because we inserted an upper->upper mapping
60 return SharedString(mapIt
->first
.pData
, mapIt
->second
.pData
);
62 // We need to insert a lower->upper mapping, so also insert
63 // an upper->upper mapping, which we can use both for when an upper string
64 // is interned, and to look up a shared upper string.
65 auto mapIt2
= mpImpl
->maStrMap
.find(aUpper
);
66 if (mapIt2
!= mpImpl
->maStrMap
.end())
68 // there is an already existing upper string
69 mapIt
->second
= mapIt2
->first
;
70 return SharedString(mapIt
->first
.pData
, mapIt
->second
.pData
);
73 // There is no already existing upper string.
74 // First, update using the iterator, can't do this later because
75 // the iterator will be invalid.
76 mapIt
->second
= aUpper
;
77 mpImpl
->maStrMap
.emplace_hint(mapIt2
, aUpper
, aUpper
);
78 return SharedString(rStr
.pData
, aUpper
.pData
);
81 void SharedStringPool::purge()
83 osl::MutexGuard
aGuard(&mpImpl
->maMutex
);
85 // Because we can have an uppercase entry mapped to itself,
86 // and then a bunch of lowercase entries mapped to that same
87 // upper-case entry, we need to scan the map twice - the first
88 // time to remove lowercase entries, and then only can we
89 // check for unused uppercase entries.
91 auto it
= mpImpl
->maStrMap
.begin();
92 auto itEnd
= mpImpl
->maStrMap
.end();
95 rtl_uString
* p1
= it
->first
.pData
;
96 rtl_uString
* p2
= it
->second
.pData
;
99 // normal case - lowercase mapped to uppercase, which
100 // means that the lowercase entry has one ref-counted
101 // entry as the key in the map
102 if (getRefCount(p1
) == 1)
104 it
= mpImpl
->maStrMap
.erase(it
);
111 it
= mpImpl
->maStrMap
.begin();
112 itEnd
= mpImpl
->maStrMap
.end();
115 rtl_uString
* p1
= it
->first
.pData
;
116 rtl_uString
* p2
= it
->second
.pData
;
119 // uppercase which is mapped to itself, which means
120 // one ref-counted entry as the key in the map, and
121 // one ref-counted entry in the value in the map
122 if (getRefCount(p1
) == 2)
124 it
= mpImpl
->maStrMap
.erase(it
);
132 size_t SharedStringPool::getCount() const
134 osl::MutexGuard
aGuard(&mpImpl
->maMutex
);
135 return mpImpl
->maStrMap
.size();
138 size_t SharedStringPool::getCountIgnoreCase() const
140 osl::MutexGuard
aGuard(&mpImpl
->maMutex
);
141 // this is only called from unit tests, so no need to be efficient
142 std::unordered_set
<OUString
> aUpperSet
;
143 for (auto const& pair
: mpImpl
->maStrMap
)
144 aUpperSet
.insert(pair
.second
);
145 return aUpperSet
.size();
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */