on main help sign in
further improvement on insert (#101)
commite55aed6f058f15ca3dbf6455b6f9a8a1ee03fc46 authorYu-zh <yu.zhang.yz862@yale.edu> dateSat 17 Oct 2020, 06:38 (6 years ago) parent73e7201
1 file changed+12−1
src/Data/TypeRepMap/Internal.hs+12−1
src/Data/TypeRepMap/Internal.hs+12−1
@@ -187,7 +187,18 @@ prop> member @a (insert (x :: f a) tm) == True
187187
188188-}
189189insert :: forall a f . Typeable a => f a -> TypeRepMap f -> TypeRepMap f
190−insert = union . one
190+insert x m
191+ | size m == 0 = one x
192+ | otherwise = case cachedBinarySearch (typeFp @a) (fingerprintAs m) (fingerprintBs m) of
193+ Nothing -> union m $ one x
194+ Just i -> m {trAnys = changeAnyArr i (trAnys m)}
195+ where
196+ changeAnyArr :: Int -> Array Any -> Array Any
197+ changeAnyArr i trAs = runST $ do
198+ let n = sizeofArray trAs
199+ mutArr <- thawArray trAs 0 n
200+ writeArray mutArr i $ toAny x
201+ unsafeFreezeArray mutArr
191202{-# INLINE insert #-}
192203
193204-- Extract the kind of a type. We use it to work around lack of syntax for