|
From: <axl...@us...> - 2010-04-20 15:47:49
|
Revision: 692
http://hgengine.svn.sourceforge.net/hgengine/?rev=692&view=rev
Author: axlecrusher
Date: 2010-04-20 15:47:42 +0000 (Tue, 20 Apr 2010)
Log Message:
-----------
add and use array3d
Modified Paths:
--------------
Mercury2/src/DataStructures/SpatialHash.h
Added Paths:
-----------
Mercury2/src/DataStructures/array3d.h
Modified: Mercury2/src/DataStructures/SpatialHash.h
===================================================================
--- Mercury2/src/DataStructures/SpatialHash.h 2010-04-20 15:45:40 UTC (rev 691)
+++ Mercury2/src/DataStructures/SpatialHash.h 2010-04-20 15:47:42 UTC (rev 692)
@@ -4,6 +4,7 @@
//#include <stdint.h>
#include <list>
#include <math.h>
+#include <array3d.h>
template<typename T>
class SpatialHash
@@ -29,8 +30,7 @@
cellCount = cellCount==0?1:cellCount;
m_cellCount = cellCount;
- uint32_t size = cellCount*cellCount*cellCount;
- m_hashTable = new std::list<T>[size];
+ m_hashTable = new Array3d< std::list<T> >(cellCount,cellCount,cellCount);
}
void Insert(float x, float y, float z, const T& d)
@@ -45,7 +45,7 @@
iz = iz % m_cellCount;
//check for and skip duplicate
- std::list<T>& cell = m_hashTable[ Index( ix, iy, iz ) ];
+ std::list<T>& cell = m_hashTable->Get( ix, iy, iz );
typename std::list<T>::iterator i = cell.begin();
for (;i != cell.end(); ++i)
@@ -70,7 +70,7 @@
std::list<T> r;
for (uint32_t iz = 0; iz < m_cellCount; ++iz)
- CopyIntoList(m_hashTable[Index(ix, iy, iz)], r);
+ CopyIntoList(m_hashTable->Get(ix, iy, iz), r);
return r;
}
@@ -87,19 +87,15 @@
std::list<T> r;
for (uint32_t iy = 0; iy < m_cellCount; ++iy)
- CopyIntoList(m_hashTable[Index(ix, iy, iz)], r);
+ CopyIntoList(m_hashTable->Get(ix, iy, iz), r);
return r;
}
private:
- inline uint32_t Index(uint32_t x, uint32_t y, uint32_t z)
- {
- return x + (m_cellCount * y) + (m_cellCount * m_cellCount * z);
- }
void DeleteHash()
{
- if (m_hashTable) delete[] m_hashTable;
+ if (m_hashTable) delete m_hashTable;
m_spacing = 1;
m_cellCount = 0;
}
@@ -111,7 +107,7 @@
for (;i != in.end(); ++i) r.push_back(*i);
}
- std::list<T>* m_hashTable;
+ Array3d< std::list<T> >* m_hashTable;
float m_spacing;
uint32_t m_cellCount;
};
Added: Mercury2/src/DataStructures/array3d.h
===================================================================
--- Mercury2/src/DataStructures/array3d.h (rev 0)
+++ Mercury2/src/DataStructures/array3d.h 2010-04-20 15:47:42 UTC (rev 692)
@@ -0,0 +1,75 @@
+#ifndef ARRAY3D_h
+#define ARRAY3D_h
+
+class Array3dOutOfBounds
+{
+ public:
+ Array3dOutOfBounds(uint16_t xx, uint16_t yy, uint16_t zz, uint16_t mx, uint16_t my, uint16_t mz)
+ :X(xx), Y(yy), Z(zz), MaxX(mx), MaxY(my), MaxZ(mz)
+ { }
+ uint16_t X,Y,Z;
+ uint16_t MaxX,MaxY,MaxZ;
+};
+
+template<typename T>
+class Array3d
+{
+ public:
+ Array3d(uint16_t x, uint16_t y, uint16_t z)
+ :m_array(0), m_x(x), m_y(y), m_z(z)
+ {
+ m_array = new T[m_x*m_y*m_z];
+ }
+
+ ~Array3d()
+ {
+ if (m_array!=0) delete[] m_array;
+ m_array=0;
+ m_x = m_y = m_z = 0;
+ }
+
+ inline T& Get(uint16_t x, uint16_t y, uint16_t z)
+ {
+ if (x>=m_x || y>=m_y || z>=m_z) throw Array3dOutOfBounds(x,y,z,m_x,m_y,m_z);
+ return m_array[x + (m_x * y) + (m_x * m_y * z)];
+ }
+
+ private:
+ T* m_array;
+ uint16_t m_x,m_y,m_z;
+};
+
+#endif
+
+
+/****************************************************************************
+ * Copyright (C) 2010 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|