75 lines
2.3 KiB
Diff
75 lines
2.3 KiB
Diff
# UNDF: UNDF-2026-000001002
|
|
--- a/src/NetPanzer/Units/UnitBucketArray.hpp
|
|
+++ b/src/NetPanzer/Units/UnitBucketArray.hpp
|
|
@@ -1,6 +1,7 @@
|
|
#ifndef _UNIT_BUCKET_ARRAY_HPP
|
|
#define _UNIT_BUCKET_ARRAY_HPP
|
|
|
|
+#include <unordered_map>
|
|
#include "ArrayUtil/BucketArrayTemplate.hpp"
|
|
#include "Units/UnitBase.hpp"
|
|
|
|
@@ -47,6 +48,9 @@ class UnitBucketArray : public UnitBucketArrayTemplate {
|
|
iXY map_size;
|
|
long map_size_x;
|
|
long map_size_y;
|
|
+
|
|
+ // UNDF: O(1) unit-to-bucket lookup instead of O(B*U) full scan
|
|
+ std::unordered_map<UnitID, unsigned long> unitBucketMap;
|
|
iXY tile_size;
|
|
|
|
public:
|
|
--- a/src/NetPanzer/Units/UnitBucketArray.cpp
|
|
+++ b/src/NetPanzer/Units/UnitBucketArray.cpp
|
|
@@ -107,6 +107,7 @@ void UnitBucketArray::addUnit(UnitBase *unit) {
|
|
|
|
unit_bucket_ptr = new UnitBucketPointer(unit);
|
|
|
|
+ unitBucketMap[unit->id] = bucket_index;
|
|
array[bucket_index].addFront(unit_bucket_ptr);
|
|
}
|
|
|
|
@@ -119,25 +120,20 @@ void UnitBucketArray::addUnit(UnitBucketPointer *unit_bucket_ptr) {
|
|
|
|
assert(bucket_index < (long)size);
|
|
|
|
+ unitBucketMap[unit->id] = bucket_index;
|
|
array[bucket_index].addFront(unit_bucket_ptr);
|
|
}
|
|
|
|
long UnitBucketArray::getUnitBucketIndex(UnitID unit_id) {
|
|
- for (unsigned long bucket_index = 0; bucket_index < size; bucket_index++) {
|
|
- UnitBucketPointer *traversal_ptr;
|
|
-
|
|
- traversal_ptr = array[bucket_index].getFront();
|
|
-
|
|
- while (traversal_ptr != 0) {
|
|
- if (traversal_ptr->unit->id == unit_id) return (long)bucket_index;
|
|
-
|
|
- traversal_ptr = traversal_ptr->next;
|
|
- }
|
|
+ // UNDF: O(1) lookup via hash map instead of O(B*U) full bucket scan
|
|
+ auto it = unitBucketMap.find(unit_id);
|
|
+ if (it != unitBucketMap.end()) {
|
|
+ return (long)it->second;
|
|
}
|
|
-
|
|
return -1;
|
|
}
|
|
|
|
@@ -207,6 +203,7 @@ bool UnitBucketArray::moveUnit(UnitID unit_id, unsigned long from_bucket_index,
|
|
move_ptr = traversal_ptr;
|
|
traversal_ptr = traversal_ptr->next;
|
|
array[from_bucket_index].removeObject(move_ptr);
|
|
+ unitBucketMap[unit_id] = to_bucket_index;
|
|
array[to_bucket_index].addFront(move_ptr);
|
|
found = true;
|
|
} else {
|
|
@@ -241,6 +238,7 @@ bool UnitBucketArray::deleteUnitBucketPointer(UnitID unit_id, iXY world_loc) {
|
|
while (traversal_ptr != 0) {
|
|
if (traversal_ptr->unit->id == unit_id) {
|
|
array[bucket_index].deleteObject(traversal_ptr);
|
|
+ unitBucketMap.erase(unit_id);
|
|
return true;
|
|
}
|
|
|