| 1 |
from pypy.interpreter.error import oefmt |
| 2 |
from pypy.objspace.std.tupleobject import (W_AbstractTupleObject, |
| 3 |
XXPRIME_1, XXPRIME_2, XXPRIME_5, xxrotate, uhash_type) |
| 4 |
from pypy.objspace.std.util import negate |
| 5 |
from rpython.rlib import jit |
| 6 |
from rpython.rlib.objectmodel import specialize |
| 7 |
from rpython.rlib.rarithmetic import intmask |
| 8 |
from rpython.rlib.unroll import unrolling_iterable |
| 9 |
from rpython.tool.sourcetools import func_with_new_name |
| 10 |
from rpython.rlib.longlong2float import float2longlong |
| 11 |
|
| 12 |
UNROLL_CUTOFF = 10 |
| 13 |
|
| 14 |
class NotSpecialised(Exception): |
| 15 |
pass |
| 16 |
|
| 17 |
|
| 18 |
def make_specialised_class(typetuple): |
| 19 |
assert type(typetuple) == tuple |
| 20 |
wraps = [] |
| 21 |
for typ in typetuple: |
| 22 |
if typ == int: |
| 23 |
wraps.append(lambda space, x: space.newint(x)) |
| 24 |
elif typ == float: |
| 25 |
wraps.append(lambda space, x: space.newfloat(x)) |
| 26 |
elif typ == object: |
| 27 |
wraps.append(lambda space, w_x: w_x) |
| 28 |
else: |
| 29 |
assert 0 |
| 30 |
|
| 31 |
typelen = len(typetuple) |
| 32 |
iter_n = unrolling_iterable(range(typelen)) |
| 33 |
|
| 34 |
class cls(W_AbstractTupleObject): |
| 35 |
_immutable_fields_ = ['value%s' % i for i in iter_n] |
| 36 |
|
| 37 |
def __init__(self, space, *values): |
| 38 |
self.space = space |
| 39 |
assert len(values) == typelen |
| 40 |
for i in iter_n: |
| 41 |
obj = values[i] |
| 42 |
val_type = typetuple[i] |
| 43 |
if val_type == int: |
| 44 |
assert isinstance(obj, int) |
| 45 |
elif val_type == float: |
| 46 |
assert isinstance(obj, float) |
| 47 |
elif val_type == str: |
| 48 |
assert isinstance(obj, str) |
| 49 |
elif val_type == object: |
| 50 |
pass |
| 51 |
else: |
| 52 |
raise AssertionError |
| 53 |
setattr(self, 'value%s' % i, obj) |
| 54 |
|
| 55 |
def length(self): |
| 56 |
return typelen |
| 57 |
|
| 58 |
def tolist(self): |
| 59 |
list_w = [None] * typelen |
| 60 |
for i in iter_n: |
| 61 |
value = getattr(self, 'value%s' % i) |
| 62 |
value = wraps[i](self.space, value) |
| 63 |
list_w[i] = value |
| 64 |
return list_w |
| 65 |
|
| 66 |
|
| 67 |
getitems_copy = func_with_new_name(tolist, 'getitems_copy') |
| 68 |
|
| 69 |
def descr_hash(self, space): |
| 70 |
acc = XXPRIME_5 |
| 71 |
|
| 72 |
for i in iter_n: |
| 73 |
value = getattr(self, 'value%s' % i) |
| 74 |
if typetuple[i] == object: |
| 75 |
lane = uhash_type(space.int_w(space.hash(value))) |
| 76 |
elif typetuple[i] == float: |
| 77 |
|
| 78 |
|
| 79 |
from pypy.objspace.std.floatobject import _hash_float, default_identity_hash |
| 80 |
from math import isnan |
| 81 |
if isnan(value): |
| 82 |
w_hash = default_identity_hash(space, space.newfloat(value)) |
| 83 |
lane = uhash_type(space.int_w(w_hash)) |
| 84 |
else: |
| 85 |
lane = uhash_type(_hash_float(value)) |
| 86 |
elif typetuple[i] == int: |
| 87 |
|
| 88 |
|
| 89 |
from pypy.objspace.std.intobject import _hash_int |
| 90 |
lane = uhash_type(_hash_int(value)) |
| 91 |
else: |
| 92 |
raise NotImplementedError |
| 93 |
|
| 94 |
acc += lane * XXPRIME_2 |
| 95 |
acc = xxrotate(acc) |
| 96 |
acc *= XXPRIME_1 |
| 97 |
|
| 98 |
acc += typelen ^ (XXPRIME_5 ^ uhash_type(3527539)) |
| 99 |
acc += (acc == uhash_type(-1)) * uhash_type(1546275796 + 1) |
| 100 |
return space.newint(intmask(acc)) |
| 101 |
|
| 102 |
def descr_eq(self, space, w_other): |
| 103 |
if not isinstance(w_other, W_AbstractTupleObject): |
| 104 |
return space.w_NotImplemented |
| 105 |
if not isinstance(w_other, cls): |
| 106 |
if typelen != w_other.length(): |
| 107 |
return space.w_False |
| 108 |
for i in iter_n: |
| 109 |
myval = getattr(self, 'value%s' % i) |
| 110 |
otherval = w_other.getitem(space, i) |
| 111 |
myval = wraps[i](self.space, myval) |
| 112 |
if not space.eq_w(myval, otherval): |
| 113 |
return space.w_False |
| 114 |
return space.w_True |
| 115 |
|
| 116 |
for i in iter_n: |
| 117 |
myval = getattr(self, 'value%s' % i) |
| 118 |
otherval = getattr(w_other, 'value%s' % i) |
| 119 |
if typetuple[i] == object: |
| 120 |
if not self.space.eq_w(myval, otherval): |
| 121 |
return space.w_False |
| 122 |
else: |
| 123 |
if myval != otherval: |
| 124 |
if typetuple[i] == float: |
| 125 |
|
| 126 |
if (float2longlong(myval) == |
| 127 |
float2longlong(otherval)): |
| 128 |
continue |
| 129 |
return space.w_False |
| 130 |
return space.w_True |
| 131 |
|
| 132 |
descr_ne = negate(descr_eq) |
| 133 |
|
| 134 |
def getitem(self, space, index): |
| 135 |
if index < 0: |
| 136 |
index += typelen |
| 137 |
for i in iter_n: |
| 138 |
if index == i: |
| 139 |
value = getattr(self, 'value%s' % i) |
| 140 |
value = wraps[i](self.space, value) |
| 141 |
return value |
| 142 |
raise oefmt(space.w_IndexError, "tuple index out of range") |
| 143 |
|
| 144 |
def _unroll_condition(self): |
| 145 |
return jit.loop_unrolling_heuristic( |
| 146 |
self, typelen, UNROLL_CUTOFF) |
| 147 |
|
| 148 |
|
| 149 |
cls.__name__ = ('W_SpecialisedTupleObject_' + |
| 150 |
''.join([t.__name__[0] for t in typetuple])) |
| 151 |
_specialisations.append(cls) |
| 152 |
return cls |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
_specialisations = [] |
| 157 |
Cls_ii = make_specialised_class((int, int)) |
| 158 |
Cls_oo = make_specialised_class((object, object)) |
| 159 |
Cls_ff = make_specialised_class((float, float)) |
| 160 |
|
| 161 |
def makespecialisedtuple(space, list_w): |
| 162 |
|
| 163 |
if len(list_w) == 2: |
| 164 |
w_arg1, w_arg2 = list_w |
| 165 |
return makespecialisedtuple2(space, w_arg1, w_arg2) |
| 166 |
else: |
| 167 |
raise NotSpecialised |
| 168 |
|
| 169 |
def makespecialisedtuple2(space, w_arg1, w_arg2): |
| 170 |
from pypy.objspace.std.listobject import is_plain_int1, plain_int_w |
| 171 |
from pypy.objspace.std.floatobject import W_FloatObject |
| 172 |
if is_plain_int1(w_arg1): |
| 173 |
if is_plain_int1(w_arg2): |
| 174 |
return Cls_ii(space, plain_int_w(space, w_arg1), |
| 175 |
plain_int_w(space, w_arg2)) |
| 176 |
elif type(w_arg1) is W_FloatObject: |
| 177 |
if type(w_arg2) is W_FloatObject: |
| 178 |
return Cls_ff(space, space.float_w(w_arg1), space.float_w(w_arg2)) |
| 179 |
return Cls_oo(space, w_arg1, w_arg2) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@specialize.arg(1) |
| 193 |
def _build_zipped_spec(space, Cls, lst1, lst2): |
| 194 |
length = min(len(lst1), len(lst2)) |
| 195 |
return [Cls(space, lst1[i], lst2[i]) for i in range(length)] |
| 196 |
|
| 197 |
def _build_zipped_spec_oo(space, w_list1, w_list2): |
| 198 |
strat1 = w_list1.strategy |
| 199 |
strat2 = w_list2.strategy |
| 200 |
length = min(strat1.length(w_list1), strat2.length(w_list2)) |
| 201 |
return [Cls_oo(space, strat1.getitem(w_list1, i), |
| 202 |
strat2.getitem(w_list2, i)) for i in range(length)] |
| 203 |
|
| 204 |
def _build_zipped_unspec(space, w_list1, w_list2): |
| 205 |
strat1 = w_list1.strategy |
| 206 |
strat2 = w_list2.strategy |
| 207 |
length = min(strat1.length(w_list1), strat2.length(w_list2)) |
| 208 |
return [space.newtuple2(strat1.getitem(w_list1, i), |
| 209 |
strat2.getitem(w_list2, i)) for i in range(length)] |
| 210 |
|
| 211 |
def specialized_zip_2_lists(space, w_list1, w_list2): |
| 212 |
from pypy.objspace.std.listobject import W_ListObject |
| 213 |
if type(w_list1) is not W_ListObject or type(w_list2) is not W_ListObject: |
| 214 |
raise oefmt(space.w_TypeError, "expected two exact lists") |
| 215 |
|
| 216 |
if space.config.objspace.std.withspecialisedtuple: |
| 217 |
intlist1 = w_list1.getitems_int() |
| 218 |
if intlist1 is not None: |
| 219 |
intlist2 = w_list2.getitems_int() |
| 220 |
if intlist2 is not None: |
| 221 |
lst_w = _build_zipped_spec( |
| 222 |
space, Cls_ii, intlist1, intlist2) |
| 223 |
return space.newlist(lst_w) |
| 224 |
else: |
| 225 |
floatlist1 = w_list1.getitems_float() |
| 226 |
if floatlist1 is not None: |
| 227 |
floatlist2 = w_list2.getitems_float() |
| 228 |
if floatlist2 is not None: |
| 229 |
lst_w = _build_zipped_spec( |
| 230 |
space, Cls_ff, floatlist1, floatlist2) |
| 231 |
return space.newlist(lst_w) |
| 232 |
|
| 233 |
lst_w = _build_zipped_spec_oo(space, w_list1, w_list2) |
| 234 |
return space.newlist(lst_w) |
| 235 |
|
| 236 |
else: |
| 237 |
lst_w = _build_zipped_unspec(space, w_list1, w_list2) |
| 238 |
return space.newlist(lst_w) |