Skip to content

Commit

Permalink
Use std::hash specializations for C4HudBarDef and C4HudBarsDef instea…
Browse files Browse the repository at this point in the history
…d of GetHash member function
  • Loading branch information
Fulgen301 committed Sep 25, 2024
1 parent 11e1529 commit aef77e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
23 changes: 9 additions & 14 deletions src/C4HudBars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ int32_t C4HudBarDef::DefaultIndex(const C4HudBarDef::Physical physical) noexcept
}
}

std::size_t C4HudBarDef::GetHash() const noexcept
{
return HashArguments(name, physical, hide, gfx, index, advance, valueIndex, value, max, visible);
}

void C4HudBarDef::CompileFunc(StdCompiler *const comp)
{
comp->Value(mkNamingAdapt(name, "Name"));
Expand Down Expand Up @@ -245,6 +240,11 @@ void C4HudBarDef::CompileFunc(StdCompiler *const comp)
// gfx and scale are restored from def.gfxs
}

std::size_t std::hash<C4HudBarDef>::operator()(const C4HudBarDef &bar) const noexcept
{
return HashArguments(bar.name, bar.physical, bar.hide, bar.gfx, bar.index, bar.advance, bar.valueIndex, bar.value, bar.max, bar.visible);
}

namespace
{
template <typename Map>
Expand Down Expand Up @@ -289,27 +289,22 @@ bool C4HudBarsDef::operator==(const C4HudBarsDef &rhs) const noexcept
return MapCompare(gfxs, rhs.gfxs) && bars == rhs.bars;
}

std::size_t C4HudBarsDef::GetHash() const noexcept
std::size_t std::hash<const C4HudBarsDef>::operator()(const C4HudBarsDef &value) const noexcept
{
std::size_t result{0};
for (const auto &gfx : gfxs)
for (const auto &gfx : value.gfxs)
{
HashCombineArguments(result, gfx.second.Key, gfx.second.File, gfx.second.Amount, gfx.second.Scale);
}

for (const auto &bardef : bars)
for (const auto &bardef : value.bars)
{
HashCombine(result, bardef.GetHash());
HashCombineArguments(result, bardef);
}

return result;
}

std::size_t std::hash<const C4HudBarsDef>::operator()(const C4HudBarsDef &value) const noexcept
{
return value.GetHash();
}

bool C4HudBarsUniquifier::LoadDefaultBars()
{
static constexpr auto File = "EnergyBars";
Expand Down
28 changes: 15 additions & 13 deletions src/C4HudBars.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class C4HudBarDef
static Hide DefaultHide(Physical physical) noexcept;
static std::int32_t DefaultIndex(Physical physical) noexcept;

std::size_t GetHash() const noexcept;
void CompileFunc(StdCompiler *comp);

public:
Expand All @@ -101,7 +100,6 @@ class C4HudBarDef
float scale{1.0f}; // calculated from gfx.scale
};


constexpr C4HudBarDef::Hide operator &(const C4HudBarDef::Hide a, const C4HudBarDef::Hide b) noexcept
{
return static_cast<C4HudBarDef::Hide>(std::to_underlying(a) & std::to_underlying(b));
Expand All @@ -118,6 +116,12 @@ constexpr C4HudBarDef::Hide operator~(const C4HudBarDef::Hide a) noexcept
return static_cast<C4HudBarDef::Hide>(~std::to_underlying(a));
}

template<>
struct std::hash<C4HudBarDef>
{
std::size_t operator()(const C4HudBarDef &bar) const noexcept;
};

class C4HudBarsDef
{
public:
Expand Down Expand Up @@ -150,8 +154,6 @@ class C4HudBarsDef

bool operator==(const C4HudBarsDef &rhs) const noexcept;

std::size_t GetHash() const noexcept;

public:
// the definiton is processed and flattened into a vector of energy bar values
// they contain everything needed to draw the energy bars
Expand All @@ -160,6 +162,15 @@ class C4HudBarsDef
Names names;
};

namespace std
{
template<>
struct hash<const C4HudBarsDef>
{
std::size_t operator()(const C4HudBarsDef &value) const noexcept;
};
}

class C4HudBars
{
public:
Expand All @@ -176,15 +187,6 @@ class C4HudBars
C4HudBar &BarVal(std::string_view name);
};

namespace std
{
template<>
struct hash<const C4HudBarsDef>
{
std::size_t operator()(const C4HudBarsDef &value) const noexcept;
};
}

class C4HudBarsUniquifier
{
public:
Expand Down

0 comments on commit aef77e1

Please sign in to comment.