Debian Codegraph @

rpython/rlib/unroll.py

raw symbols SCIP
1 from rpython.tool.uid import uid
2
3 # Support for explicit specialization: in code using global constants
4 # that are instances of SpecTag, code paths are not merged when
5 # the same variable holds a different SpecTag instance.
6
7 class SpecTag(object):
8 __slots__ = ()
9
10 def __repr__(self):
11 return '%s(0x%x)' % (self.__class__.__name__, uid(self))
12
13 def _freeze_(self):
14 return True
15
16
17 # 'for' iteration over iterables wrapped in an instance
18 # of unrolling_iterable will be unrolled by the flow space,
19 # like in:
20 # names = unrolling_iterable(['a', 'b', 'c'])
21 # def f(x):
22 # for name in names:
23 # setattr(x, name, 0)
24
25 class unrolling_iterable(SpecTag):
26
27 def __init__(self, iterable):
28 self._items = list(iterable)
29 self._head = _unroller(self._items)
30
31 def __iter__(self):
32 return iter(self._items)
33
34 def get_unroller(self):
35 return self._head
36
37
38 class _unroller(SpecTag):
39
40 def __init__(self, items, i=0):
41 self._items = items
42 self._i = i
43 self._next = None
44
45 def step(self):
46 v = self._items[self._i]
47 if self._next is None:
48 self._next = _unroller(self._items, self._i+1)
49 return v, self._next