|
|
|
@ -307,23 +307,23 @@ typedef int ImPoolIdx;
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct IMGUI_API ImPool
|
|
|
|
|
{
|
|
|
|
|
ImVector<T> Data; // Contiguous data
|
|
|
|
|
ImVector<T> Buf; // Contiguous data
|
|
|
|
|
ImGuiStorage Map; // ID->Index
|
|
|
|
|
ImPoolIdx FreeIdx; // Next free idx to use
|
|
|
|
|
|
|
|
|
|
ImPool() { FreeIdx = 0; }
|
|
|
|
|
~ImPool() { Clear(); }
|
|
|
|
|
T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Data[idx] : NULL; }
|
|
|
|
|
T* GetByIndex(ImPoolIdx n) { return &Data[n]; }
|
|
|
|
|
ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Data.Data && p < Data.Data + Data.Size); return (ImPoolIdx)(p - Data.Data); }
|
|
|
|
|
T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Data[*p_idx]; *p_idx = FreeIdx; return Add(); }
|
|
|
|
|
bool Contains(const T* p) const { return (p >= Data.Data && p < Data.Data + Data.Size); }
|
|
|
|
|
void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Data[idx].~T(); } Map.Clear(); Data.clear(); FreeIdx = 0; }
|
|
|
|
|
T* Add() { int idx = FreeIdx; if (idx == Data.Size) { Data.resize(Data.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Data[idx]; } IM_PLACEMENT_NEW(&Data[idx]) T(); return &Data[idx]; }
|
|
|
|
|
T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Buf[idx] : NULL; }
|
|
|
|
|
T* GetByIndex(ImPoolIdx n) { return &Buf[n]; }
|
|
|
|
|
ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Buf.Data && p < Buf.Data + Buf.Size); return (ImPoolIdx)(p - Buf.Data); }
|
|
|
|
|
T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Buf[*p_idx]; *p_idx = FreeIdx; return Add(); }
|
|
|
|
|
bool Contains(const T* p) const { return (p >= Buf.Data && p < Buf.Data + Buf.Size); }
|
|
|
|
|
void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Buf[idx].~T(); } Map.Clear(); Buf.clear(); FreeIdx = 0; }
|
|
|
|
|
T* Add() { int idx = FreeIdx; if (idx == Buf.Size) { Buf.resize(Buf.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Buf[idx]; } IM_PLACEMENT_NEW(&Buf[idx]) T(); return &Buf[idx]; }
|
|
|
|
|
void Remove(ImGuiID key, const T* p) { Remove(key, GetIndex(p)); }
|
|
|
|
|
void Remove(ImGuiID key, ImPoolIdx idx) { Data[idx].~T(); *(int*)&Data[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); }
|
|
|
|
|
void Reserve(int capacity) { Data.reserve(capacity); Map.Data.reserve(capacity); }
|
|
|
|
|
int GetSize() const { return Data.Size; }
|
|
|
|
|
void Remove(ImGuiID key, ImPoolIdx idx) { Buf[idx].~T(); *(int*)&Buf[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); }
|
|
|
|
|
void Reserve(int capacity) { Buf.reserve(capacity); Map.Data.reserve(capacity); }
|
|
|
|
|
int GetSize() const { return Buf.Size; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|