Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
adding docs to code, getting pom files to build/package scaladocs and…
Browse files Browse the repository at this point in the history
… source jars
  • Loading branch information
philipsoutham committed Oct 8, 2015
1 parent ecac68e commit 4fde43c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 20 deletions.
50 changes: 47 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,46 @@
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>doc-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
Expand Down Expand Up @@ -67,7 +102,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<version>2.6</version>
<configuration>
<archive>
<index>true</index>
Expand All @@ -82,7 +117,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
Expand Down Expand Up @@ -131,4 +166,13 @@
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</reporting>
</project>
52 changes: 48 additions & 4 deletions scala-fnv_2.10-1.0.0.pom
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,53 @@
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.10</artifactId>
<version>2.2.5</version>
<version>3.0.0-M9</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>doc-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
Expand Down Expand Up @@ -67,7 +102,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<version>2.6</version>
<configuration>
<archive>
<index>true</index>
Expand All @@ -82,7 +117,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
Expand Down Expand Up @@ -131,4 +166,13 @@
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</reporting>
</project>
40 changes: 31 additions & 9 deletions src/main/scala/com/philipsoutham/hash/FNV.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.philipsoutham.hash

/**
* Object FNV implements FNV-1 and FNV-1a, non-cryptographic hash functions created by Glenn Fowler, Landon Curt Noll, and Phong Vo.
* See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function.
*/
object FNV {

private val INIT32 = BigInt("811c9dc5", 16);
Expand All @@ -8,14 +11,33 @@ object FNV {
private val PRIME64 = BigInt("100000001b3", 16);
private val MOD32 = BigInt("2").pow(32);
private val MOD64 = BigInt("2").pow(64);
private val Z = 0xff

@inline private def calc(prime: BigInt, mod: BigInt)(hash: BigInt, b: Byte): BigInt = ((hash * prime) % mod) ^ (b & Z)
@inline private def calcA(prime: BigInt, mod: BigInt)(hash: BigInt, b: Byte): BigInt = ((hash ^ (b & Z)) * prime) % mod
private val MASK = 0xff

@inline def hash32(data: Array[Byte]): BigInt = data.foldLeft(INIT32)(calc(PRIME32, MOD32))
@inline def hash32a(data: Array[Byte]): BigInt = data.foldLeft(INIT32)(calcA(PRIME32, MOD32))
@inline private final def calc(prime: BigInt, mod: BigInt)(hash: BigInt, b: Byte): BigInt = ((hash * prime) % mod) ^ (b & MASK)
@inline private final def calcA(prime: BigInt, mod: BigInt)(hash: BigInt, b: Byte): BigInt = ((hash ^ (b & MASK)) * prime) % mod

@inline def hash64(data: Array[Byte]): BigInt = data.foldLeft(INIT64)(calc(PRIME64, MOD64))
@inline def hash64a(data: Array[Byte]): BigInt = data.foldLeft(INIT64)(calcA(PRIME64, MOD64))
/**
* Calculates 32bit FNV-1 hash
* @param data the data to be hashed
* @return a 32bit hash value
*/
@inline final def hash32(data: Array[Byte]): BigInt = data.foldLeft(INIT32)(calc(PRIME32, MOD32))
/**
* Calculates 32bit FNV-1a hash
* @param data the data to be hashed
* @return a 32bit hash value
*/
@inline final def hash32a(data: Array[Byte]): BigInt = data.foldLeft(INIT32)(calcA(PRIME32, MOD32))
/**
* Calculates 64bit FNV-1 hash
* @param data the data to be hashed
* @return a 64bit hash value
*/
@inline final def hash64(data: Array[Byte]): BigInt = data.foldLeft(INIT64)(calc(PRIME64, MOD64))
/**
* Calculates 64bit FNV-1a hash
* @param data the data to be hashed
* @return a 64bit hash value
*/
@inline final def hash64a(data: Array[Byte]): BigInt = data.foldLeft(INIT64)(calcA(PRIME64, MOD64))
}
8 changes: 4 additions & 4 deletions src/test/scala/com/philipsoutham/hash/FNVSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -773,22 +773,22 @@ class FNVSpec extends UnitSpec {
assert(correctHash32a.length == 148)
assert(correctHash64a.length == 148)
}
"FNV hash32" must "match reference data" in {
"FNV hash32 results" must "match reference values" in {
data.zip(correctHash32).foreach { x =>
assert(FNV.hash32(x._1) == x._2)
}
}
"FNV hash32a" must "match reference data" in {
"FNV hash32a results" must "match reference values" in {
data.zip(correctHash32a).foreach { x =>
assert(FNV.hash32a(x._1) == x._2)
}
}
"FNV hash64" must "match reference data" in {
"FNV hash64 results" must "match reference values" in {
data.zip(correctHash64).foreach { x =>
assert(FNV.hash64(x._1) == x._2)
}
}
"FNV hash64a" must "match reference data" in {
"FNV hash64a results" must "match reference values" in {
data.zip(correctHash64a).foreach { x =>
assert(FNV.hash64a(x._1) == x._2)
}
Expand Down

0 comments on commit 4fde43c

Please sign in to comment.