1
0
Fork 0

Codechange: Move GetNthSetBit() to BaseBitSet.

This now returns the correct type, or std::nullopt instead of -1.
pull/14266/head
Peter Nelson 2025-05-13 20:26:10 +01:00 committed by Peter Nelson
parent c50ee282f9
commit c8a336f760
2 changed files with 21 additions and 25 deletions

View File

@ -239,6 +239,21 @@ public:
return CountBits(this->base()); return CountBits(this->base());
} }
/**
* Get the value of the Nth set bit.
* @param n The Nth set bit from which we want to know the value.
* @return The value of the Nth set bit, or std::nullopt if no Nth bit set.
*/
std::optional<Tvalue_type> GetNthSetBit(uint n) const
{
for (auto i : *this) {
if (n == 0) return i;
--n;
}
return std::nullopt;
}
auto begin() const { return SetBitIterator<Tvalue_type>(this->data).begin(); } auto begin() const { return SetBitIterator<Tvalue_type>(this->data).begin(); }
auto end() const { return SetBitIterator<Tvalue_type>(this->data).end(); } auto end() const { return SetBitIterator<Tvalue_type>(this->data).end(); }

View File

@ -88,25 +88,6 @@ private:
Dimension icon_size{}; ///< Dimensions of company icon Dimension icon_size{}; ///< Dimensions of company icon
Dimension exclusive_size{}; ///< Dimensions of exclusive icon Dimension exclusive_size{}; ///< Dimensions of exclusive icon
/**
* Get the position of the Nth set bit.
*
* If there is no Nth bit set return -1
*
* @param n The Nth set bit from which we want to know the position
* @return The position of the Nth set bit, or -1 if no Nth bit set.
*/
int GetNthSetBit(int n)
{
if (n >= 0) {
for (uint i : SetBitIterator(this->enabled_actions.base())) {
n--;
if (n < 0) return i;
}
}
return -1;
}
/** /**
* Gets all town authority actions enabled in settings. * Gets all town authority actions enabled in settings.
* *
@ -314,14 +295,14 @@ public:
case WID_TA_COMMAND_LIST: { case WID_TA_COMMAND_LIST: {
int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, GetCharacterHeight(FS_NORMAL)) - 1; int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, GetCharacterHeight(FS_NORMAL)) - 1;
y = GetNthSetBit(y); auto action = this->enabled_actions.GetNthSetBit(y);
if (y >= 0) { if (!action.has_value()) break;
this->sel_action = static_cast<TownAction>(y);
this->SetDirty(); this->sel_action = *action;
} this->SetDirty();
/* When double-clicking, continue */ /* When double-clicking, continue */
if (click_count == 1 || y < 0 || !this->available_actions.Test(this->sel_action)) break; if (click_count == 1 || !this->available_actions.Test(this->sel_action)) break;
[[fallthrough]]; [[fallthrough]];
} }