Skip to content

Commit

Permalink
Merge pull request #528 from rtfeldman/vendor-murmur3
Browse files Browse the repository at this point in the history
Vendor Murmur3 for now
  • Loading branch information
rtfeldman authored Sep 17, 2020
2 parents dd876f3 + 4d930a5 commit 466e0c9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
1 change: 0 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
},
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"Skinney/murmur3": "2.0.7 <= v < 3.0.0",
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/json": "1.0.0 <= v < 2.0.0",
"elm/virtual-dom": "1.0.0 <= v < 2.0.0",
Expand Down
134 changes: 134 additions & 0 deletions src/ElmCssVendor/Murmur3.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
module ElmCssVendor.Murmur3 exposing (hashString)

{-| Murmur 3 hash function for hashing strings
The MIT License (MIT)
Copyright (c) 2016 Robin Heggelund Hansen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@docs hashString
-}

import Bitwise as Bit


type alias HashData =
{ shift : Int
, seed : Int
, hash : Int
, charsProcessed : Int
}


c1 : Int
c1 =
0xCC9E2D51


c2 : Int
c2 =
0x1B873593


{-| Takes a seed and a string. Produces a hash (integer).
Given the same seed and string, it will always produce the same hash.
hashString 1234 "Turn me into a hash" == 4138100590
-}
hashString : Int -> String -> Int
hashString seed str =
str
|> String.foldl hashFold (HashData 0 seed 0 0)
|> finalize


hashFold : Char -> HashData -> HashData
hashFold c data =
let
res =
Char.toCode c
|> Bit.and 0xFF
|> Bit.shiftLeftBy data.shift
|> Bit.or data.hash
in
-- Using case-of instead of == avoids costly .cmp check
case data.shift of
24 ->
{ shift = 0
, seed = mix data.seed res
, hash = 0
, charsProcessed = data.charsProcessed + 1
}

_ ->
{ shift = data.shift + 8
, seed = data.seed
, hash = res
, charsProcessed = data.charsProcessed + 1
}


mix : Int -> Int -> Int
mix h1 k1 =
(k1
|> multiplyBy c1
|> rotlBy 15
|> multiplyBy c2
|> Bit.xor h1
|> rotlBy 13
|> multiplyBy 5
)
+ 0xE6546B64


finalize : HashData -> Int
finalize data =
let
acc =
if data.hash /= 0 then
data.hash
|> multiplyBy c1
|> rotlBy 15
|> multiplyBy c2
|> Bit.xor data.seed

else
data.seed

h0 =
Bit.xor acc data.charsProcessed

h1 =
Bit.xor h0 (Bit.shiftRightZfBy 16 h0)
|> multiplyBy 0x85EBCA6B

h2 =
Bit.xor h1 (Bit.shiftRightZfBy 13 h1)
|> multiplyBy 0xC2B2AE35
in
Bit.xor h2 (Bit.shiftRightZfBy 16 h2)
|> Bit.shiftRightZfBy 0


{-| 32-bit multiplication
-}
multiplyBy : Int -> Int -> Int
multiplyBy b a =
(Bit.and a 0xFFFF * b) + Bit.shiftLeftBy 16 (Bit.and (Bit.shiftRightZfBy 16 a * b) 0xFFFF)


{-| Given a 32bit int and an int representing a number of bit positions,
returns the 32bit int rotated left by that number of positions.
-}
rotlBy : Int -> Int -> Int
rotlBy b a =
Bit.or
(Bit.shiftLeftBy b a)
(Bit.shiftRightZfBy (32 - b) a)
2 changes: 1 addition & 1 deletion src/Hash.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Hash exposing (fromString)

import ElmCssVendor.Murmur3 as Murmur3
import Hex
import Murmur3


fromString : String -> String
Expand Down
2 changes: 1 addition & 1 deletion src/VirtualDom/Styled.elm
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import Css.Preprocess as Preprocess exposing (Style)
import Css.Preprocess.Resolve as Resolve
import Css.Structure as Structure
import Dict exposing (Dict)
import ElmCssVendor.Murmur3 as Murmur3
import Hex
import Json.Decode
import Json.Encode
import Murmur3
import VirtualDom


Expand Down

0 comments on commit 466e0c9

Please sign in to comment.