@@ -1,5 +1,7 @@
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE FlexibleInstances #-}
4-- | An 'OMap' behaves much like a 'Map', with mostly the same asymptotics, but
6-- | An 'OMap' behaves much like a 'Map', with mostly the same asymptotics, but
5-- also remembers the order that keys were inserted. All operations whose
7-- also remembers the order that keys were inserted. All operations whose
@@ -20,6 +22,7 @@ module Data.Map.Ordered
20 -- * If both sides contain the same key, the tuple's value wins
22 -- * If both sides contain the same key, the tuple's value wins
21 , (<|), (|<), (>|), (|>)
23 , (<|), (|<), (>|), (|>)
22 , (<>|), (|<>), unionWithL, unionWithR
24 , (<>|), (|<>), unionWithL, unionWithR
25+ , Bias(Bias, unbiased), L, R
23 -- * Deletion
26 -- * Deletion
24 , delete, filter, (\\)
27 , delete, filter, (\\)
25 , (|/\), (/\|), intersectionWith
28 , (|/\), (/\|), intersectionWith
@@ -37,7 +40,11 @@ import Data.Data
37import Data.Foldable (Foldable, foldl', foldMap)
40import Data.Foldable (Foldable, foldl', foldMap)
38import Data.Function (on)
41import Data.Function (on)
39import Data.Map (Map)
42import Data.Map (Map)
40−import Data.Map.Util (Index, Tag, maxTag, minTag, nextHigherTag, nextLowerTag, readsPrecList, showsPrecList)
43+import Data.Map.Util
45+#if MIN_VERSION_base(4,9,0)
41import Prelude hiding (filter, lookup, null)
48import Prelude hiding (filter, lookup, null)
42import qualified Data.Map as M
49import qualified Data.Map as M
@@ -68,6 +75,37 @@ fromListConstr = mkConstr oMapDataType "fromList" [] Prefix
68oMapDataType :: DataType
75oMapDataType :: DataType
69oMapDataType = mkDataType "Data.Map.Ordered.Map" [fromListConstr]
76oMapDataType = mkDataType "Data.Map.Ordered.Map" [fromListConstr]
78+#if MIN_VERSION_base(4,9,0)
79+instance (Ord k, Semigroup v) => Semigroup (Bias L (OMap k v)) where
80+ Bias o <> Bias o' = Bias (unionWithL (const (<>)) o o')
81+instance (Ord k, Semigroup v) => Semigroup (Bias R (OMap k v)) where
82+ Bias o <> Bias o' = Bias (unionWithR (const (<>)) o o')
85+-- | Empty maps and map union. When combining two sets that share elements, the
86+-- indices of the left argument are preferred, and the values are combined with
89+-- See the asymptotics of 'unionWithL'.
90+instance (Ord k, Monoid v) => Monoid (Bias L (OMap k v)) where
92+ mappend (Bias o) (Bias o') = Bias (unionWithL (const mappend) o o')
94+-- | Empty maps and map union. When combining two sets that share elements, the
95+-- indices of the right argument are preferred, and the values are combined
98+-- See the asymptotics of 'unionWithR'.
99+instance (Ord k, Monoid v) => Monoid (Bias R (OMap k v)) where
101+ mappend (Bias o) (Bias o') = Bias (unionWithR (const mappend) o o')
103+-- | Values are traversed in insertion order, not key order.
105+-- /O(n*log(n))/ where /n/ is the size of the map.
106+instance Ord k => Traversable (OMap k) where
107+ traverse f (OMap tvs kvs) = fromKV <$> traverse (\(k,v) -> (,) k <$> f v) kvs
71infixr 5 <|, |< -- copy :
109infixr 5 <|, |< -- copy :
72infixl 5 >|, |>
110infixl 5 >|, |>
73infixr 6 <>|, |<> -- copy <>
111infixr 6 <>|, |<> -- copy <>
@@ -207,6 +245,10 @@ fromTV :: Ord k => Map k (Tag, v) -> OMap k v
207fromTV tvs = OMap tvs kvs where
245fromTV tvs = OMap tvs kvs where
208 kvs = M.fromList [(t,(k,v)) | (k,(t,v)) <- M.toList tvs]
246 kvs = M.fromList [(t,(k,v)) | (k,(t,v)) <- M.toList tvs]
248+fromKV :: Ord k => Map Tag (k, v) -> OMap k v
249+fromKV kvs = OMap tvs kvs where
250+ tvs = M.fromList [(k,(t,v)) | (t,(k,v)) <- M.toList kvs]
210findIndex :: Ord k => k -> OMap k v -> Maybe Index
252findIndex :: Ord k => k -> OMap k v -> Maybe Index
211findIndex k o@(OMap tvs kvs) = do
253findIndex k o@(OMap tvs kvs) = do
212 (t, _) <- M.lookup k tvs
254 (t, _) <- M.lookup k tvs