blob: ad72b5a1addb7ecea16bd245c3b1ac69b300ea1d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/* -*- 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 <svl/sharedstring.hxx>
#include <unotools/charclass.hxx>
#include <osl/mutex.hxx>
#include <unordered_map>
#include <unordered_set>
namespace svl {
namespace {
sal_Int32 getRefCount( const rtl_uString* p )
{
return (p->refCount & 0x3FFFFFFF);
}
}
struct SharedStringPool::Impl
{
mutable osl::Mutex maMutex;
// set of upper-case, so we can share these as the value in the maStrMap
std::unordered_set<OUString> maStrPoolUpper;
// map with rtl_uString* as key so we can avoid some ref-counting
std::unordered_map<OUString,rtl_uString*> maStrMap;
const CharClass& mrCharClass;
explicit Impl( const CharClass& rCharClass ) : mrCharClass(rCharClass) {}
};
SharedStringPool::SharedStringPool( const CharClass& rCharClass ) :
mpImpl(new Impl(rCharClass)) {}
SharedStringPool::~SharedStringPool()
{
}
SharedString SharedStringPool::intern( const OUString& rStr )
{
osl::MutexGuard aGuard(&mpImpl->maMutex);
auto mapIt = mpImpl->maStrMap.find(rStr);
if (mapIt == mpImpl->maStrMap.end())
{
// This is a new string insertion. Establish mapping to upper-case variant.
OUString aUpper = mpImpl->mrCharClass.uppercase(rStr);
auto insertResult = mpImpl->maStrPoolUpper.insert(aUpper);
mapIt = mpImpl->maStrMap.emplace_hint(mapIt, rStr, insertResult.first->pData);
}
return SharedString(mapIt->first.pData, mapIt->second);
}
void SharedStringPool::purge()
{
osl::MutexGuard aGuard(&mpImpl->maMutex);
std::unordered_set<OUString> aNewStrPoolUpper;
{
auto it = mpImpl->maStrMap.begin(), itEnd = mpImpl->maStrMap.end();
while (it != itEnd)
{
const rtl_uString* p = it->first.pData;
if (getRefCount(p) == 1)
it = mpImpl->maStrMap.erase(it);
else
{
// Still referenced outside the pool. Keep it.
aNewStrPoolUpper.insert(it->second);
++it;
}
}
}
mpImpl->maStrPoolUpper = std::move(aNewStrPoolUpper);
}
size_t SharedStringPool::getCount() const
{
osl::MutexGuard aGuard(&mpImpl->maMutex);
return mpImpl->maStrMap.size();
}
size_t SharedStringPool::getCountIgnoreCase() const
{
osl::MutexGuard aGuard(&mpImpl->maMutex);
return mpImpl->maStrPoolUpper.size();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|