<s> from six . moves import intern <EOL> class CircularListType ( object ) : <EOL> """<STR_LIT>""" <EOL> class List ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> def __init__ ( self , list_type ) : <EOL> self . list_type = list_type <EOL> self . head = self <EOL> self . list_type . node_init ( self . head ) <EOL> def __getattr__ ( self , name ) : <EOL> if ( name == self . list_type . next_name ) : <EOL> return self . _next_slot_ <EOL> if ( name == self . list_type . prev_name ) : <EOL> return self . _prev_slot_ <EOL> raise AttributeError ( name ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if ( name in CircularListType . List . __slots__ ) : <EOL> object . __setattr__ ( self , name , value ) <EOL> return <EOL> if ( name == self . list_type . next_name ) : <EOL> self . _next_slot_ = value <EOL> return <EOL> if ( name == self . list_type . prev_name ) : <EOL> self . _prev_slot_ = value <EOL> return <EOL> raise AttributeError ( name ) <EOL> def is_empty ( self ) : <EOL> return not self . list_type . node_is_on_list ( self . head ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> for node in self : <EOL> self . remove ( node ) <EOL> def is_on_list ( self , node ) : <EOL> return self . list_type . node_is_on_list ( node ) <EOL> def append ( self , node ) : <EOL> self . list_type . node_insert_before ( self . head , node ) <EOL> def prepend ( self , node ) : <EOL> self . list_type . node_insert_after ( self . head , node ) <EOL> def __iter__ ( self ) : <EOL> return self . generator ( ) <EOL> def remove ( self , node ) : <EOL> """<STR_LIT>""" <EOL> self . list_type . node_unlink ( node ) <EOL> self . list_type . node_del_attrs ( node ) <EOL> def pop_first ( self ) : <EOL> """<STR_LIT>""" <EOL> node = self . list_type . node_next ( self . head ) <EOL> if ( node is self . head ) : <EOL> return None <EOL> self . remove ( node ) <EOL> return node <EOL> def generator ( self ) : <EOL> """<STR_LIT>""" <EOL> node = self . list_type . node_next ( self . head ) <EOL> next = self . list_type . node_next ( node ) <EOL> while ( node is not self . head ) : <EOL> yield node <EOL> node = next <EOL> next = self . list_type . node_next ( node ) <EOL> def __init__ ( self , next_attr_name = None , prev_attr_name = None ) : <EOL> """<STR_LIT>""" <EOL> self . next_name = intern ( next_attr_name ) <EOL> self . prev_name = intern ( prev_attr_name ) <EOL> def create ( self ) : <EOL> return CircularListType . List ( self ) <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . create ( ) <EOL> def node_init ( self , node ) : <EOL> assert ( not self . node_is_on_list ( node ) ) <EOL> self . node_set_next ( node , node ) <EOL> self . node_set_prev ( node , node ) <EOL> def node_next ( self , node ) : <EOL> try : <EOL> return getattr ( node , self . next_name ) <EOL> except AttributeError : <EOL> return None <EOL> def node_set_next ( self , node , next ) : <EOL> setattr ( node , self . next_name , next ) <EOL> def node_prev ( self , node ) : <EOL> try : <EOL> return getattr ( node , self . prev_name ) <EOL> except AttributeError : <EOL> return None <EOL> def node_set_prev ( self , node , prev ) : <EOL> setattr ( node , self . prev_name , prev ) <EOL> def node_del_attrs ( self , node ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> delattr ( node , self . next_name ) <EOL> delattr ( node , self . prev_name ) <EOL> except AttributeError : <EOL> pass <EOL> def node_is_on_list ( self , node ) : <EOL> """<STR_LIT>""" <EOL> next = self . node_next ( node ) <EOL> if next == node or next is None : <EOL> assert ( self . node_prev ( node ) is next ) <EOL> return False <EOL> return True <EOL> def node_insert_after ( self , node , new_node ) : <EOL> """<STR_LIT>""" <EOL> assert ( not self . node_is_on_list ( new_node ) ) <EOL> assert ( node is not new_node ) <EOL> next = self . node_next ( node ) <EOL> assert ( next is not None ) <EOL> self . node_set_next ( node , new_node ) <EOL> self . node_set_prev ( new_node , node ) <EOL> self . node_set_next ( new_node , next ) <EOL> self . node_set_prev ( next , new_node ) <EOL> def node_insert_before ( self , node , new_node ) : <EOL> """<STR_LIT>""" <EOL> assert ( not self . node_is_on_list ( new_node ) ) <EOL> assert ( node is not new_node ) <EOL> prev = self . node_prev ( node ) <EOL> assert ( prev is not None ) <EOL> self . node_set_prev ( node , new_node ) <EOL> self . node_set_next ( new_node , node ) <EOL> self . node_set_prev ( new_node , prev ) <EOL> self . node_set_next ( prev , new_node ) <EOL> def node_unlink ( self , node ) : <EOL> if not self . node_is_on_list ( node ) : <EOL> return <EOL> prev = self . node_prev ( node ) <EOL> next = self . node_next ( node ) <EOL> self . node_set_next ( prev , next ) <EOL> self . node_set_prev ( next , prev ) <EOL> self . node_set_next ( node , node ) <EOL> self . node_set_prev ( node , node ) </s>
