Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
ffe911d2
Commit
ffe911d2
authored
Jul 24, 2014
by
Damien George
Browse files
py: Make long ints hashable.
Addresses issue #765.
parent
4ecb700f
Changes
7
Hide whitespace changes
Inline
Side-by-side
py/mpz.c
View file @
ffe911d2
...
...
@@ -1213,6 +1213,22 @@ mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) {
}
#endif
// must return actual int value if it fits in mp_int_t
mp_int_t
mpz_hash
(
const
mpz_t
*
z
)
{
mp_int_t
val
=
0
;
mpz_dig_t
*
d
=
z
->
dig
+
z
->
len
;
while
(
--
d
>=
z
->
dig
)
{
val
=
(
val
<<
DIG_SIZE
)
|
*
d
;
}
if
(
z
->
neg
!=
0
)
{
val
=
-
val
;
}
return
val
;
}
// TODO check that this correctly handles overflow in all cases
mp_int_t
mpz_as_int
(
const
mpz_t
*
i
)
{
mp_int_t
val
=
0
;
...
...
py/mpz.h
View file @
ffe911d2
...
...
@@ -96,6 +96,7 @@ void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const m
mpz_t
*
mpz_div
(
const
mpz_t
*
lhs
,
const
mpz_t
*
rhs
);
mpz_t
*
mpz_mod
(
const
mpz_t
*
lhs
,
const
mpz_t
*
rhs
);
mp_int_t
mpz_hash
(
const
mpz_t
*
z
);
mp_int_t
mpz_as_int
(
const
mpz_t
*
z
);
bool
mpz_as_int_checked
(
const
mpz_t
*
z
,
mp_int_t
*
value
);
#if MICROPY_PY_BUILTINS_FLOAT
...
...
py/obj.c
View file @
ffe911d2
...
...
@@ -24,6 +24,7 @@
* THE SOFTWARE.
*/
#include
<stdint.h>
#include
<stdio.h>
#include
<stdarg.h>
#include
<assert.h>
...
...
@@ -33,6 +34,8 @@
#include
"misc.h"
#include
"qstr.h"
#include
"obj.h"
#include
"mpz.h"
#include
"objint.h"
#include
"runtime0.h"
#include
"runtime.h"
#include
"stackctrl.h"
...
...
@@ -152,6 +155,8 @@ mp_int_t mp_obj_hash(mp_obj_t o_in) {
return
1
;
// needs to hash to same as the integer 1, since True==1
}
else
if
(
MP_OBJ_IS_SMALL_INT
(
o_in
))
{
return
MP_OBJ_SMALL_INT_VALUE
(
o_in
);
}
else
if
(
MP_OBJ_IS_TYPE
(
o_in
,
&
mp_type_int
))
{
return
mp_obj_int_hash
(
o_in
);
}
else
if
(
MP_OBJ_IS_STR
(
o_in
)
||
MP_OBJ_IS_TYPE
(
o_in
,
&
mp_type_bytes
))
{
return
mp_obj_str_get_hash
(
o_in
);
}
else
if
(
MP_OBJ_IS_TYPE
(
o_in
,
&
mp_type_NoneType
))
{
...
...
py/objint.c
View file @
ffe911d2
...
...
@@ -215,6 +215,10 @@ char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_ob
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
mp_int_t
mp_obj_int_hash
(
mp_obj_t
self_in
)
{
return
MP_OBJ_SMALL_INT_VALUE
(
self_in
);
}
bool
mp_obj_int_is_positive
(
mp_obj_t
self_in
)
{
return
mp_obj_get_int
(
self_in
)
>=
0
;
}
...
...
py/objint.h
View file @
ffe911d2
...
...
@@ -38,6 +38,7 @@ char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_ob
int
base
,
const
char
*
prefix
,
char
base_char
,
char
comma
);
char
*
mp_obj_int_formatted_impl
(
char
**
buf
,
int
*
buf_size
,
int
*
fmt_size
,
mp_const_obj_t
self_in
,
int
base
,
const
char
*
prefix
,
char
base_char
,
char
comma
);
mp_int_t
mp_obj_int_hash
(
mp_obj_t
self_in
);
bool
mp_obj_int_is_positive
(
mp_obj_t
self_in
);
mp_obj_t
mp_obj_int_unary_op
(
int
op
,
mp_obj_t
o_in
);
mp_obj_t
mp_obj_int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
);
...
...
py/objint_longlong.c
View file @
ffe911d2
...
...
@@ -55,6 +55,16 @@
const
mp_obj_int_t
mp_maxsize_obj
=
{{
&
mp_type_int
},
INT_MAX
};
#endif
mp_int_t
mp_obj_int_hash
(
mp_obj_t
self_in
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
return
MP_OBJ_SMALL_INT_VALUE
(
self_in
);
}
mp_obj_int_t
*
self
=
self_in
;
// truncate value to fit in mp_int_t, which gives the same hash as
// small int if the value fits without truncation
return
self
->
val
;
}
bool
mp_obj_int_is_positive
(
mp_obj_t
self_in
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
return
MP_OBJ_SMALL_INT_VALUE
(
self_in
)
>=
0
;
...
...
py/objint_mpz.c
View file @
ffe911d2
...
...
@@ -96,6 +96,14 @@ char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_con
return
str
;
}
mp_int_t
mp_obj_int_hash
(
mp_obj_t
self_in
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
return
MP_OBJ_SMALL_INT_VALUE
(
self_in
);
}
mp_obj_int_t
*
self
=
self_in
;
return
mpz_hash
(
&
self
->
mpz
);
}
bool
mp_obj_int_is_positive
(
mp_obj_t
self_in
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
return
MP_OBJ_SMALL_INT_VALUE
(
self_in
)
>=
0
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment