@@ -1,5 +1,7 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE DeriveDataTypeable #-}
23{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE FlexibleInstances #-}
35
46-- | An 'OMap' behaves much like a 'Map', with mostly the same asymptotics, but
57-- also remembers the order that keys were inserted. All operations whose
@@ -20,6 +22,7 @@ module Data.Map.Ordered
2022 -- * If both sides contain the same key, the tuple's value wins
2123 , (<|), (|<), (>|), (|>)
2224 , (<>|), (|<>), unionWithL, unionWithR
25+ , Bias(Bias, unbiased), L, R
2326 -- * Deletion
2427 , delete, filter, (\\)
2528 , (|/\), (/\|), intersectionWith
@@ -37,7 +40,11 @@ import Data.Data
3740import Data.Foldable (Foldable, foldl', foldMap)
3841import Data.Function (on)
3942import Data.Map (Map)
40−import Data.Map.Util (Index, Tag, maxTag, minTag, nextHigherTag, nextLowerTag, readsPrecList, showsPrecList)
43+import Data.Map.Util
44+import Data.Monoid
45+#if MIN_VERSION_base(4,9,0)
46+import Data.Semigroup
47+#endif
4148import Prelude hiding (filter, lookup, null)
4249import qualified Data.Map as M
4350
@@ -68,6 +75,37 @@ fromListConstr = mkConstr oMapDataType "fromList" [] Prefix
6875oMapDataType :: DataType
6976oMapDataType = mkDataType "Data.Map.Ordered.Map" [fromListConstr]
7077
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')
83+#endif
84+
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
87+-- 'mappend'.
88+--
89+-- See the asymptotics of 'unionWithL'.
90+instance (Ord k, Monoid v) => Monoid (Bias L (OMap k v)) where
91+ mempty = Bias empty
92+ mappend (Bias o) (Bias o') = Bias (unionWithL (const mappend) o o')
93+
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
96+-- with 'mappend'.
97+--
98+-- See the asymptotics of 'unionWithR'.
99+instance (Ord k, Monoid v) => Monoid (Bias R (OMap k v)) where
100+ mempty = Bias empty
101+ mappend (Bias o) (Bias o') = Bias (unionWithR (const mappend) o o')
102+
103+-- | Values are traversed in insertion order, not key order.
104+--
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
108+
71109infixr 5 <|, |< -- copy :
72110infixl 5 >|, |>
73111infixr 6 <>|, |<> -- copy <>
@@ -207,6 +245,10 @@ fromTV :: Ord k => Map k (Tag, v) -> OMap k v
207245fromTV tvs = OMap tvs kvs where
208246 kvs = M.fromList [(t,(k,v)) | (k,(t,v)) <- M.toList tvs]
209247
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]
251+
210252findIndex :: Ord k => k -> OMap k v -> Maybe Index
211253findIndex k o@(OMap tvs kvs) = do
212254 (t, _) <- M.lookup k tvs