Storm 1.13.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
StringEncoding.h
Go to the documentation of this file.
1#pragma once
2#include <ranges>
3#include <string_view>
4
7
8namespace storm::umb {
9
10auto inline stringVectorView(SEQ<char> const& strings, CSR const& stringMapping) {
11 STORM_LOG_ASSERT(!stringMapping.has_value() || std::ranges::size(stringMapping.value()) > 0, "stringMapping CSR must not be empty.");
12 STORM_LOG_ASSERT(stringMapping.has_value() == strings.has_value(), "stringMapping must be present iff strings is present.");
13 auto const numEntries = stringMapping.has_value() ? std::ranges::size(stringMapping.value()) - 1 : 0;
14 return std::ranges::iota_view(0ull, numEntries) |
15 std::ranges::views::transform([stringPtr = strings.has_value() ? strings.value().data() : nullptr, &stringMapping](auto i) -> std::string_view {
16 // Note: this is only executed if numEntries is positive, i.e., if there actually are strings
17 STORM_LOG_ASSERT(stringPtr != nullptr, "Expected strings to be present if there are entries in the string mapping.");
18 return std::string_view(stringPtr + stringMapping.value()[i], stringMapping.value()[i + 1] - stringMapping.value()[i]);
19 });
20}
21
23 public:
24 StringsBuilder(typename SEQ<char>::value_type& strings, typename CSR::value_type& stringMapping) : strings(strings), stringMapping(stringMapping) {
25 if (stringMapping.empty()) {
26 stringMapping.push_back(0);
27 }
28 STORM_LOG_ASSERT(stringMapping.front() == 0, "String mapping CSR must start with 0.");
29 STORM_LOG_ASSERT(stringMapping.back() == strings.size(), "String mapping CSR must end with the size of the strings.");
30 }
31
32 void reserve(uint64_t numStrings) {
33 strings.reserve(strings.size() + numStrings * 10); // assume average string length of 10
34 stringMapping.reserve(stringMapping.size() + numStrings);
35 }
36
37 std::string_view at(uint64_t index) const {
38 STORM_LOG_ASSERT(index + 1 < stringMapping.size(), "String index out of bounds.");
39 return std::string_view(strings.data() + stringMapping[index], stringMapping[index + 1] - stringMapping[index]);
40 }
41
42 std::size_t size() const {
43 return stringMapping.size() - 1;
44 }
45
50 uint64_t push_back(std::string_view str) {
51 strings.insert(strings.end(), str.begin(), str.end());
52 stringMapping.push_back(strings.size());
53 return size() - 1;
54 }
55
59 uint64_t findOrPushBack(std::string_view str) {
60 // search for existing string (note: this is a linear search)
61 auto const numStrings = stringMapping.size() - 1;
62 for (uint64_t i = 0; i < numStrings; ++i) {
63 if (at(i) == str) {
64 return i;
65 }
66 }
67 // not found, insert new string
68 return push_back(str);
69 }
70
71 void finalize() {
72 STORM_LOG_ASSERT(stringMapping.back() == strings.size(), "Final string mapping does not match string size.");
73 strings.shrink_to_fit();
74 stringMapping.shrink_to_fit();
75 }
76
77 private:
78 typename SEQ<char>::value_type& strings;
79 typename CSR::value_type& stringMapping;
80};
81
82} // namespace storm::umb
std::size_t size() const
uint64_t findOrPushBack(std::string_view str)
If the given string already exists, returns its index.
std::string_view at(uint64_t index) const
void reserve(uint64_t numStrings)
uint64_t push_back(std::string_view str)
inserts the string at the end of the vector and returns its index
StringsBuilder(typename SEQ< char >::value_type &strings, typename CSR::value_type &stringMapping)
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
OptionalVectorType< uint64_t > CSR
Definition FileTypes.h:31
TO1< T > SEQ
Definition FileTypes.h:29
auto stringVectorView(SEQ< char > const &strings, CSR const &stringMapping)