Cleanup: Use std::vector in DeterministicSpriteGroup.

(cherry picked from commit 1aeaf39954)
pull/259/head
Peter Nelson 3 years ago committed by Jonathan G Rennison
parent d3b9d19c5a
commit a69a1d19a9

@ -5169,16 +5169,13 @@ static void NewSpriteGroup(ByteReader *buf)
case 2: group->size = DSG_SIZE_DWORD; varsize = 4; break;
}
static std::vector<DeterministicSpriteGroupAdjust> adjusts;
adjusts.clear();
/* Loop through the var adjusts. Unfortunately we don't know how many we have
* from the outset, so we shall have to keep reallocing. */
do {
DeterministicSpriteGroupAdjust &adjust = adjusts.emplace_back();
DeterministicSpriteGroupAdjust &adjust = group->adjusts.emplace_back();
/* The first var adjust doesn't have an operation specified, so we set it to add. */
adjust.operation = adjusts.size() == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf->ReadByte();
adjust.operation = group->adjusts.size() == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf->ReadByte();
adjust.variable = buf->ReadByte();
if (adjust.variable == 0x7E) {
/* Link subroutine group */
@ -5203,10 +5200,6 @@ static void NewSpriteGroup(ByteReader *buf)
/* Continue reading var adjusts while bit 5 is set. */
} while (HasBit(varadjust, 5));
group->num_adjusts = (uint)adjusts.size();
group->adjusts = MallocT<DeterministicSpriteGroupAdjust>(group->num_adjusts);
MemCpyT(group->adjusts, adjusts.data(), group->num_adjusts);
std::vector<DeterministicSpriteGroupRange> ranges;
ranges.resize(buf->ReadByte());
for (uint i = 0; i < ranges.size(); i++) {
@ -5243,27 +5236,20 @@ static void NewSpriteGroup(ByteReader *buf)
}
assert(target.size() == bounds.size());
std::vector<DeterministicSpriteGroupRange> optimised;
for (uint j = 0; j < bounds.size(); ) {
if (target[j] != group->default_group) {
DeterministicSpriteGroupRange r;
DeterministicSpriteGroupRange &r = group->ranges.emplace_back();
r.group = target[j];
r.low = bounds[j];
while (j < bounds.size() && target[j] == r.group) {
j++;
}
r.high = j < bounds.size() ? bounds[j] - 1 : UINT32_MAX;
optimised.push_back(r);
} else {
j++;
}
}
group->num_ranges = (uint)optimised.size(); // cast is safe, there should never be 2**31 elements here
if (group->num_ranges > 0) {
group->ranges = MallocT<DeterministicSpriteGroupRange>(group->num_ranges);
MemCpyT(group->ranges, &optimised.front(), group->num_ranges);
}
break;
}

@ -56,12 +56,6 @@ TemporaryStorageArray<int32, 0x110> _temp_store;
}
}
DeterministicSpriteGroup::~DeterministicSpriteGroup()
{
free(this->adjusts);
free(this->ranges);
}
RandomizedSpriteGroup::~RandomizedSpriteGroup()
{
free(this->groups);
@ -207,8 +201,8 @@ const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) con
ScopeResolver *scope = object.GetScope(this->var_scope);
for (i = 0; i < this->num_adjusts; i++) {
DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
for (i = 0; i < this->adjusts.size(); i++) {
const DeterministicSpriteGroupAdjust *adjust = &this->adjusts[i];
/* Try to get the variable. We shall assume it is available, unless told otherwise. */
GetVariableExtra extra(adjust->and_mask << adjust->shift_num);
@ -253,16 +247,16 @@ const SpriteGroup *DeterministicSpriteGroup::Resolve(ResolverObject &object) con
return &nvarzero;
}
if (this->num_ranges > 4) {
DeterministicSpriteGroupRange *lower = std::lower_bound(this->ranges + 0, this->ranges + this->num_ranges, value, RangeHighComparator);
if (lower != this->ranges + this->num_ranges && lower->low <= value) {
if (this->ranges.size() > 4) {
const auto &lower = std::lower_bound(this->ranges.begin(), this->ranges.end(), value, RangeHighComparator);
if (lower != this->ranges.end() && lower->low <= value) {
assert(lower->low <= value && value <= lower->high);
return SpriteGroup::Resolve(lower->group, object, false);
}
} else {
for (i = 0; i < this->num_ranges; i++) {
if (this->ranges[i].low <= value && value <= this->ranges[i].high) {
return SpriteGroup::Resolve(this->ranges[i].group, object, false);
for (const auto &range : this->ranges) {
if (range.low <= value && value <= range.high) {
return SpriteGroup::Resolve(range.group, object, false);
}
}
}

@ -166,15 +166,12 @@ struct DeterministicSpriteGroupRange {
struct DeterministicSpriteGroup : SpriteGroup {
DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
~DeterministicSpriteGroup();
VarSpriteGroupScope var_scope;
DeterministicSpriteGroupSize size;
uint num_adjusts;
uint num_ranges;
bool calculated_result;
DeterministicSpriteGroupAdjust *adjusts;
DeterministicSpriteGroupRange *ranges; // Dynamically allocated
std::vector<DeterministicSpriteGroupAdjust> adjusts;
std::vector<DeterministicSpriteGroupRange> ranges; // Dynamically allocated
/* Dynamically allocated, this is the sole owner */
const SpriteGroup *default_group;

Loading…
Cancel
Save