<s> """<STR_LIT>""" <EOL> from struct import calcsize , pack , pack_into , unpack_from <EOL> import operator <EOL> try : <EOL> import _rawffi <EOL> _RAWARRAY = _rawffi . Array ( '<STR_LIT:c>' ) <EOL> def bytebuffer ( size ) : <EOL> return _RAWARRAY ( size , autofree = True ) <EOL> def getbufaddress ( buf ) : <EOL> return buf . buffer <EOL> except ImportError : <EOL> try : <EOL> from __pypy__ import bytebuffer <EOL> def getbufaddress ( buf ) : <EOL> return <NUM_LIT:0> <EOL> except ImportError : <EOL> import ctypes <EOL> bytebuffer = ctypes . create_string_buffer <EOL> def getbufaddress ( buf ) : <EOL> voidp = ctypes . cast ( ctypes . pointer ( buf ) , ctypes . c_void_p ) <EOL> return voidp . value <EOL> TYPECODES = "<STR_LIT>" <EOL> class array ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __new__ ( cls , typecode , initializer = [ ] , ** extrakwds ) : <EOL> self = object . __new__ ( cls ) <EOL> if cls is array and extrakwds : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if not isinstance ( typecode , str ) or len ( typecode ) != <NUM_LIT:1> : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % type ( typecode ) ) <EOL> if typecode not in TYPECODES : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( TYPECODES ) ) <EOL> self . _data = bytebuffer ( <NUM_LIT:0> ) <EOL> self . typecode = typecode <EOL> self . itemsize = calcsize ( typecode ) <EOL> if isinstance ( initializer , list ) : <EOL> self . fromlist ( initializer ) <EOL> elif isinstance ( initializer , str ) : <EOL> self . fromstring ( initializer ) <EOL> elif isinstance ( initializer , unicode ) and self . typecode == "<STR_LIT:u>" : <EOL> self . fromunicode ( initializer ) <EOL> else : <EOL> self . extend ( initializer ) <EOL> return self <EOL> def _clear ( self ) : <EOL> self . _data = bytebuffer ( <NUM_LIT:0> ) <EOL> def fromfile ( self , f , n ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( f , file ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> size = self . itemsize * n <EOL> item = f . read ( size ) <EOL> if len ( item ) < size : <EOL> raise EOFError ( "<STR_LIT>" ) <EOL> self . fromstring ( item ) <EOL> def fromlist ( self , l ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( l , list ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . _fromiterable ( l ) <EOL> def fromstring ( self , s ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , unicode ) : <EOL> s = str ( s ) <EOL> self . _frombuffer ( s ) <EOL> def _frombuffer ( self , s ) : <EOL> length = len ( s ) <EOL> if length % self . itemsize != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> boundary = len ( self . _data ) <EOL> newdata = bytebuffer ( boundary + length ) <EOL> newdata [ : boundary ] = self . _data <EOL> newdata [ boundary : ] = s <EOL> self . _data = newdata <EOL> def fromunicode ( self , ustr ) : <EOL> """<STR_LIT>""" <EOL> if not self . typecode == "<STR_LIT:u>" : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if not isinstance ( ustr , unicode ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _frombuffer ( buffer ( ustr ) ) <EOL> def tofile ( self , f ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( f , file ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> f . write ( self . tostring ( ) ) <EOL> def tolist ( self ) : <EOL> """<STR_LIT>""" <EOL> count = len ( self . _data ) // self . itemsize <EOL> return list ( unpack_from ( '<STR_LIT>' % ( count , self . typecode ) , self . _data ) ) <EOL> def tostring ( self ) : <EOL> return self . _data [ : ] <EOL> def __buffer__ ( self ) : <EOL> return buffer ( self . _data ) <EOL> def tounicode ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . typecode != "<STR_LIT:u>" : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return u"<STR_LIT>" . join ( self . tolist ( ) ) <EOL> def byteswap ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . itemsize not in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> ] : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> itemsize = self . itemsize <EOL> bytes = self . _data <EOL> for start in range ( <NUM_LIT:0> , len ( bytes ) , itemsize ) : <EOL> stop = start + itemsize <EOL> bytes [ start : stop ] = bytes [ start : stop ] [ : : - <NUM_LIT:1> ] <EOL> def buffer_info ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( getbufaddress ( self . _data ) , len ( self ) ) <EOL> read = fromfile <EOL> write = tofile <EOL> def __repr__ ( self ) : <EOL> if len ( self . _data ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" % self . typecode <EOL> elif self . typecode == "<STR_LIT:c>" : <EOL> return "<STR_LIT>" % ( self . typecode , repr ( self . tostring ( ) ) ) <EOL> elif self . typecode == "<STR_LIT:u>" : <EOL> return "<STR_LIT>" % ( self . typecode , repr ( self . tounicode ( ) ) ) <EOL> else : <EOL> return "<STR_LIT>" % ( self . typecode , repr ( self . tolist ( ) ) ) <EOL> def __copy__ ( self ) : <EOL> a = array ( self . typecode ) <EOL> a . _data = bytebuffer ( len ( self . _data ) ) <EOL> a . _data [ : ] = self . _data <EOL> return a <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> return NotImplemented <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return buffer ( self . _data ) == buffer ( other . _data ) <EOL> else : <EOL> return self . tolist ( ) == other . tolist ( ) <EOL> def __ne__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> return NotImplemented <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return buffer ( self . _data ) != buffer ( other . _data ) <EOL> else : <EOL> return self . tolist ( ) != other . tolist ( ) <EOL> def __lt__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> return NotImplemented <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return buffer ( self . _data ) < buffer ( other . _data ) <EOL> else : <EOL> return self . tolist ( ) < other . tolist ( ) <EOL> def __gt__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> return NotImplemented <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return buffer ( self . _data ) > buffer ( other . _data ) <EOL> else : <EOL> return self . tolist ( ) > other . tolist ( ) <EOL> def __le__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> return NotImplemented <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return buffer ( self . _data ) <= buffer ( other . _data ) <EOL> else : <EOL> return self . tolist ( ) <= other . tolist ( ) <EOL> def __ge__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> return NotImplemented <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return buffer ( self . _data ) >= buffer ( other . _data ) <EOL> else : <EOL> return self . tolist ( ) >= other . tolist ( ) <EOL> def __reduce__ ( self ) : <EOL> dict = getattr ( self , '<STR_LIT>' , None ) <EOL> data = self . tostring ( ) <EOL> if data : <EOL> initargs = ( self . typecode , data ) <EOL> else : <EOL> initargs = ( self . typecode , ) <EOL> return ( type ( self ) , initargs , dict ) <EOL> def append ( self , x ) : <EOL> """<STR_LIT>""" <EOL> self . _frombuffer ( pack ( self . typecode , x ) ) <EOL> def count ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return operator . countOf ( self , x ) <EOL> def extend ( self , iterable ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( iterable , array ) and not self . typecode == iterable . typecode : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . _fromiterable ( iterable ) <EOL> def index ( self , x ) : <EOL> """<STR_LIT>""" <EOL> return operator . indexOf ( self , x ) <EOL> def insert ( self , i , x ) : <EOL> """<STR_LIT>""" <EOL> seqlength = len ( self ) <EOL> if i < <NUM_LIT:0> : <EOL> i += seqlength <EOL> if i < <NUM_LIT:0> : <EOL> i = <NUM_LIT:0> <EOL> elif i > seqlength : <EOL> i = seqlength <EOL> boundary = i * self . itemsize <EOL> data = pack ( self . typecode , x ) <EOL> newdata = bytebuffer ( len ( self . _data ) + len ( data ) ) <EOL> newdata [ : boundary ] = self . _data [ : boundary ] <EOL> newdata [ boundary : boundary + self . itemsize ] = data <EOL> newdata [ boundary + self . itemsize : ] = self . _data [ boundary : ] <EOL> self . _data = newdata <EOL> def pop ( self , i = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> seqlength = len ( self ) <EOL> if i < <NUM_LIT:0> : <EOL> i += seqlength <EOL> if not ( <NUM_LIT:0> <= i < seqlength ) : <EOL> raise IndexError ( i ) <EOL> boundary = i * self . itemsize <EOL> result = unpack_from ( self . typecode , self . _data , boundary ) [ <NUM_LIT:0> ] <EOL> newdata = bytebuffer ( len ( self . _data ) - self . itemsize ) <EOL> newdata [ : boundary ] = self . _data [ : boundary ] <EOL> newdata [ boundary : ] = self . _data [ boundary + self . itemsize : ] <EOL> self . _data = newdata <EOL> return result <EOL> def remove ( self , x ) : <EOL> """<STR_LIT>""" <EOL> self . pop ( self . index ( x ) ) <EOL> def reverse ( self ) : <EOL> """<STR_LIT>""" <EOL> lst = self . tolist ( ) <EOL> lst . reverse ( ) <EOL> self . _clear ( ) <EOL> self . fromlist ( lst ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _data ) // self . itemsize <EOL> def __add__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if self . typecode != other . typecode : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return array ( self . typecode , buffer ( self . _data ) + buffer ( other . _data ) ) <EOL> def __mul__ ( self , repeat ) : <EOL> return array ( self . typecode , buffer ( self . _data ) * repeat ) <EOL> __rmul__ = __mul__ <EOL> def __getitem__ ( self , i ) : <EOL> seqlength = len ( self ) <EOL> if isinstance ( i , slice ) : <EOL> start , stop , step = i . indices ( seqlength ) <EOL> if step != <NUM_LIT:1> : <EOL> sublist = self . tolist ( ) [ i ] <EOL> return array ( self . typecode , sublist ) <EOL> if start < <NUM_LIT:0> : <EOL> start = <NUM_LIT:0> <EOL> if stop < start : <EOL> stop = start <EOL> assert stop <= seqlength <EOL> return array ( self . typecode , self . _data [ start * self . itemsize : <EOL> stop * self . itemsize ] ) <EOL> else : <EOL> if i < <NUM_LIT:0> : <EOL> i += seqlength <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> return self . _data [ i ] <EOL> if not ( <NUM_LIT:0> <= i < seqlength ) : <EOL> raise IndexError ( i ) <EOL> boundary = i * self . itemsize <EOL> return unpack_from ( self . typecode , self . _data , boundary ) [ <NUM_LIT:0> ] <EOL> def __getslice__ ( self , i , j ) : <EOL> return self . __getitem__ ( slice ( i , j ) ) <EOL> def __setitem__ ( self , i , x ) : <EOL> if isinstance ( i , slice ) : <EOL> if ( not isinstance ( x , array ) <EOL> or self . typecode != x . typecode ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> seqlength = len ( self ) <EOL> start , stop , step = i . indices ( seqlength ) <EOL> if step != <NUM_LIT:1> : <EOL> sublist = self . tolist ( ) <EOL> sublist [ i ] = x . tolist ( ) <EOL> self . _clear ( ) <EOL> self . fromlist ( sublist ) <EOL> return <EOL> if start < <NUM_LIT:0> : <EOL> start = <NUM_LIT:0> <EOL> if stop < start : <EOL> stop = start <EOL> assert stop <= seqlength <EOL> boundary1 = start * self . itemsize <EOL> boundary2 = stop * self . itemsize <EOL> boundary2new = boundary1 + len ( x . _data ) <EOL> if boundary2 == boundary2new : <EOL> self . _data [ boundary1 : boundary2 ] = x . _data <EOL> else : <EOL> newdata = bytebuffer ( len ( self . _data ) + boundary2new - boundary2 ) <EOL> newdata [ : boundary1 ] = self . _data [ : boundary1 ] <EOL> newdata [ boundary1 : boundary2new ] = x . _data <EOL> newdata [ boundary2new : ] = self . _data [ boundary2 : ] <EOL> self . _data = newdata <EOL> else : <EOL> seqlength = len ( self ) <EOL> if i < <NUM_LIT:0> : <EOL> i += seqlength <EOL> if self . typecode == '<STR_LIT:c>' : <EOL> self . _data [ i ] = x <EOL> return <EOL> if not ( <NUM_LIT:0> <= i < seqlength ) : <EOL> raise IndexError ( i ) <EOL> boundary = i * self . itemsize <EOL> pack_into ( self . typecode , self . _data , boundary , x ) <EOL> def __setslice__ ( self , i , j , x ) : <EOL> self . __setitem__ ( slice ( i , j ) , x ) <EOL> def __delitem__ ( self , i ) : <EOL> if isinstance ( i , slice ) : <EOL> seqlength = len ( self ) <EOL> start , stop , step = i . indices ( seqlength ) <EOL> if start < <NUM_LIT:0> : <EOL> start = <NUM_LIT:0> <EOL> if stop < start : <EOL> stop = start <EOL> assert stop <= seqlength <EOL> if step != <NUM_LIT:1> : <EOL> sublist = self . tolist ( ) <EOL> del sublist [ i ] <EOL> self . _clear ( ) <EOL> self . fromlist ( sublist ) <EOL> return <EOL> dellength = stop - start <EOL> boundary1 = start * self . itemsize <EOL> boundary2 = stop * self . itemsize <EOL> newdata = bytebuffer ( len ( self . _data ) - ( boundary2 - boundary1 ) ) <EOL> newdata [ : boundary1 ] = self . _data [ : boundary1 ] <EOL> newdata [ boundary1 : ] = self . _data [ boundary2 : ] <EOL> self . _data = newdata <EOL> else : <EOL> seqlength = len ( self ) <EOL> if i < <NUM_LIT:0> : <EOL> i += seqlength <EOL> if not ( <NUM_LIT:0> <= i < seqlength ) : <EOL> raise IndexError ( i ) <EOL> boundary = i * self . itemsize <EOL> newdata = bytebuffer ( len ( self . _data ) - self . itemsize ) <EOL> newdata [ : boundary ] = self . _data [ : boundary ] <EOL> newdata [ boundary : ] = self . _data [ boundary + self . itemsize : ] <EOL> self . _data = newdata <EOL> def __delslice__ ( self , i , j ) : <EOL> self . __delitem__ ( slice ( i , j ) ) <EOL> def __contains__ ( self , item ) : <EOL> for x in self : <EOL> if x == item : <EOL> return True <EOL> return False <EOL> def __iadd__ ( self , other ) : <EOL> if not isinstance ( other , array ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . extend ( other ) <EOL> return self <EOL> def __imul__ ( self , repeat ) : <EOL> newdata = buffer ( self . _data ) * repeat <EOL> self . _data = bytebuffer ( len ( newdata ) ) <EOL> self . _data [ : ] = newdata <EOL> return self <EOL> def __iter__ ( self ) : <EOL> p = <NUM_LIT:0> <EOL> typecode = self . typecode <EOL> itemsize = self . itemsize <EOL> while p < len ( self . _data ) : <EOL> yield unpack_from ( typecode , self . _data , p ) [ <NUM_LIT:0> ] <EOL> p += itemsize <EOL> def _fromiterable ( self , iterable ) : <EOL> iterable = tuple ( iterable ) <EOL> n = len ( iterable ) <EOL> boundary = len ( self . _data ) <EOL> newdata = bytebuffer ( boundary + n * self . itemsize ) <EOL> newdata [ : boundary ] = self . _data <EOL> pack_into ( '<STR_LIT>' % ( n , self . typecode ) , newdata , boundary , * iterable ) <EOL> self . _data = newdata <EOL> ArrayType = array </s>
