DimensionalHash 0.1.3 → 0.1.4
raw patch · 3 files changed
+42/−26 lines, 3 filessetup-changedPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Data.DimensionalHash: isHashable :: (MortonNumber a, Bits a) => [a] -> Bool
+ Data.DimensionalHash: maxBitSize :: (MortonNumber a, Bits a) => [a] -> Int
+ Data.DimensionalHash: maxSize :: (MortonNumber a, Bits a) => [a] -> Int
- Data.DimensionalHash: class Bits a => MortonNumber a
+ Data.DimensionalHash: class (Bits a, Integral a) => MortonNumber a
Files
- Data/DimensionalHash.hs +40/−24
- DimensionalHash.cabal +1/−1
- Setup.hs +1/−1
Data/DimensionalHash.hs view
@@ -11,36 +11,31 @@ import Data.Int import Data.Word --- |Return an element having all bits set to 0, excepting the bit at n -takeBitAt :: (Bits a) => a -> Int -> a -takeBitAt x n = x .&. (shiftL 1 n) - --- |Return a list have shifted elements by (n + index) -shiftElements :: (Bits a) => [a] -> Int -> [a] -shiftElements [] n = [] -shiftElements (x:xs) n = [(shiftL x n)] ++ (shiftElements xs (n+1)) - --- |Return a list have been shifted elements by n + index -concatBits :: (Bits a) => [a] -> Int -> Int -> a -concatBits list n npos = foldl (\acc x -> acc .|. x) 0 shiftedMap - where - shiftedMap = shiftElements mapList npos - mapList = map (\x -> (takeBitAt x n)) list - -hash :: (Bits a) => [a] -> Int -> Int -> Int -> a -hash list n npos precision - | length list == 1 = head list - | n < precision = (concatBits list n npos) .|. (hash list (n + 1) (npos + (length list) - 1) precision) - | otherwise = 0 +import Control.Exception +import Data.Typeable -- | recursevely compute the morton number. -class (Bits a) => (MortonNumber a) where +class (Bits a, Integral a) => (MortonNumber a) where dimensionalHash :: (Bits a) => [a] -> a - dimensionalHash list = hash list 0 0 (bitSize (head list)) + dimensionalHash [] = 0 + dimensionalHash list = hash list 0 (bitSize (head list)) + + maxBitSize :: (Bits a) => [a] -> Int + maxBitSize list = div (bitSize (head list)) (length list) + + maxSize :: (Bits a) => [a] -> Int + maxSize list = 2 ^ (maxBitSize list) + + isHashable :: (Bits a) => [a] -> Bool + isHashable [x] = True + isHashable list = (length filteredList) == 0 + where + filteredList = filter (\x -> (fromIntegral x) > (maxSize list)) list -- Integer haven't a fixed bitsize instance MortonNumber Integer where - dimensionalHash list = hash list 0 0 32 + dimensionalHash list = hash list 0 32 + maxBitSize list = div 32 (length list) -- Types that have a fixed bitsize are instance of MortonNumber instance MortonNumber Int8 @@ -53,3 +48,24 @@ instance MortonNumber Word32 instance MortonNumber Word64 +hash :: (Bits a) => [a] -> Int -> Int -> a +hash list n precision + | length list == 1 = head list + | n < precision = (concatBits list n) .|. (hash list (n + 1) precision) + | otherwise = 0 + +-- |Return a list have been shifted elements by n + index +concatBits :: (Bits a) => [a] -> Int -> a +concatBits list n = foldl (\acc x -> acc .|. x) 0 shiftedMap + where + shiftedMap = shiftElements mapList (n * ((length list) - 1)) + mapList = map (\x -> (takeBitAt x n)) list + +-- |Return a list have shifted elements by (n + index) +shiftElements :: (Bits a) => [a] -> Int -> [a] +shiftElements [] n = [] +shiftElements (x:xs) n = [(shiftL x n)] ++ (shiftElements xs (n+1)) + +-- |Return an element having all bits set to 0, excepting the bit at n +takeBitAt :: (Bits a) => a -> Int -> a +takeBitAt x n = x .&. (shiftL 1 n)
DimensionalHash.cabal view
@@ -1,5 +1,5 @@ name: DimensionalHash -version: 0.1.3 +version: 0.1.4 build-Type: Simple cabal-version: >= 1.6 tested-with: GHC == 7.0.3
Setup.hs view
@@ -1,2 +1,2 @@ import Distribution.Simple -main = defaultMainWithHooks defaultUserHooks+main = defaultMain