-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit2.py
430 lines (343 loc) · 13.3 KB
/
circuit2.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python
import json # Used when TRACE=jsonp
import os # Used to get the TRACE environment variable
import re # Used when TRACE=jsonp
import sys # Used to smooth over the range / xrange issue.
import avl
# Python 3 doesn't have xrange, and range behaves like xrange.
if sys.version_info >= (3,):
xrange = range
# Circuit verification library.
class Wire(object):
"""A wire in an on-chip circuit.
Wires are immutable, and are either horizontal or vertical.
"""
def __init__(self, name, x1, y1, x2, y2):
"""Creates a wire.
Raises an ValueError if the coordinates don't make up a horizontal wire
or a vertical wire.
Args:
name: the wire's user-visible name
x1: the X coordinate of the wire's first endpoint
y1: the Y coordinate of the wire's first endpoint
x2: the X coordinate of the wire's last endpoint
y2: the Y coordinate of the wire's last endpoint
"""
# Normalize the coordinates.
if x1 > x2:
x1, x2 = x2, x1
if y1 > y2:
y1, y2 = y2, y1
self.name = name
self.x1, self.y1 = x1, y1
self.x2, self.y2 = x2, y2
self.object_id = Wire.next_object_id()
if not (self.is_horizontal() or self.is_vertical()):
raise ValueError(str(self) + ' is neither horizontal nor vertical')
def is_horizontal(self):
"""True if the wire's endpoints have the same Y coordinates."""
return self.y1 == self.y2
def is_vertical(self):
"""True if the wire's endpoints have the same X coordinates."""
return self.x1 == self.x2
def intersects(self, other_wire):
"""True if this wire intersects another wire."""
# NOTE: we assume that wires can only cross, but not overlap.
if self.is_horizontal() == other_wire.is_horizontal():
return False
if self.is_horizontal():
h = self
v = other_wire
else:
h = other_wire
v = self
return v.y1 <= h.y1 and h.y1 <= v.y2 and h.x1 <= v.x1 and v.x1 <= h.x2
def __repr__(self):
# :nodoc: nicer formatting to help with debugging
return('<wire ' + self.name + ' (' + str(self.x1) + ',' + str(self.y1) +
')-(' + str(self.x2) + ',' + str(self.y2) + ')>')
def as_json(self):
"""Dict that obeys the JSON format restrictions, representing the wire."""
return {'id': self.name, 'x': [self.x1, self.x2], 'y': [self.y1, self.y2]}
# Next number handed out by Wire.next_object_id()
_next_id = 0
@staticmethod
def next_object_id():
"""Returns a unique numerical ID to be used as a Wire's object_id."""
id = Wire._next_id
Wire._next_id += 1
return id
class WireLayer(object):
"""The layout of one layer of wires in a chip."""
def __init__(self):
"""Creates a layer layout with no wires."""
self.wires = {}
def wires(self):
"""The wires in the layout."""
self.wires.values()
def add_wire(self, name, x1, y1, x2, y2):
"""Adds a wire to a layer layout.
Args:
name: the wire's unique name
x1: the X coordinate of the wire's first endpoint
y1: the Y coordinate of the wire's first endpoint
x2: the X coordinate of the wire's last endpoint
y2: the Y coordinate of the wire's last endpoint
Raises an exception if the wire isn't perfectly horizontal (y1 = y2) or
perfectly vertical (x1 = x2)."""
if name in self.wires:
raise ValueError('Wire name ' + name + ' not unique')
self.wires[name] = Wire(name, x1, y1, x2, y2)
def as_json(self):
"""Dict that obeys the JSON format restrictions, representing the layout."""
return { 'wires': [wire.as_json() for wire in self.wires.values()] }
@staticmethod
def from_file(file):
"""Builds a wire layer layout by reading a textual description from a file.
Args:
file: a File object supplying the input
Returns a new Simulation instance."""
layer = WireLayer()
while True:
command = file.readline().split()
if command[0] == 'wire':
coordinates = [float(token) for token in command[2:6]]
layer.add_wire(command[1], *coordinates)
elif command[0] == 'done':
break
return layer
class RangeIndex(object):
"""Array-based range index implementation."""
def __init__(self):
"""Initially empty range index."""
self.data = avl.AVL()
self.count = -1
def add(self, key):
"""Inserts a key in the range index."""
if key is None:
raise ValueError('Cannot insert nil in the index')
self.data.insert(key)
def remove(self, key):
"""Removes a key from the range index."""
self.data.delete(key)
def list(self, first_key, last_key):
"""List of values for the keys that fall within [first_key, last_key]."""
lowest_ca = self.lca(first_key, last_key)
result = []
self.node_list(lowest_ca, first_key, last_key, result)
self.count = len(result)
return result
def node_list(self, node, first_key, last_key, result):
if node is None:
return
if first_key <= node.key and last_key >= node.key:
result.append(node.key)
if node.key >= first_key:
self.node_list(node.left, first_key, last_key, result)
if node.key <= last_key:
self.node_list(node.right, first_key, last_key, result)
def lca(self, first_key, last_key):
node = self.data.root
while node is not None and (first_key <= node.key and last_key >= node.key):
if first_key < node.key:
node = node.left
else:
node = node.right
return node
def count(self, first_key, last_key):
"""Number of keys that fall within [first_key, last_key]."""
if self.count == -1:
list(self, first_key, last_key)
return self.count
else:
return self.count
class TracedRangeIndex(RangeIndex):
"""Augments RangeIndex to build a trace for the visualizer."""
def __init__(self, trace):
"""Sets the object receiving tracing info."""
RangeIndex.__init__(self)
self.trace = trace
def add(self, key):
self.trace.append({'type': 'add', 'id': key.wire.name})
RangeIndex.add(self, key)
def remove(self, key):
self.trace.append({'type': 'delete', 'id': key.wire.name})
RangeIndex.remove(self, key)
def list(self, first_key, last_key):
result = RangeIndex.list(self, first_key, last_key)
self.trace.append({'type': 'list', 'from': first_key.key,
'to': last_key.key,
'ids': [key.wire.name for key in result]})
return result
def count(self, first_key, last_key):
result = RangeIndex.count(self, first_key, last_key)
self.trace.append({'type': 'list', 'from': first_key.key,
'to': last_key.key, 'count': result})
return result
class ResultSet(object):
"""Records the result of the circuit verifier (pairs of crossing wires)."""
def __init__(self):
"""Creates an empty result set."""
self.crossings = []
def add_crossing(self, wire1, wire2):
"""Records the fact that two wires are crossing."""
self.crossings.append(sorted([wire1.name, wire2.name]))
def write_to_file(self, file):
"""Write the result to a file."""
for crossing in self.crossings:
file.write(' '.join(crossing))
file.write('\n')
class TracedResultSet(ResultSet):
"""Augments ResultSet to build a trace for the visualizer."""
def __init__(self, trace):
"""Sets the object receiving tracing info."""
ResultSet.__init__(self)
self.trace = trace
def add_crossing(self, wire1, wire2):
self.trace.append({'type': 'crossing', 'id1': wire1.name,
'id2': wire2.name})
ResultSet.add_crossing(self, wire1, wire2)
class KeyWirePair(object):
"""Wraps a wire and the key representing it in the range index.
Once created, a key-wire pair is immutable."""
def __init__(self, key, wire):
"""Creates a new key for insertion in the range index."""
self.key = key
if wire is None:
raise ValueError('Use KeyWirePairL or KeyWirePairH for queries')
self.wire = wire
self.wire_id = wire.object_id
def __lt__(self, other):
# :nodoc: Delegate comparison to keys.
return (self.key < other.key or
(self.key == other.key and self.wire_id < other.wire_id))
def __le__(self, other):
# :nodoc: Delegate comparison to keys.
return (self.key < other.key or
(self.key == other.key and self.wire_id <= other.wire_id))
def __gt__(self, other):
# :nodoc: Delegate comparison to keys.
return (self.key > other.key or
(self.key == other.key and self.wire_id > other.wire_id))
def __ge__(self, other):
# :nodoc: Delegate comparison to keys.
return (self.key > other.key or
(self.key == other.key and self.wire_id >= other.wire_id))
def __eq__(self, other):
# :nodoc: Delegate comparison to keys.
return self.key == other.key and self.wire_id == other.wire_id
def __ne__(self, other):
# :nodoc: Delegate comparison to keys.
return self.key == other.key and self.wire_id == other.wire_id
def __hash__(self):
# :nodoc: Delegate comparison to keys.
return hash([self.key, self.wire_id])
def __repr__(self):
# :nodoc: nicer formatting to help with debugging
return '<key: ' + str(self.key) + ' wire: ' + str(self.wire) + '>'
class KeyWirePairL(KeyWirePair):
"""A KeyWirePair that is used as the low end of a range query.
This KeyWirePair is smaller than all other KeyWirePairs with the same key."""
def __init__(self, key):
self.key = key
self.wire = None
self.wire_id = -1000000000
class KeyWirePairH(KeyWirePair):
"""A KeyWirePair that is used as the high end of a range query.
This KeyWirePair is larger than all other KeyWirePairs with the same key."""
def __init__(self, key):
self.key = key
self.wire = None
# HACK(pwnall): assuming 1 billion objects won't fit into RAM.
self.wire_id = 1000000000
class CrossVerifier(object):
"""Checks whether a wire network has any crossing wires."""
def __init__(self, layer):
"""Verifier for a layer of wires.
Once created, the verifier can list the crossings between wires (the
wire_crossings method) or count the crossings (count_crossings)."""
self.events = []
self._events_from_layer(layer)
self.events.sort()
self.index = RangeIndex()
self.result_set = ResultSet()
self.performed = False
def count_crossings(self):
"""Returns the number of pairs of wires that cross each other."""
if self.performed:
raise
self.performed = True
return self._compute_crossings(True)
def wire_crossings(self):
"""An array of pairs of wires that cross each other."""
if self.performed:
raise
self.performed = True
return self._compute_crossings(False)
def _events_from_layer(self, layer):
"""Populates the sweep line events from the wire layer."""
for wire in layer.wires.values():
if wire.is_horizontal():
self.events.append([wire.x1, 0, wire.object_id, 'add', wire])
self.events.append([wire.x2, 2, wire.object_id, 'remove', wire])
else:
self.events.append([wire.x1, 1, wire.object_id, 'query', wire])
def _compute_crossings(self, count_only):
"""Implements count_crossings and wire_crossings."""
if count_only:
result = 0
else:
result = self.result_set
for event in self.events:
event_x, event_type, wire = event[0], event[3], event[4]
if event_type == 'add':
self.index.add(KeyWirePair(wire.y1, wire))
elif event_type == 'remove':
self.index.remove(KeyWirePair(wire.y1, wire))
elif event_type == 'query':
self.trace_sweep_line(event_x)
cross_wires = []
for kwp in self.index.list(KeyWirePairL(wire.y1),
KeyWirePairH(wire.y2)):
cross_wires.append(kwp.wire)
if count_only:
result += len(cross_wires)
else:
for cross_wire in cross_wires:
result.add_crossing(wire, cross_wire)
return result
def trace_sweep_line(self, x):
"""When tracing is enabled, adds info about where the sweep line is.
Args:
x: the coordinate of the vertical sweep line
"""
# NOTE: this is overridden in TracedCrossVerifier
pass
class TracedCrossVerifier(CrossVerifier):
"""Augments CrossVerifier to build a trace for the visualizer."""
def __init__(self, layer):
CrossVerifier.__init__(self, layer)
self.trace = []
self.index = TracedRangeIndex(self.trace)
self.result_set = TracedResultSet(self.trace)
def trace_sweep_line(self, x):
self.trace.append({'type': 'sweep', 'x': x})
def trace_as_json(self):
"""List that obeys the JSON format restrictions with the verifier trace."""
return self.trace
# Command-line controller.
if __name__ == '__main__':
import sys
layer = WireLayer.from_file(sys.stdin)
verifier = CrossVerifier(layer)
if os.environ.get('TRACE') == 'jsonp':
verifier = TracedCrossVerifier(layer)
result = verifier.wire_crossings()
json_obj = {'layer': layer.as_json(), 'trace': verifier.trace_as_json()}
sys.stdout.write('onJsonp(')
json.dump(json_obj, sys.stdout)
sys.stdout.write(');\n')
elif os.environ.get('TRACE') == 'list':
verifier.wire_crossings().write_to_file(sys.stdout)
else:
sys.stdout.write(str(verifier.count_crossings()) + "\n")