<s> """<STR_LIT>""" <EOL> from copy import deepcopy <EOL> class range_t ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , initset = set ( ) ) : <EOL> self . __has = set ( ) <EOL> if not isinstance ( initset , set ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> for t in initset : <EOL> if not isinstance ( t , tuple ) or len ( t ) != <NUM_LIT:2> or t [ <NUM_LIT:1> ] <= t [ <NUM_LIT:0> ] or t [ <NUM_LIT:1> ] < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . __has = initset <EOL> self . __optimize ( ) <EOL> def __match_l ( self , k , _set ) : <EOL> """<STR_LIT>""" <EOL> return { r for r in _set if k [ <NUM_LIT:0> ] in range ( * r ) or k [ <NUM_LIT:1> ] in range ( * r ) or ( k [ <NUM_LIT:0> ] < r [ <NUM_LIT:0> ] and k [ <NUM_LIT:1> ] >= r [ <NUM_LIT:1> ] ) } <EOL> def __optimize ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> for ( begin , end ) in sorted ( self . __has ) : <EOL> if ret and begin <= ret [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] < end : <EOL> ret [ - <NUM_LIT:1> ] = ( ret [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , end ) <EOL> elif not ret or begin > ret [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] : <EOL> ret . append ( ( begin , end ) ) <EOL> self . __has = set ( ret ) <EOL> def __val_convert ( self , val ) : <EOL> """<STR_LIT>""" <EOL> converted = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if isinstance ( val , range ) and <NUM_LIT:0> <= val . start < val . stop and val . step == <NUM_LIT:1> : <EOL> converted = ( val . start , val . stop ) <EOL> elif ( isinstance ( val , tuple ) or isinstance ( val , list ) ) and <NUM_LIT:0> <= val [ <NUM_LIT:0> ] < val [ <NUM_LIT:1> ] and len ( val ) == <NUM_LIT:2> : <EOL> converted = val <EOL> elif isinstance ( val , int ) : <EOL> converted = ( val , val + <NUM_LIT:1> ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return converted <EOL> def contains ( self , val ) : <EOL> """<STR_LIT>""" <EOL> ( start , end ) = self . __val_convert ( val ) <EOL> retlen = <NUM_LIT:0> <EOL> for r in self . __has : <EOL> if start < r [ <NUM_LIT:1> ] and end > r [ <NUM_LIT:0> ] : <EOL> retlen += ( ( end < r [ <NUM_LIT:1> ] and end ) or r [ <NUM_LIT:1> ] ) - ( ( start > r [ <NUM_LIT:0> ] and start ) or r [ <NUM_LIT:0> ] ) <EOL> return retlen <EOL> def __contains__ ( self , val ) : <EOL> """<STR_LIT>""" <EOL> conv = self . __val_convert ( val ) <EOL> return self . contains ( val ) == conv [ <NUM_LIT:1> ] - conv [ <NUM_LIT:0> ] <EOL> def match ( self , val ) : <EOL> """<STR_LIT>""" <EOL> conv = self . __val_convert ( val ) <EOL> return self . __match_l ( conv , self . __has ) <EOL> def toset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __has <EOL> def __add ( self , val ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( val , range_t ) : <EOL> val = { self . __val_convert ( val ) } <EOL> else : <EOL> val = val . toset ( ) <EOL> __has = deepcopy ( self . __has ) <EOL> __has . update ( val ) <EOL> return __has <EOL> def __add__ ( self , val ) : <EOL> """<STR_LIT>""" <EOL> return range_t ( self . __add ( val ) ) <EOL> def __iadd__ ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . __has = self . __add ( val ) <EOL> self . __optimize ( ) <EOL> return self <EOL> def __sub__ ( self , val ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( val , range_t ) : <EOL> val = { self . __val_convert ( val ) } <EOL> else : <EOL> val = val . toset ( ) <EOL> __has = deepcopy ( self . __has ) <EOL> for v in val : <EOL> common = self . __match_l ( v , __has ) <EOL> if not common : continue <EOL> __has . difference_update ( common ) <EOL> minmax = ( min ( { l [ <NUM_LIT:0> ] for l in common } ) , max ( { r [ <NUM_LIT:1> ] for r in common } ) ) <EOL> if minmax [ <NUM_LIT:0> ] < v [ <NUM_LIT:0> ] : __has . add ( ( minmax [ <NUM_LIT:0> ] , v [ <NUM_LIT:0> ] ) ) <EOL> if minmax [ <NUM_LIT:1> ] > v [ <NUM_LIT:1> ] : __has . add ( ( v [ <NUM_LIT:1> ] , minmax [ <NUM_LIT:1> ] ) ) <EOL> return range_t ( __has ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = <NUM_LIT:0> <EOL> for t in self . __has : <EOL> ret += t [ <NUM_LIT:1> ] - t [ <NUM_LIT:0> ] <EOL> return ret <EOL> def __eq__ ( self , val ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( val , range_t ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return self . __has == val . toset ( ) </s>
