00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 from enhanced_grid import GridWindow2D
00035 from enhanced_grid import GridCol2D
00036 from enhanced_grid import GridRow2D
00037 from enhanced_grid import is_slice
00038
00039
00040 def make_grid_2d(width, height, initial_item=None):
00041 grid = [None] * (width * height)
00042
00043 for i in xrange(width):
00044 for j in xrange(height):
00045 grid[i + j * width] = initial_item
00046
00047 return grid
00048
00049
00050 def make_grid_2d2(width, height, initial_item=None):
00051 grid = [None] * width
00052
00053 for i in xrange(width):
00054 grid[i] = [None] * height
00055
00056 for j in xrange(height):
00057 grid[i][j] = initial_item
00058
00059 return grid
00060
00061
00062
00063
00064
00065
00066
00067
00068 class PrincipleContainer:
00069 pass
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 class Container:
00083
00084
00085
00086
00087 def __init__(self, dims):
00088 self.dims = dims
00089
00090 count = 1
00091
00092 for dim in dims:
00093 count *= dim
00094
00095 self.count = count
00096
00097
00098
00099 def __eq__(self, other):
00100 if other == None:
00101 return False
00102 if self.dims != other.dims:
00103 return False
00104
00105 for cell1, cell2 in zip(self.cell_iter(), other.cell_iter()):
00106 if cell1 != cell2:
00107 return False
00108 return True
00109
00110
00111 def __ne__(self, other):
00112 return not (self == other)
00113
00114
00115
00116
00117
00118 def __len__(self):
00119 return self.dims[0]
00120
00121
00122
00123
00124
00125 def min_max(self):
00126 cell_iter = self.cell_iter()
00127 min = max = cell_iter.next()
00128
00129 for cell in cell_iter:
00130 if cell > max:
00131 max = cell
00132 elif cell < min:
00133 min = cell
00134 return min, max
00135
00136 def copy_from(self, other):
00137 for index in self.index_iter():
00138 self[index] = other[index]
00139
00140
00141
00142
00143
00144
00145
00146
00147 def clone(self):
00148 new_grid = self.__clonetype__(self.dims)
00149 new_grid.copy_from(self)
00150
00151 return new_grid
00152
00153
00154 class Container2D (Container):
00155 def __init__(self, width, height):
00156 Container.__init__(self, (width, height))
00157 self.width = width
00158 self.height = height
00159 self.__clonetype__ = Grid2D
00160
00161 def __str__(self):
00162
00163 glst = []
00164
00165 for i in xrange(self.width):
00166 gcol = []
00167
00168 for j in xrange(self.height):
00169 gcol.append(self[i, j])
00170
00171 glst.append(gcol)
00172
00173 return glst.__repr__()
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 def __iter__(self):
00186 for i in xrange(self.width):
00187 yield self[i, ...]
00188 raise StopIteration
00189
00190
00191
00192
00193
00194
00195
00196
00197 def cell_iter(self):
00198 for i in xrange(self.width):
00199 for j in xrange(self.height):
00200 yield self.get_item_i(i, j)
00201 raise StopIteration
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 def window_index_iter(self, p0, p1):
00213 x0, y0 = p0
00214 x1, y1 = p1
00215 for i in xrange(max(0, x0), min(x1, self.width)):
00216 for j in xrange(max(0, y0), min(y1, self.height)):
00217 yield (i, j)
00218 raise StopIteration
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 def wrapped_window_index_iter(self, p0, p1):
00232 x0, y0 = p0
00233 x1, y1 = p1
00234 for i in xrange(x0, x1):
00235 for j in xrange(y0, y1):
00236 yield (i % self.width, j % self.height)
00237 raise StopIteration
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 def window_iter(self, p0, p1):
00248 x0, y0 = p0
00249 x1, y1 = p1
00250 for i in xrange(max(0, x0), min(x1, self.width)):
00251 for j in xrange(max(0, y0), min(y1, self.height)):
00252 yield self.get_item_i(i, j)
00253 raise StopIteration
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 def wrapped_window_iter(self, p0, p1):
00268 x0, y0 = p0
00269 x1, y1 = p1
00270 for i in xrange(x0, x1):
00271 for j in xrange(y0, y1):
00272 yield self.get_item_i(i % self.width, j % self.height)
00273 raise StopIteration
00274
00275
00276
00277
00278
00279 def square_index_iter(self, p, n):
00280 x, y = p
00281 return self.window_index_iter((x - n, y - n), (x + n + 1, y + n +1))
00282
00283
00284
00285
00286
00287
00288
00289
00290 def wrapped_square_index_iter(self, p, n):
00291 x, y = p
00292 return self.wrapped_window_index_iter((x - n, y - n), (x + n + 1, y + n +1))
00293
00294
00295
00296
00297
00298
00299 def square_iter(self, p, n):
00300 x, y = p
00301 return self.window_iter((x - n, y - n), (x + n + 1, y + n +1))
00302
00303
00304
00305
00306
00307
00308
00309
00310 def wrapped_square_iter(self, p, n):
00311 x, y = p
00312 return self.wrapped_window_iter((x - n, y - n), (x + n + 1, y + n +1))
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 def index_iter(self):
00335 for i in xrange(self.width):
00336 for j in xrange(self.height):
00337 yield i, j
00338 raise StopIteration
00339
00340
00341 class Grid2D (Container2D, PrincipleContainer):
00342 def __init__(self, dims, initial_item = None):
00343 (width, height) = dims
00344 Container2D.__init__(self, width, height)
00345 self.grid = make_grid_2d(width, height, initial_item)
00346
00347
00348
00349
00350
00351
00352
00353 def cell_iter(self):
00354 for i in xrange(self.width):
00355 for j in xrange(self.height):
00356 yield self.get_item_i(i, j)
00357 raise StopIteration
00358
00359 def get_item_i(self, x, y):
00360 return self.grid[x + y * self.width]
00361
00362
00363 def set_item_i(self, x, y, item):
00364 self.grid[x + y * self.width] = item
00365
00366
00367 def __getitem__(self, p):
00368 x, y = p
00369 return self.grid[x + y * self.width]
00370
00371
00372 def __setitem__(self, p, item):
00373 x, y = p
00374 self.grid[x + y * self.width] = item
00375