diff options
Diffstat (limited to 'src/Data/Word64Map.hs')
-rw-r--r-- | src/Data/Word64Map.hs | 66 |
1 files changed, 0 insertions, 66 deletions
diff --git a/src/Data/Word64Map.hs b/src/Data/Word64Map.hs deleted file mode 100644 index adc9c27e..00000000 --- a/src/Data/Word64Map.hs +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | {-# LANGUAGE RankNTypes #-} | ||
2 | {-# LANGUAGE ScopedTypeVariables #-} | ||
3 | {-# LANGUAGE UnboxedTuples #-} | ||
4 | module Data.Word64Map where | ||
5 | |||
6 | import Data.Bits | ||
7 | import qualified Data.IntMap as IntMap | ||
8 | ;import Data.IntMap (IntMap) | ||
9 | import Data.Monoid | ||
10 | import Data.Typeable | ||
11 | import Data.Word | ||
12 | |||
13 | -- | Since 'Int' may be 32 or 64 bits, this function is provided as a | ||
14 | -- convenience to test if an integral type, such as 'Data.Word.Word64', can be | ||
15 | -- safely transformed into an 'Int' for use with 'IntMap'. | ||
16 | -- | ||
17 | -- Returns 'True' if the proxied type can be losslessly converted to 'Int' using | ||
18 | -- 'fromIntegral'. | ||
19 | fitsInInt :: forall proxy word. (Bounded word, Integral word) => proxy word -> Bool | ||
20 | fitsInInt proxy = (original == casted) | ||
21 | where | ||
22 | original = div maxBound 2 :: word | ||
23 | casted = fromIntegral (fromIntegral original :: Int) :: word | ||
24 | |||
25 | newtype Word64Map a = Word64Map (IntMap (IntMap a)) | ||
26 | |||
27 | size :: Word64Map a -> Int | ||
28 | size (Word64Map m) = getSum $ foldMap (\n -> Sum (IntMap.size n)) m | ||
29 | |||
30 | empty :: Word64Map a | ||
31 | empty = Word64Map IntMap.empty | ||
32 | |||
33 | -- Warning: This function assumes an 'Int' is either 64 or 32 bits. | ||
34 | keyFrom64 :: Word64 -> (# Int,Int #) | ||
35 | keyFrom64 w8 = | ||
36 | if fitsInInt (Proxy :: Proxy Word64) | ||
37 | then (# fromIntegral w8 , 0 #) | ||
38 | else (# fromIntegral (w8 `shiftR` 32), fromIntegral w8 #) | ||
39 | {-# INLINE keyFrom64 #-} | ||
40 | |||
41 | lookup :: Word64 -> Word64Map b -> Maybe b | ||
42 | lookup w8 (Word64Map m) | (# hi,lo #) <- keyFrom64 w8 = do | ||
43 | m' <- IntMap.lookup hi m | ||
44 | IntMap.lookup lo m' | ||
45 | {-# INLINE lookup #-} | ||
46 | |||
47 | insert :: Word64 -> b -> Word64Map b -> Word64Map b | ||
48 | insert w8 b (Word64Map m) | (# hi,lo #) <- keyFrom64 w8 | ||
49 | = Word64Map $ IntMap.alter (Just . maybe (IntMap.singleton lo b) | ||
50 | (IntMap.insert lo b)) | ||
51 | hi | ||
52 | m | ||
53 | {-# INLINE insert #-} | ||
54 | |||
55 | delete :: Word64 -> Word64Map b -> Word64Map b | ||
56 | delete w8 (Word64Map m) | (# hi,lo #) <- keyFrom64 w8 | ||
57 | = Word64Map $ IntMap.alter (maybe Nothing | ||
58 | (\m' -> case IntMap.delete lo m' of | ||
59 | m'' | IntMap.null m'' -> Nothing | ||
60 | m'' -> Just m'')) | ||
61 | hi | ||
62 | m | ||
63 | {-# INLINE delete #-} | ||
64 | |||
65 | |||
66 | |||