infinitegrid
// Untested class InfiniteGrid<T> { public var cells : Hash<Array<T>>; public function new() { cells = new Hash(); } public inline function posStr(x,y) { return Std.string(x)+"_"+Std.string(y); } public function add(x,y,val) { var pos = posStr(x,y); if (!cells.exists(pos)) { cells.set(pos,new Array<T>()); } var ar = cells.get(pos); ar.push(val); } public function remove(x,y,val) { var pos = posStr(x,y); if (cells.exists(pos)) { var ar = cells.get(pos); var result = ar.remove(val); if (ar.length<1) cells.remove(pos); return result; } else return false; } public function query(l,t,w,h) { var result = new Array<T>(); var x = l; var y = t; while (x<l+w) { while (y<t+h) { var pos = posStr(x,y); if (cells.exists(pos)) for (n in cells.get(pos)) result.push(n); x++; } y++; x = l; } return result; } }
version #8482, modified 2010-05-30 11:10:10 by rtf