-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
deep hash code for byte[] and other array in tuple #746
base: master
Are you sure you want to change the base?
Changes from 3 commits
7870b26
03c130a
b846ea1
92e30be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,33 @@ | |
|
||
(bootstrap) | ||
|
||
(def ARRAY-TYPES-MAP | ||
{ | ||
(type (to-array [])) nil | ||
(type (byte-array 0)) nil | ||
(type (char-array 0)) nil | ||
(type (short-array 0)) nil | ||
(type (int-array 0)) nil | ||
(type (long-array 0)) nil | ||
(type (float-array 0)) nil | ||
(type (double-array 0)) nil | ||
(type (boolean-array 0)) nil | ||
}) | ||
|
||
(defn deep-hash-code [alist] | ||
"deep hash code based on array/list content. | ||
it's the same as java.util.Arrays.deepHashCode(), | ||
but without convert a list to array to avoid copy." | ||
(reduce | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can make this code much faster by not doing the reduce (which is constantly boxing and unboxing the numbers) and instead iterating through the list. It's doable in Clojure, though I'm ok with writing this code in Java and then calling it from here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean ".intValue" and "+" cause constantly boxing and unboxing the numbers? Can we use unchecked-add-int and unchecked-multiply-int to avoid boxing and unboxing? |
||
#(.intValue (+ (.intValue (* 31 %1)) (hash-code %2))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this even compile? hash-code isn't declared anywhere There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am really sorry that the code cause compiling error and I have not tested it carefully. I just tested it in Clojure REPL using load-file. I'll fix it and test it on cluster. |
||
1 alist)) | ||
|
||
(defn hash-code [obj] | ||
(let [t (type obj)] | ||
(if (contains? ARRAY-TYPES-MAP t) | ||
(deep-hash-code obj) | ||
(.hashCode obj)))) | ||
|
||
(defn list-hash-code [^List alist] | ||
(.hashCode alist)) | ||
(deep-hash-code alist)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a set. The syntax is #{}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It should be a set instead of map. It's due to my mis-understanding that contains? only work with map.