Make Version more space efficient

This commit is contained in:
Georg Hagen
2025-10-28 16:39:11 +01:00
parent c1a7a3a4e4
commit a72f7ec501
3 changed files with 299 additions and 35 deletions

View File

@@ -43,7 +43,7 @@ namespace OpenVulkano
}
private:
static int ToNumber(std::string_view numberStr)
static int ToNumber(const std::string_view numberStr)
{
if (numberStr.empty()) return 0;
int tmp = 0;
@@ -55,7 +55,7 @@ namespace OpenVulkano
{
do
{
size_t blockStart = offset;
const size_t blockStart = offset;
while (versionStr.size() > offset && versionStr[offset] >= '0' && versionStr[offset] <= '9') offset++;
comps.push_back(ToNumber(versionStr.substr(blockStart, offset - blockStart)));
if (offset < versionStr.size() && versionStr[offset] != '.') break;
@@ -67,7 +67,7 @@ namespace OpenVulkano
{
do
{
size_t blockStart = offset;
const size_t blockStart = offset;
while (versionStr.size() > offset && versionStr[offset] != '-' && versionStr[offset] != ' ' && versionStr[offset] != '\0') offset++;
tags.emplace_back(versionStr.data() + blockStart, offset - blockStart);
if (offset >= versionStr.size() || versionStr[offset] == ' ' || versionStr[offset] == '\0') break;
@@ -92,14 +92,7 @@ namespace OpenVulkano
}
}
Version::Version(uint32_t major, uint32_t minor, uint32_t patch, const uint32_t build)
: m_versionComponents(build ? std::initializer_list{major, minor, patch, build} : std::initializer_list{major, minor, patch})
, m_buildNumber(build)
, m_versionString(ToString(major, minor, patch, build))
{}
Version::Version(std::string_view versionString, bool ignoreTags)
: m_versionString(versionString)
VersionDataExtendedData::VersionDataExtendedData(const std::string_view& versionString, const bool ignoreTags)
{
ComponentDecodeHolder helper(versionString);
if (!helper.appleBuildComponents.empty())
@@ -115,11 +108,11 @@ namespace OpenVulkano
{
if (std::regex_match(tag, tagRegex))
{
auto t = ReadTagValue(tag);
if (t.first == 't' || t.first == 'T')
m_timestamp = t.second;
const auto [fst, snd] = ReadTagValue(tag);
if (fst == 't' || fst == 'T')
m_timestamp = snd;
else
m_buildNumber = t.second;
m_buildNumber = snd;
}
}
}
@@ -133,34 +126,54 @@ namespace OpenVulkano
}
}
Version::Version(uint32_t major, uint32_t minor, uint32_t patch, const uint32_t build)
: m_data(VersionDataCompact(major, minor, patch, build))
, m_versionString(ToString(major, minor, patch, build))
{}
Version::Version(const std::string_view versionString, const bool ignoreTags)
: m_versionString(versionString)
{
VersionDataExtendedData data(versionString, ignoreTags);
auto compact = data.GetCompactVersion();
if (compact)
m_data = compact.value();
else
m_data = std::move(data);
}
//region Compare functions
int Version::CompareBuildNr(const Version& other) const
{
if (m_buildNumber == 0 || other.m_buildNumber == 0) return 0;
if (m_buildNumber > other.m_buildNumber) return 1;
if (m_buildNumber < other.m_buildNumber) return -1;
const uint64_t build = Build(), otherBuild = other.Build();
if (build == 0 || otherBuild == 0) return 0;
if (build > otherBuild) return 1;
if (build < otherBuild) return -1;
return 0;
}
int Version::CompareTimestamp(const Version& other) const
{
if (m_timestamp == 0 || other.m_timestamp == 0) return 0;
if (m_timestamp > other.m_timestamp) return 1;
if (m_timestamp < other.m_timestamp) return -1;
const uint64_t ts = Timestamp(), oTs = other.Timestamp();
if (ts == 0 || oTs == 0) return 0;
if (ts > oTs) return 1;
if (ts < oTs) return -1;
return 0;
}
int Version::CompareComponents(const Version& other) const
{
size_t minCount = std::min(m_versionComponents.size(), other.m_versionComponents.size());
const auto comp = GetVersionComponents();
const auto otherComp = other.GetVersionComponents();
const size_t minCount = std::min(comp.size(), otherComp.size());
for(size_t i = 0; i < minCount; i++)
{
if (m_versionComponents[i] > other.m_versionComponents[i]) return 1;
if (m_versionComponents[i] < other.m_versionComponents[i]) return -1;
if (comp[i] > otherComp[i]) return 1;
if (comp[i] < otherComp[i]) return -1;
}
if (m_versionComponents.size() != other.m_versionComponents.size())
if (comp.size() != otherComp.size())
{
for(size_t i = minCount; i < std::max(m_versionComponents.size(), other.m_versionComponents.size()); i++)
for(size_t i = minCount; i < std::max(comp.size(), otherComp.size()); i++)
{
if (GetComponent(i) > other.GetComponent(i)) return 1;
if (GetComponent(i) < other.GetComponent(i)) return -1;