| 1 |
import sys |
| 2 |
from rpython.tool.pairtype import pairtype |
| 3 |
from rpython.flowspace.model import Constant |
| 4 |
from rpython.rtyper.rdict import AbstractDictRepr, AbstractDictIteratorRepr |
| 5 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 6 |
from rpython.rlib import objectmodel, jit, rgc, types |
| 7 |
from rpython.rlib.signature import signature |
| 8 |
from rpython.rlib.objectmodel import specialize, likely, not_rpython |
| 9 |
from rpython.rtyper.debug import ll_assert |
| 10 |
from rpython.rlib.rarithmetic import r_uint, intmask |
| 11 |
from rpython.rtyper import rmodel |
| 12 |
from rpython.rtyper.error import TyperError |
| 13 |
from rpython.rtyper.annlowlevel import llhelper |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@jit.look_inside_iff(lambda d, key, hash, flag: jit.isvirtual(d)) |
| 47 |
@jit.oopspec('ordereddict.lookup(d, key, hash, flag)') |
| 48 |
def ll_call_lookup_function(d, key, hash, flag): |
| 49 |
while True: |
| 50 |
fun = d.lookup_function_no & FUNC_MASK |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if likely(fun == FUNC_BYTE): |
| 56 |
return ll_dict_lookup(d, key, hash, flag, TYPE_BYTE) |
| 57 |
elif fun == FUNC_SHORT: |
| 58 |
return ll_dict_lookup(d, key, hash, flag, TYPE_SHORT) |
| 59 |
elif IS_64BIT and fun == FUNC_INT: |
| 60 |
return ll_dict_lookup(d, key, hash, flag, TYPE_INT) |
| 61 |
elif fun == FUNC_LONG: |
| 62 |
return ll_dict_lookup(d, key, hash, flag, TYPE_LONG) |
| 63 |
else: |
| 64 |
ll_dict_create_initial_index(d) |
| 65 |
|
| 66 |
|
| 67 |
def get_ll_dict(DICTKEY, DICTVALUE, get_custom_eq_hash=None, DICT=None, |
| 68 |
ll_fasthash_function=None, ll_hash_function=None, |
| 69 |
ll_eq_function=None, method_cache={}, simple_hash_eq=False, |
| 70 |
dummykeyobj=None, dummyvalueobj=None, rtyper=None): |
| 71 |
|
| 72 |
|
| 73 |
if DICT is None: |
| 74 |
DICT = lltype.GcForwardReference() |
| 75 |
|
| 76 |
entryfields = [] |
| 77 |
entrymeths = { |
| 78 |
'allocate': lltype.typeMethod(_ll_malloc_entries), |
| 79 |
'delete': _ll_free_entries, |
| 80 |
'must_clear_key': (isinstance(DICTKEY, lltype.Ptr) |
| 81 |
and DICTKEY._needsgc()), |
| 82 |
'must_clear_value': (isinstance(DICTVALUE, lltype.Ptr) |
| 83 |
and DICTVALUE._needsgc()), |
| 84 |
} |
| 85 |
if getattr(ll_eq_function, 'no_direct_compare', False): |
| 86 |
entrymeths['no_direct_compare'] = True |
| 87 |
|
| 88 |
|
| 89 |
entryfields.append(("key", DICTKEY)) |
| 90 |
|
| 91 |
|
| 92 |
if dummykeyobj: |
| 93 |
|
| 94 |
entrymeths['dummy_obj'] = dummykeyobj |
| 95 |
entrymeths['valid'] = ll_valid_from_key |
| 96 |
entrymeths['mark_deleted'] = ll_mark_deleted_in_key |
| 97 |
|
| 98 |
entrymeths['must_clear_key'] = False |
| 99 |
|
| 100 |
elif dummyvalueobj: |
| 101 |
|
| 102 |
entrymeths['dummy_obj'] = dummyvalueobj |
| 103 |
entrymeths['valid'] = ll_valid_from_value |
| 104 |
entrymeths['mark_deleted'] = ll_mark_deleted_in_value |
| 105 |
|
| 106 |
entrymeths['must_clear_value'] = False |
| 107 |
|
| 108 |
else: |
| 109 |
|
| 110 |
entryfields.append(("f_valid", lltype.Bool)) |
| 111 |
entrymeths['valid'] = ll_valid_from_flag |
| 112 |
entrymeths['mark_deleted'] = ll_mark_deleted_in_flag |
| 113 |
|
| 114 |
|
| 115 |
entryfields.append(("value", DICTVALUE)) |
| 116 |
|
| 117 |
if simple_hash_eq: |
| 118 |
assert get_custom_eq_hash is not None |
| 119 |
entrymeths['entry_hash'] = ll_hash_custom_fast |
| 120 |
elif ll_fasthash_function is None: |
| 121 |
entryfields.append(("f_hash", lltype.Signed)) |
| 122 |
entrymeths['entry_hash'] = ll_hash_from_cache |
| 123 |
else: |
| 124 |
entrymeths['entry_hash'] = ll_hash_recomputed |
| 125 |
entrymeths['fasthashfn'] = ll_fasthash_function |
| 126 |
|
| 127 |
|
| 128 |
DICTENTRY = lltype.Struct("odictentry", *entryfields) |
| 129 |
DICTENTRYARRAY = lltype.GcArray(DICTENTRY, |
| 130 |
adtmeths=entrymeths) |
| 131 |
fields = [ ("num_live_items", lltype.Signed), |
| 132 |
("num_ever_used_items", lltype.Signed), |
| 133 |
("resize_counter", lltype.Signed), |
| 134 |
("indexes", llmemory.GCREF), |
| 135 |
("lookup_function_no", lltype.Signed), |
| 136 |
("entries", lltype.Ptr(DICTENTRYARRAY)) ] |
| 137 |
if get_custom_eq_hash is not None: |
| 138 |
r_rdict_eqfn, r_rdict_hashfn = get_custom_eq_hash() |
| 139 |
fields.extend([ ("fnkeyeq", r_rdict_eqfn.lowleveltype), |
| 140 |
("fnkeyhash", r_rdict_hashfn.lowleveltype) ]) |
| 141 |
adtmeths = { |
| 142 |
'keyhash': ll_keyhash_custom, |
| 143 |
'keyeq': ll_keyeq_custom, |
| 144 |
'r_rdict_eqfn': r_rdict_eqfn, |
| 145 |
'r_rdict_hashfn': r_rdict_hashfn, |
| 146 |
'paranoia': not simple_hash_eq, |
| 147 |
} |
| 148 |
else: |
| 149 |
|
| 150 |
ll_keyhash = ll_hash_function |
| 151 |
ll_keyeq = ll_eq_function |
| 152 |
ll_keyhash = lltype.staticAdtMethod(ll_keyhash) |
| 153 |
if ll_keyeq is not None: |
| 154 |
ll_keyeq = lltype.staticAdtMethod(ll_keyeq) |
| 155 |
adtmeths = { |
| 156 |
'keyhash': ll_keyhash, |
| 157 |
'keyeq': ll_keyeq, |
| 158 |
'paranoia': False, |
| 159 |
} |
| 160 |
adtmeths['KEY'] = DICTKEY |
| 161 |
adtmeths['VALUE'] = DICTVALUE |
| 162 |
adtmeths['lookup_function'] = lltype.staticAdtMethod(ll_call_lookup_function) |
| 163 |
adtmeths['allocate'] = lltype.typeMethod(_ll_malloc_dict) |
| 164 |
|
| 165 |
DICT.become(lltype.GcStruct("dicttable", adtmeths=adtmeths, |
| 166 |
*fields)) |
| 167 |
return DICT |
| 168 |
|
| 169 |
|
| 170 |
class OrderedDictRepr(AbstractDictRepr): |
| 171 |
|
| 172 |
def __init__(self, rtyper, key_repr, value_repr, dictkey, dictvalue, |
| 173 |
custom_eq_hash=None, force_non_null=False, simple_hash_eq=False): |
| 174 |
|
| 175 |
self.rtyper = rtyper |
| 176 |
self.finalized = False |
| 177 |
self.DICT = lltype.GcForwardReference() |
| 178 |
self.lowleveltype = lltype.Ptr(self.DICT) |
| 179 |
self.custom_eq_hash = custom_eq_hash is not None |
| 180 |
self.simple_hash_eq = simple_hash_eq |
| 181 |
if not isinstance(key_repr, rmodel.Repr): |
| 182 |
assert callable(key_repr) |
| 183 |
self._key_repr_computer = key_repr |
| 184 |
else: |
| 185 |
self.external_key_repr, self.key_repr = self.pickkeyrepr(key_repr) |
| 186 |
if not isinstance(value_repr, rmodel.Repr): |
| 187 |
assert callable(value_repr) |
| 188 |
self._value_repr_computer = value_repr |
| 189 |
else: |
| 190 |
self.external_value_repr, self.value_repr = self.pickrepr(value_repr) |
| 191 |
self.dictkey = dictkey |
| 192 |
self.dictvalue = dictvalue |
| 193 |
self.dict_cache = {} |
| 194 |
self._custom_eq_hash_repr = custom_eq_hash |
| 195 |
|
| 196 |
|
| 197 |
def _externalvsinternal(self, rtyper, item_repr): |
| 198 |
return rmodel.externalvsinternal(self.rtyper, item_repr) |
| 199 |
|
| 200 |
def _setup_repr(self): |
| 201 |
if 'key_repr' not in self.__dict__: |
| 202 |
key_repr = self._key_repr_computer() |
| 203 |
self.external_key_repr, self.key_repr = self.pickkeyrepr(key_repr) |
| 204 |
if 'value_repr' not in self.__dict__: |
| 205 |
self.external_value_repr, self.value_repr = rmodel.externalvsinternal(self.rtyper, self._value_repr_computer(), gcref=True) |
| 206 |
if isinstance(self.DICT, lltype.GcForwardReference): |
| 207 |
DICTKEY = self.key_repr.lowleveltype |
| 208 |
DICTVALUE = self.value_repr.lowleveltype |
| 209 |
|
| 210 |
|
| 211 |
s_key = self.dictkey.s_value |
| 212 |
s_value = self.dictvalue.s_value |
| 213 |
kwd = {} |
| 214 |
if self.custom_eq_hash: |
| 215 |
self.r_rdict_eqfn, self.r_rdict_hashfn = ( |
| 216 |
self._custom_eq_hash_repr()) |
| 217 |
kwd['get_custom_eq_hash'] = self._custom_eq_hash_repr |
| 218 |
kwd['simple_hash_eq'] = self.simple_hash_eq |
| 219 |
else: |
| 220 |
kwd['ll_hash_function'] = self.key_repr.get_ll_hash_function() |
| 221 |
kwd['ll_eq_function'] = self.key_repr.get_ll_eq_function() |
| 222 |
kwd['ll_fasthash_function'] = self.key_repr.get_ll_fasthash_function() |
| 223 |
kwd['dummykeyobj'] = self.key_repr.get_ll_dummyval_obj(self.rtyper, |
| 224 |
s_key) |
| 225 |
kwd['dummyvalueobj'] = self.value_repr.get_ll_dummyval_obj( |
| 226 |
self.rtyper, s_value) |
| 227 |
get_ll_dict(DICTKEY, DICTVALUE, DICT=self.DICT, |
| 228 |
rtyper=self.rtyper, **kwd) |
| 229 |
|
| 230 |
|
| 231 |
def convert_const(self, dictobj): |
| 232 |
from rpython.rtyper.lltypesystem import llmemory |
| 233 |
|
| 234 |
|
| 235 |
if dictobj is None: |
| 236 |
return lltype.nullptr(self.DICT) |
| 237 |
if not isinstance(dictobj, (dict, objectmodel.r_dict)): |
| 238 |
raise TypeError("expected a dict: %r" % (dictobj,)) |
| 239 |
try: |
| 240 |
key = Constant(dictobj) |
| 241 |
return self.dict_cache[key] |
| 242 |
except KeyError: |
| 243 |
self.setup() |
| 244 |
self.setup_final() |
| 245 |
l_dict = ll_newdict_size(self.DICT, len(dictobj)) |
| 246 |
ll_no_initial_index(l_dict) |
| 247 |
self.dict_cache[key] = l_dict |
| 248 |
r_key = self.key_repr |
| 249 |
if r_key.lowleveltype == llmemory.Address: |
| 250 |
raise TypeError("No prebuilt dicts of address keys") |
| 251 |
r_value = self.value_repr |
| 252 |
if isinstance(dictobj, objectmodel.r_dict): |
| 253 |
|
| 254 |
if self.r_rdict_eqfn.lowleveltype != lltype.Void: |
| 255 |
l_fn = self.r_rdict_eqfn.convert_const(dictobj.key_eq) |
| 256 |
l_dict.fnkeyeq = l_fn |
| 257 |
if self.r_rdict_hashfn.lowleveltype != lltype.Void: |
| 258 |
l_fn = self.r_rdict_hashfn.convert_const(dictobj.key_hash) |
| 259 |
l_dict.fnkeyhash = l_fn |
| 260 |
|
| 261 |
for dictkeycontainer, dictvalue in dictobj._dict.items(): |
| 262 |
llkey = r_key.convert_const(dictkeycontainer.key) |
| 263 |
llvalue = r_value.convert_const(dictvalue) |
| 264 |
_ll_dict_insert_no_index(l_dict, llkey, llvalue) |
| 265 |
return l_dict |
| 266 |
|
| 267 |
else: |
| 268 |
for dictkey, dictvalue in dictobj.items(): |
| 269 |
llkey = r_key.convert_const(dictkey) |
| 270 |
llvalue = r_value.convert_const(dictvalue) |
| 271 |
_ll_dict_insert_no_index(l_dict, llkey, llvalue) |
| 272 |
return l_dict |
| 273 |
|
| 274 |
def rtype_len(self, hop): |
| 275 |
v_dict, = hop.inputargs(self) |
| 276 |
return hop.gendirectcall(ll_dict_len, v_dict) |
| 277 |
|
| 278 |
def rtype_bool(self, hop): |
| 279 |
v_dict, = hop.inputargs(self) |
| 280 |
return hop.gendirectcall(ll_dict_bool, v_dict) |
| 281 |
|
| 282 |
def make_iterator_repr(self, *variant): |
| 283 |
return DictIteratorRepr(self, *variant) |
| 284 |
|
| 285 |
def rtype_method_get(self, hop): |
| 286 |
if hop.nb_args == 3: |
| 287 |
v_dict, v_key, v_default = hop.inputargs(self, self.key_repr, |
| 288 |
self.value_repr) |
| 289 |
else: |
| 290 |
v_dict, v_key = hop.inputargs(self, self.key_repr) |
| 291 |
v_default = hop.inputconst(self.value_repr, None) |
| 292 |
hop.exception_cannot_occur() |
| 293 |
v_res = hop.gendirectcall(ll_dict_get, v_dict, v_key, v_default) |
| 294 |
return self.recast_value(hop.llops, v_res) |
| 295 |
|
| 296 |
def rtype_method_setdefault(self, hop): |
| 297 |
v_dict, v_key, v_default = hop.inputargs(self, self.key_repr, |
| 298 |
self.value_repr) |
| 299 |
hop.exception_cannot_occur() |
| 300 |
v_res = hop.gendirectcall(ll_dict_setdefault, v_dict, v_key, v_default) |
| 301 |
return self.recast_value(hop.llops, v_res) |
| 302 |
|
| 303 |
def rtype_method_copy(self, hop): |
| 304 |
v_dict, = hop.inputargs(self) |
| 305 |
hop.exception_cannot_occur() |
| 306 |
return hop.gendirectcall(ll_dict_copy, v_dict) |
| 307 |
|
| 308 |
def rtype_method_update(self, hop): |
| 309 |
v_dic1, v_dic2 = hop.inputargs(self, self) |
| 310 |
hop.exception_cannot_occur() |
| 311 |
return hop.gendirectcall(ll_dict_update, v_dic1, v_dic2) |
| 312 |
|
| 313 |
def rtype_method__prepare_dict_update(self, hop): |
| 314 |
v_dict, v_num = hop.inputargs(self, lltype.Signed) |
| 315 |
hop.exception_cannot_occur() |
| 316 |
hop.gendirectcall(ll_prepare_dict_update, v_dict, v_num) |
| 317 |
|
| 318 |
def _rtype_method_kvi(self, hop, ll_func, extraarg=None): |
| 319 |
v_dic, = hop.inputargs(self) |
| 320 |
r_list = hop.r_result |
| 321 |
cLIST = hop.inputconst(lltype.Void, r_list.lowleveltype.TO) |
| 322 |
hop.exception_cannot_occur() |
| 323 |
if extraarg: |
| 324 |
c_extraarg = hop.inputconst(lltype.Void, extraarg) |
| 325 |
else: |
| 326 |
c_extraarg = hop.inputconst(lltype.Void, None) |
| 327 |
return hop.gendirectcall(ll_func, cLIST, v_dic, c_extraarg) |
| 328 |
|
| 329 |
def rtype_method_keys(self, hop): |
| 330 |
return self._rtype_method_kvi(hop, ll_dict_keys) |
| 331 |
|
| 332 |
def rtype_method_values(self, hop): |
| 333 |
return self._rtype_method_kvi(hop, ll_dict_values) |
| 334 |
|
| 335 |
def rtype_method_items(self, hop): |
| 336 |
EXTERNAL_ELEM = hop.r_result.external_item_repr.lowleveltype |
| 337 |
return self._rtype_method_kvi(hop, ll_dict_items, EXTERNAL_ELEM) |
| 338 |
|
| 339 |
def rtype_bltn_list(self, hop): |
| 340 |
return self._rtype_method_kvi(hop, ll_dict_keys) |
| 341 |
|
| 342 |
def rtype_method_iterkeys(self, hop): |
| 343 |
hop.exception_cannot_occur() |
| 344 |
return DictIteratorRepr(self, "keys").newiter(hop) |
| 345 |
|
| 346 |
def rtype_method_itervalues(self, hop): |
| 347 |
hop.exception_cannot_occur() |
| 348 |
return DictIteratorRepr(self, "values").newiter(hop) |
| 349 |
|
| 350 |
def rtype_method_iteritems(self, hop): |
| 351 |
hop.exception_cannot_occur() |
| 352 |
return DictIteratorRepr(self, "items").newiter(hop) |
| 353 |
|
| 354 |
def rtype_method_iterkeys_with_hash(self, hop): |
| 355 |
v_dic, = hop.inputargs(self) |
| 356 |
hop.exception_is_here() |
| 357 |
hop.gendirectcall(ll_ensure_indexes, v_dic) |
| 358 |
return DictIteratorRepr(self, "keys_with_hash").newiter(hop) |
| 359 |
|
| 360 |
def rtype_method_iteritems_with_hash(self, hop): |
| 361 |
v_dic, = hop.inputargs(self) |
| 362 |
hop.exception_is_here() |
| 363 |
hop.gendirectcall(ll_ensure_indexes, v_dic) |
| 364 |
return DictIteratorRepr(self, "items_with_hash").newiter(hop) |
| 365 |
|
| 366 |
def rtype_method_clear(self, hop): |
| 367 |
v_dict, = hop.inputargs(self) |
| 368 |
hop.exception_cannot_occur() |
| 369 |
return hop.gendirectcall(ll_dict_clear, v_dict) |
| 370 |
|
| 371 |
def rtype_method_popitem(self, hop): |
| 372 |
v_dict, = hop.inputargs(self) |
| 373 |
r_tuple = hop.r_result |
| 374 |
cTUPLE = hop.inputconst(lltype.Void, r_tuple.lowleveltype) |
| 375 |
hop.exception_is_here() |
| 376 |
return hop.gendirectcall(ll_dict_popitem, cTUPLE, v_dict) |
| 377 |
|
| 378 |
def rtype_method_pop(self, hop): |
| 379 |
if hop.nb_args == 2: |
| 380 |
v_args = hop.inputargs(self, self.key_repr) |
| 381 |
target = ll_dict_pop |
| 382 |
elif hop.nb_args == 3: |
| 383 |
v_args = hop.inputargs(self, self.key_repr, self.value_repr) |
| 384 |
target = ll_dict_pop_default |
| 385 |
hop.exception_is_here() |
| 386 |
v_res = hop.gendirectcall(target, *v_args) |
| 387 |
return self.recast_value(hop.llops, v_res) |
| 388 |
|
| 389 |
def rtype_method_contains_with_hash(self, hop): |
| 390 |
v_dict, v_key, v_hash = hop.inputargs(self, self.key_repr, |
| 391 |
lltype.Signed) |
| 392 |
hop.exception_is_here() |
| 393 |
return hop.gendirectcall(ll_dict_contains_with_hash, |
| 394 |
v_dict, v_key, v_hash) |
| 395 |
|
| 396 |
def rtype_method_setitem_with_hash(self, hop): |
| 397 |
v_dict, v_key, v_hash, v_value = hop.inputargs( |
| 398 |
self, self.key_repr, lltype.Signed, self.value_repr) |
| 399 |
if self.custom_eq_hash: |
| 400 |
hop.exception_is_here() |
| 401 |
else: |
| 402 |
hop.exception_cannot_occur() |
| 403 |
hop.gendirectcall(ll_dict_setitem_with_hash, |
| 404 |
v_dict, v_key, v_hash, v_value) |
| 405 |
|
| 406 |
def rtype_method_getitem_with_hash(self, hop): |
| 407 |
v_dict, v_key, v_hash = hop.inputargs( |
| 408 |
self, self.key_repr, lltype.Signed) |
| 409 |
if not self.custom_eq_hash: |
| 410 |
hop.has_implicit_exception(KeyError) |
| 411 |
hop.exception_is_here() |
| 412 |
v_res = hop.gendirectcall(ll_dict_getitem_with_hash, |
| 413 |
v_dict, v_key, v_hash) |
| 414 |
return self.recast_value(hop.llops, v_res) |
| 415 |
|
| 416 |
def rtype_method_delitem_with_hash(self, hop): |
| 417 |
v_dict, v_key, v_hash = hop.inputargs( |
| 418 |
self, self.key_repr, lltype.Signed) |
| 419 |
if not self.custom_eq_hash: |
| 420 |
hop.has_implicit_exception(KeyError) |
| 421 |
hop.exception_is_here() |
| 422 |
hop.gendirectcall(ll_dict_delitem_with_hash, v_dict, v_key, v_hash) |
| 423 |
|
| 424 |
def rtype_method_delitem_if_value_is(self, hop): |
| 425 |
v_dict, v_key, v_value = hop.inputargs( |
| 426 |
self, self.key_repr, self.value_repr) |
| 427 |
hop.exception_cannot_occur() |
| 428 |
hop.gendirectcall(ll_dict_delitem_if_value_is, v_dict, v_key, v_value) |
| 429 |
|
| 430 |
def rtype_method_move_to_end(self, hop): |
| 431 |
v_dict, v_key, v_last = hop.inputargs( |
| 432 |
self, self.key_repr, lltype.Bool) |
| 433 |
if not self.custom_eq_hash: |
| 434 |
hop.has_implicit_exception(KeyError) |
| 435 |
hop.exception_is_here() |
| 436 |
hop.gendirectcall(ll_dict_move_to_end, v_dict, v_key, v_last) |
| 437 |
|
| 438 |
|
| 439 |
class __extend__(pairtype(OrderedDictRepr, rmodel.Repr)): |
| 440 |
|
| 441 |
def rtype_getitem((r_dict, r_key), hop): |
| 442 |
v_dict, v_key = hop.inputargs(r_dict, r_dict.key_repr) |
| 443 |
if not r_dict.custom_eq_hash: |
| 444 |
hop.has_implicit_exception(KeyError) |
| 445 |
hop.exception_is_here() |
| 446 |
v_res = hop.gendirectcall(ll_dict_getitem, v_dict, v_key) |
| 447 |
return r_dict.recast_value(hop.llops, v_res) |
| 448 |
|
| 449 |
def rtype_delitem((r_dict, r_key), hop): |
| 450 |
v_dict, v_key = hop.inputargs(r_dict, r_dict.key_repr) |
| 451 |
if not r_dict.custom_eq_hash: |
| 452 |
hop.has_implicit_exception(KeyError) |
| 453 |
hop.exception_is_here() |
| 454 |
hop.gendirectcall(ll_dict_delitem, v_dict, v_key) |
| 455 |
|
| 456 |
def rtype_setitem((r_dict, r_key), hop): |
| 457 |
v_dict, v_key, v_value = hop.inputargs(r_dict, r_dict.key_repr, r_dict.value_repr) |
| 458 |
if r_dict.custom_eq_hash: |
| 459 |
hop.exception_is_here() |
| 460 |
else: |
| 461 |
hop.exception_cannot_occur() |
| 462 |
hop.gendirectcall(ll_dict_setitem, v_dict, v_key, v_value) |
| 463 |
|
| 464 |
def rtype_contains((r_dict, r_key), hop): |
| 465 |
v_dict, v_key = hop.inputargs(r_dict, r_dict.key_repr) |
| 466 |
hop.exception_is_here() |
| 467 |
return hop.gendirectcall(ll_dict_contains, v_dict, v_key) |
| 468 |
|
| 469 |
class __extend__(pairtype(OrderedDictRepr, OrderedDictRepr)): |
| 470 |
def convert_from_to((r_dict1, r_dict2), v, llops): |
| 471 |
|
| 472 |
|
| 473 |
if r_dict1.dictkey is None or r_dict2.dictkey is None: |
| 474 |
return NotImplemented |
| 475 |
if r_dict1.dictkey is not r_dict2.dictkey: |
| 476 |
return NotImplemented |
| 477 |
if r_dict1.dictvalue is None or r_dict2.dictvalue is None: |
| 478 |
return NotImplemented |
| 479 |
if r_dict1.dictvalue is not r_dict2.dictvalue: |
| 480 |
return NotImplemented |
| 481 |
return v |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
DICTINDEX_LONG = lltype.Ptr(lltype.GcArray(lltype.Unsigned)) |
| 490 |
DICTINDEX_INT = lltype.Ptr(lltype.GcArray(rffi.UINT)) |
| 491 |
DICTINDEX_SHORT = lltype.Ptr(lltype.GcArray(rffi.USHORT)) |
| 492 |
DICTINDEX_BYTE = lltype.Ptr(lltype.GcArray(rffi.UCHAR)) |
| 493 |
|
| 494 |
IS_64BIT = sys.maxint != 2 ** 31 - 1 |
| 495 |
|
| 496 |
if IS_64BIT: |
| 497 |
FUNC_SHIFT = 3 |
| 498 |
FUNC_MASK = 0x07 |
| 499 |
FUNC_BYTE, FUNC_SHORT, FUNC_INT, FUNC_LONG, FUNC_MUST_REINDEX = range(5) |
| 500 |
else: |
| 501 |
FUNC_SHIFT = 2 |
| 502 |
FUNC_MASK = 0x03 |
| 503 |
FUNC_BYTE, FUNC_SHORT, FUNC_LONG, FUNC_MUST_REINDEX = range(4) |
| 504 |
TYPE_BYTE = rffi.UCHAR |
| 505 |
TYPE_SHORT = rffi.USHORT |
| 506 |
TYPE_INT = rffi.UINT |
| 507 |
TYPE_LONG = lltype.Unsigned |
| 508 |
|
| 509 |
def ll_no_initial_index(d): |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
ll_assert(d.num_live_items == d.num_ever_used_items, |
| 516 |
"ll_no_initial_index(): dict already in use") |
| 517 |
d.lookup_function_no = FUNC_MUST_REINDEX |
| 518 |
d.indexes = lltype.nullptr(llmemory.GCREF.TO) |
| 519 |
|
| 520 |
def ll_malloc_indexes_and_choose_lookup(d, n): |
| 521 |
|
| 522 |
if n <= 256: |
| 523 |
d.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, |
| 524 |
lltype.malloc(DICTINDEX_BYTE.TO, n, |
| 525 |
zero=True)) |
| 526 |
d.lookup_function_no = FUNC_BYTE |
| 527 |
elif n <= 65536: |
| 528 |
d.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, |
| 529 |
lltype.malloc(DICTINDEX_SHORT.TO, n, |
| 530 |
zero=True)) |
| 531 |
d.lookup_function_no = FUNC_SHORT |
| 532 |
elif IS_64BIT and n <= 2 ** 32: |
| 533 |
d.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, |
| 534 |
lltype.malloc(DICTINDEX_INT.TO, n, |
| 535 |
zero=True)) |
| 536 |
d.lookup_function_no = FUNC_INT |
| 537 |
else: |
| 538 |
d.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, |
| 539 |
lltype.malloc(DICTINDEX_LONG.TO, n, |
| 540 |
zero=True)) |
| 541 |
d.lookup_function_no = FUNC_LONG |
| 542 |
|
| 543 |
def ll_clear_indexes(d, n): |
| 544 |
fun = d.lookup_function_no & FUNC_MASK |
| 545 |
d.lookup_function_no = fun |
| 546 |
if fun == FUNC_BYTE: |
| 547 |
rgc.ll_arrayclear(lltype.cast_opaque_ptr(DICTINDEX_BYTE, d.indexes)) |
| 548 |
elif fun == FUNC_SHORT: |
| 549 |
rgc.ll_arrayclear(lltype.cast_opaque_ptr(DICTINDEX_SHORT, d.indexes)) |
| 550 |
elif IS_64BIT and fun == FUNC_INT: |
| 551 |
rgc.ll_arrayclear(lltype.cast_opaque_ptr(DICTINDEX_INT, d.indexes)) |
| 552 |
elif fun == FUNC_LONG: |
| 553 |
rgc.ll_arrayclear(lltype.cast_opaque_ptr(DICTINDEX_LONG, d.indexes)) |
| 554 |
else: |
| 555 |
assert False |
| 556 |
|
| 557 |
@objectmodel.always_inline |
| 558 |
def _ll_write_indexes(d, i, value, T): |
| 559 |
INDEXES = _ll_ptr_to_array_of(T) |
| 560 |
indexes = lltype.cast_opaque_ptr(INDEXES, d.indexes) |
| 561 |
cast_value = rffi.cast(T, value) |
| 562 |
ll_assert(intmask(cast_value) == value, "indexes are a too small type of array") |
| 563 |
indexes[i] = cast_value |
| 564 |
|
| 565 |
@jit.dont_look_inside |
| 566 |
def ll_call_insert_clean_function(d, hash, i): |
| 567 |
assert i >= 0 |
| 568 |
fun = d.lookup_function_no & FUNC_MASK |
| 569 |
if fun == FUNC_BYTE: |
| 570 |
ll_dict_store_clean(d, hash, i, TYPE_BYTE) |
| 571 |
elif fun == FUNC_SHORT: |
| 572 |
ll_dict_store_clean(d, hash, i, TYPE_SHORT) |
| 573 |
elif IS_64BIT and fun == FUNC_INT: |
| 574 |
ll_dict_store_clean(d, hash, i, TYPE_INT) |
| 575 |
elif fun == FUNC_LONG: |
| 576 |
ll_dict_store_clean(d, hash, i, TYPE_LONG) |
| 577 |
else: |
| 578 |
|
| 579 |
ll_assert(False, "ll_call_insert_clean_function(): invalid lookup_fun") |
| 580 |
assert False |
| 581 |
|
| 582 |
def ll_call_delete_by_entry_index(d, hash, i, replace_with): |
| 583 |
|
| 584 |
|
| 585 |
fun = d.lookup_function_no & FUNC_MASK |
| 586 |
if fun == FUNC_BYTE: |
| 587 |
ll_dict_delete_by_entry_index(d, hash, i, replace_with, TYPE_BYTE) |
| 588 |
elif fun == FUNC_SHORT: |
| 589 |
ll_dict_delete_by_entry_index(d, hash, i, replace_with, TYPE_SHORT) |
| 590 |
elif IS_64BIT and fun == FUNC_INT: |
| 591 |
ll_dict_delete_by_entry_index(d, hash, i, replace_with, TYPE_INT) |
| 592 |
elif fun == FUNC_LONG: |
| 593 |
ll_dict_delete_by_entry_index(d, hash, i, replace_with, TYPE_LONG) |
| 594 |
else: |
| 595 |
|
| 596 |
ll_assert(False, "ll_call_delete_by_entry_index(): invalid lookup_fun") |
| 597 |
assert False |
| 598 |
|
| 599 |
def ll_valid_from_flag(entries, i): |
| 600 |
return entries[i].f_valid |
| 601 |
|
| 602 |
def ll_valid_from_key(entries, i): |
| 603 |
ENTRIES = lltype.typeOf(entries).TO |
| 604 |
dummy = ENTRIES.dummy_obj.ll_dummy_value |
| 605 |
return entries[i].key != dummy |
| 606 |
|
| 607 |
def ll_valid_from_value(entries, i): |
| 608 |
ENTRIES = lltype.typeOf(entries).TO |
| 609 |
dummy = ENTRIES.dummy_obj.ll_dummy_value |
| 610 |
return entries[i].value != dummy |
| 611 |
|
| 612 |
def ll_mark_deleted_in_flag(entries, i): |
| 613 |
entries[i].f_valid = False |
| 614 |
|
| 615 |
def ll_mark_deleted_in_key(entries, i): |
| 616 |
ENTRIES = lltype.typeOf(entries).TO |
| 617 |
dummy = ENTRIES.dummy_obj.ll_dummy_value |
| 618 |
entries[i].key = dummy |
| 619 |
|
| 620 |
def ll_mark_deleted_in_value(entries, i): |
| 621 |
ENTRIES = lltype.typeOf(entries).TO |
| 622 |
dummy = ENTRIES.dummy_obj.ll_dummy_value |
| 623 |
entries[i].value = dummy |
| 624 |
|
| 625 |
@signature(types.any(), types.any(), types.int(), returns=types.any()) |
| 626 |
def ll_hash_from_cache(entries, d, i): |
| 627 |
return entries[i].f_hash |
| 628 |
|
| 629 |
@signature(types.any(), types.any(), types.int(), returns=types.any()) |
| 630 |
def ll_hash_recomputed(entries, d, i): |
| 631 |
ENTRIES = lltype.typeOf(entries).TO |
| 632 |
return ENTRIES.fasthashfn(entries[i].key) |
| 633 |
|
| 634 |
@signature(types.any(), types.any(), types.int(), returns=types.any()) |
| 635 |
def ll_hash_custom_fast(entries, d, i): |
| 636 |
DICT = lltype.typeOf(d).TO |
| 637 |
key = entries[i].key |
| 638 |
return objectmodel.hlinvoke(DICT.r_rdict_hashfn, d.fnkeyhash, key) |
| 639 |
|
| 640 |
def ll_keyhash_custom(d, key): |
| 641 |
DICT = lltype.typeOf(d).TO |
| 642 |
return objectmodel.hlinvoke(DICT.r_rdict_hashfn, d.fnkeyhash, key) |
| 643 |
|
| 644 |
def ll_keyeq_custom(d, key1, key2): |
| 645 |
DICT = lltype.typeOf(d).TO |
| 646 |
return objectmodel.hlinvoke(DICT.r_rdict_eqfn, d.fnkeyeq, key1, key2) |
| 647 |
|
| 648 |
def ll_dict_len(d): |
| 649 |
return d.num_live_items |
| 650 |
|
| 651 |
def ll_dict_bool(d): |
| 652 |
|
| 653 |
return bool(d) and d.num_live_items != 0 |
| 654 |
|
| 655 |
def ll_dict_getitem(d, key): |
| 656 |
return ll_dict_getitem_with_hash(d, key, d.keyhash(key)) |
| 657 |
|
| 658 |
def ll_dict_getitem_with_hash(d, key, hash): |
| 659 |
index = d.lookup_function(d, key, hash, FLAG_LOOKUP) |
| 660 |
if index >= 0: |
| 661 |
return d.entries[index].value |
| 662 |
else: |
| 663 |
raise KeyError |
| 664 |
|
| 665 |
def ll_dict_setitem(d, key, value): |
| 666 |
ll_dict_setitem_with_hash(d, key, d.keyhash(key), value) |
| 667 |
|
| 668 |
def ll_dict_setitem_with_hash(d, key, hash, value): |
| 669 |
index = d.lookup_function(d, key, hash, FLAG_STORE) |
| 670 |
_ll_dict_setitem_lookup_done(d, key, value, hash, index) |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
@jit.look_inside_iff(lambda d, key, value, hash, i: jit.isvirtual(d) and jit.isconstant(key)) |
| 675 |
def _ll_dict_setitem_lookup_done(d, key, value, hash, i): |
| 676 |
ENTRY = lltype.typeOf(d.entries).TO.OF |
| 677 |
if i >= 0: |
| 678 |
entry = d.entries[i] |
| 679 |
entry.value = value |
| 680 |
else: |
| 681 |
reindexed = False |
| 682 |
if len(d.entries) == d.num_ever_used_items: |
| 683 |
try: |
| 684 |
reindexed = ll_dict_grow(d) |
| 685 |
except: |
| 686 |
_ll_dict_rescue(d) |
| 687 |
raise |
| 688 |
rc = d.resize_counter - 3 |
| 689 |
if rc <= 0: |
| 690 |
try: |
| 691 |
ll_dict_resize(d) |
| 692 |
reindexed = True |
| 693 |
except: |
| 694 |
_ll_dict_rescue(d) |
| 695 |
raise |
| 696 |
rc = d.resize_counter - 3 |
| 697 |
ll_assert(rc > 0, "ll_dict_resize failed?") |
| 698 |
if reindexed: |
| 699 |
ll_call_insert_clean_function(d, hash, d.num_ever_used_items) |
| 700 |
|
| 701 |
d.resize_counter = rc |
| 702 |
entry = d.entries[d.num_ever_used_items] |
| 703 |
entry.key = key |
| 704 |
entry.value = value |
| 705 |
if hasattr(ENTRY, 'f_hash'): |
| 706 |
entry.f_hash = hash |
| 707 |
if hasattr(ENTRY, 'f_valid'): |
| 708 |
entry.f_valid = True |
| 709 |
d.num_ever_used_items += 1 |
| 710 |
d.num_live_items += 1 |
| 711 |
|
| 712 |
@jit.dont_look_inside |
| 713 |
def _ll_dict_rescue(d): |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
ll_dict_reindex(d, _ll_len_of_d_indexes(d)) |
| 718 |
_ll_dict_rescue._dont_inline_ = True |
| 719 |
|
| 720 |
@not_rpython |
| 721 |
def _ll_dict_insert_no_index(d, key, value): |
| 722 |
|
| 723 |
ENTRY = lltype.typeOf(d.entries).TO.OF |
| 724 |
entry = d.entries[d.num_ever_used_items] |
| 725 |
entry.key = key |
| 726 |
entry.value = value |
| 727 |
|
| 728 |
if hasattr(ENTRY, 'f_valid'): |
| 729 |
entry.f_valid = True |
| 730 |
d.num_ever_used_items += 1 |
| 731 |
d.num_live_items += 1 |
| 732 |
rc = d.resize_counter - 3 |
| 733 |
d.resize_counter = rc |
| 734 |
|
| 735 |
@jit.dont_look_inside |
| 736 |
def ll_len_of_d_indexes(d): |
| 737 |
return _ll_len_of_d_indexes(d) |
| 738 |
|
| 739 |
def _ll_len_of_d_indexes(d): |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
return lltype.length_of_simple_gcarray_from_opaque(d.indexes) |
| 744 |
|
| 745 |
def _overallocate_entries_len(baselen): |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
newsize = baselen + (baselen >> 3) |
| 753 |
return newsize + 8 |
| 754 |
|
| 755 |
@jit.look_inside_iff(lambda d: jit.isvirtual(d)) |
| 756 |
def ll_dict_grow(d): |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
if d.num_live_items < d.num_ever_used_items // 2: |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
ll_dict_remove_deleted_items(d) |
| 767 |
return True |
| 768 |
|
| 769 |
new_allocated = _overallocate_entries_len(len(d.entries)) |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
if _ll_dict_entries_size_too_big(d, new_allocated): |
| 780 |
ll_dict_remove_deleted_items(d) |
| 781 |
assert d.num_live_items == d.num_ever_used_items |
| 782 |
return True |
| 783 |
|
| 784 |
newitems = lltype.malloc(lltype.typeOf(d).TO.entries.TO, new_allocated) |
| 785 |
rgc.ll_arraycopy(d.entries, newitems, 0, 0, len(d.entries)) |
| 786 |
d.entries = newitems |
| 787 |
return False |
| 788 |
|
| 789 |
def _ll_dict_entries_size_too_big(d, new_allocated): |
| 790 |
fun = d.lookup_function_no & FUNC_MASK |
| 791 |
if fun == FUNC_BYTE: |
| 792 |
assert d.num_live_items < ((1 << 8) - MIN_INDEXES_MINUS_ENTRIES) |
| 793 |
return new_allocated > ((1 << 8) - MIN_INDEXES_MINUS_ENTRIES) |
| 794 |
elif fun == FUNC_SHORT: |
| 795 |
assert d.num_live_items < ((1 << 16) - MIN_INDEXES_MINUS_ENTRIES) |
| 796 |
return new_allocated > ((1 << 16) - MIN_INDEXES_MINUS_ENTRIES) |
| 797 |
elif IS_64BIT and fun == FUNC_INT: |
| 798 |
assert d.num_live_items < ((1 << 32) - MIN_INDEXES_MINUS_ENTRIES) |
| 799 |
return new_allocated > ((1 << 32) - MIN_INDEXES_MINUS_ENTRIES) |
| 800 |
return False |
| 801 |
|
| 802 |
@jit.dont_look_inside |
| 803 |
def ll_dict_remove_deleted_items(d): |
| 804 |
if d.num_live_items < len(d.entries) // 4: |
| 805 |
|
| 806 |
|
| 807 |
new_allocated = _overallocate_entries_len(d.num_live_items) |
| 808 |
newitems = lltype.malloc(lltype.typeOf(d).TO.entries.TO, new_allocated) |
| 809 |
else: |
| 810 |
newitems = d.entries |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 815 |
llop.gc_writebarrier(lltype.Void, newitems) |
| 816 |
|
| 817 |
ENTRIES = lltype.typeOf(d).TO.entries.TO |
| 818 |
ENTRY = ENTRIES.OF |
| 819 |
isrc = 0 |
| 820 |
idst = 0 |
| 821 |
isrclimit = d.num_ever_used_items |
| 822 |
while isrc < isrclimit: |
| 823 |
if d.entries.valid(isrc): |
| 824 |
src = d.entries[isrc] |
| 825 |
dst = newitems[idst] |
| 826 |
dst.key = src.key |
| 827 |
dst.value = src.value |
| 828 |
if hasattr(ENTRY, 'f_hash'): |
| 829 |
dst.f_hash = src.f_hash |
| 830 |
if hasattr(ENTRY, 'f_valid'): |
| 831 |
assert src.f_valid |
| 832 |
dst.f_valid = True |
| 833 |
idst += 1 |
| 834 |
isrc += 1 |
| 835 |
assert d.num_live_items == idst |
| 836 |
d.num_ever_used_items = idst |
| 837 |
if ((ENTRIES.must_clear_key or ENTRIES.must_clear_value) and |
| 838 |
d.entries == newitems): |
| 839 |
|
| 840 |
|
| 841 |
while idst < isrclimit: |
| 842 |
entry = newitems[idst] |
| 843 |
if ENTRIES.must_clear_key: |
| 844 |
entry.key = lltype.nullptr(ENTRY.key.TO) |
| 845 |
if ENTRIES.must_clear_value: |
| 846 |
entry.value = lltype.nullptr(ENTRY.value.TO) |
| 847 |
idst += 1 |
| 848 |
else: |
| 849 |
d.entries = newitems |
| 850 |
|
| 851 |
ll_dict_reindex(d, _ll_len_of_d_indexes(d)) |
| 852 |
|
| 853 |
|
| 854 |
def ll_dict_delitem(d, key): |
| 855 |
ll_dict_delitem_with_hash(d, key, d.keyhash(key)) |
| 856 |
|
| 857 |
def ll_dict_delitem_with_hash(d, key, hash): |
| 858 |
index = d.lookup_function(d, key, hash, FLAG_LOOKUP) |
| 859 |
if index < 0: |
| 860 |
raise KeyError |
| 861 |
_ll_dict_del(d, hash, index) |
| 862 |
|
| 863 |
def ll_dict_delitem_if_value_is(d, key, value): |
| 864 |
hash = d.keyhash(key) |
| 865 |
index = d.lookup_function(d, key, hash, FLAG_LOOKUP) |
| 866 |
if index < 0: |
| 867 |
return |
| 868 |
if d.entries[index].value != value: |
| 869 |
return |
| 870 |
_ll_dict_del(d, hash, index) |
| 871 |
|
| 872 |
def _ll_dict_del_entry(d, index): |
| 873 |
d.entries.mark_deleted(index) |
| 874 |
d.num_live_items -= 1 |
| 875 |
|
| 876 |
ENTRIES = lltype.typeOf(d.entries).TO |
| 877 |
ENTRY = ENTRIES.OF |
| 878 |
entry = d.entries[index] |
| 879 |
if ENTRIES.must_clear_key: |
| 880 |
entry.key = lltype.nullptr(ENTRY.key.TO) |
| 881 |
if ENTRIES.must_clear_value: |
| 882 |
entry.value = lltype.nullptr(ENTRY.value.TO) |
| 883 |
|
| 884 |
@jit.look_inside_iff(lambda d, h, i: jit.isvirtual(d) and jit.isconstant(i)) |
| 885 |
def _ll_dict_del(d, hash, index): |
| 886 |
ll_call_delete_by_entry_index(d, hash, index, DELETED) |
| 887 |
_ll_dict_del_entry(d, index) |
| 888 |
|
| 889 |
if d.num_live_items == 0: |
| 890 |
|
| 891 |
d.num_ever_used_items = 0 |
| 892 |
d.lookup_function_no &= FUNC_MASK |
| 893 |
|
| 894 |
elif index == d.num_ever_used_items - 1: |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
i = index |
| 901 |
while True: |
| 902 |
i -= 1 |
| 903 |
assert i >= 0 |
| 904 |
if d.entries.valid(i): |
| 905 |
break |
| 906 |
d.num_ever_used_items = i + 1 |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
if d.num_live_items + DICT_INITSIZE <= len(d.entries) / 8: |
| 911 |
ll_dict_resize(d) |
| 912 |
|
| 913 |
def ll_dict_resize(d): |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
num_extra = min(d.num_live_items + 1, 30000) |
| 920 |
_ll_dict_resize_to(d, num_extra) |
| 921 |
ll_dict_resize.oopspec = 'odict.resize(d)' |
| 922 |
|
| 923 |
def _ll_dict_resize_to(d, num_extra): |
| 924 |
new_estimate = (d.num_live_items + num_extra) * 2 |
| 925 |
new_size = DICT_INITSIZE |
| 926 |
while new_size <= new_estimate: |
| 927 |
new_size *= 2 |
| 928 |
|
| 929 |
if new_size < _ll_len_of_d_indexes(d): |
| 930 |
ll_dict_remove_deleted_items(d) |
| 931 |
else: |
| 932 |
ll_dict_reindex(d, new_size) |
| 933 |
|
| 934 |
def ll_ensure_indexes(d): |
| 935 |
num = d.lookup_function_no |
| 936 |
if num == FUNC_MUST_REINDEX: |
| 937 |
ll_dict_create_initial_index(d) |
| 938 |
else: |
| 939 |
ll_assert((num & FUNC_MASK) != FUNC_MUST_REINDEX, |
| 940 |
"bad combination in lookup_function_no") |
| 941 |
|
| 942 |
@jit.look_inside_iff(lambda d: jit.isvirtual(d)) |
| 943 |
def ll_dict_create_initial_index(d): |
| 944 |
"""Create the initial index for a dictionary. The common case is |
| 945 |
that 'd' is empty. The uncommon case is that it is a prebuilt |
| 946 |
dictionary frozen by translation, in which case we must rehash all |
| 947 |
entries. The common case must be seen by the JIT. |
| 948 |
""" |
| 949 |
if d.num_live_items == 0: |
| 950 |
ll_malloc_indexes_and_choose_lookup(d, DICT_INITSIZE) |
| 951 |
d.resize_counter = DICT_INITSIZE * 2 |
| 952 |
else: |
| 953 |
ll_dict_rehash_after_translation(d) |
| 954 |
|
| 955 |
@jit.dont_look_inside |
| 956 |
def ll_dict_rehash_after_translation(d): |
| 957 |
assert d.num_live_items == d.num_ever_used_items |
| 958 |
assert not d.indexes |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
ENTRY = lltype.typeOf(d.entries).TO.OF |
| 965 |
for i in range(d.num_ever_used_items): |
| 966 |
assert d.entries.valid(i) |
| 967 |
d_entry = d.entries[i] |
| 968 |
h = d.keyhash(d_entry.key) |
| 969 |
if hasattr(ENTRY, 'f_hash'): |
| 970 |
d_entry.f_hash = h |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
new_size = DICT_INITSIZE |
| 975 |
while new_size * 2 - d.num_live_items * 3 <= 0: |
| 976 |
new_size *= 2 |
| 977 |
ll_dict_reindex(d, new_size) |
| 978 |
|
| 979 |
def ll_dict_reindex(d, new_size): |
| 980 |
if bool(d.indexes) and _ll_len_of_d_indexes(d) == new_size: |
| 981 |
ll_clear_indexes(d, new_size) |
| 982 |
else: |
| 983 |
ll_malloc_indexes_and_choose_lookup(d, new_size) |
| 984 |
d.resize_counter = new_size * 2 - d.num_live_items * 3 |
| 985 |
ll_assert(d.resize_counter > 0, "reindex: resize_counter <= 0") |
| 986 |
ll_assert((d.lookup_function_no >> FUNC_SHIFT) == 0, |
| 987 |
"reindex: lookup_fun >> SHIFT") |
| 988 |
|
| 989 |
entries = d.entries |
| 990 |
i = 0 |
| 991 |
ibound = d.num_ever_used_items |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
fun = d.lookup_function_no |
| 996 |
if fun == FUNC_BYTE: |
| 997 |
while i < ibound: |
| 998 |
if entries.valid(i): |
| 999 |
ll_dict_store_clean(d, entries.entry_hash(d, i), i, TYPE_BYTE) |
| 1000 |
i += 1 |
| 1001 |
elif fun == FUNC_SHORT: |
| 1002 |
while i < ibound: |
| 1003 |
if entries.valid(i): |
| 1004 |
ll_dict_store_clean(d, entries.entry_hash(d, i), i, TYPE_SHORT) |
| 1005 |
i += 1 |
| 1006 |
elif IS_64BIT and fun == FUNC_INT: |
| 1007 |
while i < ibound: |
| 1008 |
if entries.valid(i): |
| 1009 |
ll_dict_store_clean(d, entries.entry_hash(d, i), i, TYPE_INT) |
| 1010 |
i += 1 |
| 1011 |
elif fun == FUNC_LONG: |
| 1012 |
while i < ibound: |
| 1013 |
if entries.valid(i): |
| 1014 |
ll_dict_store_clean(d, entries.entry_hash(d, i), i, TYPE_LONG) |
| 1015 |
i += 1 |
| 1016 |
else: |
| 1017 |
assert False |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
PERTURB_SHIFT = 5 |
| 1022 |
|
| 1023 |
FREE = 0 |
| 1024 |
DELETED = 1 |
| 1025 |
VALID_OFFSET = 2 |
| 1026 |
MIN_INDEXES_MINUS_ENTRIES = VALID_OFFSET + 1 |
| 1027 |
|
| 1028 |
FLAG_LOOKUP = 0 |
| 1029 |
FLAG_STORE = 1 |
| 1030 |
|
| 1031 |
@specialize.memo() |
| 1032 |
def _ll_ptr_to_array_of(T): |
| 1033 |
return lltype.Ptr(lltype.GcArray(T)) |
| 1034 |
|
| 1035 |
@jit.look_inside_iff(lambda d, key, hash, store_flag, T: |
| 1036 |
jit.isvirtual(d) and jit.isconstant(key)) |
| 1037 |
@jit.oopspec('ordereddict.lookup(d, key, hash, store_flag, T)') |
| 1038 |
def ll_dict_lookup(d, key, hash, store_flag, T): |
| 1039 |
INDEXES = _ll_ptr_to_array_of(T) |
| 1040 |
entries = d.entries |
| 1041 |
indexes = lltype.cast_opaque_ptr(INDEXES, d.indexes) |
| 1042 |
mask = len(indexes) - 1 |
| 1043 |
i = r_uint(hash & mask) |
| 1044 |
|
| 1045 |
ENTRIES = lltype.typeOf(entries).TO |
| 1046 |
direct_compare = not hasattr(ENTRIES, 'no_direct_compare') |
| 1047 |
index = rffi.cast(lltype.Signed, indexes[intmask(i)]) |
| 1048 |
if index >= VALID_OFFSET: |
| 1049 |
checkingkey = entries[index - VALID_OFFSET].key |
| 1050 |
if direct_compare and checkingkey == key: |
| 1051 |
return index - VALID_OFFSET |
| 1052 |
if d.keyeq is not None and entries.entry_hash(d, index - VALID_OFFSET) == hash: |
| 1053 |
|
| 1054 |
|
| 1055 |
found = d.keyeq(checkingkey, key) |
| 1056 |
|
| 1057 |
if d.paranoia: |
| 1058 |
if (entries != d.entries or lltype.cast_opaque_ptr(llmemory.GCREF, indexes) != d.indexes or |
| 1059 |
not entries.valid(index - VALID_OFFSET) or |
| 1060 |
entries[index - VALID_OFFSET].key != checkingkey): |
| 1061 |
|
| 1062 |
return ll_dict_lookup(d, key, hash, store_flag, T) |
| 1063 |
if found: |
| 1064 |
return index - VALID_OFFSET |
| 1065 |
deletedslot = -1 |
| 1066 |
elif index == DELETED: |
| 1067 |
deletedslot = intmask(i) |
| 1068 |
else: |
| 1069 |
|
| 1070 |
if store_flag == FLAG_STORE: |
| 1071 |
_ll_write_indexes(d, i, d.num_ever_used_items + VALID_OFFSET, T) |
| 1072 |
return -1 |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
perturb = r_uint(hash) |
| 1077 |
while 1: |
| 1078 |
|
| 1079 |
i = (i << 2) + i + perturb + 1 |
| 1080 |
i = i & mask |
| 1081 |
index = rffi.cast(lltype.Signed, indexes[intmask(i)]) |
| 1082 |
if index == FREE: |
| 1083 |
if store_flag == FLAG_STORE: |
| 1084 |
if deletedslot == -1: |
| 1085 |
deletedslot = intmask(i) |
| 1086 |
_ll_write_indexes(d, deletedslot, d.num_ever_used_items + VALID_OFFSET, T) |
| 1087 |
return -1 |
| 1088 |
elif index >= VALID_OFFSET: |
| 1089 |
checkingkey = entries[index - VALID_OFFSET].key |
| 1090 |
if direct_compare and checkingkey == key: |
| 1091 |
return index - VALID_OFFSET |
| 1092 |
if d.keyeq is not None and entries.entry_hash(d, index - VALID_OFFSET) == hash: |
| 1093 |
|
| 1094 |
|
| 1095 |
found = d.keyeq(checkingkey, key) |
| 1096 |
if d.paranoia: |
| 1097 |
if (entries != d.entries or lltype.cast_opaque_ptr(llmemory.GCREF, indexes) != d.indexes or |
| 1098 |
not entries.valid(index - VALID_OFFSET) or |
| 1099 |
entries[index - VALID_OFFSET].key != checkingkey): |
| 1100 |
|
| 1101 |
return ll_dict_lookup(d, key, hash, store_flag, T) |
| 1102 |
if found: |
| 1103 |
return index - VALID_OFFSET |
| 1104 |
elif deletedslot == -1: |
| 1105 |
deletedslot = intmask(i) |
| 1106 |
perturb >>= PERTURB_SHIFT |
| 1107 |
|
| 1108 |
def ll_dict_store_clean(d, hash, index, T): |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
INDEXES = _ll_ptr_to_array_of(T) |
| 1113 |
indexes = lltype.cast_opaque_ptr(INDEXES, d.indexes) |
| 1114 |
mask = len(indexes) - 1 |
| 1115 |
i = r_uint(hash & mask) |
| 1116 |
perturb = r_uint(hash) |
| 1117 |
while rffi.cast(lltype.Signed, indexes[i]) != FREE: |
| 1118 |
i = (i << 2) + i + perturb + 1 |
| 1119 |
i = i & mask |
| 1120 |
perturb >>= PERTURB_SHIFT |
| 1121 |
_ll_write_indexes(d, i, index + VALID_OFFSET, T) |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
@jit.unroll_safe |
| 1127 |
def ll_dict_delete_by_entry_index(d, hash, locate_index, replace_with, T): |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
INDEXES = _ll_ptr_to_array_of(T) |
| 1134 |
indexes = lltype.cast_opaque_ptr(INDEXES, d.indexes) |
| 1135 |
mask = len(indexes) - 1 |
| 1136 |
i = r_uint(hash & mask) |
| 1137 |
perturb = r_uint(hash) |
| 1138 |
locate_value = locate_index + VALID_OFFSET |
| 1139 |
while rffi.cast(lltype.Signed, indexes[i]) != locate_value: |
| 1140 |
assert rffi.cast(lltype.Signed, indexes[i]) != FREE |
| 1141 |
i = (i << 2) + i + perturb + 1 |
| 1142 |
i = i & mask |
| 1143 |
perturb >>= PERTURB_SHIFT |
| 1144 |
_ll_write_indexes(d, i, replace_with, T) |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
DICT_INITSIZE = 16 |
| 1153 |
|
| 1154 |
|
| 1155 |
@specialize.memo() |
| 1156 |
def _ll_empty_array(DICT): |
| 1157 |
"""Memo function: cache a single prebuilt allocated empty array.""" |
| 1158 |
return DICT.entries.TO.allocate(0) |
| 1159 |
|
| 1160 |
def ll_newdict(DICT): |
| 1161 |
d = DICT.allocate() |
| 1162 |
d.entries = _ll_empty_array(DICT) |
| 1163 |
|
| 1164 |
|
| 1165 |
d.num_live_items = 0 |
| 1166 |
d.num_ever_used_items = 0 |
| 1167 |
ll_no_initial_index(d) |
| 1168 |
return d |
| 1169 |
OrderedDictRepr.ll_newdict = staticmethod(ll_newdict) |
| 1170 |
|
| 1171 |
def ll_newdict_size(DICT, orig_length_estimate): |
| 1172 |
length_estimate = (orig_length_estimate // 2) * 3 |
| 1173 |
n = DICT_INITSIZE |
| 1174 |
while n < length_estimate: |
| 1175 |
n *= 2 |
| 1176 |
d = DICT.allocate() |
| 1177 |
d.entries = DICT.entries.TO.allocate(orig_length_estimate) |
| 1178 |
ll_malloc_indexes_and_choose_lookup(d, n) |
| 1179 |
d.num_live_items = 0 |
| 1180 |
d.num_ever_used_items = 0 |
| 1181 |
d.resize_counter = n * 2 |
| 1182 |
return d |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
def _ll_malloc_dict(DICT): |
| 1188 |
return lltype.malloc(DICT) |
| 1189 |
def _ll_malloc_entries(ENTRIES, n): |
| 1190 |
return lltype.malloc(ENTRIES, n, zero=True) |
| 1191 |
def _ll_free_entries(entries): |
| 1192 |
pass |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
def get_ll_dictiter(DICTPTR): |
| 1200 |
return lltype.Ptr(lltype.GcStruct('dictiter', |
| 1201 |
('dict', DICTPTR), |
| 1202 |
('index', lltype.Signed))) |
| 1203 |
|
| 1204 |
class DictIteratorRepr(AbstractDictIteratorRepr): |
| 1205 |
|
| 1206 |
def __init__(self, r_dict, variant="keys"): |
| 1207 |
self.r_dict = r_dict |
| 1208 |
self.variant = variant |
| 1209 |
self.lowleveltype = get_ll_dictiter(r_dict.lowleveltype) |
| 1210 |
if variant == 'reversed': |
| 1211 |
self.ll_dictiter = ll_dictiter_reversed |
| 1212 |
self._ll_dictnext = _ll_dictnext_reversed |
| 1213 |
else: |
| 1214 |
self.ll_dictiter = ll_dictiter |
| 1215 |
self._ll_dictnext = _ll_dictnext |
| 1216 |
|
| 1217 |
|
| 1218 |
def ll_dictiter(ITERPTR, d): |
| 1219 |
iter = lltype.malloc(ITERPTR.TO) |
| 1220 |
iter.dict = d |
| 1221 |
|
| 1222 |
iter.index = d.lookup_function_no >> FUNC_SHIFT |
| 1223 |
return iter |
| 1224 |
|
| 1225 |
@jit.look_inside_iff(lambda iter: jit.isvirtual(iter) |
| 1226 |
and (iter.dict is None or |
| 1227 |
jit.isvirtual(iter.dict))) |
| 1228 |
@jit.oopspec("odictiter.next(iter)") |
| 1229 |
def _ll_dictnext(iter): |
| 1230 |
dict = iter.dict |
| 1231 |
if dict: |
| 1232 |
entries = dict.entries |
| 1233 |
index = iter.index |
| 1234 |
assert index >= 0 |
| 1235 |
entries_len = dict.num_ever_used_items |
| 1236 |
while index < entries_len: |
| 1237 |
nextindex = index + 1 |
| 1238 |
if entries.valid(index): |
| 1239 |
iter.index = nextindex |
| 1240 |
return index |
| 1241 |
else: |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
if index == (dict.lookup_function_no >> FUNC_SHIFT): |
| 1251 |
dict.lookup_function_no += (1 << FUNC_SHIFT) |
| 1252 |
|
| 1253 |
|
| 1254 |
ll_assert((dict.lookup_function_no & FUNC_MASK) != |
| 1255 |
FUNC_MUST_REINDEX, "bad combination in _ll_dictnext") |
| 1256 |
index = nextindex |
| 1257 |
|
| 1258 |
iter.dict = lltype.nullptr(lltype.typeOf(iter).TO.dict.TO) |
| 1259 |
raise StopIteration |
| 1260 |
|
| 1261 |
def ll_dictiter_reversed(ITERPTR, d): |
| 1262 |
iter = lltype.malloc(ITERPTR.TO) |
| 1263 |
iter.dict = d |
| 1264 |
iter.index = d.num_ever_used_items |
| 1265 |
return iter |
| 1266 |
|
| 1267 |
def _ll_dictnext_reversed(iter): |
| 1268 |
dict = iter.dict |
| 1269 |
if dict: |
| 1270 |
entries = dict.entries |
| 1271 |
index = iter.index - 1 |
| 1272 |
while index >= 0: |
| 1273 |
if entries.valid(index): |
| 1274 |
iter.index = index |
| 1275 |
return index |
| 1276 |
index = index - 1 |
| 1277 |
|
| 1278 |
iter.dict = lltype.nullptr(lltype.typeOf(iter).TO.dict.TO) |
| 1279 |
raise StopIteration |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
def ll_dict_get(dict, key, default): |
| 1285 |
index = dict.lookup_function(dict, key, dict.keyhash(key), FLAG_LOOKUP) |
| 1286 |
if index < 0: |
| 1287 |
return default |
| 1288 |
else: |
| 1289 |
return dict.entries[index].value |
| 1290 |
|
| 1291 |
def ll_dict_setdefault(dict, key, default): |
| 1292 |
hash = dict.keyhash(key) |
| 1293 |
index = dict.lookup_function(dict, key, hash, FLAG_STORE) |
| 1294 |
if index < 0: |
| 1295 |
_ll_dict_setitem_lookup_done(dict, key, default, hash, -1) |
| 1296 |
return default |
| 1297 |
else: |
| 1298 |
return dict.entries[index].value |
| 1299 |
|
| 1300 |
@jit.look_inside_iff(lambda dict: jit.isvirtual(dict)) |
| 1301 |
def ll_dict_copy(dict): |
| 1302 |
DICT = lltype.typeOf(dict).TO |
| 1303 |
if dict.num_live_items == 0: |
| 1304 |
return ll_newdict(DICT) |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
if not jit.we_are_jitted(): |
| 1310 |
ll_ensure_indexes(dict) |
| 1311 |
else: |
| 1312 |
num = dict.lookup_function_no |
| 1313 |
assert (num & FUNC_MASK) != FUNC_MUST_REINDEX |
| 1314 |
|
| 1315 |
newdict = DICT.allocate() |
| 1316 |
newdict.entries = DICT.entries.TO.allocate(len(dict.entries)) |
| 1317 |
|
| 1318 |
newdict.num_live_items = dict.num_live_items |
| 1319 |
newdict.num_ever_used_items = dict.num_ever_used_items |
| 1320 |
if hasattr(DICT, 'fnkeyeq'): |
| 1321 |
newdict.fnkeyeq = dict.fnkeyeq |
| 1322 |
if hasattr(DICT, 'fnkeyhash'): |
| 1323 |
newdict.fnkeyhash = dict.fnkeyhash |
| 1324 |
|
| 1325 |
rgc.ll_arraycopy(dict.entries, newdict.entries, 0, 0, |
| 1326 |
newdict.num_ever_used_items) |
| 1327 |
|
| 1328 |
fun = dict.lookup_function_no & FUNC_MASK |
| 1329 |
if fun == FUNC_BYTE: |
| 1330 |
indexes = lltype.cast_opaque_ptr(DICTINDEX_BYTE, dict.indexes) |
| 1331 |
n = len(indexes) |
| 1332 |
newindexes = lltype.malloc(DICTINDEX_BYTE.TO, n, zero=True) |
| 1333 |
rgc.ll_arraycopy(indexes, newindexes, 0, 0, n) |
| 1334 |
newdict.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, newindexes) |
| 1335 |
newdict.lookup_function_no = FUNC_BYTE |
| 1336 |
elif fun == FUNC_SHORT: |
| 1337 |
indexes = lltype.cast_opaque_ptr(DICTINDEX_SHORT, dict.indexes) |
| 1338 |
n = len(indexes) |
| 1339 |
newindexes = lltype.malloc(DICTINDEX_SHORT.TO, n, zero=True) |
| 1340 |
rgc.ll_arraycopy(indexes, newindexes, 0, 0, n) |
| 1341 |
newdict.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, newindexes) |
| 1342 |
newdict.lookup_function_no = FUNC_SHORT |
| 1343 |
elif IS_64BIT and fun == FUNC_INT: |
| 1344 |
indexes = lltype.cast_opaque_ptr(DICTINDEX_INT, dict.indexes) |
| 1345 |
n = len(indexes) |
| 1346 |
newindexes = lltype.malloc(DICTINDEX_INT.TO, n, zero=True) |
| 1347 |
rgc.ll_arraycopy(indexes, newindexes, 0, 0, n) |
| 1348 |
newdict.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, newindexes) |
| 1349 |
newdict.lookup_function_no = FUNC_INT |
| 1350 |
else: |
| 1351 |
indexes = lltype.cast_opaque_ptr(DICTINDEX_LONG, dict.indexes) |
| 1352 |
n = len(indexes) |
| 1353 |
newindexes = lltype.malloc(DICTINDEX_LONG.TO, n, zero=True) |
| 1354 |
rgc.ll_arraycopy(indexes, newindexes, 0, 0, n) |
| 1355 |
newdict.indexes = lltype.cast_opaque_ptr(llmemory.GCREF, newindexes) |
| 1356 |
newdict.lookup_function_no = FUNC_LONG |
| 1357 |
newdict.resize_counter = dict.resize_counter |
| 1358 |
return newdict |
| 1359 |
|
| 1360 |
def ll_dict_clear(d): |
| 1361 |
if d.num_ever_used_items == 0: |
| 1362 |
return |
| 1363 |
DICT = lltype.typeOf(d).TO |
| 1364 |
old_entries = d.entries |
| 1365 |
d.entries = _ll_empty_array(DICT) |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
ll_malloc_indexes_and_choose_lookup(d, DICT_INITSIZE) |
| 1371 |
d.num_live_items = 0 |
| 1372 |
d.num_ever_used_items = 0 |
| 1373 |
d.resize_counter = DICT_INITSIZE * 2 |
| 1374 |
|
| 1375 |
ll_dict_clear.oopspec = 'odict.clear(d)' |
| 1376 |
|
| 1377 |
@jit.look_inside_iff(lambda dic1, dic2: jit.isvirtual(dic1) and jit.isvirtual(dic2)) |
| 1378 |
def ll_dict_update(dic1, dic2): |
| 1379 |
if dic1 == dic2: |
| 1380 |
return |
| 1381 |
ll_ensure_indexes(dic2) |
| 1382 |
ll_prepare_dict_update(dic1, dic2.num_live_items) |
| 1383 |
i = 0 |
| 1384 |
while i < dic2.num_ever_used_items: |
| 1385 |
entries = dic2.entries |
| 1386 |
if entries.valid(i): |
| 1387 |
entry = entries[i] |
| 1388 |
hash = entries.entry_hash(dic2, i) |
| 1389 |
key = entry.key |
| 1390 |
value = entry.value |
| 1391 |
index = dic1.lookup_function(dic1, key, hash, FLAG_STORE) |
| 1392 |
_ll_dict_setitem_lookup_done(dic1, key, value, hash, index) |
| 1393 |
i += 1 |
| 1394 |
|
| 1395 |
def ll_prepare_dict_update(d, num_extra): |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
ll_ensure_indexes(d) |
| 1408 |
x = num_extra - d.num_live_items |
| 1409 |
jit.conditional_call(d.resize_counter <= x * 3, |
| 1410 |
_ll_dict_resize_to, d, num_extra) |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
def recast(P, v): |
| 1418 |
|
| 1419 |
if P is llmemory.GCREF: |
| 1420 |
return lltype.cast_opaque_ptr(llmemory.GCREF, v) |
| 1421 |
if lltype.typeOf(v) is llmemory.GCREF: |
| 1422 |
return lltype.cast_opaque_ptr(P, v) |
| 1423 |
if isinstance(P, lltype.Ptr): |
| 1424 |
return lltype.cast_pointer(P, v) |
| 1425 |
else: |
| 1426 |
return v |
| 1427 |
|
| 1428 |
def _make_ll_keys_values_items(kind): |
| 1429 |
def ll_kvi(LIST, dic, EXTERNAL_ELEM=None): |
| 1430 |
res = LIST.ll_newlist(dic.num_live_items) |
| 1431 |
entries = dic.entries |
| 1432 |
dlen = dic.num_ever_used_items |
| 1433 |
items = res.ll_items() |
| 1434 |
ELEM = lltype.typeOf(items).TO.OF |
| 1435 |
i = 0 |
| 1436 |
p = 0 |
| 1437 |
while i < dlen: |
| 1438 |
if entries.valid(i): |
| 1439 |
if ELEM is not lltype.Void: |
| 1440 |
entry = entries[i] |
| 1441 |
if kind == 'items': |
| 1442 |
r = lltype.malloc(EXTERNAL_ELEM.TO) |
| 1443 |
r.item0 = recast(EXTERNAL_ELEM.TO.item0, entry.key) |
| 1444 |
r.item1 = recast(EXTERNAL_ELEM.TO.item1, entry.value) |
| 1445 |
items[p] = recast(ELEM, r) |
| 1446 |
elif kind == 'keys': |
| 1447 |
items[p] = recast(ELEM, entry.key) |
| 1448 |
elif kind == 'values': |
| 1449 |
items[p] = recast(ELEM, entry.value) |
| 1450 |
p += 1 |
| 1451 |
i += 1 |
| 1452 |
assert p == res.ll_length() |
| 1453 |
return res |
| 1454 |
if kind != "items": |
| 1455 |
ll_kvi.oopspec = 'odict.%s(dic)' % kind |
| 1456 |
return ll_kvi |
| 1457 |
|
| 1458 |
ll_dict_keys = _make_ll_keys_values_items('keys') |
| 1459 |
ll_dict_values = _make_ll_keys_values_items('values') |
| 1460 |
ll_dict_items = _make_ll_keys_values_items('items') |
| 1461 |
|
| 1462 |
def ll_dict_contains(d, key): |
| 1463 |
return ll_dict_contains_with_hash(d, key, d.keyhash(key)) |
| 1464 |
|
| 1465 |
def ll_dict_contains_with_hash(d, key, hash): |
| 1466 |
i = d.lookup_function(d, key, hash, FLAG_LOOKUP) |
| 1467 |
return i >= 0 |
| 1468 |
|
| 1469 |
def _ll_getnextitem(dic): |
| 1470 |
if dic.num_live_items == 0: |
| 1471 |
raise KeyError |
| 1472 |
|
| 1473 |
ll_ensure_indexes(dic) |
| 1474 |
entries = dic.entries |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
while True: |
| 1481 |
i = dic.num_ever_used_items - 1 |
| 1482 |
if entries.valid(i): |
| 1483 |
break |
| 1484 |
dic.num_ever_used_items -= 1 |
| 1485 |
|
| 1486 |
return i |
| 1487 |
|
| 1488 |
def ll_dict_popitem(ELEM, dic): |
| 1489 |
i = _ll_getnextitem(dic) |
| 1490 |
entry = dic.entries[i] |
| 1491 |
r = lltype.malloc(ELEM.TO) |
| 1492 |
r.item0 = recast(ELEM.TO.item0, entry.key) |
| 1493 |
r.item1 = recast(ELEM.TO.item1, entry.value) |
| 1494 |
_ll_dict_del(dic, dic.entries.entry_hash(dic, i), i) |
| 1495 |
return r |
| 1496 |
|
| 1497 |
def ll_dict_pop(dic, key): |
| 1498 |
hash = dic.keyhash(key) |
| 1499 |
index = dic.lookup_function(dic, key, hash, FLAG_LOOKUP) |
| 1500 |
if index < 0: |
| 1501 |
raise KeyError |
| 1502 |
value = dic.entries[index].value |
| 1503 |
_ll_dict_del(dic, hash, index) |
| 1504 |
return value |
| 1505 |
|
| 1506 |
def ll_dict_pop_default(dic, key, dfl): |
| 1507 |
hash = dic.keyhash(key) |
| 1508 |
index = dic.lookup_function(dic, key, hash, FLAG_LOOKUP) |
| 1509 |
if index < 0: |
| 1510 |
return dfl |
| 1511 |
value = dic.entries[index].value |
| 1512 |
_ll_dict_del(dic, hash, index) |
| 1513 |
return value |
| 1514 |
|
| 1515 |
def ll_dict_move_to_end(d, key, last): |
| 1516 |
if last: |
| 1517 |
ll_dict_move_to_last(d, key) |
| 1518 |
else: |
| 1519 |
ll_dict_move_to_first(d, key) |
| 1520 |
|
| 1521 |
def ll_dict_move_to_last(d, key): |
| 1522 |
hash = d.keyhash(key) |
| 1523 |
old_index = d.lookup_function(d, key, hash, FLAG_LOOKUP) |
| 1524 |
if old_index < 0: |
| 1525 |
raise KeyError |
| 1526 |
|
| 1527 |
if old_index == d.num_ever_used_items - 1: |
| 1528 |
return |
| 1529 |
|
| 1530 |
|
| 1531 |
old_entry = d.entries[old_index] |
| 1532 |
key = old_entry.key |
| 1533 |
value = old_entry.value |
| 1534 |
_ll_dict_del_entry(d, old_index) |
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
ll_call_delete_by_entry_index(d, hash, old_index, |
| 1542 |
replace_with = VALID_OFFSET + d.num_ever_used_items) |
| 1543 |
_ll_dict_setitem_lookup_done(d, key, value, hash, -1) |
| 1544 |
|
| 1545 |
def ll_dict_move_to_first(d, key): |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
if d.num_live_items < len(d.entries) // 2 - 16: |
| 1558 |
ll_dict_remove_deleted_items(d) |
| 1559 |
|
| 1560 |
hash = d.keyhash(key) |
| 1561 |
old_index = d.lookup_function(d, key, hash, FLAG_LOOKUP) |
| 1562 |
if old_index <= 0: |
| 1563 |
if old_index < 0: |
| 1564 |
raise KeyError |
| 1565 |
else: |
| 1566 |
|
| 1567 |
return |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
must_reindex = False |
| 1572 |
if d.entries.valid(0): |
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
must_reindex = True |
| 1577 |
new_allocated = _overallocate_entries_len(d.num_ever_used_items) |
| 1578 |
if not _ll_dict_entries_size_too_big(d, new_allocated): |
| 1579 |
|
| 1580 |
idst = ((new_allocated - d.num_ever_used_items) * 3) // 4 |
| 1581 |
ll_assert(idst > 0, "overallocate did not do enough") |
| 1582 |
newitems = lltype.malloc(lltype.typeOf(d).TO.entries.TO, new_allocated) |
| 1583 |
rgc.ll_arraycopy(d.entries, newitems, 0, idst, d.num_ever_used_items) |
| 1584 |
d.entries = newitems |
| 1585 |
i = 0 |
| 1586 |
while i < idst: |
| 1587 |
d.entries.mark_deleted(i) |
| 1588 |
i += 1 |
| 1589 |
d.num_ever_used_items += idst |
| 1590 |
old_index += idst |
| 1591 |
idst -= 1 |
| 1592 |
|
| 1593 |
else: |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
ll_assert(d.num_live_items < d.num_ever_used_items, "deleted entries exist, but num_live_items == num_ever_used_items?!") |
| 1598 |
old_index = _ll_dict_move_to_first_shift_items(d, old_index) |
| 1599 |
idst = d.lookup_function_no >> FUNC_SHIFT |
| 1600 |
idst -= 1 |
| 1601 |
else: |
| 1602 |
idst = d.lookup_function_no >> FUNC_SHIFT |
| 1603 |
|
| 1604 |
while not d.entries.valid(idst): |
| 1605 |
idst += 1 |
| 1606 |
if idst == old_index: |
| 1607 |
d.lookup_function_no = ((d.lookup_function_no & FUNC_MASK) | |
| 1608 |
(old_index << FUNC_SHIFT)) |
| 1609 |
return |
| 1610 |
idst -= 1 |
| 1611 |
d.lookup_function_no = ((d.lookup_function_no & FUNC_MASK) | |
| 1612 |
(idst << FUNC_SHIFT)) |
| 1613 |
|
| 1614 |
|
| 1615 |
ll_assert(d.entries.valid(old_index), |
| 1616 |
"ll_dict_move_to_first: lost old_index") |
| 1617 |
ENTRY = lltype.typeOf(d.entries).TO.OF |
| 1618 |
old_entry = d.entries[old_index] |
| 1619 |
key = old_entry.key |
| 1620 |
value = old_entry.value |
| 1621 |
if hasattr(ENTRY, 'f_hash'): |
| 1622 |
ll_assert(old_entry.f_hash == hash, |
| 1623 |
"ll_dict_move_to_first: bad hash") |
| 1624 |
_ll_dict_del_entry(d, old_index) |
| 1625 |
|
| 1626 |
|
| 1627 |
ll_assert(not d.entries.valid(idst), |
| 1628 |
"ll_dict_move_to_first: overwriting idst") |
| 1629 |
new_entry = d.entries[idst] |
| 1630 |
new_entry.key = key |
| 1631 |
new_entry.value = value |
| 1632 |
if hasattr(ENTRY, 'f_hash'): |
| 1633 |
new_entry.f_hash = hash |
| 1634 |
if hasattr(ENTRY, 'f_valid'): |
| 1635 |
new_entry.f_valid = True |
| 1636 |
d.num_live_items += 1 |
| 1637 |
|
| 1638 |
|
| 1639 |
if must_reindex: |
| 1640 |
ll_dict_reindex(d, _ll_len_of_d_indexes(d)) |
| 1641 |
else: |
| 1642 |
ll_call_delete_by_entry_index(d, hash, old_index, |
| 1643 |
replace_with = VALID_OFFSET + idst) |
| 1644 |
|
| 1645 |
def _ll_dict_move_to_first_shift_items(d, old_index): |
| 1646 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
entries = d.entries |
| 1652 |
llop.gc_writebarrier(lltype.Void, entries) |
| 1653 |
|
| 1654 |
ENTRIES = lltype.typeOf(d).TO.entries.TO |
| 1655 |
ENTRY = ENTRIES.OF |
| 1656 |
idst = isrc = d.num_ever_used_items - 1 |
| 1657 |
new_index = -1 |
| 1658 |
while isrc >= 0: |
| 1659 |
if entries.valid(isrc): |
| 1660 |
if isrc == old_index: |
| 1661 |
new_index = idst |
| 1662 |
src = entries[isrc] |
| 1663 |
dst = entries[idst] |
| 1664 |
dst.key = src.key |
| 1665 |
dst.value = src.value |
| 1666 |
if hasattr(ENTRY, 'f_hash'): |
| 1667 |
dst.f_hash = src.f_hash |
| 1668 |
if hasattr(ENTRY, 'f_valid'): |
| 1669 |
assert src.f_valid |
| 1670 |
dst.f_valid = True |
| 1671 |
idst -= 1 |
| 1672 |
isrc -= 1 |
| 1673 |
idst += 1 |
| 1674 |
assert d.num_ever_used_items - d.num_live_items == idst |
| 1675 |
assert new_index >= 0 |
| 1676 |
|
| 1677 |
i = 0 |
| 1678 |
while i < idst: |
| 1679 |
d.entries.mark_deleted(i) |
| 1680 |
i += 1 |
| 1681 |
d.lookup_function_no = ((d.lookup_function_no & FUNC_MASK) | |
| 1682 |
(idst << FUNC_SHIFT)) |
| 1683 |
return new_index |