-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
254 lines (162 loc) · 6.87 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
class Wrap:
"""Constants that define how Grids handle out of range coordinate."""
( none,
repeat,
clamp ) = range(3)
class Grid(object):
def __init__( self, node_factory,
width,
height,
x_wrap = Wrap.none,
y_wrap = Wrap.none ):
"""Create a grid object.
node_factory -- Callable that takes the x and y coordinate of the node
and returns a node object. x_wrap and y_wrap parameters should be
one of (Wrap.none, Wrap.repeat, Wrap.clamp).
width -- Width of the grid.
height -- Height of the grid.
x_wrap -- How to handle out of range x coordinates
y_wrap -- How to handle out of range y coordinates
"""
self.node_factory = node_factory
self.width = width
self.height = height
nodes = []
for y in xrange(height):
nodes.append( [node_factory(x, y) for x in xrange(width)] )
self.nodes = nodes
self._x_wrap = x_wrap
self._y_wrap = y_wrap
self._wrap_functions = [ self._make_wrap(self._x_wrap, self.width),
self._make_wrap(self._y_wrap, self.height) ]
def _get_x_wrap(self):
return self._x_wrap
def _set_x_wrap(self, x_wrap):
self._x_wrap = x_wrap
self._wrap_functions[0] = self._make_wrap(self._x_wrap, self.width)
x_wrap = property(_get_x_wrap, _set_x_wrap, None, "X wrap")
def _get_y_wrap(self):
return self._y_wrap
def _set_y_wrap(self, y_wrap):
self._y_wrap = y_wrap
self._wrap_functions[1] = self._make_wrap(self._y_wrap, self.width)
y_wrap = property(_get_y_wrap, _set_y_wrap, None, "Y wrap")
def _make_wrap(self, wrap, edge):
if wrap is None or wrap == Wrap.none:
def do_wrap(value):
return value
elif wrap == Wrap.repeat:
def do_wrap(value):
return value % edge
elif wrap == Wrap.clamp:
def do_wrap(value):
if value < 0:
return 0
if value > edge:
value = edge
return value
elif wrap == Wrap.error:
def do_wrap(value):
return None
else:
raise ValueError("Unknown wrap mode")
return do_wrap
def wrap(self, coord):
x, y = coord
wrap_x, wrap_y = self._wrap_functions
return ( wrap_x(x), wrap_y(y) )
def wrap_x(self, x):
"""Wraps an x coordinate.
x -- X Coordinate
"""
return self._wrap_functions[0](x)
def wrap_y(self, y):
"""Wraps a y coordinate.
y -- Y Coordinate.
"""
return self._wrap_functions[1](y)
def get_size(self):
"""Retrieves the size of the grid as a tuple (width, height)."""
return self.width, self.height
def __getitem__(self, coord):
x, y = coord
if isinstance(x, slice) or isinstance(y, slice):
if isinstance(x, slice):
x_indices = x.indices(self.width)
else:
x_indices = [x]
if isinstance(y, slice):
y_indices = y.indices(self.height)
else:
y_indices = [y]
try:
wrap_x = self.wrap_x
ret = []
for y_index in xrange(*y_indices):
nodes_y = self.nodes[ wrap_y(y_index) ]
for x_index in xrange(*x_indices):
ret.append( nodes_y[ wrap_x(x_index) ] )
except IndexError:
raise IndexError, "Slice out of range"
return ret
x, y = self.wrap(coord)
if x < 0 or y < 0:
raise IndexError, "coordinate out of range"
try:
return self.nodes[y][x]
except IndexError:
raise IndexError, "coordinate out of range"
def __iter__(self):
for row in self.nodes:
for node in row:
yield node
def __contains__(self, value):
for row in self.nodes:
if node in row:
return True
return False
def clear(self):
"""Resets the grid."""
node_factory = self.node_factory
nodes = []
for y in xrange(height):
nodes.append( [node_factory(x, y) for x in xrange(width)] )
self.nodes = nodes
def get(self, x, y, default=None):
"""Retrieves a node from the grid.
x -- X coordinate
y -- Y coordinate
default -- Default value to use if coord is out of range
"""
x, y = self.wrap(coord)
if x < 0 or y < 0 or x >= self.width or y >= self.height:
return default
try:
return self.nodes[y][x]
except IndexError:
raise IndexError, "coordinate out of range"
def get_nodes(self, coord, size, wrap=False):
x, y = coord
x1, y1 = self.wrap(coord)
w, h = size
x2, y2 = self.wrap((x+w, y+h))
if x1 > x2:
x1, x2 = x2, x1
if y1 > y2:
y1, y2 = y2, y1
ret = []
wrap_x, wrap_y = self._wrap_functions
for y_coord in xrange(y1, y2):
ret += self.nodes[y_coord][x1:x2]
return ret
if __name__ == "__main__":
class Square(object):
def __init__(self, x, y):
self.value = (x, y)
def __str__(self):
return str(self.value)
def __repr__(self):
return str(self.value)
g = Grid(Square, 100, 100, x_wrap = Wrap.repeat)
print g[10:20, 10:20]
print g.get_nodes((-2, 0), (5, 5))