80 lines
2.6 KiB
Diff
80 lines
2.6 KiB
Diff
# UNDF: UNDF-2026-000001001
|
|
--- a/src/NetPanzer/Units/UnitInterface.hpp
|
|
+++ b/src/NetPanzer/Units/UnitInterface.hpp
|
|
@@ -1,6 +1,7 @@
|
|
#ifndef _UNITINTERFACE_HPP
|
|
#define _UNITINTERFACE_HPP
|
|
|
|
+#include <unordered_map>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
@@ -44,6 +45,7 @@ class UnitInterface {
|
|
|
|
private:
|
|
static Units units;
|
|
+ static std::unordered_map<UnitBase*, size_t> playerUnitIndex;
|
|
static PlayerUnitList* playerUnitLists;
|
|
|
|
static UnitBucketArray unit_bucket_array;
|
|
--- a/src/NetPanzer/Units/UnitInterface.cpp
|
|
+++ b/src/NetPanzer/Units/UnitInterface.cpp
|
|
@@ -46,6 +46,7 @@
|
|
// UnitList * UnitInterface::unit_lists;
|
|
UnitInterface::Units UnitInterface::units;
|
|
UnitInterface::PlayerUnitList* UnitInterface::playerUnitLists = 0;
|
|
+std::unordered_map<UnitBase*, size_t> UnitInterface::playerUnitIndex;
|
|
UnitBucketArray UnitInterface::unit_bucket_array;
|
|
|
|
PlayerID UnitInterface::max_players;
|
|
@@ -89,6 +90,7 @@ void UnitInterface::cleanUp() {
|
|
for (Units::iterator i = units.begin(); i != units.end(); ++i)
|
|
delete i->second;
|
|
units.clear();
|
|
+ playerUnitIndex.clear();
|
|
}
|
|
|
|
void UnitInterface::reset() {
|
|
@@ -100,6 +102,7 @@ void UnitInterface::reset() {
|
|
for (Units::iterator i = units.begin(); i != units.end(); ++i)
|
|
delete i->second;
|
|
units.clear();
|
|
+ playerUnitIndex.clear();
|
|
}
|
|
|
|
// ******************************************************************
|
|
@@ -165,15 +168,25 @@ void UnitInterface::removeUnit(Units::iterator i) {
|
|
unit_bucket_array.deleteUnitBucketPointer(unit->id,
|
|
unit->unit_state.location);
|
|
PlayerUnitList& plist = playerUnitLists[unit->player->getID()];
|
|
|
|
- PlayerUnitList::iterator pi = std::find(plist.begin(), plist.end(), unit);
|
|
- assert(pi != plist.end());
|
|
- if (pi != plist.end()) plist.erase(pi);
|
|
+ // UNDF: O(1) removal via index map instead of O(U) std::find on vector
|
|
+ auto it = playerUnitIndex.find(unit);
|
|
+ assert(it != playerUnitIndex.end());
|
|
+ if (it != playerUnitIndex.end()) {
|
|
+ size_t idx = it->second;
|
|
+ if (idx < plist.size() - 1) {
|
|
+ UnitBase* back = plist.back();
|
|
+ plist[idx] = back;
|
|
+ playerUnitIndex[back] = idx;
|
|
+ }
|
|
+ plist.pop_back();
|
|
+ playerUnitIndex.erase(it);
|
|
+ }
|
|
|
|
units.erase(i);
|
|
delete unit;
|
|
}
|
|
|
|
// ******************************************************************
|
|
@@ -297,6 +310,7 @@ void UnitInterface::addNewUnit(UnitBase* unit) {
|
|
units.insert(std::make_pair(unit->id, unit));
|
|
|
|
Uint16 player_index = unit->player->getID();
|
|
+ playerUnitIndex[unit] = playerUnitLists[player_index].size();
|
|
playerUnitLists[player_index].push_back(unit);
|
|
|
|
unit_bucket_array.addUnit(unit);
|