@@ -1,13 +1,14 @@
11{-# OPTIONS_GHC -fno-warn-orphans #-}
22
3+{-# LANGUAGE BangPatterns #-}
34{-# LANGUAGE ExplicitForAll #-}
45{-# LANGUAGE GADTs #-}
56{-# LANGUAGE KindSignatures #-}
7+{-# LANGUAGE MagicHash #-}
68{-# LANGUAGE MultiParamTypeClasses #-}
79{-# LANGUAGE PolyKinds #-}
810{-# LANGUAGE ScopedTypeVariables #-}
911{-# LANGUAGE TypeFamilies #-}
10−{-# LANGUAGE MagicHash, BangPatterns #-}
1112
1213
1314module Data.TypeRep.OptimalVector
@@ -23,7 +24,6 @@ module Data.TypeRep.OptimalVector
2324import Prelude hiding (lookup)
2425
2526import Control.Arrow ((&&&))
26−import Data.Foldable (for_)
2727import Data.Proxy (Proxy (..))
2828import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
2929import Data.Word (Word64)
@@ -31,11 +31,8 @@ import GHC.Base hiding (empty)
3131import GHC.Exts (sortWith)
3232import GHC.Fingerprint (Fingerprint (..))
3333import Unsafe.Coerce (unsafeCoerce)
34−import GHC.Prim
3534
3635import qualified Data.Vector as V
37−import qualified Data.Vector.Generic as G
38−import qualified Data.Vector.Generic.Mutable as M
3936import qualified Data.Vector.Unboxed as Unboxed
4037
4138data TypeRepVector f = TypeRepVect
@@ -89,19 +86,27 @@ fromList tfs = TypeRepVect (Unboxed.fromList fpAs) (Unboxed.fromList fpBs) (V.fr
8986
9087-- | Returns the index is found.
9188binarySearch :: Fingerprint -> Unboxed.Vector Word64 -> Unboxed.Vector Word64 -> Maybe Int
92−binarySearch (Fingerprint a _) fpAs _ =
89+binarySearch (Fingerprint a b) fpAs fpBs =
9390 let
9491 !(I# len) = Unboxed.length fpAs
9592 ind = I# (binSearchHelp (-1#) len)
9693 in
9794 -- FIXME: Check second component!
98− if a == (fpAs Unboxed.! ind) then Just ind else Nothing
95+ if a == (fpAs Unboxed.! ind) && (b == (fpBs Unboxed.! ind))
96+ then Just ind
97+ else checkfpBs (ind + 1) (I# len)
9998 where
10099 binSearchHelp :: Int# -> Int# -> Int#
101100 binSearchHelp l r = case l <# (r -# 1#) of
102101 0# -> r
103− _ ->
102+ _ ->
104103 let m = uncheckedIShiftRA# (l +# r) 1# in
105104 if Unboxed.unsafeIndex fpAs (I# m) < a
106105 then binSearchHelp m r
107106 else binSearchHelp l m
107+
108+ checkfpBs :: Int -> Int -> Maybe Int
109+ checkfpBs i len
110+ | i >= len = Nothing
111+ | a == (fpAs Unboxed.! i) && b == (fpBs Unboxed.! i) = Just i
112+ | otherwise = checkfpBs (i + 1) len