Skip to content

Commit

Permalink
feat: re-version, update to latest edition, github actions
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Zak <[email protected]>
  • Loading branch information
rjzak committed Aug 22, 2023
1 parent a767aec commit e3ed5b7
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 90 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @rjzak
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
reviewers:
- "rjzak"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
reviewers:
- "rjzak"
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Test
on: [ push, pull_request ]
jobs:
native:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Rust toolchain
run: rustup show && rustup update
- name: Build murmurhash3
run: cargo build
- name: cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
/target
/Cargo.lock
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]

name = "murmurhash3"
version = "0.0.6"
authors = ["mhallin <[email protected]>"]
name = "malwaredb-murmurhash3"
version = "0.1.0"
authors = ["mhallin <[email protected]>", "Richard Zak <[email protected]>"]
edition = "2021"
description = "MurmurHash3 implementation"
license = "MIT"
readme = "README.rst"
Expand Down
14 changes: 5 additions & 9 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
use std::hash::{BuildHasher, Hasher};

use mmh3_32::murmurhash3_x86_32;
use crate::mmh3_32::murmurhash3_x86_32;

pub struct Murmur3Hasher {
seed: u32,
bytes: Vec<u8>,
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default)]
pub struct Murmur3HashState {
seed: u32,
}

impl Murmur3HashState {
pub fn new() -> Murmur3HashState {
return Murmur3HashState { seed: 0 };
}

pub fn with_seed(seed: u32) -> Murmur3HashState {
return Murmur3HashState { seed };
Murmur3HashState { seed }
}
}

impl Hasher for Murmur3Hasher {
fn finish(&self) -> u64 {
return murmurhash3_x86_32(&self.bytes, self.seed) as u64;
murmurhash3_x86_32(&self.bytes, self.seed) as u64
}

fn write(&mut self, bytes: &[u8]) {
Expand All @@ -51,7 +47,7 @@ mod test {

#[test]
fn use_in_hashmap() {
let mut hashmap = HashMap::with_capacity_and_hasher(0, Murmur3HashState::new());
let mut hashmap = HashMap::with_capacity_and_hasher(0, Murmur3HashState::default());
hashmap.insert("one", 1);
hashmap.insert("two", 2);

Expand Down
105 changes: 54 additions & 51 deletions src/mmh3_128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ fn fmix64(mut k: u64) -> u64 {
k = k.wrapping_mul(0xc4ceb9fe1a85ec53u64);
k ^= k >> 33;

return k;
k
}

fn get_128_block(bytes: &[u8], index: usize) -> (u64, u64) {
let b64: &[u64] = unsafe { mem::transmute(bytes) };

return (b64[index], b64[index + 1]);
(b64[index], b64[index + 1])
}

pub fn murmurhash3_x64_128(bytes: &[u8], seed: u64) -> (u64, u64) {
Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn murmurhash3_x64_128(bytes: &[u8], seed: u64) -> (u64, u64) {
k1 ^= (bytes[(block_count * read_size) as usize + 1] as u64) << 8;
}
if len & 15 >= 1 {
k1 ^= bytes[(block_count * read_size) as usize + 0] as u64;
k1 ^= bytes[(block_count * read_size) as usize] as u64;
k1 = k1.wrapping_mul(c1);
k1 = k1.rotate_left(31);
k1 = k1.wrapping_mul(c2);
Expand All @@ -118,7 +118,7 @@ pub fn murmurhash3_x64_128(bytes: &[u8], seed: u64) -> (u64, u64) {
h1 = h1.wrapping_add(h2);
h2 = h2.wrapping_add(h1);

return (h1, h2);
(h1, h2)
}

#[cfg(test)]
Expand All @@ -127,77 +127,80 @@ mod test {

#[test]
fn test_empty_string() {
assert!(murmurhash3_x64_128("".as_bytes(), 0) == (0, 0));
assert_eq!(murmurhash3_x64_128("".as_bytes(), 0), (0, 0));
}

#[test]
fn test_tail_lengths() {
assert!(
murmurhash3_x64_128("1".as_bytes(), 0) == (8213365047359667313, 10676604921780958775)
assert_eq!(
murmurhash3_x64_128("1".as_bytes(), 0),
(8213365047359667313, 10676604921780958775)
);
assert!(
murmurhash3_x64_128("12".as_bytes(), 0) == (5355690773644049813, 9855895140584599837)
assert_eq!(
murmurhash3_x64_128("12".as_bytes(), 0),
(5355690773644049813, 9855895140584599837)
);
assert!(
murmurhash3_x64_128("123".as_bytes(), 0) == (10978418110857903978, 4791445053355511657)
assert_eq!(
murmurhash3_x64_128("123".as_bytes(), 0),
(10978418110857903978, 4791445053355511657)
);
assert!(
murmurhash3_x64_128("1234".as_bytes(), 0) == (619023178690193332, 3755592904005385637)
assert_eq!(
murmurhash3_x64_128("1234".as_bytes(), 0),
(619023178690193332, 3755592904005385637)
);
assert!(
murmurhash3_x64_128("12345".as_bytes(), 0)
== (2375712675693977547, 17382870096830835188)
assert_eq!(
murmurhash3_x64_128("12345".as_bytes(), 0),
(2375712675693977547, 17382870096830835188)
);
assert!(
murmurhash3_x64_128("123456".as_bytes(), 0)
== (16435832985690558678, 5882968373513761278)
assert_eq!(
murmurhash3_x64_128("123456".as_bytes(), 0),
(16435832985690558678, 5882968373513761278)
);
assert!(
murmurhash3_x64_128("1234567".as_bytes(), 0)
== (3232113351312417698, 4025181827808483669)
assert_eq!(
murmurhash3_x64_128("1234567".as_bytes(), 0),
(3232113351312417698, 4025181827808483669)
);
assert!(
murmurhash3_x64_128("12345678".as_bytes(), 0)
== (4272337174398058908, 10464973996478965079)
assert_eq!(
murmurhash3_x64_128("12345678".as_bytes(), 0),
(4272337174398058908, 10464973996478965079)
);
assert!(
murmurhash3_x64_128("123456789".as_bytes(), 0)
== (4360720697772133540, 11094893415607738629)
assert_eq!(
murmurhash3_x64_128("123456789".as_bytes(), 0),
(4360720697772133540, 11094893415607738629)
);
assert!(
murmurhash3_x64_128("123456789a".as_bytes(), 0)
== (12594836289594257748, 2662019112679848245)
assert_eq!(
murmurhash3_x64_128("123456789a".as_bytes(), 0),
(12594836289594257748, 2662019112679848245)
);
assert!(
murmurhash3_x64_128("123456789ab".as_bytes(), 0)
== (6978636991469537545, 12243090730442643750)
assert_eq!(
murmurhash3_x64_128("123456789ab".as_bytes(), 0),
(6978636991469537545, 12243090730442643750)
);
assert!(
murmurhash3_x64_128("123456789abc".as_bytes(), 0)
== (211890993682310078, 16480638721813329343)
assert_eq!(
murmurhash3_x64_128("123456789abc".as_bytes(), 0),
(211890993682310078, 16480638721813329343)
);
assert!(
murmurhash3_x64_128("123456789abcd".as_bytes(), 0)
== (12459781455342427559, 3193214493011213179)
assert_eq!(
murmurhash3_x64_128("123456789abcd".as_bytes(), 0),
(12459781455342427559, 3193214493011213179)
);
assert!(
murmurhash3_x64_128("123456789abcde".as_bytes(), 0)
== (12538342858731408721, 9820739847336455216)
assert_eq!(
murmurhash3_x64_128("123456789abcde".as_bytes(), 0),
(12538342858731408721, 9820739847336455216)
);
assert!(
murmurhash3_x64_128("123456789abcdef".as_bytes(), 0)
== (9165946068217512774, 2451472574052603025)
assert_eq!(
murmurhash3_x64_128("123456789abcdef".as_bytes(), 0),
(9165946068217512774, 2451472574052603025)
);
assert!(
murmurhash3_x64_128("123456789abcdef1".as_bytes(), 0)
== (9259082041050667785, 12459473952842597282)
assert_eq!(
murmurhash3_x64_128("123456789abcdef1".as_bytes(), 0),
(9259082041050667785, 12459473952842597282)
);
}

#[test]
fn test_large_data() {
assert!(murmurhash3_x64_128("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0)
== (9455322759164802692, 17863277201603478371));
assert_eq!(murmurhash3_x64_128("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0), (9455322759164802692, 17863277201603478371));
}

#[cfg(feature = "nightly")]
Expand Down
21 changes: 10 additions & 11 deletions src/mmh3_32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ fn fmix32(mut h: u32) -> u32 {
h = h.wrapping_mul(0xc2b2ae35);
h ^= h >> 16;

return h;
h
}

fn get_32_block(bytes: &[u8], index: usize) -> u32 {
let b32: &[u32] = unsafe { mem::transmute(bytes) };

return b32[index];
b32[index]
}

pub fn murmurhash3_x86_32(bytes: &[u8], seed: u32) -> u32 {
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn murmurhash3_x86_32(bytes: &[u8], seed: u32) -> u32 {
k1 ^= (bytes[(block_count * read_size) as usize + 1] as u32) << 8;
}
if len & 3 >= 1 {
k1 ^= bytes[(block_count * read_size) as usize + 0] as u32;
k1 ^= bytes[(block_count * read_size) as usize] as u32;
k1 = k1.wrapping_mul(c1);
k1 = k1.rotate_left(15);
k1 = k1.wrapping_mul(c2);
Expand All @@ -56,7 +56,7 @@ pub fn murmurhash3_x86_32(bytes: &[u8], seed: u32) -> u32 {
h1 ^= bytes.len() as u32;
h1 = fmix32(h1);

return h1;
h1
}

#[cfg(test)]
Expand All @@ -65,21 +65,20 @@ mod test {

#[test]
fn test_empty_string() {
assert!(murmurhash3_x86_32("".as_bytes(), 0) == 0);
assert_eq!(murmurhash3_x86_32("".as_bytes(), 0), 0);
}

#[test]
fn test_tail_lengths() {
assert!(murmurhash3_x86_32("1".as_bytes(), 0) == 2484513939);
assert!(murmurhash3_x86_32("12".as_bytes(), 0) == 4191350549);
assert!(murmurhash3_x86_32("123".as_bytes(), 0) == 2662625771);
assert!(murmurhash3_x86_32("1234".as_bytes(), 0) == 1914461635);
assert_eq!(murmurhash3_x86_32("1".as_bytes(), 0), 2484513939);
assert_eq!(murmurhash3_x86_32("12".as_bytes(), 0), 4191350549);
assert_eq!(murmurhash3_x86_32("123".as_bytes(), 0), 2662625771);
assert_eq!(murmurhash3_x86_32("1234".as_bytes(), 0), 1914461635);
}

#[test]
fn test_large_data() {
assert!(murmurhash3_x86_32("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0)
== 1004899618);
assert_eq!(murmurhash3_x86_32("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0), 1004899618);
}

#[cfg(feature = "nightly")]
Expand Down

0 comments on commit e3ed5b7

Please sign in to comment.