metaforce/Runtime/Character/TSegIdMap.hpp

76 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 <memory>
#include <utility>
#include "Runtime/Character/CSegId.hpp"
2016-04-13 23:07:18 +00:00
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:
explicit TSegIdMap(const CSegId& capacity) : x1_capacity(capacity), xd0_bones(new T[capacity]) {}
2018-12-08 05:30:43 +00:00
T& operator[](const CSegId& id) { return SetElement(id); }
const T& operator[](const CSegId& id) const { return xd0_bones[x8_indirectionMap[id].second]; }
2018-12-08 05:30:43 +00:00
T& SetElement(const CSegId& id, T&& obj) {
size_t idx;
if (HasElement(id)) {
idx = x8_indirectionMap[id].second;
} else {
x8_indirectionMap[id] = std::make_pair(xd4_curPrevBone, x0_boneCount);
2018-12-08 05:30:43 +00:00
xd4_curPrevBone = id;
idx = x0_boneCount;
++x0_boneCount;
}
2018-12-08 05:30:43 +00:00
xd0_bones[idx] = std::move(obj);
return xd0_bones[idx];
}
2018-12-08 05:30:43 +00:00
T& SetElement(const CSegId& id) {
size_t idx;
if (HasElement(id)) {
idx = x8_indirectionMap[id].second;
} else {
x8_indirectionMap[id] = std::make_pair(xd4_curPrevBone, x0_boneCount);
2018-12-08 05:30:43 +00:00
xd4_curPrevBone = id;
idx = x0_boneCount;
++x0_boneCount;
}
2018-12-08 05:30:43 +00:00
return xd0_bones[idx];
}
2018-12-08 05:30:43 +00:00
void DelElement(const CSegId& id) {
if (!HasElement(id)) {
return;
2016-04-16 03:24:25 +00:00
}
if (id == xd4_curPrevBone) {
xd4_curPrevBone = x8_indirectionMap[id].first;
}
x8_indirectionMap[id] = {};
--x0_boneCount;
2018-12-08 05:30:43 +00:00
}
bool HasElement(const CSegId& id) const { return x8_indirectionMap[id].first.IsValid(); }
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