Make Version more space efficient
This commit is contained in:
@@ -43,7 +43,7 @@ namespace OpenVulkano
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static int ToNumber(std::string_view numberStr)
|
static int ToNumber(const std::string_view numberStr)
|
||||||
{
|
{
|
||||||
if (numberStr.empty()) return 0;
|
if (numberStr.empty()) return 0;
|
||||||
int tmp = 0;
|
int tmp = 0;
|
||||||
@@ -55,7 +55,7 @@ namespace OpenVulkano
|
|||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
size_t blockStart = offset;
|
const size_t blockStart = offset;
|
||||||
while (versionStr.size() > offset && versionStr[offset] >= '0' && versionStr[offset] <= '9') offset++;
|
while (versionStr.size() > offset && versionStr[offset] >= '0' && versionStr[offset] <= '9') offset++;
|
||||||
comps.push_back(ToNumber(versionStr.substr(blockStart, offset - blockStart)));
|
comps.push_back(ToNumber(versionStr.substr(blockStart, offset - blockStart)));
|
||||||
if (offset < versionStr.size() && versionStr[offset] != '.') break;
|
if (offset < versionStr.size() && versionStr[offset] != '.') break;
|
||||||
@@ -67,7 +67,7 @@ namespace OpenVulkano
|
|||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
size_t blockStart = offset;
|
const size_t blockStart = offset;
|
||||||
while (versionStr.size() > offset && versionStr[offset] != '-' && versionStr[offset] != ' ' && versionStr[offset] != '\0') offset++;
|
while (versionStr.size() > offset && versionStr[offset] != '-' && versionStr[offset] != ' ' && versionStr[offset] != '\0') offset++;
|
||||||
tags.emplace_back(versionStr.data() + blockStart, offset - blockStart);
|
tags.emplace_back(versionStr.data() + blockStart, offset - blockStart);
|
||||||
if (offset >= versionStr.size() || versionStr[offset] == ' ' || versionStr[offset] == '\0') break;
|
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)
|
VersionDataExtendedData::VersionDataExtendedData(const std::string_view& versionString, const bool ignoreTags)
|
||||||
: 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)
|
|
||||||
{
|
{
|
||||||
ComponentDecodeHolder helper(versionString);
|
ComponentDecodeHolder helper(versionString);
|
||||||
if (!helper.appleBuildComponents.empty())
|
if (!helper.appleBuildComponents.empty())
|
||||||
@@ -115,11 +108,11 @@ namespace OpenVulkano
|
|||||||
{
|
{
|
||||||
if (std::regex_match(tag, tagRegex))
|
if (std::regex_match(tag, tagRegex))
|
||||||
{
|
{
|
||||||
auto t = ReadTagValue(tag);
|
const auto [fst, snd] = ReadTagValue(tag);
|
||||||
if (t.first == 't' || t.first == 'T')
|
if (fst == 't' || fst == 'T')
|
||||||
m_timestamp = t.second;
|
m_timestamp = snd;
|
||||||
else
|
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
|
//region Compare functions
|
||||||
int Version::CompareBuildNr(const Version& other) const
|
int Version::CompareBuildNr(const Version& other) const
|
||||||
{
|
{
|
||||||
if (m_buildNumber == 0 || other.m_buildNumber == 0) return 0;
|
const uint64_t build = Build(), otherBuild = other.Build();
|
||||||
if (m_buildNumber > other.m_buildNumber) return 1;
|
if (build == 0 || otherBuild == 0) return 0;
|
||||||
if (m_buildNumber < other.m_buildNumber) return -1;
|
if (build > otherBuild) return 1;
|
||||||
|
if (build < otherBuild) return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Version::CompareTimestamp(const Version& other) const
|
int Version::CompareTimestamp(const Version& other) const
|
||||||
{
|
{
|
||||||
if (m_timestamp == 0 || other.m_timestamp == 0) return 0;
|
const uint64_t ts = Timestamp(), oTs = other.Timestamp();
|
||||||
if (m_timestamp > other.m_timestamp) return 1;
|
if (ts == 0 || oTs == 0) return 0;
|
||||||
if (m_timestamp < other.m_timestamp) return -1;
|
if (ts > oTs) return 1;
|
||||||
|
if (ts < oTs) return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Version::CompareComponents(const Version& other) const
|
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++)
|
for(size_t i = 0; i < minCount; i++)
|
||||||
{
|
{
|
||||||
if (m_versionComponents[i] > other.m_versionComponents[i]) return 1;
|
if (comp[i] > otherComp[i]) return 1;
|
||||||
if (m_versionComponents[i] < other.m_versionComponents[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;
|
||||||
if (GetComponent(i) < other.GetComponent(i)) return -1;
|
if (GetComponent(i) < other.GetComponent(i)) return -1;
|
||||||
|
|||||||
@@ -10,16 +10,199 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
namespace OpenVulkano
|
namespace OpenVulkano
|
||||||
{
|
{
|
||||||
class Version
|
struct VersionDataCompact
|
||||||
|
{
|
||||||
|
std::array<uint32_t, 4> m_versionComponents;
|
||||||
|
|
||||||
|
VersionDataCompact(const uint32_t major, const uint32_t minor, const uint32_t patch, const uint32_t build)
|
||||||
|
: m_versionComponents({major, minor, patch, build})
|
||||||
|
{}
|
||||||
|
|
||||||
|
[[nodiscard]] uint32_t GetComponent(const size_t compId) const
|
||||||
|
{
|
||||||
|
if (m_versionComponents.size() < compId + 1) return 0u;
|
||||||
|
return m_versionComponents[compId];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VersionDataExtendedData
|
||||||
{
|
{
|
||||||
std::vector<uint32_t> m_versionComponents;
|
std::vector<uint32_t> m_versionComponents;
|
||||||
std::vector<std::string> m_tagComponents;
|
std::vector<std::string> m_tagComponents;
|
||||||
uint64_t m_timestamp = 0, m_buildNumber = 0;
|
uint64_t m_timestamp = 0, m_buildNumber = 0;
|
||||||
|
|
||||||
|
VersionDataExtendedData() = default;
|
||||||
|
VersionDataExtendedData(const std::string_view& versionString, bool ignoreTags);
|
||||||
|
|
||||||
|
[[nodiscard]] uint32_t GetComponent(const size_t compId) const
|
||||||
|
{
|
||||||
|
if (m_versionComponents.size() < compId + 1) return 0u;
|
||||||
|
return m_versionComponents[compId];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<VersionDataCompact> GetCompactVersion(const bool force = false) const
|
||||||
|
{
|
||||||
|
if (!force)
|
||||||
|
{
|
||||||
|
if (m_versionComponents.size() > 3 && m_buildNumber != 0) return {};
|
||||||
|
if (m_versionComponents.size() > 4 || !m_tagComponents.empty()) return {};
|
||||||
|
}
|
||||||
|
return VersionDataCompact(GetComponent(0), GetComponent(1), GetComponent(2), m_buildNumber ? m_buildNumber : GetComponent(3));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VersionDataExtended
|
||||||
|
{
|
||||||
|
std::array<uint32_t, 2> m_leadComponents;
|
||||||
|
std::unique_ptr<VersionDataExtendedData> m_data;
|
||||||
|
|
||||||
|
VersionDataExtended() : m_leadComponents({ UINT32_MAX, UINT32_MAX }), m_data(std::make_unique<VersionDataExtendedData>())
|
||||||
|
{}
|
||||||
|
|
||||||
|
VersionDataExtended(const VersionDataExtended& o) : m_leadComponents(o.m_leadComponents), m_data(std::make_unique<VersionDataExtendedData>(*o.m_data))
|
||||||
|
{}
|
||||||
|
|
||||||
|
VersionDataExtended(VersionDataExtended&& o) noexcept
|
||||||
|
: m_leadComponents(o.m_leadComponents), m_data(std::move(o.m_data))
|
||||||
|
{
|
||||||
|
o.m_leadComponents = { 0, 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionDataExtended(VersionDataExtendedData&& data) :
|
||||||
|
m_leadComponents({ UINT32_MAX, UINT32_MAX }), m_data(std::make_unique<VersionDataExtendedData>(std::move(data)))
|
||||||
|
{}
|
||||||
|
|
||||||
|
VersionDataExtended& operator=(VersionDataExtended&& o) noexcept
|
||||||
|
{
|
||||||
|
m_leadComponents = std::move(o.m_leadComponents);
|
||||||
|
m_data = std::move(o.m_data);
|
||||||
|
o.m_data = nullptr;
|
||||||
|
o.m_leadComponents = { 0, 0 };
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionDataExtended& operator=(const VersionDataExtended& o)
|
||||||
|
{
|
||||||
|
m_leadComponents = o.m_leadComponents;
|
||||||
|
m_data = std::make_unique<VersionDataExtendedData>(*o.m_data);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
union VersionData
|
||||||
|
{
|
||||||
|
VersionDataCompact compact;
|
||||||
|
VersionDataExtended extended;
|
||||||
|
|
||||||
|
VersionData() { memset(&this->compact, 0, sizeof(VersionDataCompact)); }
|
||||||
|
|
||||||
|
VersionData(VersionData&& o) noexcept
|
||||||
|
{
|
||||||
|
if (o.IsExtended())
|
||||||
|
{
|
||||||
|
new (&extended) VersionDataExtended(std::move(o.extended));
|
||||||
|
}
|
||||||
|
else compact = o.compact;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData(const VersionData& o)
|
||||||
|
{
|
||||||
|
if (o.IsExtended()) new (&extended)VersionDataExtended(o.extended);
|
||||||
|
else compact = o.compact;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData(const VersionDataCompact& other) { new (&compact)VersionDataCompact(other); }
|
||||||
|
|
||||||
|
VersionData(VersionDataCompact&& other) { new (&compact) VersionDataCompact(std::move(other)); }
|
||||||
|
|
||||||
|
VersionData(const VersionDataExtended& other)
|
||||||
|
{
|
||||||
|
new (&extended)VersionDataExtended(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData(VersionDataExtended&& other) { new (&extended) VersionDataExtended(std::move(other)); }
|
||||||
|
|
||||||
|
~VersionData()
|
||||||
|
{
|
||||||
|
if (IsExtended())
|
||||||
|
{
|
||||||
|
extended.~VersionDataExtended();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData& operator =(const VersionDataCompact& other)
|
||||||
|
{
|
||||||
|
compact = other;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData& operator =(VersionDataExtendedData&& other)
|
||||||
|
{
|
||||||
|
extended.m_leadComponents = { UINT32_MAX, UINT32_MAX };
|
||||||
|
extended.m_data = std::make_unique<VersionDataExtendedData>(std::move(other));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData& operator =(VersionData&& other) noexcept
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
if (other.IsExtended())
|
||||||
|
{
|
||||||
|
extended.m_leadComponents = { UINT32_MAX, UINT32_MAX };
|
||||||
|
extended.m_data = std::move(other.extended.m_data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
compact = other.compact;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
VersionData& operator =(const VersionData& other)
|
||||||
|
{
|
||||||
|
Clear();
|
||||||
|
if (other.IsExtended())
|
||||||
|
extended = other.extended;
|
||||||
|
else
|
||||||
|
compact = other.compact;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsExtended() const
|
||||||
|
{
|
||||||
|
return compact.m_versionComponents[0] > INT32_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t GetTimestamp() const
|
||||||
|
{
|
||||||
|
if (IsExtended()) return extended.m_data->m_timestamp;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void Clear()
|
||||||
|
{
|
||||||
|
if (IsExtended())
|
||||||
|
{
|
||||||
|
extended.~VersionDataExtended();
|
||||||
|
}
|
||||||
|
memset(&compact, 0, sizeof(compact));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Version
|
||||||
|
{
|
||||||
|
VersionData m_data;
|
||||||
std::string m_versionString;
|
std::string m_versionString;
|
||||||
bool m_preRelease = false;
|
|
||||||
|
bool IsExtendedData() const
|
||||||
|
{
|
||||||
|
return m_data.compact.m_versionComponents[0] > INT32_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Version() : Version(0, 0) {}
|
Version() : Version(0, 0) {}
|
||||||
@@ -34,10 +217,16 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
explicit Version(double version);
|
explicit Version(double version);
|
||||||
|
|
||||||
|
Version(const Version& other) = default;
|
||||||
|
Version(Version&& other) = default;
|
||||||
|
Version& operator =(const Version& other) = default;
|
||||||
|
Version& operator=(Version&& other) = default;
|
||||||
|
|
||||||
[[nodiscard]] uint32_t GetComponent(const size_t compId) const
|
[[nodiscard]] uint32_t GetComponent(const size_t compId) const
|
||||||
{
|
{
|
||||||
if (m_versionComponents.size() < compId + 1) return 0u;
|
if (IsExtendedData())
|
||||||
return m_versionComponents[compId];
|
return m_data.extended.m_data->GetComponent(compId);
|
||||||
|
return m_data.compact.GetComponent(compId);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] uint32_t Major() const { return GetComponent(0); }
|
[[nodiscard]] uint32_t Major() const { return GetComponent(0); }
|
||||||
@@ -46,13 +235,32 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
[[nodiscard]] uint32_t Patch() const { return GetComponent(2); }
|
[[nodiscard]] uint32_t Patch() const { return GetComponent(2); }
|
||||||
|
|
||||||
[[nodiscard]] uint64_t Build() const { return m_buildNumber; }
|
[[nodiscard]] uint64_t Build() const
|
||||||
|
{
|
||||||
|
if (IsExtendedData()) return m_data.extended.m_data->m_buildNumber;
|
||||||
|
return m_data.compact.m_versionComponents[3];
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool IsPreRelease() const { return m_preRelease; }
|
[[nodiscard]] uint64_t Timestamp() const
|
||||||
|
{
|
||||||
|
return m_data.GetTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] const std::vector<std::string>& GetTags() const { return m_tagComponents; }
|
//[[nodiscard]] bool IsPreRelease() const { return m_preRelease; }
|
||||||
|
|
||||||
[[nodiscard]] const decltype(m_versionComponents)& GetVersionComponents() const { return m_versionComponents; }
|
[[nodiscard]] const std::vector<std::string>& GetTags() const
|
||||||
|
{
|
||||||
|
static std::vector<std::string> fallbackTags;
|
||||||
|
if (IsExtendedData())
|
||||||
|
return m_data.extended.m_data->m_tagComponents;
|
||||||
|
return fallbackTags;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::span<const uint32_t> GetVersionComponents() const
|
||||||
|
{
|
||||||
|
if (IsExtendedData()) return m_data.extended.m_data->m_versionComponents;
|
||||||
|
return m_data.compact.m_versionComponents;
|
||||||
|
}
|
||||||
|
|
||||||
//region Conversion operators
|
//region Conversion operators
|
||||||
[[nodiscard]] explicit operator uint32_t() const { return (Major() << 20) | ((Minor() & 0x3FF) << 10) | (Patch() & 0x3FF); }
|
[[nodiscard]] explicit operator uint32_t() const { return (Major() << 20) | ((Minor() & 0x3FF) << 10) | (Patch() & 0x3FF); }
|
||||||
|
|||||||
@@ -559,4 +559,47 @@ TEST_CASE("testUnimportantVersionParts", "[Version]")
|
|||||||
REQUIRE(Version("1.0.0.3") == Version("1.0.0.3.0.0.0"));
|
REQUIRE(Version("1.0.0.3") == Version("1.0.0.3.0.0.0"));
|
||||||
REQUIRE(Version("1.0.0.3") == Version("1.0.0.3.0.00.0"));
|
REQUIRE(Version("1.0.0.3") == Version("1.0.0.3.0.00.0"));
|
||||||
REQUIRE(Version("1.0.0.3") == Version("1.00.0.3"));
|
REQUIRE(Version("1.0.0.3") == Version("1.00.0.3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("testCopyConstructorAndAssignment", "[Version]")
|
||||||
|
{
|
||||||
|
Version original("1.5.3");
|
||||||
|
Version copyConstructed(original);
|
||||||
|
REQUIRE(copyConstructed == original);
|
||||||
|
REQUIRE(static_cast<const std::string&>(copyConstructed) == static_cast<const std::string&>(original));
|
||||||
|
|
||||||
|
Version copyAssigned("2.0");
|
||||||
|
copyAssigned = original;
|
||||||
|
REQUIRE(copyAssigned == original);
|
||||||
|
REQUIRE(static_cast<const std::string&>(copyAssigned) == static_cast<const std::string&>(original));
|
||||||
|
|
||||||
|
Version extended("1.2.3-ALPHA-B99-T11");
|
||||||
|
Version cpConstructedExtended(extended);
|
||||||
|
REQUIRE(cpConstructedExtended.Timestamp() == 11);
|
||||||
|
REQUIRE(cpConstructedExtended == extended);
|
||||||
|
|
||||||
|
Version extended3 = extended;
|
||||||
|
REQUIRE(extended3.Timestamp() == 11);
|
||||||
|
REQUIRE(extended3 == extended);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("testMoveConstructorAndAssignment", "[Version]")
|
||||||
|
{
|
||||||
|
Version original("3.1.4");
|
||||||
|
Version movedConstructed(std::move(original));
|
||||||
|
REQUIRE(static_cast<const std::string&>(movedConstructed) == "3.1.4");
|
||||||
|
|
||||||
|
Version another("0.9.9");
|
||||||
|
another = std::move(movedConstructed);
|
||||||
|
REQUIRE(static_cast<const std::string&>(another) == "3.1.4");
|
||||||
|
|
||||||
|
Version extended("1.2.3-ALPHA-B99-T11");
|
||||||
|
Version mvConstructedExtended(std::move(extended));
|
||||||
|
REQUIRE(mvConstructedExtended.Timestamp() == 11);
|
||||||
|
REQUIRE(mvConstructedExtended != extended);
|
||||||
|
|
||||||
|
Version extended3 = std::move(mvConstructedExtended);
|
||||||
|
REQUIRE(extended3.Timestamp() == 11);
|
||||||
|
REQUIRE(extended3 != extended);
|
||||||
|
REQUIRE(extended3 != mvConstructedExtended);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user