<s> from pypy . rpython . lltypesystem . lltype import Ptr , GcStruct , Signed , malloc , Void <EOL> from pypy . rpython . rrange import AbstractRangeRepr , AbstractRangeIteratorRepr <EOL> def ll_length ( l ) : <EOL> if l . step > <NUM_LIT:0> : <EOL> lo = l . start <EOL> hi = l . stop <EOL> step = l . step <EOL> else : <EOL> lo = l . stop <EOL> hi = l . start <EOL> step = - l . step <EOL> if hi <= lo : <EOL> return <NUM_LIT:0> <EOL> n = ( hi - lo - <NUM_LIT:1> ) // step + <NUM_LIT:1> <EOL> return n <EOL> def ll_getitem_fast ( l , index ) : <EOL> return l . start + index * l . step <EOL> RANGEST = GcStruct ( "<STR_LIT>" , ( "<STR_LIT:start>" , Signed ) , ( "<STR_LIT>" , Signed ) , ( "<STR_LIT>" , Signed ) , <EOL> adtmeths = { <EOL> "<STR_LIT>" : ll_length , <EOL> "<STR_LIT>" : ll_getitem_fast , <EOL> } , <EOL> hints = { '<STR_LIT>' : True } ) <EOL> RANGESTITER = GcStruct ( "<STR_LIT>" , ( "<STR_LIT>" , Signed ) , ( "<STR_LIT>" , Signed ) , ( "<STR_LIT>" , Signed ) ) <EOL> class RangeRepr ( AbstractRangeRepr ) : <EOL> RANGEST = Ptr ( RANGEST ) <EOL> RANGESTITER = Ptr ( RANGESTITER ) <EOL> getfield_opname = "<STR_LIT>" <EOL> def __init__ ( self , step , * args ) : <EOL> self . RANGE = Ptr ( GcStruct ( "<STR_LIT>" , ( "<STR_LIT:start>" , Signed ) , ( "<STR_LIT>" , Signed ) , <EOL> adtmeths = { <EOL> "<STR_LIT>" : ll_length , <EOL> "<STR_LIT>" : ll_getitem_fast , <EOL> "<STR_LIT>" : step , <EOL> } , <EOL> hints = { '<STR_LIT>' : True } ) ) <EOL> self . RANGEITER = Ptr ( GcStruct ( "<STR_LIT>" , ( "<STR_LIT>" , Signed ) , ( "<STR_LIT>" , Signed ) ) ) <EOL> AbstractRangeRepr . __init__ ( self , step , * args ) <EOL> self . ll_newrange = ll_newrange <EOL> self . ll_newrangest = ll_newrangest <EOL> def make_iterator_repr ( self ) : <EOL> return RangeIteratorRepr ( self ) <EOL> def ll_newrange ( RANGE , start , stop ) : <EOL> l = malloc ( RANGE . TO ) <EOL> l . start = start <EOL> l . stop = stop <EOL> return l <EOL> def ll_newrangest ( start , stop , step ) : <EOL> if step == <NUM_LIT:0> : <EOL> raise ValueError <EOL> l = malloc ( RANGEST ) <EOL> l . start = start <EOL> l . stop = stop <EOL> l . step = step <EOL> return l <EOL> class RangeIteratorRepr ( AbstractRangeIteratorRepr ) : <EOL> def __init__ ( self , * args ) : <EOL> AbstractRangeIteratorRepr . __init__ ( self , * args ) <EOL> self . ll_rangeiter = ll_rangeiter <EOL> def ll_rangeiter ( ITERPTR , rng ) : <EOL> iter = malloc ( ITERPTR . TO ) <EOL> iter . next = rng . start <EOL> iter . stop = rng . stop <EOL> if ITERPTR . TO is RANGESTITER : <EOL> iter . step = rng . step <EOL> return iter </s>
