OpenXLSX 1.10.0
Loading...
Searching...
No Matches
XLSharedStrings.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLSHAREDSTRINGS_HPP
2#define OPENXLSX_XLSHAREDSTRINGS_HPP
3
4#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif // _MSC_VER
9
10#include <deque>
11#include <functional> // std::reference_wrapper
12#include <limits> // std::numeric_limits
13#include <ostream> // std::basic_ostream
14#include <shared_mutex> // std::shared_mutex
15#include <string>
16#include <ankerl/unordered_dense.h> // O(1) ankerl::unordered_dense string lookup
17
18// ===== OpenXLSX Includes ===== //
19#include "OpenXLSX-Exports.hpp"
20#include "XLStringArena.hpp"
21#include "XLXmlData.hpp"
22#include "XLXmlFile.hpp"
23#include <string_view>
24#include <vector>
25
26namespace OpenXLSX
27{
29 using is_transparent = void;
30 auto operator()(std::string_view str) const noexcept -> uint64_t {
31 return ankerl::unordered_dense::hash<std::string_view>{}(str);
32 }
33 };
34
35 // Alias for flat hash map to allow easy substitution in the future
36 template<typename Key, typename Value>
37 using FlatHashMap = ankerl::unordered_dense::map<Key, Value, StringViewHash, std::equal_to<>>;
38
39 constexpr size_t XLMaxSharedStrings = (std::numeric_limits<int32_t>::max)(); // pull request #261: wrapped max in parentheses to
40 // prevent expansion of windows.h "max" macro
41
44 std::vector<std::string_view> cache{};
46 std::unique_ptr<std::shared_mutex> mutex{std::make_unique<std::shared_mutex>()};
47
48 void clear() {
49 arena.clear();
50 cache.clear();
51 index.clear();
52 // mutex remains intact
53 }
54 };
55
56 class XLSharedStrings; // forward declaration
57 typedef std::reference_wrapper<const XLSharedStrings> XLSharedStringsRef;
58
59 extern const XLSharedStrings
60 XLSharedStringsDefaulted; // to be used for default initialization of all references of type XLSharedStrings
61
67 class OPENXLSX_EXPORT XLSharedStrings : public XLXmlFile
68 {
69 //---------- Friend Declarations ----------//
70 friend class XLDocument; // for access to protected function rewriteXmlFromCache
71
72 //----------------------------------------------------------------------------------------------------------------------
73 // Public Member Functions
74 //----------------------------------------------------------------------------------------------------------------------
75
76 public:
80 XLSharedStrings() = default;
81
87 explicit XLSharedStrings(XLXmlData* xmlData,
89
94
99 XLSharedStrings(const XLSharedStrings& other) = default;
100
105 XLSharedStrings(XLSharedStrings&& other) noexcept = default;
106
112 XLSharedStrings& operator=(const XLSharedStrings& other) = default;
113
119 XLSharedStrings& operator=(XLSharedStrings&& other) noexcept = default;
120
125 int32_t stringCount() const
126 {
127 if (!m_state) return 0;
128 if (m_state->mutex) {
129 std::shared_lock<std::shared_mutex> lock(*m_state->mutex);
130 return static_cast<int32_t>(m_state->cache.size());
131 }
132 return static_cast<int32_t>(m_state->cache.size());
133 }
134
140 int32_t getStringIndex(std::string_view str) const;
141
147 bool stringExists(std::string_view str) const;
148
154 const char* getString(int32_t index) const;
155
161 std::string_view getStringView(int32_t index) const;
162
168 int32_t appendString(std::string_view str) const;
169
176 void reserveStrings(size_t n) const;
177
182 size_t memoryUsageBytes() const noexcept;
183
190 int32_t getOrCreateStringIndex(std::string_view str) const;
191
199 void clearString(int32_t index) const;
200
204 void print(std::basic_ostream<char>& ostr) const;
205
206 protected:
211 int32_t rewriteXmlFromCache();
212
218 XLAllocatedMemory generateRawAllocatedSstXml() const;
219
226 void rebuild(const std::vector<int32_t>& indexMap, int32_t newStringCount);
227
228 private:
229 XLSharedStringsState* m_state{};
236 mutable bool m_domDirty{false};
237 };
238} // namespace OpenXLSX
239
240#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
241# pragma warning(pop)
242#endif // _MSC_VER
243
244#endif // OPENXLSX_XLSHAREDSTRINGS_HPP
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:82
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:68
XLSharedStrings & operator=(XLSharedStrings &&other) noexcept=default
~XLSharedStrings()
Destructor.
XLSharedStrings(const XLSharedStrings &other)=default
XLSharedStrings(XLSharedStrings &&other) noexcept=default
int32_t stringCount() const
return the amount of shared string entries currently in the cache
Definition XLSharedStrings.hpp:125
XLSharedStrings & operator=(const XLSharedStrings &other)=default
String memory pool (Arena Allocator) with block recycling.
Definition XLStringArena.hpp:23
void clear() noexcept
Recycle all blocks for reuse without freeing heap memory.
Definition XLStringArena.hpp:76
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:68
The XLXmlFile class provides an interface for derived classes to use. It functions as an ancestor to ...
Definition XLXmlFile.hpp:42
Definition IZipArchive.hpp:18
const XLSharedStrings XLSharedStringsDefaulted
Definition XLSharedStrings.cpp:17
ankerl::unordered_dense::map< Key, Value, StringViewHash, std::equal_to<> > FlatHashMap
Definition XLSharedStrings.hpp:37
std::reference_wrapper< const XLSharedStrings > XLSharedStringsRef
Definition XLSharedStrings.hpp:57
constexpr size_t XLMaxSharedStrings
Definition XLSharedStrings.hpp:39
Definition XLCellIterator.hpp:121
Definition XLSharedStrings.hpp:28
void is_transparent
Definition XLSharedStrings.hpp:29
auto operator()(std::string_view str) const noexcept -> uint64_t
Definition XLSharedStrings.hpp:30
Auto-managed malloc memory for Zero-Copy serialization.
Definition XLXmlData.hpp:26
Definition XLSharedStrings.hpp:42
XLStringArena arena
Definition XLSharedStrings.hpp:43
void clear()
Definition XLSharedStrings.hpp:48
std::unique_ptr< std::shared_mutex > mutex
Definition XLSharedStrings.hpp:46
std::vector< std::string_view > cache
Definition XLSharedStrings.hpp:44
FlatHashMap< std::string_view, int32_t > index
Definition XLSharedStrings.hpp:45