module Data.IntMap.Polymorphic.Strict
  ( module Data.IntMap.Polymorphic.Strict
  , IntMap
  ) where

import           Control.Arrow                (first, second)
import qualified Data.IntMap.Lazy             as M
import           Data.IntMap.Polymorphic.Type (IntMap (..))

empty :: IntMap k v
empty :: IntMap k v
empty = IntMap v -> IntMap k v
forall k v. IntMap v -> IntMap k v
IntMap IntMap v
forall a. IntMap a
M.empty

singleton :: Integral k => k -> v -> IntMap k v
singleton :: k -> v -> IntMap k v
singleton k
k = IntMap v -> IntMap k v
forall k v. IntMap v -> IntMap k v
IntMap (IntMap v -> IntMap k v) -> (v -> IntMap v) -> v -> IntMap k v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> v -> IntMap v
forall a. Key -> a -> IntMap a
M.singleton (k -> Key
forall a b. (Integral a, Num b) => a -> b
fromIntegral k
k)

insert :: Integral k => k -> v -> IntMap k v -> IntMap k v
insert :: k -> v -> IntMap k v -> IntMap k v
insert k
k v
v IntMap k v
m = IntMap v -> IntMap k v
forall k v. IntMap v -> IntMap k v
IntMap (IntMap v -> IntMap k v) -> IntMap v -> IntMap k v
forall a b. (a -> b) -> a -> b
$ Key -> v -> IntMap v -> IntMap v
forall a. Key -> a -> IntMap a -> IntMap a
M.insert (k -> Key
forall a b. (Integral a, Num b) => a -> b
fromIntegral k
k) v
v (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m)

delete :: Integral k => k -> IntMap k v -> IntMap k v
delete :: k -> IntMap k v -> IntMap k v
delete k
k IntMap k v
m = IntMap v -> IntMap k v
forall k v. IntMap v -> IntMap k v
IntMap (IntMap v -> IntMap k v) -> IntMap v -> IntMap k v
forall a b. (a -> b) -> a -> b
$ Key -> IntMap v -> IntMap v
forall a. Key -> IntMap a -> IntMap a
M.delete (k -> Key
forall a b. (Integral a, Num b) => a -> b
fromIntegral k
k) (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m)

updateLookupWithKey :: Integral k => (k -> v -> Maybe v) -> k -> IntMap k v -> (Maybe v, IntMap k v)
updateLookupWithKey :: (k -> v -> Maybe v) -> k -> IntMap k v -> (Maybe v, IntMap k v)
updateLookupWithKey k -> v -> Maybe v
f k
k IntMap k v
m = (IntMap v -> IntMap k v)
-> (Maybe v, IntMap v) -> (Maybe v, IntMap k v)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second IntMap v -> IntMap k v
forall k v. IntMap v -> IntMap k v
IntMap ((Maybe v, IntMap v) -> (Maybe v, IntMap k v))
-> (Maybe v, IntMap v) -> (Maybe v, IntMap k v)
forall a b. (a -> b) -> a -> b
$ (Key -> v -> Maybe v) -> Key -> IntMap v -> (Maybe v, IntMap v)
forall a.
(Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)
M.updateLookupWithKey (k -> v -> Maybe v
f (k -> v -> Maybe v) -> (Key -> k) -> Key -> v -> Maybe v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> k
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (k -> Key
forall a b. (Integral a, Num b) => a -> b
fromIntegral k
k) (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m)

toList :: Integral k => IntMap k v -> [(k, v)]
toList :: IntMap k v -> [(k, v)]
toList = ((Key, v) -> (k, v)) -> [(Key, v)] -> [(k, v)]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map ((Key -> k) -> (Key, v) -> (k, v)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Key -> k
forall a b. (Integral a, Num b) => a -> b
fromIntegral) ([(Key, v)] -> [(k, v)])
-> (IntMap k v -> [(Key, v)]) -> IntMap k v -> [(k, v)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap v -> [(Key, v)]
forall a. IntMap a -> [(Key, a)]
M.toList (IntMap v -> [(Key, v)])
-> (IntMap k v -> IntMap v) -> IntMap k v -> [(Key, v)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep

toAscList :: Integral k => IntMap k v -> [(k, v)]
toAscList :: IntMap k v -> [(k, v)]
toAscList = ((Key, v) -> (k, v)) -> [(Key, v)] -> [(k, v)]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map ((Key -> k) -> (Key, v) -> (k, v)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Key -> k
forall a b. (Integral a, Num b) => a -> b
fromIntegral) ([(Key, v)] -> [(k, v)])
-> (IntMap k v -> [(Key, v)]) -> IntMap k v -> [(k, v)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap v -> [(Key, v)]
forall a. IntMap a -> [(Key, a)]
M.toAscList (IntMap v -> [(Key, v)])
-> (IntMap k v -> IntMap v) -> IntMap k v -> [(Key, v)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep

lookup :: Integral k => k -> IntMap k v -> Maybe v
lookup :: k -> IntMap k v -> Maybe v
lookup k
k IntMap k v
m = Key -> IntMap v -> Maybe v
forall a. Key -> IntMap a -> Maybe a
M.lookup (k -> Key
forall a b. (Integral a, Num b) => a -> b
fromIntegral k
k) (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m)

(!) :: Integral k => IntMap k v -> k -> v
(!) IntMap k v
m k
k = IntMap v -> Key -> v
forall a. IntMap a -> Key -> a
(M.!) (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m) (k -> Key
forall a b. (Integral a, Num b) => a -> b
fromIntegral k
k)

null :: IntMap k v -> Bool
null :: IntMap k v -> Bool
null = IntMap v -> Bool
forall a. IntMap a -> Bool
M.null (IntMap v -> Bool)
-> (IntMap k v -> IntMap v) -> IntMap k v -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep

size :: IntMap k v -> Int
size :: IntMap k v -> Key
size = IntMap v -> Key
forall a. IntMap a -> Key
M.size (IntMap v -> Key) -> (IntMap k v -> IntMap v) -> IntMap k v -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep

union :: IntMap k v -> IntMap k v -> IntMap k v
union :: IntMap k v -> IntMap k v -> IntMap k v
union IntMap k v
m1 IntMap k v
m2 = IntMap v -> IntMap k v
forall k v. IntMap v -> IntMap k v
IntMap (IntMap v -> IntMap k v) -> IntMap v -> IntMap k v
forall a b. (a -> b) -> a -> b
$ IntMap v -> IntMap v -> IntMap v
forall a. IntMap a -> IntMap a -> IntMap a
M.union (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m1) (IntMap k v -> IntMap v
forall k v. IntMap k v -> IntMap v
rep IntMap k v
m2)

map :: (a -> b) -> IntMap k a -> IntMap k b
map :: (a -> b) -> IntMap k a -> IntMap k b
map a -> b
f = IntMap b -> IntMap k b
forall k v. IntMap v -> IntMap k v
IntMap (IntMap b -> IntMap k b)
-> (IntMap k a -> IntMap b) -> IntMap k a -> IntMap k b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> IntMap a -> IntMap b
forall a b. (a -> b) -> IntMap a -> IntMap b
M.map a -> b
f (IntMap a -> IntMap b)
-> (IntMap k a -> IntMap a) -> IntMap k a -> IntMap b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap k a -> IntMap a
forall k v. IntMap k v -> IntMap v
rep