@@ -1,5 +1,3 @@
3module Network.Wai.Handler.Warp.MultiMap (
1module Network.Wai.Handler.Warp.MultiMap (
@@ -15,11 +13,14 @@ module Network.Wai.Handler.Warp.MultiMap (
16import Data.IntMap.Strict (IntMap)
14import Data.IntMap.Strict (IntMap)
17import qualified Data.IntMap.Strict as I
15import qualified Data.IntMap.Strict as I
16+import qualified Data.List.NonEmpty as NE
18−import qualified Network.Wai.Handler.Warp.Some as S
19+import Network.Wai.Handler.Warp.Imports hiding ((<>), union, empty, insert)
20----------------------------------------------------------------
21----------------------------------------------------------------
22−type MMap v = IntMap (S.Some v)
23+type MMap v = IntMap (NonEmpty v)
24----------------------------------------------------------------
25----------------------------------------------------------------
@@ -35,7 +36,7 @@ empty = I.empty
37singleton :: Int -> v -> MMap v
38singleton :: Int -> v -> MMap v
38−singleton k v = I.singleton k (S.singleton v)
39+singleton k v = I.singleton k (v :| [])
40----------------------------------------------------------------
41----------------------------------------------------------------
@@ -43,19 +44,19 @@ singleton k v = I.singleton k (S.singleton v)
43search :: Int -> MMap v -> Maybe v
44search :: Int -> MMap v -> Maybe v
44search k m = case I.lookup k m of
45search k m = case I.lookup k m of
45 Nothing -> Nothing
46 Nothing -> Nothing
46− Just s -> Just $! S.top s
47+ Just s -> Just $! NE.head s
48-- | O(log n)
49-- | O(log n)
49searchWith :: Int -> (v -> Bool) -> MMap v -> Maybe v
50searchWith :: Int -> (v -> Bool) -> MMap v -> Maybe v
50searchWith k f m = case I.lookup k m of
51searchWith k f m = case I.lookup k m of
51− Nothing -> Nothing
52+ Nothing -> Nothing
52− Just s -> S.lookupWith f s
53+ Just nxs -> find f $ NE.toList nxs
54----------------------------------------------------------------
55----------------------------------------------------------------
56-- | O(log n)
57-- | O(log n)
57insert :: Int -> v -> MMap v -> MMap v
58insert :: Int -> v -> MMap v -> MMap v
58−insert k v m = I.insertWith S.union k (S.singleton v) m
59+insert k v m = I.insertWith (<>) k (v :| []) m
60----------------------------------------------------------------
61----------------------------------------------------------------
@@ -63,7 +64,7 @@ insert k v m = I.insertWith S.union k (S.singleton v) m
63toList :: MMap v -> [v]
64toList :: MMap v -> [v]
64toList m = concatMap f $ I.toAscList m
65toList m = concatMap f $ I.toAscList m
66− f (_,s) = S.toList s
67+ f (_,s) = NE.toList s
68----------------------------------------------------------------
69----------------------------------------------------------------
@@ -75,7 +76,7 @@ pruneWith m action = I.fromAscList <$> go (I.toDescList m) []
76 go [] acc = return acc
77 go [] acc = return acc
77 go ((k,s):kss) acc = do
78 go ((k,s):kss) acc = do
78− mt <- S.prune action s
79+ mt <- prune action s
79 case mt of
80 case mt of
80 Nothing -> go kss acc
81 Nothing -> go kss acc
81 Just t -> go kss ((k,t) : acc)
82 Just t -> go kss ((k,t) : acc)
@@ -84,4 +85,15 @@ pruneWith m action = I.fromAscList <$> go (I.toDescList m) []
85-- O(n + m) where N is the size of the second argument
86-- O(n + m) where N is the size of the second argument
86merge :: MMap v -> MMap v -> MMap v
87merge :: MMap v -> MMap v -> MMap v
87−merge m1 m2 = I.unionWith S.union m1 m2
88+merge m1 m2 = I.unionWith (<>) m1 m2
90+----------------------------------------------------------------
92+prune :: (a -> IO Bool) -> NonEmpty a -> IO (Maybe (NonEmpty a))
93+prune act nxs = NE.nonEmpty <$> go (NE.toList nxs)
99+ return $ if keep then x:rs else rs