Changeset - r25354:5abb78ea0e29
[Not reviewed]
master
0 3 0
Peter Nelson - 3 years ago 2021-05-01 23:00:06
peter1138@openttd.org
Cleanup: Use std::vector in DeterministicSpriteGroup.
3 files changed with 13 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/newgrf.cpp
Show inline comments
 
@@ -5024,16 +5024,13 @@ static void NewSpriteGroup(ByteReader *b
 
				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 */
 
@@ -5058,10 +5055,6 @@ static void NewSpriteGroup(ByteReader *b
 
				/* 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++) {
 
@@ -5098,27 +5091,20 @@ static void NewSpriteGroup(ByteReader *b
 
			}
 
			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;
 
		}
 

	
src/newgrf_spritegroup.cpp
Show inline comments
 
@@ -53,12 +53,6 @@ TemporaryStorageArray<int32, 0x110> _tem
 
	}
 
}
 

	
 
DeterministicSpriteGroup::~DeterministicSpriteGroup()
 
{
 
	free(this->adjusts);
 
	free(this->ranges);
 
}
 

	
 
RandomizedSpriteGroup::~RandomizedSpriteGroup()
 
{
 
	free(this->groups);
 
@@ -205,8 +199,8 @@ const SpriteGroup *DeterministicSpriteGr
 

	
 
	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. */
 
		bool available = true;
 
@@ -250,16 +244,16 @@ const SpriteGroup *DeterministicSpriteGr
 
		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);
 
			}
 
		}
 
	}
src/newgrf_spritegroup.h
Show inline comments
 
@@ -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;
0 comments (0 inline, 0 general)