Changeset - r23534:ced2648be38e
[Not reviewed]
master
0 9 0
Henry Wilson - 5 years ago 2019-02-20 19:27:10
m3henry@googlemail.com
Codechange: Replaced SmallVector::Find() with std::find()
9 files changed with 41 insertions and 37 deletions:
0 comments (0 inline, 0 general)
src/core/smallvec_type.hpp
Show inline comments
 
@@ -67,18 +67,6 @@ public:
 
	~SmallVector() = default;
 

	
 
	/**
 
	 * Search for the first occurrence of an item.
 
	 * The '!=' operator of T is used for comparison.
 
	 * @param item Item to search for
 
	 * @return The position of the item, or -1 when not present
 
	 */
 
	inline int FindIndex(const T &item) const
 
	{
 
		auto const it = std::find(std::vector<T>::begin(), std::vector<T>::end(), item);
 
		return it == std::vector<T>::end() ? -1 : it - std::vector<T>::begin();
 
	}
 

	
 
	/**
 
	 * Tests whether a item is present in the vector, and appends it to the end if not.
 
	 * The '!=' operator of T is used for comparison.
 
	 * @param item Item to test for
 
@@ -133,7 +121,25 @@ public:
 
};
 

	
 
/**
 
 * Helper function to extend a vector by more than one element
 
 * Helper function to get the index of an item
 
 * Consider using std::set, std::unordered_set or std::flat_set in new code
 
 *
 
 * @param vec A reference to the vector to be extended
 
 * @param item Reference to the item to be search for
 
 *
 
 * @return Index of element if found, otherwise -1
 
 */
 
template <typename T>
 
int find_index(std::vector<T> const& vec, T const& item)
 
{
 
	auto const it = std::find(vec.begin(), vec.end(), item);
 
	if (it != vec.end()) return it - vec.begin();
 

	
 
	return -1;
 
}
 

	
 
/**
 
 * Helper function to append N default-constructed elements and get a pointer to the first new element
 
 * Consider using std::back_inserter in new code
 
 *
 
 * @param vec A reference to the vector to be extended
src/network/core/host.cpp
Show inline comments
 
@@ -136,7 +136,7 @@ static void NetworkFindBroadcastIPsInter
 
		memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
 
		((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
 
		NetworkAddress addr(address, sizeof(sockaddr));
 
		if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) *broadcast->Append() = addr;
 
		if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
 
	}
 

	
 
	free(ifo);
src/network/network_content_gui.cpp
Show inline comments
 
@@ -435,12 +435,8 @@ class NetworkContentListWindow : public 
 
	{
 
		if (!this->content.Sort()) return;
 

	
 
		for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
 
			if (*iter == this->selected) {
 
				this->list_pos = iter - this->content.Begin();
 
				break;
 
			}
 
		}
 
		int idx = find_index(this->content, this->selected);
 
		if (idx >= 0) this->list_pos = idx;
 
	}
 

	
 
	/** Filter content by tags/name */
 
@@ -478,11 +474,10 @@ class NetworkContentListWindow : public 
 
		if (!changed) return;
 

	
 
		/* update list position */
 
		for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
 
			if (*iter == this->selected) {
 
				this->list_pos = iter - this->content.Begin();
 
				return;
 
			}
 
		int idx = find_index(this->content, this->selected);
 
		if (idx >= 0) {
 
			this->list_pos = idx;
 
			return;
 
		}
 

	
 
		/* previously selected item not in list anymore */
src/newgrf.cpp
Show inline comments
 
@@ -8383,8 +8383,8 @@ static void BuildCargoTranslationMap()
 
			_cur.grffile->cargo_map[c] = cs->bitnum;
 
		} else {
 
			/* Check the translation table for this cargo's label */
 
			int index = _cur.grffile->cargo_list.FindIndex(cs->label);
 
			if (index >= 0) _cur.grffile->cargo_map[c] = index;
 
			int idx = find_index(_cur.grffile->cargo_list, {cs->label});
 
			if (idx >= 0) _cur.grffile->cargo_map[c] = idx;
 
		}
 
	}
 
}
 
@@ -9226,7 +9226,7 @@ static void FinalisePriceBaseMultipliers
 
		GRFFile *dest = GetFileByGRFID(override);
 
		if (dest == NULL) continue;
 

	
 
		grf_overrides[i] = _grf_files.FindIndex(dest);
 
		grf_overrides[i] = find_index(_grf_files, dest);
 
		assert(grf_overrides[i] >= 0);
 
	}
 

	
src/newgrf_engine.cpp
Show inline comments
 
@@ -1245,8 +1245,8 @@ void CommitVehicleListOrderChanges()
 
		EngineID target = _engine_mngr.GetID(id_source->type, local_target, id_source->grfid);
 
		if (target == INVALID_ENGINE) continue;
 

	
 
		int source_index = ordering.FindIndex(source);
 
		int target_index = ordering.FindIndex(target);
 
		int source_index = find_index(ordering, source);
 
		int target_index = find_index(ordering, target);
 

	
 
		assert(source_index >= 0 && target_index >= 0);
 
		assert(source_index != target_index);
src/newgrf_gui.cpp
Show inline comments
 
@@ -1486,8 +1486,10 @@ private:
 
		this->avails.Sort();
 

	
 
		if (this->avail_sel != NULL) {
 
			this->avail_pos = this->avails.FindIndex(this->avail_sel);
 
			if (this->avail_pos < 0) this->avail_sel = NULL;
 
			this->avail_pos = find_index(this->avails, this->avail_sel);
 
			if (this->avail_pos == -1) {
 
				this->avail_sel = NULL;
 
			}
 
		}
 

	
 
		this->vscroll2->SetCount(this->avails.size()); // Update the scrollbar
src/newgrf_railtype.cpp
Show inline comments
 
@@ -143,8 +143,9 @@ uint8 GetReverseRailTypeTranslation(Rail
 

	
 
	/* Look for a matching rail type label in the table */
 
	RailTypeLabel label = GetRailTypeInfo(railtype)->label;
 
	int index = grffile->railtype_list.FindIndex(label);
 
	if (index >= 0) return index;
 

	
 
	int idx = find_index(grffile->railtype_list, label);
 
	if (idx >= 0) return idx;
 

	
 
	/* If not found, return as invalid */
 
	return 0xFF;
src/os/windows/string_uniscribe.cpp
Show inline comments
 
@@ -424,7 +424,7 @@ static std::vector<SCRIPT_ITEM> Uniscrib
 
			if (!UniscribeShapeRun(this->text_buffer, run)) return NULL;
 
		}
 

	
 
		*line->Append() = new UniscribeVisualRun(run, cur_pos);
 
		line->push_back(new UniscribeVisualRun(run, cur_pos));
 
		cur_pos += run.total_advance;
 
	}
 

	
src/timetable_cmd.cpp
Show inline comments
 
@@ -298,10 +298,9 @@ CommandCost CmdSetTimetableStart(TileInd
 
			QSortT(vehs.Begin(), vehs.size(), &VehicleTimetableSorter);
 
		}
 

	
 
		int base = vehs.FindIndex(v);
 
		int idx = vehs.begin() - std::find(vehs.begin(), vehs.end(), v);
 

	
 
		for (Vehicle **viter = vehs.Begin(); viter != vehs.End(); viter++) {
 
			int idx = (viter - vehs.Begin()) - base;
 
			Vehicle *w = *viter;
 

	
 
			w->lateness_counter = 0;
 
@@ -309,6 +308,7 @@ CommandCost CmdSetTimetableStart(TileInd
 
			/* Do multiplication, then division to reduce rounding errors. */
 
			w->timetable_start = start_date + idx * total_duration / num_vehs / DAY_TICKS;
 
			SetWindowDirty(WC_VEHICLE_TIMETABLE, w->index);
 
			++idx;
 
		}
 

	
 
	}
0 comments (0 inline, 0 general)