BWAPI
|
00001 #pragma once 00002 /*** 00003 * LGPL Code from BWAPI: 00004 * http://www.google.com/p/bwapi/ 00005 */ 00006 00007 #include <list> 00008 00009 namespace Util 00010 { 00019 template <class Type> 00020 class QuadTree 00021 { 00022 public : 00028 QuadTree(unsigned int width = 1, unsigned int height = 1); 00030 ~QuadTree(void); 00035 unsigned int getWidth(void) const; 00040 unsigned int getHeight(void) const; 00047 std::list<Type*>* getItems(unsigned int x, unsigned int y, unsigned int level = 0); 00054 void addItem(unsigned int x, unsigned int y, Type *item); 00055 void clear(unsigned int x, unsigned int y); 00056 private : 00058 unsigned int width; 00060 unsigned int height; 00065 RectangleArray<std::list<Type*> >* data; 00067 unsigned int depth; 00068 }; 00069 //---------------------------------------------- CONSTRUCTOR ----------------------------------------------- 00070 template <class Type> 00071 QuadTree<Type>::QuadTree(unsigned int width, unsigned int height) 00072 :width(width) 00073 ,height(height) 00074 ,depth(ceil(log2(max(width,height))) 00075 { 00076 this->data = new RectangleArray<std::list<Type*> >[depth]; 00077 unsigned int localWidth = width; 00078 unsigned int localHeight = height; 00079 for (unsigned int i = 0; i < this->depth; ++i) 00080 { 00081 this->data[i].resize(localWidth, localHeight); 00082 localWidth = (localWidth >= 2) (localWidth+1)/2 : 1; 00083 localHeight = (localHeight >=2) (localHeight+1)/2 : 1; 00084 } 00085 } 00086 //----------------------------------------------- DESTRUCTOR ----------------------------------------------- 00087 template <class Type> 00088 RectangleArray<Type>::~RectangleArray(void) 00089 { 00090 delete [] data; 00091 } 00092 //----------------------------------------------- GET WIDTH ------------------------------------------------ 00093 template <class Type> 00094 unsigned int RectangleArray<Type>::getWidth(void) const 00095 { 00096 return this->width; 00097 } 00098 //----------------------------------------------- SET WIDTH ------------------------------------------------ 00099 template <class Type> 00100 void RectangleArray<Type>::setWidth(unsigned int width) 00101 { 00102 this->width = width; 00103 } 00104 //----------------------------------------------- GET HEIGHT ----------------------------------------------- 00105 template <class Type> 00106 unsigned int RectangleArray<Type>::getHeight(void) const 00107 { 00108 return this->height; 00109 } 00110 //------------------------------------------------ GET ITEM ------------------------------------------------ 00111 template <class Type> 00112 std::list<Type*>* getItems(unsigned int x, unsigned int y, unsigned int level = 0); 00113 { 00114 return this->data[level][x][y]; 00115 } 00116 //------------------------------------------------ ADD ITEM ------------------------------------------------ 00117 template <class Type> 00118 void RectangleArray<Type>::addItem(unsigned int x, unsigned int y, Type* item) 00119 { 00120 for (unsigned int i = 0; i < this->depth; ++i) 00121 this->data[i][x<<i][y<<i].push_bach(item); 00122 } 00123 //------------------------------------------------- CLEAR -------------------------------------------------- 00124 template <class Type> 00125 void RectangleArray<Type>::clear(unsigned int x, unsigned int y) 00126 { 00127 for (unsigned int i = 0; i < this->depth; ++i) 00128 this->data[i][x<<i][y<<i].clear(); 00129 } 00130 //---------------------------------------------------------------------------------------------------------- 00131 } 00132