@@ -1,4 +1,5 @@
1{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE GADTs #-}
3{-# LANGUAGE KindSignatures #-}
4{-# LANGUAGE KindSignatures #-}
4{-# LANGUAGE MagicHash #-}
5{-# LANGUAGE MagicHash #-}
@@ -20,12 +21,17 @@ module Data.TypeRep.CacheMap
20 -- * Helpful testing functions
21 -- * Helpful testing functions
22 , fromList
23 , fromList
25import Prelude hiding (lookup)
27import Prelude hiding (lookup)
27import Control.Arrow ((&&&))
29import Control.Arrow ((&&&))
30+import Data.Function (on)
31+import Data.IntMap.Strict (IntMap)
28import Data.Kind (Type)
32import Data.Kind (Type)
33+import Data.List (nubBy)
34+import Data.Maybe (fromJust)
29import Data.Proxy (Proxy (..))
35import Data.Proxy (Proxy (..))
30import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
36import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
31import Data.Word (Word64)
37import Data.Word (Word64)
@@ -36,6 +42,7 @@ import GHC.Prim (eqWord#, ltWord#)
36import GHC.Word (Word64 (..))
42import GHC.Word (Word64 (..))
37import Unsafe.Coerce (unsafeCoerce)
43import Unsafe.Coerce (unsafeCoerce)
45+import qualified Data.IntMap.Strict as IM
39import qualified Data.Vector as V
46import qualified Data.Vector as V
40import qualified Data.Vector.Unboxed as Unboxed
47import qualified Data.Vector.Unboxed as Unboxed
@@ -45,6 +52,14 @@ data TypeRepMap (f :: k -> Type) = TypeRepMap
45 , anys :: V.Vector Any
52 , anys :: V.Vector Any
55+instance Show (TypeRepMap f) where
58+toFps :: TypeRepMap f -> [Fingerprint]
59+toFps TypeRepMap{..} = zipWith Fingerprint
60+ (Unboxed.toList fingerprintAs)
61+ (Unboxed.toList fingerprintBs)
48fromAny :: Any -> f a
63fromAny :: Any -> f a
49fromAny = unsafeCoerce
64fromAny = unsafeCoerce
@@ -57,10 +72,7 @@ insert :: forall a f . Typeable a => f a -> TypeRepMap f -> TypeRepMap f
57insert x = fromListPairs . addX . toPairList
72insert x = fromListPairs . addX . toPairList
59 toPairList :: TypeRepMap f -> [(Fingerprint, Any)]
74 toPairList :: TypeRepMap f -> [(Fingerprint, Any)]
60− toPairList (TypeRepMap as bs ans) = zip (zipWith Fingerprint
75+ toPairList trMap = zip (toFps trMap) (V.toList $ anys trMap)
65 pairX :: (Fingerprint, Any)
77 pairX :: (Fingerprint, Any)
66 pairX@(fpX, _) = (calcFp x, unsafeCoerce x)
78 pairX@(fpX, _) = (calcFp x, unsafeCoerce x)
@@ -114,7 +126,10 @@ cachedBinarySearch (Fingerprint (W64# a) (W64# b)) fpAs fpBs = inline (go 0#)
114----------------------------------------------------------------------------
126----------------------------------------------------------------------------
116data TF f where
128data TF f where
117− TF :: Typeable a => f a -> TF f
129+ TF :: Typeable a => f a -> TF f
131+instance Show (TF f) where
132+ show (TF tf) = show $ calcFp tf
119fromF :: Typeable a => f a -> Proxy a
134fromF :: Typeable a => f a -> Proxy a
120fromF _ = Proxy
135fromF _ = Proxy
@@ -126,7 +141,7 @@ fromListPairs :: [(Fingerprint, Any)] -> TypeRepMap f
126fromListPairs kvs = TypeRepMap (Unboxed.fromList fpAs) (Unboxed.fromList fpBs) (V.fromList ans)
141fromListPairs kvs = TypeRepMap (Unboxed.fromList fpAs) (Unboxed.fromList fpBs) (V.fromList ans)
128 (fpAs, fpBs) = unzip $ map (\(Fingerprint a b) -> (a, b)) fps
143 (fpAs, fpBs) = unzip $ map (\(Fingerprint a b) -> (a, b)) fps
129− (fps, ans) = unzip $ breadthFirst $ fromListToTree $ sortWith fst kvs
144+ (fps, ans) = unzip $ fromSortedList $ sortWith fst $ nubPairs kvs
131fromList :: forall f . [TF f] -> TypeRepMap f
146fromList :: forall f . [TF f] -> TypeRepMap f
132fromList = fromListPairs . map (fp &&& an)
147fromList = fromListPairs . map (fp &&& an)
@@ -137,34 +152,22 @@ fromList = fromListPairs . map (fp &&& an)
137 an :: TF f -> Any
152 an :: TF f -> Any
138 an (TF x) = unsafeCoerce x
153 an (TF x) = unsafeCoerce x
155+nubPairs :: (Eq a) => [(a, b)] -> [(a, b)]
156+nubPairs = nubBy ((==) `on` fst)
140----------------------------------------------------------------------------
158----------------------------------------------------------------------------
141−-- Tree
159+-- Tree-like conversion
142----------------------------------------------------------------------------
160----------------------------------------------------------------------------
144−data Tree a = Leaf | Node a (Tree a) (Tree a)
162+fromSortedList :: forall a . [a] -> [a]
145− deriving (Show)
163+fromSortedList l = IM.elems $ fst $ go 0 0 mempty (IM.fromList $ zip [0..] l)
147−fromListToTree :: [a] -> Tree a
148−fromListToTree [] = Leaf
151− in case splitAt (len `div` 2) xs of
153− (ls, []) -> Node (last ls) (fromListToTree $ init ls) Leaf
154− ([], (r:rs)) -> Node r Leaf $ fromListToTree rs
155− (ls, r:rs) -> Node r (fromListToTree ls) (fromListToTree rs)
157−breadthFirst :: Tree a -> [a]
158−breadthFirst tree = bf [tree]
160− bf :: [Tree a] -> [a]
165+ -- state monad could be used here, but it's another dependency
161− bf [] = []
166+ go :: Int -> Int -> IntMap a -> IntMap a -> (IntMap a, Int)
162− bf xs = mapValue xs ++ bf (concatMap children xs)
167+ go i first result vector =
163−
168+ if i >= IM.size vector
164− mapValue :: [Tree a] -> [a]
169+ then (result, first)
165− mapValue [] = []
170+ else do
166− mapValue (Leaf : xs) = mapValue xs
171+ let (newResult, newFirst) = go (2 * i + 1) first result vector
167− mapValue (Node a _ _ : xs) = a : mapValue xs
172+ let withCur = IM.insert i (fromJust $ IM.lookup newFirst vector) newResult
168−
173+ go (2 * i + 2) (newFirst + 1) withCur vector
170− children (Node _ l r) = [l, r]