git.iko.soy/typeable/ordered-containers
on master help sign in
Merge pull request #8 from RyanGlScott/data-instances

Add Data/Typeable instances for OMap and OSet

commitaad319f31fc8676649d02c9dd8fbe3276077d4f1 authorDaniel Wagner <me@dmwit.com> dateSun 24 Mar 2019, 10:52 (8 years ago) parentba45a459d5fb84
2 files changed+70
Data/Map/Ordered.hs+35 Data/Set/Ordered.hs+35
Data/Map/Ordered.hs+35
@@ -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
Data/Set/Ordered.hs+35
@@ -1,3 +1,8 @@
1+{-# LANGUAGE CPP #-}
2+#if __GLASGOW_HASKELL__
3+{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
4+#endif
5+
16-- | An 'OSet' behaves much like a 'Set', with all the same asymptotics, but
27-- also remembers the order that values were inserted.
38module Data.Set.Ordered
@@ -33,6 +38,10 @@ import Data.Set (Set) -- so the haddocks link to the right place
3338import Prelude hiding (filter, foldr, lookup, null)
3439import qualified Data.Map as M
3540
41+#if __GLASGOW_HASKELL__
42+import Data.Data
43+#endif
44+
3645data OSet a = OSet !(Map a Tag) !(Map Tag a)
3746
3847-- | Values appear in insertion order, not ascending order.
@@ -42,6 +51,32 @@ instance Ord a => Ord (OSet a) where compare = compare `on` toList
4251instance Show a => Show (OSet a) where showsPrec = showsPrecList toList
4352instance (Ord a, Read a) => Read (OSet a) where readsPrec = readsPrecList fromList
4453
54+#if __GLASGOW_HASKELL__
55+# if __GLASGOW_HASKELL__ >= 708
56+deriving instance Typeable OSet
57+# else
58+deriving instance Typeable1 OSet
59+# endif
60+
61+-- This instance preserves data abstraction at the cost of inefficiency.
62+-- We provide limited reflection services for the sake of data abstraction.
63+
64+instance (Data a, Ord a) => Data (OSet a) where
65+ gfoldl f z set = z fromList `f` (toList set)
66+ toConstr _ = fromListConstr
67+ gunfold k z c = case constrIndex c of
68+ 1 -> k (z fromList)
69+ _ -> error "gunfold"
70+ dataTypeOf _ = oSetDataType
71+ dataCast1 f = gcast1 f
72+
73+fromListConstr :: Constr
74+fromListConstr = mkConstr oSetDataType "fromList" [] Prefix
75+
76+oSetDataType :: DataType
77+oSetDataType = mkDataType "Data.Set.Ordered.Set" [fromListConstr]
78+#endif
79+
4580infixr 5 <|, |< -- copy :
4681infixl 5 >|, |>
4782infixr 6 <>|, |<> -- copy <>