on main help sign in
Fix binsearch to check Fingerprint b
c125615vrom911
src/Data/TypeRep/OptimalVector.hs+13−8
@@ -1,13 +1,14 @@
1{-# OPTIONS_GHC -fno-warn-orphans #-}
1{-# OPTIONS_GHC -fno-warn-orphans #-}
2
2
3+{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE ExplicitForAll #-}
4{-# LANGUAGE ExplicitForAll #-}
4{-# LANGUAGE GADTs #-}
5{-# LANGUAGE GADTs #-}
5{-# LANGUAGE KindSignatures #-}
6{-# LANGUAGE KindSignatures #-}
7+{-# LANGUAGE MagicHash #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE PolyKinds #-}
9{-# LANGUAGE PolyKinds #-}
8{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE TypeFamilies #-}
10−{-# LANGUAGE MagicHash, BangPatterns #-}
11
12
12
13
13module Data.TypeRep.OptimalVector
14module Data.TypeRep.OptimalVector
@@ -23,7 +24,6 @@ module Data.TypeRep.OptimalVector
23import Prelude hiding (lookup)
24import Prelude hiding (lookup)
24
25
25import Control.Arrow ((&&&))
26import Control.Arrow ((&&&))
26−import Data.Foldable (for_)
27import Data.Proxy (Proxy (..))
27import Data.Proxy (Proxy (..))
28import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
28import Data.Typeable (Typeable, typeRep, typeRepFingerprint)
29import Data.Word (Word64)
29import Data.Word (Word64)
@@ -31,11 +31,8 @@ import GHC.Base hiding (empty)
31import GHC.Exts (sortWith)
31import GHC.Exts (sortWith)
32import GHC.Fingerprint (Fingerprint (..))
32import GHC.Fingerprint (Fingerprint (..))
33import Unsafe.Coerce (unsafeCoerce)
33import Unsafe.Coerce (unsafeCoerce)
34−import GHC.Prim
35
34
36import qualified Data.Vector as V
35import qualified Data.Vector as V
37−import qualified Data.Vector.Generic as G
38−import qualified Data.Vector.Generic.Mutable as M
39import qualified Data.Vector.Unboxed as Unboxed
36import qualified Data.Vector.Unboxed as Unboxed
40
37
41data TypeRepVector f = TypeRepVect
38data TypeRepVector f = TypeRepVect
@@ -89,19 +86,27 @@ fromList tfs = TypeRepVect (Unboxed.fromList fpAs) (Unboxed.fromList fpBs) (V.fr
89
86
90-- | Returns the index is found.
87-- | Returns the index is found.
91binarySearch :: Fingerprint -> Unboxed.Vector Word64 -> Unboxed.Vector Word64 -> Maybe Int
88binarySearch :: Fingerprint -> Unboxed.Vector Word64 -> Unboxed.Vector Word64 -> Maybe Int
92−binarySearch (Fingerprint a _) fpAs _ =
89+binarySearch (Fingerprint a b) fpAs fpBs =
93 let
90 let
94 !(I# len) = Unboxed.length fpAs
91 !(I# len) = Unboxed.length fpAs
95 ind = I# (binSearchHelp (-1#) len)
92 ind = I# (binSearchHelp (-1#) len)
96 in
93 in
97 -- FIXME: Check second component!
94 -- 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)
99 where
98 where
100 binSearchHelp :: Int# -> Int# -> Int#
99 binSearchHelp :: Int# -> Int# -> Int#
101 binSearchHelp l r = case l <# (r -# 1#) of
100 binSearchHelp l r = case l <# (r -# 1#) of
102 0# -> r
101 0# -> r
103− _ ->
102+ _ ->
104 let m = uncheckedIShiftRA# (l +# r) 1# in
103 let m = uncheckedIShiftRA# (l +# r) 1# in
105 if Unboxed.unsafeIndex fpAs (I# m) < a
104 if Unboxed.unsafeIndex fpAs (I# m) < a
106 then binSearchHelp m r
105 then binSearchHelp m r
107 else binSearchHelp l m
106 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