You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/11/21 17:38:49 UTC

[commons-codec] branch master updated (03e5497 -> 5204e89)

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git.


    from 03e5497  Added @since 1.14 to MurmurHash3 additions.
     new 31e094a  Javadoc clean-up.
     new 5204e89  Moved magic numbers to constants.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/commons/codec/digest/MurmurHash2.java   | 118 ++++++++++-----------
 1 file changed, 59 insertions(+), 59 deletions(-)


[commons-codec] 01/02: Javadoc clean-up.

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 31e094ab90d238cfbd98b9ed8c715d997f58123a
Author: aherbert <ah...@apache.org>
AuthorDate: Thu Nov 21 17:36:22 2019 +0000

    Javadoc clean-up.
    
    Removed 'offset' from javadoc examples.
    Change 32 bit to 32-bit.
    Change 64 bit to 64-bit.
    Change descriptions to match those in MurmurHash3.
---
 .../apache/commons/codec/digest/MurmurHash2.java   | 72 +++++++++++-----------
 1 file changed, 36 insertions(+), 36 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
index e349ff9..abf0cfc 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
@@ -53,12 +53,12 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 32 bit hash from byte array with the given length and seed.
+     * Generates a 32-bit hash from byte array with the given length and seed.
      *
-     * @param data   byte array to hash
-     * @param length length of the array to hash
-     * @param seed   initial seed value
-     * @return 32 bit hash of the given array
+     * @param data The input byte array
+     * @param length The length of the array
+     * @param seed The initial seed value
+     * @return The 32-bit hash
      */
     public static int hash32(final byte[] data, final int length, final int seed) {
         // 'm' and 'r' are mixing constants generated offline.
@@ -105,17 +105,17 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 32 bit hash from byte array with the given length and a default seed value.
+     * Generates a 32-bit hash from byte array with the given length and a default seed value.
      * This is a helper method that will produce the same result as:
      *
      * <pre>
      * int seed = 0x9747b28c;
-     * int hash = MurmurHash2.hash32(data, offset, length, seed);
+     * int hash = MurmurHash2.hash32(data, length, seed);
      * </pre>
      *
-     * @param data   byte array to hash
-     * @param length length of the array to hash
-     * @return 32 bit hash of the given array
+     * @param data The input byte array
+     * @param length The length of the array
+     * @return The 32-bit hash
      * @see #hash32(byte[], int, int)
      */
     public static int hash32(final byte[] data, final int length) {
@@ -123,7 +123,7 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 32 bit hash from a string with a default seed.
+     * Generates a 32-bit hash from a string with a default seed.
      * The string is converted to bytes using the default encoding.
      * This is a helper method that will produce the same result as:
      *
@@ -133,8 +133,8 @@ public final class MurmurHash2 {
      * int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
      * </pre>
      *
-     * @param text string to hash
-     * @return 32 bit hash of the given string
+     * @param text The input string
+     * @return The 32-bit hash
      * @see #hash32(byte[], int, int)
      */
     public static int hash32(final String text) {
@@ -143,7 +143,7 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 32 bit hash from a substring with a default seed value.
+     * Generates a 32-bit hash from a substring with a default seed value.
      * The string is converted to bytes using the default encoding.
      * This is a helper method that will produce the same result as:
      *
@@ -153,10 +153,10 @@ public final class MurmurHash2 {
      * int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
      * </pre>
      *
-     * @param text   string to hash
-     * @param from   starting index
-     * @param length length of the substring to hash
-     * @return 32 bit hash of the given string
+     * @param text The input string
+     * @param from The starting index
+     * @param length The length of the substring
+     * @return The 32-bit hash
      * @see #hash32(byte[], int, int)
      */
     public static int hash32(final String text, final int from, final int length) {
@@ -164,12 +164,12 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 64 bit hash from byte array of the given length and seed.
+     * Generates a 64-bit hash from byte array of the given length and seed.
      *
-     * @param data   byte array to hash
-     * @param length length of the array to hash
-     * @param seed   initial seed value
-     * @return 64 bit hash of the given array
+     * @param data The input byte array
+     * @param length The length of the array
+     * @param seed The initial seed value
+     * @return The 64-bit hash of the given array
      */
     public static long hash64(final byte[] data, final int length, final int seed) {
         final long m = 0xc6a4a7935bd1e995L;
@@ -219,17 +219,17 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 64 bit hash from byte array with given length and a default seed value.
+     * Generates a 64-bit hash from byte array with given length and a default seed value.
      * This is a helper method that will produce the same result as:
      *
      * <pre>
      * int seed = 0xe17a1465;
-     * int hash = MurmurHash2.hash64(data, offset, length, seed);
+     * int hash = MurmurHash2.hash64(data, length, seed);
      * </pre>
      *
-     * @param data   byte array to hash
-     * @param length length of the array to hash
-     * @return 64 bit hash of the given string
+     * @param data The input byte array
+     * @param length The length of the array
+     * @return The 64-bit hash
      * @see #hash64(byte[], int, int)
      */
     public static long hash64(final byte[] data, final int length) {
@@ -237,7 +237,7 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 64 bit hash from a string with a default seed.
+     * Generates a 64-bit hash from a string with a default seed.
      * The string is converted to bytes using the default encoding.
      * This is a helper method that will produce the same result as:
      *
@@ -247,8 +247,8 @@ public final class MurmurHash2 {
      * int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
      * </pre>
      *
-     * @param text string to hash
-     * @return 64 bit hash of the given string
+     * @param text The input string
+     * @return The 64-bit hash
      * @see #hash64(byte[], int, int)
      */
     public static long hash64(final String text) {
@@ -257,7 +257,7 @@ public final class MurmurHash2 {
     }
 
     /**
-     * Generates 64 bit hash from a substring with a default seed value.
+     * Generates a 64-bit hash from a substring with a default seed value.
      * The string is converted to bytes using the default encoding.
      * This is a helper method that will produce the same result as:
      *
@@ -267,10 +267,10 @@ public final class MurmurHash2 {
      * int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
      * </pre>
      *
-     * @param text   string to hash
-     * @param from   starting index
-     * @param length length of the substring to hash
-     * @return 64 bit hash of the given array
+     * @param text The The input string
+     * @param from The starting index
+     * @param length The length of the substring
+     * @return The 64-bit hash
      * @see #hash64(byte[], int, int)
      */
     public static long hash64(final String text, final int from, final int length) {


[commons-codec] 02/02: Moved magic numbers to constants.

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 5204e89987d2cb9de75207226f97452578eb024f
Author: aherbert <ah...@apache.org>
AuthorDate: Thu Nov 21 17:38:45 2019 +0000

    Moved magic numbers to constants.
---
 .../apache/commons/codec/digest/MurmurHash2.java   | 46 +++++++++++-----------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
index abf0cfc..bbbb142 100644
--- a/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
+++ b/src/main/java/org/apache/commons/codec/digest/MurmurHash2.java
@@ -48,6 +48,14 @@ package org.apache.commons.codec.digest;
  */
 public final class MurmurHash2 {
 
+    // Constants for 32-bit variant
+    private static final int M32 = 0x5bd1e995;
+    private static final int R32 = 24;
+
+    // Constants for 64-bit variant
+    private static final long M64 = 0xc6a4a7935bd1e995L;
+    private static final int R64 = 47;
+
     /** No instance methods. */
     private MurmurHash2() {
     }
@@ -61,11 +69,6 @@ public final class MurmurHash2 {
      * @return The 32-bit hash
      */
     public static int hash32(final byte[] data, final int length, final int seed) {
-        // 'm' and 'r' are mixing constants generated offline.
-        // They're not really 'magic', they just happen to work well.
-        final int m = 0x5bd1e995;
-        final int r = 24;
-
         // Initialize the hash to a random value
         int h = seed ^ length;
 
@@ -76,10 +79,10 @@ public final class MurmurHash2 {
         for (int i = 0; i < nblocks; i++) {
             final int index = (i << 2);
             int k = getLittleEndianInt(data, index);
-            k *= m;
-            k ^= k >>> r;
-            k *= m;
-            h *= m;
+            k *= M32;
+            k ^= k >>> R32;
+            k *= M32;
+            h *= M32;
             h ^= k;
         }
 
@@ -92,13 +95,13 @@ public final class MurmurHash2 {
             h ^= (data[index + 1] & 0xff) << 8;
         case 1:
             h ^= (data[index] & 0xff);
-            h *= m;
+            h *= M32;
         }
 
         // Do a few final mixes of the hash to ensure the last few
         // bytes are well-incorporated.
         h ^= h >>> 13;
-        h *= m;
+        h *= M32;
         h ^= h >>> 15;
 
         return h;
@@ -172,10 +175,7 @@ public final class MurmurHash2 {
      * @return The 64-bit hash of the given array
      */
     public static long hash64(final byte[] data, final int length, final int seed) {
-        final long m = 0xc6a4a7935bd1e995L;
-        final int r = 47;
-
-        long h = (seed & 0xffffffffl) ^ (length * m);
+        long h = (seed & 0xffffffffL) ^ (length * M64);
 
         final int nblocks = length >> 3;
 
@@ -184,12 +184,12 @@ public final class MurmurHash2 {
             final int index = (i << 3);
             long k = getLittleEndianLong(data, index);
 
-            k *= m;
-            k ^= k >>> r;
-            k *= m;
+            k *= M64;
+            k ^= k >>> R64;
+            k *= M64;
 
             h ^= k;
-            h *= m;
+            h *= M64;
         }
 
         final int index = (nblocks << 3);
@@ -208,12 +208,12 @@ public final class MurmurHash2 {
             h ^= ((long) data[index + 1] & 0xff) << 8;
         case 1:
             h ^= ((long) data[index] & 0xff);
-            h *= m;
+            h *= M64;
         }
 
-        h ^= h >>> r;
-        h *= m;
-        h ^= h >>> r;
+        h ^= h >>> R64;
+        h *= M64;
+        h ^= h >>> R64;
 
         return h;
     }