Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Data/BloomFilter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ module Data.BloomFilter

-- | The raw bit array used by the immutable 'Bloom' type.
, bitArray
, serialize
, unsafeDeserialize
) where

import Control.Monad (liftM, forM_)
Expand All @@ -85,6 +87,9 @@ import qualified Data.BloomFilter.Mutable.Internal as MB
import Data.BloomFilter.Mutable.Internal (Hash, MBloom)
import Data.Word (Word32)

import Data.Serialize
import Data.ByteString (ByteString)

import Prelude hiding (elem, length, notElem,
(/), (*), div, divMod, mod, rem)

Expand Down Expand Up @@ -320,6 +325,21 @@ logPower2 k = go 0 k
where go j 1 = j
go j n = go (j+1) (n `shiftR` 1)

serialize :: Bloom a -> ByteString
serialize bloom = encode (shift bloom, mask bloom, bitArray bloom)

unsafeDeserialize :: (a -> [Hash]) -> ByteString -> Either String (Bloom a)
unsafeDeserialize hashes bs =
case decode bs of
Left err -> Left err
Right (shift, mask, bitArray) -> Right $ B
{ hashes = hashes
, shift = shift
, mask = mask
, bitArray = bitArray
}


-- $overview
--
-- Each of the functions for creating Bloom filters accepts two parameters:
Expand Down
1 change: 1 addition & 0 deletions bloomfilter.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ library
array,
base >= 4.4 && < 5,
bytestring >= 0.9,
cereal >= 0.4,
deepseq
exposed-modules: Data.BloomFilter
Data.BloomFilter.Easy
Expand Down