{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.Strict.Map.Internal where

import Data.Map.Lazy                  as L
import Data.Strict.Map.Autogen.Strict as S

import Control.Monad
import Data.Binary
import Data.Foldable.WithIndex
import Data.Functor.WithIndex
import Data.Traversable.WithIndex
import Data.Semigroup (Semigroup (..)) -- helps with compatibility
import Data.Strict.Classes

instance (Eq k, Ord k) => Strict (L.Map k v) (S.Map k v) where
  toStrict :: Map k v -> Map k v
toStrict = forall k a. Ord k => [(k, a)] -> Map k a
S.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
L.toList
  toLazy :: Map k v -> Map k v
toLazy = forall k a. Ord k => [(k, a)] -> Map k a
L.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
S.toList
  {-# INLINE toStrict #-}
  {-# INLINE toLazy #-}

-- code copied from indexed-traversable

instance FunctorWithIndex k (S.Map k) where
  imap :: forall a b. (k -> a -> b) -> Map k a -> Map k b
imap = forall k a b. (k -> a -> b) -> Map k a -> Map k b
S.mapWithKey
  {-# INLINE imap #-}

instance FoldableWithIndex k (S.Map k) where
  ifoldMap :: forall m a. Monoid m => (k -> a -> m) -> Map k a -> m
ifoldMap = forall m k a. Monoid m => (k -> a -> m) -> Map k a -> m
S.foldMapWithKey
  {-# INLINE ifoldMap #-}
  ifoldr :: forall a b. (k -> a -> b -> b) -> b -> Map k a -> b
ifoldr   = forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
S.foldrWithKey
  {-# INLINE ifoldr #-}
  ifoldl' :: forall b a. (k -> b -> a -> b) -> b -> Map k a -> b
ifoldl'  = forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
S.foldlWithKey' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip
  {-# INLINE ifoldl' #-}

instance TraversableWithIndex k (S.Map k) where
  itraverse :: forall (f :: * -> *) a b.
Applicative f =>
(k -> a -> f b) -> Map k a -> f (Map k b)
itraverse = forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
S.traverseWithKey
  {-# INLINE itraverse #-}

-- code copied from binary

instance (Binary k, Binary e) => Binary (S.Map k e) where
    put :: Map k e -> Put
put Map k e
m = forall t. Binary t => t -> Put
put (forall k a. Map k a -> Int
S.size Map k e
m) forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall t. Binary t => t -> Put
put (forall k a. Map k a -> [(k, a)]
S.toAscList Map k e
m)
    get :: Get (Map k e)
get   = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall k a. [(k, a)] -> Map k a
S.fromDistinctAscList forall t. Binary t => Get t
get