diff --git a/insert-ordered-containers.cabal b/insert-ordered-containers.cabal index 6cc0026..0b39efb 100644 --- a/insert-ordered-containers.cabal +++ b/insert-ordered-containers.cabal @@ -1,6 +1,5 @@ name: insert-ordered-containers -version: 0.2.1.0 -x-revision: 5 +version: 0.2.2.0 synopsis: Associative containers retaining insertion order for traversals. description: Associative containers retaining insertion order for traversals. @@ -45,7 +44,7 @@ library , semigroups >=0.16.2.2 && <0.19 , text >=1.2.0.6 && <1.3 , transformers >=0.3.0.0 && <0.6 - , unordered-containers >=0.2.7.0 && <0.3 + , unordered-containers >=0.2.9.0 && <0.3 exposed-modules: Data.HashMap.Strict.InsOrd default-language: Haskell2010 diff --git a/src/Data/HashMap/Strict/InsOrd.hs b/src/Data/HashMap/Strict/InsOrd.hs index 5b98107..a594e7d 100644 --- a/src/Data/HashMap/Strict/InsOrd.hs +++ b/src/Data/HashMap/Strict/InsOrd.hs @@ -114,6 +114,10 @@ import qualified Data.HashMap.Strict as HashMap import qualified GHC.Exts as Exts #endif +#if MIN_VERSION_base(4,9,0) +import Data.Functor.Classes +#endif + ------------------------------------------------------------------------------- -- Strict Pair Int a ------------------------------------------------------------------------------- @@ -136,6 +140,9 @@ incPK i (P j x) = P (i + j) x instance Eq a => Eq (P a) where P _ a == P _ b = a == b +instance Ord a => Ord (P a) where + compare (P _ a) (P _ b) = compare a b + instance Show a => Show (P a) where showsPrec d (P _ x) = showsPrec d x @@ -157,6 +164,39 @@ data InsOrdHashMap k v = InsOrdHashMap instance (Eq k, Eq v) => Eq (InsOrdHashMap k v) where InsOrdHashMap _ a == InsOrdHashMap _ b = a == b +instance (Ord k, Ord v) => Ord (InsOrdHashMap k v) where + compare (InsOrdHashMap _ a) (InsOrdHashMap _ b) = compare a b + +#if MIN_VERSION_base(4,9,0) +liftP :: (t1 -> t2 -> t3) -> P t1 -> P t2 -> t3 +liftP f pa pb = f (getPV pa) (getPV pb) + +instance Eq k => Eq1 (InsOrdHashMap k) where + liftEq f (InsOrdHashMap _ a) (InsOrdHashMap _ b) = liftEq (liftP f) a b + +instance Eq2 InsOrdHashMap where + liftEq2 f g (InsOrdHashMap _ a) (InsOrdHashMap _ b) = + liftEq2 f (liftP g) a b + +instance Ord k => Ord1 (InsOrdHashMap k) where + liftCompare f (InsOrdHashMap _ a) (InsOrdHashMap _ b) = + liftCompare (liftP f) a b + +instance Ord2 InsOrdHashMap where + liftCompare2 f g (InsOrdHashMap _ a) (InsOrdHashMap _ b) = + liftCompare2 f (liftP g) a b + +instance Show2 InsOrdHashMap where + liftShowsPrec2 spk slk spv slv d m = + showsUnaryWith (liftShowsPrec sp sl) "fromList" d (toList m) + where + sp = liftShowsPrec2 spk slk spv slv + sl = liftShowList2 spk slk spv slv + +instance Show k => Show1 (InsOrdHashMap k) where + liftShowsPrec = liftShowsPrec2 showsPrec showList +#endif + instance (Show k, Show v) => Show (InsOrdHashMap k v) where showsPrec d m = showParen (d > 10) $ showString "fromList " . showsPrec 11 (toList m)