sk0x50 commented on a change in pull request #9177:
URL: https://github.com/apache/ignite/pull/9177#discussion_r661363591
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12106,4 +12106,33 @@ public static long uncompressedSize(File zip) throws
IOException {
return size;
}
}
+
+ /**
+ * Map object hash to some index between 0 and specified size via modulo
operation.
+ *
+ * @param hash Object hash.
+ * @param size Size greater than 0.
+ * @return Calculated index in range [0..size).
+ */
+ public static int hashToIndex(int hash, int size) {
+ return safeAbs(hash % size);
+ }
+
+ /**
+ * Map object hash to some index between 0 and specified size using
+ * approach similar to {@link HashMap#hash(Object)}. In case
+ * <code>size</code> is not a power of 2 we fallback to modulo operation.
+ *
+ * @param hash Object hash.
+ * @param size Size greater than 0.
+ * @return Calculated index in range [0..size).
+ */
+ public static int hashToIndexOpt(int hash, int size, boolean isSizePow2) {
+ int h = (hash ^ (hash >>> 16));
+
+ return isSizePow2
Review comment:
Well, this implementation relies on the user input and does not
explicitly check the fact that the size variable is a power of two.
It seems to me, it is easy to write a code as follows: hashToIndex(hash, 9,
true) that leads to that only two results can be returned in this case - 0 and
8.
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12106,4 +12106,33 @@ public static long uncompressedSize(File zip) throws
IOException {
return size;
}
}
+
+ /**
+ * Map object hash to some index between 0 and specified size via modulo
operation.
+ *
+ * @param hash Object hash.
+ * @param size Size greater than 0.
+ * @return Calculated index in range [0..size).
+ */
+ public static int hashToIndex(int hash, int size) {
+ return safeAbs(hash % size);
+ }
+
+ /**
+ * Map object hash to some index between 0 and specified size using
+ * approach similar to {@link HashMap#hash(Object)}. In case
+ * <code>size</code> is not a power of 2 we fallback to modulo operation.
+ *
+ * @param hash Object hash.
+ * @param size Size greater than 0.
+ * @return Calculated index in range [0..size).
+ */
+ public static int hashToIndexOpt(int hash, int size, boolean isSizePow2) {
Review comment:
Well, I don't think that you need to provide a unique name here,
overloading is enough.
##########
File path:
modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12106,4 +12106,33 @@ public static long uncompressedSize(File zip) throws
IOException {
return size;
}
}
+
+ /**
+ * Map object hash to some index between 0 and specified size via modulo
operation.
+ *
+ * @param hash Object hash.
+ * @param size Size greater than 0.
+ * @return Calculated index in range [0..size).
+ */
+ public static int hashToIndex(int hash, int size) {
+ return safeAbs(hash % size);
+ }
+
+ /**
+ * Map object hash to some index between 0 and specified size using
+ * approach similar to {@link HashMap#hash(Object)}. In case
+ * <code>size</code> is not a power of 2 we fallback to modulo operation.
+ *
+ * @param hash Object hash.
+ * @param size Size greater than 0.
+ * @return Calculated index in range [0..size).
Review comment:
Please add to Javadoc a new parameter which is `isSizePow2`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]