<s> from pypy . rpython . lltypesystem import lltype , llmemory <EOL> from pypy . rpython . lltypesystem import rdict <EOL> from pypy . rlib . objectmodel import we_are_translated <EOL> alloc_count = <NUM_LIT:0> <EOL> def count_alloc ( delta ) : <EOL> "<STR_LIT>" <EOL> global alloc_count <EOL> alloc_count += delta <EOL> def newdict ( length_estimate = <NUM_LIT:0> ) : <EOL> return rdict . ll_newdict_size ( DICT , length_estimate ) <EOL> def dict_allocate ( ) : <EOL> if not we_are_translated ( ) : count_alloc ( + <NUM_LIT:1> ) <EOL> return lltype . malloc ( DICT , flavor = "<STR_LIT>" ) <EOL> def dict_delete ( d ) : <EOL> dict_delete_entries ( d . entries ) <EOL> lltype . free ( d , flavor = "<STR_LIT>" ) <EOL> if not we_are_translated ( ) : count_alloc ( - <NUM_LIT:1> ) <EOL> def dict_allocate_entries ( n ) : <EOL> if not we_are_translated ( ) : count_alloc ( + <NUM_LIT:1> ) <EOL> entries = lltype . malloc ( ENTRIES , n , flavor = "<STR_LIT>" ) <EOL> i = <NUM_LIT:0> <EOL> while i < n : <EOL> entries [ i ] . key = llmemory . NULL <EOL> i += <NUM_LIT:1> <EOL> return entries <EOL> def dict_delete_entries ( entries ) : <EOL> lltype . free ( entries , flavor = "<STR_LIT>" ) <EOL> if not we_are_translated ( ) : count_alloc ( - <NUM_LIT:1> ) <EOL> _hash = llmemory . cast_adr_to_int <EOL> def dict_keyhash ( d , key ) : <EOL> return _hash ( key ) <EOL> def dict_entry_valid ( entries , i ) : <EOL> return entries [ i ] . key != llmemory . NULL <EOL> def dict_entry_hash ( entries , i ) : <EOL> return _hash ( entries [ i ] . key ) <EOL> def dict_get ( d , key , default = llmemory . NULL ) : <EOL> return rdict . ll_get ( d , key , default ) <EOL> def dict_add ( d , key ) : <EOL> rdict . ll_dict_setitem ( d , key , llmemory . NULL ) <EOL> def dict_insertclean ( d , key , value ) : <EOL> rdict . ll_dict_insertclean ( d , key , value , _hash ( key ) ) <EOL> def dict_foreach ( d , callback , arg ) : <EOL> entries = d . entries <EOL> i = len ( entries ) - <NUM_LIT:1> <EOL> while i >= <NUM_LIT:0> : <EOL> if dict_entry_valid ( entries , i ) : <EOL> callback ( entries [ i ] . key , entries [ i ] . value , arg ) <EOL> i -= <NUM_LIT:1> <EOL> dict_foreach . _annspecialcase_ = '<STR_LIT>' <EOL> ENTRY = lltype . Struct ( '<STR_LIT>' , ( '<STR_LIT:key>' , llmemory . Address ) , <EOL> ( '<STR_LIT:value>' , llmemory . Address ) ) <EOL> ENTRIES = lltype . Array ( ENTRY , <EOL> adtmeths = { <EOL> '<STR_LIT>' : dict_allocate_entries , <EOL> '<STR_LIT>' : dict_delete_entries , <EOL> '<STR_LIT>' : dict_entry_valid , <EOL> '<STR_LIT>' : dict_entry_valid , <EOL> '<STR_LIT>' : dict_entry_hash , <EOL> } ) <EOL> DICT = lltype . Struct ( '<STR_LIT>' , ( '<STR_LIT>' , lltype . Ptr ( ENTRIES ) ) , <EOL> ( '<STR_LIT>' , lltype . Signed ) , <EOL> ( '<STR_LIT>' , lltype . Signed ) , <EOL> adtmeths = { <EOL> '<STR_LIT>' : dict_allocate , <EOL> '<STR_LIT>' : dict_delete , <EOL> '<STR_LIT>' : rdict . ll_dict_len , <EOL> '<STR_LIT>' : rdict . ll_contains , <EOL> '<STR_LIT>' : rdict . ll_dict_setitem , <EOL> '<STR_LIT>' : dict_get , <EOL> '<STR_LIT>' : dict_add , <EOL> '<STR_LIT>' : dict_insertclean , <EOL> '<STR_LIT>' : rdict . ll_clear , <EOL> '<STR_LIT>' : dict_foreach , <EOL> '<STR_LIT>' : dict_keyhash , <EOL> '<STR_LIT>' : None , <EOL> } ) </s>
