Ver Fonte

Fix IdArray::destroy() not updating internal tables and add begin()/end() iterators

Daniele Bartolini há 12 anos atrás
pai
commit
72e228c624
1 ficheiros alterados com 40 adições e 2 exclusões
  1. 40 2
      engine/core/containers/IdArray.h

+ 40 - 2
engine/core/containers/IdArray.h

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Allocator.h"
 #include "Types.h"
 #include "List.h"
+#include "Log.h"
 
 namespace crown
 {
@@ -58,6 +59,11 @@ public:
 
 	T&				lookup(const Id& id);
 
+	T*				begin();
+	const T*		begin() const;
+	T*				end();
+	const T*		end() const;
+
 private:
 
 	// Returns the next available unique id.
@@ -154,10 +160,14 @@ inline void IdArray<MAX_NUM_ID, T>::destroy(Id id)
 
 	// Swap with last element
 	m_objects[m_sparse_to_dense[id.index]] = m_objects.back();
+	uint32_t last = m_objects.size() - 1;
 	m_objects.pop_back();
 
-	// Update conversion tables
-	//m_sparse_to_dense[m_dense_to_sparse[m_dense.size() - 1]] = id.index;
+	// Update tables
+	uint16_t std = m_sparse_to_dense[id.index];
+	uint16_t dts = m_dense_to_sparse[last];
+	m_sparse_to_dense[dts] = std;
+	m_dense_to_sparse[std] = dts;
 }
 
 //-----------------------------------------------------------------------------
@@ -185,4 +195,32 @@ inline uint16_t IdArray<MAX_NUM_ID, T>::next_id()
 	return m_next_id++;
 }
 
+//-----------------------------------------------------------------------------
+template <uint32_t MAX_NUM_ID, typename T>
+inline T* IdArray<MAX_NUM_ID, T>::begin()
+{
+	return m_objects.begin();
+}
+
+//-----------------------------------------------------------------------------
+template <uint32_t MAX_NUM_ID, typename T>
+inline const T* IdArray<MAX_NUM_ID, T>::begin() const
+{
+	return m_objects.begin();
+}
+
+//-----------------------------------------------------------------------------
+template <uint32_t MAX_NUM_ID, typename T>
+inline T* IdArray<MAX_NUM_ID, T>::end()
+{
+	return m_objects.end();
+}
+
+//-----------------------------------------------------------------------------
+template <uint32_t MAX_NUM_ID, typename T>
+inline const T* IdArray<MAX_NUM_ID, T>::end() const
+{
+	return m_objects.end();
+}
+
 } // namespace crown