def impl_integer(l, index, item): index = handle_index(l, index) castedindex = _cast(index, indexty) casteditem = _cast(item, itemty) status = _list_setitem(l, castedindex, casteditem) if status == ListStatus.LIST_OK: return else: raise AssertionError("internal list error during settitem")
def impl(d, key, value): castedkey = _cast(key, keyty) castedval = _cast(value, valty) status = _dict_insert(d, castedkey, hash(castedkey), castedval) if status == Status.OK: return elif status == Status.OK_REPLACED: # replaced # XXX handle refcount return elif status == Status.ERR_CMP_FAILED: raise ValueError('key comparison failed') else: raise RuntimeError('dict.__setitem__ failed unexpectedly')
def impl(l, item, start=None, end=None): casteditem = _cast(item, itemty) for i in handle_slice(l, slice(start, end, 1)): if l[i] == casteditem: return i else: raise ValueError("item not in list")
def impl(l, item): casteditem = _cast(item, itemty) total = 0 for i in l: if i == casteditem: total += 1 return total
def impl(l, item): casteditem = _cast(item, itemty) for i in l: if i == casteditem: return True else: return False
def impl(l, item): casteditem = _cast(item, itemty) for i, n in enumerate(l): if casteditem == n: l.pop(i) return else: raise ValueError("list.remove(x): x not in list")
def integer_impl(l, index): index = handle_index(l, index) castedindex = _cast(index, indexty) status, item = _list_getitem(l, castedindex) if status == ListStatus.LIST_OK: return _nonoptional(item) else: raise AssertionError("internal list error during getitem")
def impl(l, item): casteditem = _cast(item, itemty) status = _list_append(l, casteditem) if status == ListStatus.LIST_OK: return elif status == ListStatus.LIST_ERR_NO_MEMORY: raise MemoryError('Unable to allocate memory to append item') else: raise RuntimeError('list.append failed unexpectedly')
def impl(d, key): castedkey = _cast(key, keyty) ix, val = _dict_lookup(d, castedkey, hash(castedkey)) if ix == DKIX.EMPTY: raise KeyError() elif ix < DKIX.EMPTY: raise AssertionError("internal dict error during lookup") else: return _nonoptional(val)
def impl(l, index=-1): if len(l) == 0: raise IndexError("pop from empty list") index = handle_index(l, index) castedindex = _cast(index, indexty) status, item = _list_pop(l, castedindex) if status == ListStatus.LIST_OK: return _nonoptional(item) else: raise AssertionError("internal list error during pop")
def impl_type_matched(da, db): if len(da) != len(db): return False for ka, va in da.items(): # Cast key from LHS to the key-type of RHS kb = _cast(ka, otherkeyty) ix, vb = _dict_lookup(db, kb, hash(kb)) if ix <= DKIX.EMPTY: # Quit early if the key is not found return False if va != vb: # Quit early if the values do not match return False return True
def impl(dct, key, default=None): castedkey = _cast(key, keyty) hashed = hash(castedkey) ix, val = _dict_lookup(dct, castedkey, hashed) if ix == DKIX.EMPTY: if should_raise: raise KeyError() else: return default elif ix < DKIX.EMPTY: raise AssertionError("internal dict error during lookup") else: status = _dict_delitem(dct,hashed, ix) if status != Status.OK: raise AssertionError("internal dict error during delitem") return val
def impl(d, k): k = _cast(k, keyty) ix, val = _dict_lookup(d, k, hash(k)) return ix > DKIX.EMPTY
def impl(dct, key, default=None): castedkey = _cast(key, keyty) ix, val = _dict_lookup(dct, key, hash(castedkey)) if ix > DKIX.EMPTY: return val return default