metaforce/Runtime/Character/TSegIdMap.hpp

61 lines
1.7 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
2016-04-13 23:07:18 +00:00
#include <map>
#include "CSegId.hpp"
2018-12-08 05:30:43 +00:00
namespace urde {
2016-04-13 23:07:18 +00:00
template <class T>
2018-12-08 05:30:43 +00:00
class TSegIdMap {
CSegId x0_boneCount = 0;
CSegId x1_capacity = 0;
u32 x4_maxCapacity = 100;
std::pair<CSegId, CSegId> x8_indirectionMap[100];
std::unique_ptr<T[]> xd0_bones;
CSegId xd4_curPrevBone = 0;
2016-04-13 23:07:18 +00:00
public:
2018-12-08 05:30:43 +00:00
TSegIdMap(const CSegId& capacity) : x1_capacity(capacity), xd0_bones(new T[capacity]) {}
T& operator[](const CSegId& id) { return SetElement(id); }
const T& operator[](const CSegId& id) const { return xd0_bones[x8_indirectionMap[id].second]; }
T& SetElement(const CSegId& id, T&& obj) {
size_t idx;
if (x8_indirectionMap[id].first == 0xff) {
x8_indirectionMap[id].first = xd4_curPrevBone;
x8_indirectionMap[id].second = x0_boneCount;
xd4_curPrevBone = id;
idx = x0_boneCount;
++x0_boneCount;
} else
idx = x8_indirectionMap[id].second;
xd0_bones[idx] = std::move(obj);
return xd0_bones[idx];
}
T& SetElement(const CSegId& id) {
size_t idx;
if (x8_indirectionMap[id].first == 0xff) {
x8_indirectionMap[id].first = xd4_curPrevBone;
x8_indirectionMap[id].second = x0_boneCount;
xd4_curPrevBone = id;
idx = x0_boneCount;
++x0_boneCount;
} else
idx = x8_indirectionMap[id].second;
return xd0_bones[idx];
}
void DelElement(const CSegId& id) {
if (x8_indirectionMap[id].first != 0xff) {
if (id == xd4_curPrevBone)
xd4_curPrevBone = x8_indirectionMap[id].first;
x8_indirectionMap[id].first = 0xff;
x8_indirectionMap[id].second = 0xff;
--x0_boneCount;
2016-04-16 03:24:25 +00:00
}
2018-12-08 05:30:43 +00:00
}
bool HasElement(const CSegId& id) const { return x8_indirectionMap[id].first != 0xff; }
2016-09-04 02:27:35 +00:00
2018-12-08 05:30:43 +00:00
u32 GetCapacity() const { return x1_capacity; }
2016-04-13 23:07:18 +00:00
};
2018-12-08 05:30:43 +00:00
} // namespace urde