Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
cf0c37d1
Commit
cf0c37d1
authored
May 29, 2014
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement tuple hashing
parent
ccc8fda6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
0 deletions
+20
-0
src/runtime/tuple.cpp
src/runtime/tuple.cpp
+16
-0
test/tests/tuples.py
test/tests/tuples.py
+4
-0
No files found.
src/runtime/tuple.cpp
View file @
cf0c37d1
...
...
@@ -19,6 +19,8 @@
#include "core/stats.h"
#include "core/types.h"
#include "codegen/compvars.h"
#include "runtime/gc_runtime.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
...
...
@@ -174,6 +176,19 @@ Box* tupleContains(BoxedTuple* self, Box* elt) {
return
False
;
}
Box
*
tupleHash
(
BoxedTuple
*
self
)
{
assert
(
self
->
cls
==
tuple_cls
);
int64_t
rtn
=
3527539
;
for
(
Box
*
e
:
self
->
elts
)
{
BoxedInt
*
h
=
hash
(
e
);
assert
(
h
->
cls
==
int_cls
);
rtn
^=
h
->
n
+
0x9e3779b9
+
(
rtn
<<
6
)
+
(
rtn
>>
2
);
}
return
boxInt
(
rtn
);
}
void
setupTuple
()
{
tuple_cls
->
giveAttr
(
"__name__"
,
boxStrConstant
(
"tuple"
));
...
...
@@ -187,6 +202,7 @@ void setupTuple() {
tuple_cls
->
giveAttr
(
"__eq__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleEq
,
NULL
,
2
,
false
)));
tuple_cls
->
giveAttr
(
"__ne__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleNe
,
NULL
,
2
,
false
)));
tuple_cls
->
giveAttr
(
"__hash__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleHash
,
BOXED_INT
,
1
,
false
)));
tuple_cls
->
giveAttr
(
"__len__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleLen
,
NULL
,
1
,
false
)));
tuple_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
tupleRepr
,
NULL
,
1
,
false
)));
tuple_cls
->
setattr
(
"__str__"
,
tuple_cls
->
peekattr
(
"__repr__"
),
NULL
,
NULL
);
...
...
test/tests/tuples.py
View file @
cf0c37d1
...
...
@@ -47,6 +47,9 @@ class T(object):
print
"eq"
,
self
.
n
,
rhs
.
n
return
self
.
n
==
rhs
.
n
def
__hash__
(
self
):
return
hash
(
self
.
n
)
def
__repr__
(
self
):
return
"<T>"
...
...
@@ -57,6 +60,7 @@ def t(l, r):
print
l
>=
r
print
l
==
r
print
l
!=
r
print
"same hash: "
,
hash
(
l
)
==
hash
(
r
)
t
(
T
(
1
),
T
(
2
))
t
(
T
(
1
),
T
(
1
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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