@@ -1,3 +1,8 @@
1+{-# LANGUAGE CPP #-}
2+#if __GLASGOW_HASKELL__
3+{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
4+#endif
5+
16-- | An 'OMap' behaves much like a 'Map', with all the same asymptotics, but
27-- also remembers the order that keys were inserted.
38module Data.Map.Ordered
@@ -34,6 +39,10 @@ import Data.Map.Util (Index, Tag, maxTag, minTag, nextHigherTag, nextLowerTag, r
3439import Prelude hiding (filter, lookup, null)
3540import qualified Data.Map as M
3641
42+#if __GLASGOW_HASKELL__
43+import Data.Data
44+#endif
45+
3746data OMap k v = OMap !(Map k (Tag, v)) !(Map Tag (k, v))
3847
3948-- | Values are produced in insertion order, not key order.
@@ -43,6 +52,32 @@ instance ( Ord k, Ord v) => Ord (OMap k v) where compare = compare `on`
4352instance ( Show k, Show v) => Show (OMap k v) where showsPrec = showsPrecList assocs
4453instance (Ord k, Read k, Read v) => Read (OMap k v) where readsPrec = readsPrecList fromList
4554
55+#if __GLASGOW_HASKELL__
56+# if __GLASGOW_HASKELL__ >= 708
57+deriving instance Typeable OMap
58+# else
59+deriving instance Typeable2 OMap
60+# endif
61+
62+-- This instance preserves data abstraction at the cost of inefficiency.
63+-- We provide limited reflection services for the sake of data abstraction.
64+
65+instance (Data k, Data a, Ord k) => Data (OMap k a) where
66+ gfoldl f z m = z fromList `f` assocs m
67+ toConstr _ = fromListConstr
68+ gunfold k z c = case constrIndex c of
69+ 1 -> k (z fromList)
70+ _ -> error "gunfold"
71+ dataTypeOf _ = oMapDataType
72+ dataCast2 f = gcast2 f
73+
74+fromListConstr :: Constr
75+fromListConstr = mkConstr oMapDataType "fromList" [] Prefix
76+
77+oMapDataType :: DataType
78+oMapDataType = mkDataType "Data.Map.Ordered.Map" [fromListConstr]
79+#endif
80+
4681infixr 5 <|, |< -- copy :
4782infixl 5 >|, |>
4883