| 202 | ## which is sorted by its x or y coordinate. |
| 203 | ## |
| 204 | class Plane(object): |
| 205 | |
| 206 | def __init__(self, objs=None, gridsize=50): |
| 207 | self._objs = [] |
| 208 | self._grid = {} |
| 209 | self.gridsize = gridsize |
| 210 | if objs is not None: |
| 211 | for obj in objs: |
| 212 | self.add(obj) |
| 213 | return |
| 214 | |
| 215 | def __repr__(self): |
| 216 | return ('<Plane objs=%r>' % list(self)) |
| 217 | |
| 218 | def __iter__(self): |
| 219 | return iter(self._objs) |
| 220 | |
| 221 | def __len__(self): |
| 222 | return len(self._objs) |
| 223 | |
| 224 | def __contains__(self, obj): |
| 225 | return obj in self._objs |
| 226 | |
| 227 | def _getrange(self, (x0,y0,x1,y1)): |
| 228 | for y in drange(y0, y1, self.gridsize): |
| 229 | for x in drange(x0, x1, self.gridsize): |
| 230 | yield (x,y) |
| 231 | return |
| 232 | |
| 233 | # add(obj): place an object. |
| 234 | def add(self, obj): |
| 235 | for k in self._getrange((obj.x0, obj.y0, obj.x1, obj.y1)): |
| 236 | if k not in self._grid: |
| 237 | r = [] |
| 238 | self._grid[k] = r |
| 239 | else: |
| 240 | r = self._grid[k] |
| 241 | r.append(obj) |
| 242 | self._objs.append(obj) |
| 243 | return |
| 244 | |
| 245 | # remove(obj): displace an object. |
| 246 | def remove(self, obj): |
| 247 | for k in self._getrange((obj.x0, obj.y0, obj.x1, obj.y1)): |
| 248 | try: |
| 249 | self._grid[k].remove(obj) |
| 250 | except (KeyError, ValueError): |
| 251 | pass |
| 252 | self._objs.remove(obj) |
| 253 | return |
| 254 | |
| 255 | # find(): finds objects that are in a certain area. |
| 256 | def find(self, (x0,y0,x1,y1)): |
| 257 | done = set() |
| 258 | for k in self._getrange((x0,y0,x1,y1)): |
| 259 | if k not in self._grid: continue |
| 260 | for obj in self._grid[k]: |
| 261 | if obj in done: continue |
no outgoing calls
no test coverage detected
searching dependent graphs…