File: unroll.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (49 lines) | stat: -rw-r--r-- 1,237 bytes parent folder | download | duplicates (9)
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
from rpython.tool.uid import uid

# Support for explicit specialization: in code using global constants
# that are instances of SpecTag, code paths are not merged when
# the same variable holds a different SpecTag instance.

class SpecTag(object):
    __slots__ = ()

    def __repr__(self):
        return '%s(0x%x)' % (self.__class__.__name__, uid(self))

    def _freeze_(self):
        return True


# 'for' iteration over iterables wrapped in an instance
# of unrolling_iterable will be unrolled by the flow space,
# like in:
#     names = unrolling_iterable(['a', 'b', 'c'])
#     def f(x):
#         for name in names:
#             setattr(x, name, 0)

class unrolling_iterable(SpecTag):

    def __init__(self, iterable):
        self._items = list(iterable)
        self._head = _unroller(self._items)

    def __iter__(self):
        return iter(self._items)

    def get_unroller(self):
        return self._head


class _unroller(SpecTag):

    def __init__(self, items, i=0):
        self._items = items
        self._i = i
        self._next = None

    def step(self):
        v = self._items[self._i]
        if self._next is None:
            self._next = _unroller(self._items, self._i+1)
        return v, self._next