Branimir Karadžić 9 лет назад
Родитель
Сommit
ca361f10ad
1 измененных файлов с 30 добавлено и 2 удалено
  1. 30 2
      include/bx/handlealloc.h

+ 30 - 2
include/bx/handlealloc.h

@@ -476,20 +476,25 @@ namespace bx
 			return false;
 		}
 
-		void removeByKey(KeyT _key)
+		bool removeByKey(KeyT _key)
 		{
 			uint32_t idx = findIndex(_key);
 			if (UINT32_MAX != idx)
 			{
 				m_handle[idx] = invalid;
 				--m_numElements;
+				update();
+				return true;
 			}
+
+			return false;
 		}
 
-		void removeByHandle(uint16_t _handle)
+		bool removeByHandle(uint16_t _handle)
 		{
 			if (invalid != _handle)
 			{
+				const uint32_t numElements = m_numElements;
 				for (uint32_t idx = 0; idx < MaxCapacityT; ++idx)
 				{
 					if (m_handle[idx] == _handle)
@@ -498,7 +503,15 @@ namespace bx
 						--m_numElements;
 					}
 				}
+
+				if (numElements != m_numElements)
+				{
+					update();
+					return true;
+				}
 			}
+
+			return false;
 		}
 
 		uint16_t find(KeyT _key) const
@@ -614,6 +627,21 @@ namespace bx
 			return result;
 		}
 
+		void update()
+		{
+			for (uint32_t idx = 0; idx < MaxCapacityT; ++idx)
+			{
+				if (m_handle[idx] != invalid)
+				{
+					const KeyT     key    = m_key[idx];
+					const uint16_t handle = m_handle[idx];
+					m_handle[idx] = invalid;
+					--m_numElements;
+					insert(key, handle);
+				}
+			}
+		}
+
 		uint32_t m_maxCapacity;
 		uint32_t m_numElements;