{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Multimap.Table.Internal
-- Maintainer  :  Ziyang Liu <free@cofree.io>
--
module Data.Multimap.Table.Internal (
  Table (..)
  , Size

  -- * Construction
  , empty
  , singleton
  , fromRowMap
  , fromColumnMap
  , transpose

  -- ** From Unordered Lists
  , fromList

  -- * Deletion\/Update
  , insert
  , delete
  , deleteRow
  , deleteColumn
  , adjust
  , adjustWithKeys
  , update
  , updateWithKeys
  , alter
  , alterWithKeys

  -- * Query
  -- ** Lookup
  , lookup
  , (!?)
  , (!)
  , hasCell
  , hasRow
  , hasColumn

  -- ** Size
  , null
  , notNull
  , size

  -- * Combine
  -- ** Union
  , union
  , unionWith
  , unionWithKeys
  , unions
  , unionsWith
  , unionsWithKeys

  -- ** Difference
  , difference

  -- * Traversal
  -- ** Map
  , map
  , mapWithKeys
  , traverseWithKeys
  , traverseMaybeWithKeys

  -- ** Folds
  , foldr
  , foldl
  , foldrWithKeys
  , foldlWithKeys
  , foldMapWithKeys

  -- ** Strict Folds
  , foldr'
  , foldl'
  , foldrWithKeys'
  , foldlWithKeys'

  -- * Conversion
  , row
  , column
  , rowMap
  , columnMap
  , rowKeys
  , columnKeys
  , rowKeysSet
  , columnKeysSet

  -- ** Lists
  , toList

  -- ** Ordered lists
  , toRowAscList
  , toColumnAscList
  , toRowDescList
  , toColumnDescList

  -- * Filter
  , filter
  , filterRow
  , filterColumn
  , filterWithKeys

  , mapMaybe
  , mapMaybeWithKeys
  , mapEither
  , mapEitherWithKeys
  ) where

import           Control.Arrow ((&&&))
import           Data.Data (Data)
import qualified Data.Foldable as Foldable
import           Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import           Data.Set (Set)

import Prelude hiding (filter, foldl, foldr, lookup, map, null)

infixl 9 !,!?

type Size = Int

newtype Table r c a = Table (Map r (Map c a), Map c (Map r a), Size)
  deriving (Table r c a -> Table r c a -> Bool
(Table r c a -> Table r c a -> Bool)
-> (Table r c a -> Table r c a -> Bool) -> Eq (Table r c a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall r c a.
(Eq r, Eq c, Eq a) =>
Table r c a -> Table r c a -> Bool
/= :: Table r c a -> Table r c a -> Bool
$c/= :: forall r c a.
(Eq r, Eq c, Eq a) =>
Table r c a -> Table r c a -> Bool
== :: Table r c a -> Table r c a -> Bool
$c== :: forall r c a.
(Eq r, Eq c, Eq a) =>
Table r c a -> Table r c a -> Bool
Eq, Eq (Table r c a)
Eq (Table r c a)
-> (Table r c a -> Table r c a -> Ordering)
-> (Table r c a -> Table r c a -> Bool)
-> (Table r c a -> Table r c a -> Bool)
-> (Table r c a -> Table r c a -> Bool)
-> (Table r c a -> Table r c a -> Bool)
-> (Table r c a -> Table r c a -> Table r c a)
-> (Table r c a -> Table r c a -> Table r c a)
-> Ord (Table r c a)
Table r c a -> Table r c a -> Bool
Table r c a -> Table r c a -> Ordering
Table r c a -> Table r c a -> Table r c a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall r c a. (Ord r, Ord c, Ord a) => Eq (Table r c a)
forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Bool
forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Ordering
forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Table r c a
min :: Table r c a -> Table r c a -> Table r c a
$cmin :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Table r c a
max :: Table r c a -> Table r c a -> Table r c a
$cmax :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Table r c a
>= :: Table r c a -> Table r c a -> Bool
$c>= :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Bool
> :: Table r c a -> Table r c a -> Bool
$c> :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Bool
<= :: Table r c a -> Table r c a -> Bool
$c<= :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Bool
< :: Table r c a -> Table r c a -> Bool
$c< :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Bool
compare :: Table r c a -> Table r c a -> Ordering
$ccompare :: forall r c a.
(Ord r, Ord c, Ord a) =>
Table r c a -> Table r c a -> Ordering
$cp1Ord :: forall r c a. (Ord r, Ord c, Ord a) => Eq (Table r c a)
Ord, Typeable (Table r c a)
DataType
Constr
Typeable (Table r c a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Table r c a -> c (Table r c a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Table r c a))
-> (Table r c a -> Constr)
-> (Table r c a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Table r c a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Table r c a)))
-> ((forall b. Data b => b -> b) -> Table r c a -> Table r c a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Table r c a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Table r c a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Table r c a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Table r c a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a))
-> Data (Table r c a)
Table r c a -> DataType
Table r c a -> Constr
(forall b. Data b => b -> b) -> Table r c a -> Table r c a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Table r c a -> c (Table r c a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Table r c a)
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Table r c a -> u
forall u. (forall d. Data d => d -> u) -> Table r c a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
Typeable (Table r c a)
forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
Table r c a -> DataType
forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
Table r c a -> Constr
forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall b. Data b => b -> b) -> Table r c a -> Table r c a
forall r c a u.
(Data r, Data c, Data a, Ord c, Ord r) =>
Int -> (forall d. Data d => d -> u) -> Table r c a -> u
forall r c a u.
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall d. Data d => d -> u) -> Table r c a -> [u]
forall r c a r r'.
(Data r, Data c, Data a, Ord c, Ord r) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
forall r c a r r'.
(Data r, Data c, Data a, Ord c, Ord r) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
forall r c a (m :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, Monad m) =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
forall r c a (m :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
forall r c a (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Table r c a)
forall r c a (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Table r c a -> c (Table r c a)
forall r c a (t :: * -> *) (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Table r c a))
forall r c a (t :: * -> * -> *) (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Table r c a))
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Table r c a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Table r c a -> c (Table r c a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Table r c a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Table r c a))
$cTable :: Constr
$tTable :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
$cgmapMo :: forall r c a (m :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
gmapMp :: (forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
$cgmapMp :: forall r c a (m :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
gmapM :: (forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
$cgmapM :: forall r c a (m :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, Monad m) =>
(forall d. Data d => d -> m d) -> Table r c a -> m (Table r c a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Table r c a -> u
$cgmapQi :: forall r c a u.
(Data r, Data c, Data a, Ord c, Ord r) =>
Int -> (forall d. Data d => d -> u) -> Table r c a -> u
gmapQ :: (forall d. Data d => d -> u) -> Table r c a -> [u]
$cgmapQ :: forall r c a u.
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall d. Data d => d -> u) -> Table r c a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
$cgmapQr :: forall r c a r r'.
(Data r, Data c, Data a, Ord c, Ord r) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
$cgmapQl :: forall r c a r r'.
(Data r, Data c, Data a, Ord c, Ord r) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Table r c a -> r
gmapT :: (forall b. Data b => b -> b) -> Table r c a -> Table r c a
$cgmapT :: forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall b. Data b => b -> b) -> Table r c a -> Table r c a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Table r c a))
$cdataCast2 :: forall r c a (t :: * -> * -> *) (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Table r c a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Table r c a))
$cdataCast1 :: forall r c a (t :: * -> *) (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Table r c a))
dataTypeOf :: Table r c a -> DataType
$cdataTypeOf :: forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
Table r c a -> DataType
toConstr :: Table r c a -> Constr
$ctoConstr :: forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
Table r c a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Table r c a)
$cgunfold :: forall r c a (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Table r c a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Table r c a -> c (Table r c a)
$cgfoldl :: forall r c a (c :: * -> *).
(Data r, Data c, Data a, Ord c, Ord r) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Table r c a -> c (Table r c a)
$cp1Data :: forall r c a.
(Data r, Data c, Data a, Ord c, Ord r) =>
Typeable (Table r c a)
Data)

instance (Show r, Show c, Show a) => Show (Table r c a) where
  showsPrec :: Int -> Table r c a -> ShowS
showsPrec Int
d Table r c a
m = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
    String -> ShowS
showString String
"fromList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(r, c, a)] -> ShowS
forall a. Show a => a -> ShowS
shows (Table r c a -> [(r, c, a)]
forall r c a. Table r c a -> [(r, c, a)]
toList Table r c a
m)

instance (Ord r, Ord c, Read r, Read c, Read a) => Read (Table r c a) where
  readsPrec :: Int -> ReadS (Table r c a)
readsPrec Int
p = Bool -> ReadS (Table r c a) -> ReadS (Table r c a)
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ReadS (Table r c a) -> ReadS (Table r c a))
-> ReadS (Table r c a) -> ReadS (Table r c a)
forall a b. (a -> b) -> a -> b
$ \ String
r -> do
    (String
"fromList",String
s) <- ReadS String
lex String
r
    ([(r, c, a)]
xs,String
t) <- ReadS [(r, c, a)]
forall a. Read a => ReadS a
reads String
s
    (Table r c a, String) -> [(Table r c a, String)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(r, c, a)] -> Table r c a
forall r c a. (Ord r, Ord c) => [(r, c, a)] -> Table r c a
fromList [(r, c, a)]
xs,String
t)

instance Functor (Table r c) where
  fmap :: (a -> b) -> Table r c a -> Table r c b
fmap = (a -> b) -> Table r c a -> Table r c b
forall a b r c. (a -> b) -> Table r c a -> Table r c b
map

instance Foldable.Foldable (Table r c) where
  foldMap :: (a -> m) -> Table r c a -> m
foldMap = (r -> c -> a -> m) -> Table r c a -> m
forall m r c a. Monoid m => (r -> c -> a -> m) -> Table r c a -> m
foldMapWithKeys ((r -> c -> a -> m) -> Table r c a -> m)
-> ((a -> m) -> r -> c -> a -> m) -> (a -> m) -> Table r c a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> m) -> r -> c -> a -> m
forall a b. a -> b -> a
const ((c -> a -> m) -> r -> c -> a -> m)
-> ((a -> m) -> c -> a -> m) -> (a -> m) -> r -> c -> a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m) -> c -> a -> m
forall a b. a -> b -> a
const
  {-# INLINE foldMap #-}

instance (Ord r, Ord c) => Traversable (Table r c) where
  traverse :: (a -> f b) -> Table r c a -> f (Table r c b)
traverse = (r -> c -> a -> f b) -> Table r c a -> f (Table r c b)
forall (t :: * -> *) r c a b.
(Applicative t, Ord r, Ord c) =>
(r -> c -> a -> t b) -> Table r c a -> t (Table r c b)
traverseWithKeys ((r -> c -> a -> f b) -> Table r c a -> f (Table r c b))
-> ((a -> f b) -> r -> c -> a -> f b)
-> (a -> f b)
-> Table r c a
-> f (Table r c b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> f b) -> r -> c -> a -> f b
forall a b. a -> b -> a
const ((c -> a -> f b) -> r -> c -> a -> f b)
-> ((a -> f b) -> c -> a -> f b)
-> (a -> f b)
-> r
-> c
-> a
-> f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f b) -> c -> a -> f b
forall a b. a -> b -> a
const
  {-# INLINE traverse #-}

instance (Ord r, Ord c) => Semigroup (Table r c a) where
  <> :: Table r c a -> Table r c a -> Table r c a
(<>) = Table r c a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
Table r c a -> Table r c a -> Table r c a
union

instance (Ord r, Ord c) => Monoid (Table r c a) where
  mempty :: Table r c a
mempty = Table r c a
forall r c a. Table r c a
empty
  mappend :: Table r c a -> Table r c a -> Table r c a
mappend = Table r c a -> Table r c a -> Table r c a
forall a. Semigroup a => a -> a -> a
(<>)

------------------------------------------------------------------------------

-- | /O(1)/. The empty table.
--
-- > size empty === 0
empty :: Table r c a
empty :: Table r c a
empty = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
forall k a. Map k a
Map.empty, Map c (Map r a)
forall k a. Map k a
Map.empty, Int
0)

-- | /O(1)/. A table with a single element.
--
-- > singleton 1 'a' "a" === fromList [(1,'a',"a")]
-- > size (singleton 1 'a' "a") === 1
singleton :: r -> c -> a -> Table r c a
singleton :: r -> c -> a -> Table r c a
singleton r
r c
c a
a = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (r -> Map c a -> Map r (Map c a)
forall k a. k -> a -> Map k a
Map.singleton r
r (c -> a -> Map c a
forall k a. k -> a -> Map k a
Map.singleton c
c a
a), c -> Map r a -> Map c (Map r a)
forall k a. k -> a -> Map k a
Map.singleton c
c (r -> a -> Map r a
forall k a. k -> a -> Map k a
Map.singleton r
r a
a), Int
1)

-- | Build a table from a list of key\/value pairs.
--
-- > fromList ([] :: [(Int, Char, String)]) === empty
fromList :: (Ord r, Ord c) => [(r, c, a)] -> Table r c a
fromList :: [(r, c, a)] -> Table r c a
fromList = ((r, c, a) -> Table r c a -> Table r c a)
-> Table r c a -> [(r, c, a)] -> Table r c a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr ((r -> c -> a -> Table r c a -> Table r c a)
-> (r, c, a) -> Table r c a -> Table r c a
forall a b c d. (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 r -> c -> a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
r -> c -> a -> Table r c a -> Table r c a
insert) Table r c a
forall r c a. Table r c a
empty

-- | Build a table from a row map.
--
-- > fromRowMap (Map.fromList [(1, Map.fromList [('a',"b"),('b',"c")]), (2, Map.fromList [('a',"d")])])
-- >   === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]
fromRowMap :: (Ord r, Ord c) => Map r (Map c a) -> Table r c a
fromRowMap :: Map r (Map c a) -> Table r c a
fromRowMap Map r (Map c a)
m = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
m', Map r (Map c a) -> Map c (Map r a)
forall r c a. (Ord r, Ord c) => Map r (Map c a) -> Map c (Map r a)
transpose' Map r (Map c a)
m', Map r (Map c a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map r (Map c a)
m')
  where m' :: Map r (Map c a)
m' = Map r (Map c a) -> Map r (Map c a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty Map r (Map c a)
m

-- | Build a table from a column map.
--
-- > fromColumnMap (Map.fromList [(1, Map.fromList [('a',"b"),('b',"c")]), (2, Map.fromList [('a',"d")])])
-- >   === fromList [('a',1,"b"),('a',2,"d"),('b',1,"c")]
fromColumnMap :: (Ord r, Ord c) => Map c (Map r a) -> Table r c a
fromColumnMap :: Map c (Map r a) -> Table r c a
fromColumnMap Map c (Map r a)
m = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map c (Map r a) -> Map r (Map c a)
forall r c a. (Ord r, Ord c) => Map r (Map c a) -> Map c (Map r a)
transpose' Map c (Map r a)
m', Map c (Map r a)
m', Map c (Map r a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map c (Map r a)
m')
  where m' :: Map c (Map r a)
m' = Map c (Map r a) -> Map c (Map r a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty Map c (Map r a)
m

-- | Flip the row and column keys.
--
-- > transpose (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === fromList [('a',1,"b"),('a',2,"d"),('b',1,"c")]
transpose :: Table r c a -> Table c r a
transpose :: Table r c a -> Table c r a
transpose (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
sz)) = (Map c (Map r a), Map r (Map c a), Int) -> Table c r a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map c (Map r a)
cm, Map r (Map c a)
rm, Int
sz)

------------------------------------------------------------------------------

-- | /O(log k)/. Associate with value with the row key and the column key.
-- If the table already contains a value for those keys, the value is replaced.
--
-- > insert 1 'a' "a" empty === singleton 1 'a' "a"
-- > insert 1 'a' "a" (fromList [(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'a',"a"),(1,'b',"c"),(2,'a',"d")]
-- > insert 1 'a' "a" (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'a',"a"),(1,'b',"c"),(2,'a',"d")]
insert :: (Ord r, Ord c) => r -> c -> a -> Table r c a -> Table r c a
insert :: r -> c -> a -> Table r c a -> Table r c a
insert r
r c
c a
a (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
forall r c a.
(Ord r, Ord c) =>
r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps' r
r c
c Map r (Map c a)
rm' Map c (Map r a)
cm'
  where
    rm' :: Map r (Map c a)
rm' = (Maybe (Map c a) -> Maybe (Map c a))
-> r -> Map r (Map c a) -> Map r (Map c a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter Maybe (Map c a) -> Maybe (Map c a)
f r
r Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = (Maybe (Map r a) -> Maybe (Map r a))
-> c -> Map c (Map r a) -> Map c (Map r a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter Maybe (Map r a) -> Maybe (Map r a)
g c
c Map c (Map r a)
cm
    f :: Maybe (Map c a) -> Maybe (Map c a)
f = Map c a -> Maybe (Map c a)
forall a. a -> Maybe a
Just (Map c a -> Maybe (Map c a))
-> (Maybe (Map c a) -> Map c a)
-> Maybe (Map c a)
-> Maybe (Map c a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map c a -> (Map c a -> Map c a) -> Maybe (Map c a) -> Map c a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (c -> a -> Map c a
forall k a. k -> a -> Map k a
Map.singleton c
c a
a) (c -> a -> Map c a -> Map c a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert c
c a
a)
    g :: Maybe (Map r a) -> Maybe (Map r a)
g = Map r a -> Maybe (Map r a)
forall a. a -> Maybe a
Just (Map r a -> Maybe (Map r a))
-> (Maybe (Map r a) -> Map r a)
-> Maybe (Map r a)
-> Maybe (Map r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map r a -> (Map r a -> Map r a) -> Maybe (Map r a) -> Map r a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (r -> a -> Map r a
forall k a. k -> a -> Map k a
Map.singleton r
r a
a) (r -> a -> Map r a -> Map r a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert r
r a
a)

-- | /O(log k)/. Remove the value associated with the given keys.
--
-- > delete 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'b',"c"),(2,'a',"d")]
-- > delete 1 'a' (fromList [(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'b',"c"),(2,'a',"d")]
delete :: (Ord r, Ord c) => r -> c -> Table r c a -> Table r c a
delete :: r -> c -> Table r c a -> Table r c a
delete r
r c
c (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
forall r c a.
(Ord r, Ord c) =>
r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps' r
r c
c Map r (Map c a)
rm' Map c (Map r a)
cm'
  where
    rm' :: Map r (Map c a)
rm' = (Map c a -> Map c a) -> r -> Map r (Map c a) -> Map r (Map c a)
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust (c -> Map c a -> Map c a
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete c
c) r
r Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = (Map r a -> Map r a) -> c -> Map c (Map r a) -> Map c (Map r a)
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust (r -> Map r a -> Map r a
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete r
r) c
c Map c (Map r a)
cm

-- | Remove an entire row.
--
-- > deleteRow 1 (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 2 'a' "d"
-- > deleteRow 3 (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]
deleteRow :: Ord r => r -> Table r c a -> Table r c a
deleteRow :: r -> Table r c a -> Table r c a
deleteRow r
r (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
rm', Map c (Map r a)
cm', Map r (Map c a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map r (Map c a)
rm')
  where
    rm' :: Map r (Map c a)
rm' = r -> Map r (Map c a) -> Map r (Map c a)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete r
r Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = Map c (Map r a) -> Map c (Map r a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty (Map c (Map r a) -> Map c (Map r a))
-> Map c (Map r a) -> Map c (Map r a)
forall a b. (a -> b) -> a -> b
$ (Map r a -> Map r a) -> Map c (Map r a) -> Map c (Map r a)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (r -> Map r a -> Map r a
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete r
r) Map c (Map r a)
cm

-- | Remove an entire column.
--
-- > deleteColumn 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 1 'b' "c"
-- > deleteColumn 'z' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]
deleteColumn :: Ord c => c -> Table r c a -> Table r c a
deleteColumn :: c -> Table r c a -> Table r c a
deleteColumn c
c (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
rm', Map c (Map r a)
cm', Map c (Map r a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map c (Map r a)
cm')
  where
    rm' :: Map r (Map c a)
rm' = Map r (Map c a) -> Map r (Map c a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty (Map r (Map c a) -> Map r (Map c a))
-> Map r (Map c a) -> Map r (Map c a)
forall a b. (a -> b) -> a -> b
$ (Map c a -> Map c a) -> Map r (Map c a) -> Map r (Map c a)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (c -> Map c a -> Map c a
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete c
c) Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = c -> Map c (Map r a) -> Map c (Map r a)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete c
c Map c (Map r a)
cm

-- | /O(log k)/, assuming the function @a -> a@ takes /O(1)/.
-- Update the value at a specific row key and column key, if exists.
--
-- > adjust ("new " ++) 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === fromList [(1,'a',"new b"),(1,'b',"c"),(2,'a',"d")]
adjust :: (Ord r, Ord c) => (a -> a) -> r -> c -> Table r c a -> Table r c a
adjust :: (a -> a) -> r -> c -> Table r c a -> Table r c a
adjust = (r -> c -> a -> a) -> r -> c -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> a -> a) -> r -> c -> Table r c a -> Table r c a
adjustWithKeys ((r -> c -> a -> a) -> r -> c -> Table r c a -> Table r c a)
-> ((a -> a) -> r -> c -> a -> a)
-> (a -> a)
-> r
-> c
-> Table r c a
-> Table r c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> a) -> r -> c -> a -> a
forall a b. a -> b -> a
const ((c -> a -> a) -> r -> c -> a -> a)
-> ((a -> a) -> c -> a -> a) -> (a -> a) -> r -> c -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> c -> a -> a
forall a b. a -> b -> a
const

-- | /O(log k)/, assuming the function @r -> c -> a -> a@ takes /O(1)/.
-- Update the value at a specific row key and column key, if exists.
--
-- > adjustWithKeys (\r c x -> show r ++ ":" ++ show c ++ ":new " ++ x) 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")])
-- >   === fromList [(1,'a',"1:'a':new b"),(1,'b',"c"),(2,'a',"d")]
adjustWithKeys
  :: (Ord r, Ord c)
  => (r -> c -> a -> a) -> r -> c -> Table r c a -> Table r c a
adjustWithKeys :: (r -> c -> a -> a) -> r -> c -> Table r c a -> Table r c a
adjustWithKeys r -> c -> a -> a
f = (r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
updateWithKeys (\r
r c
c a
a -> a -> Maybe a
forall a. a -> Maybe a
Just (r -> c -> a -> a
f r
r c
c a
a))

-- | /O(log k)/, assuming the function @a -> 'Maybe' a@ takes /O(1)/.
-- The expression (@'update' f r c table@) updates the value at the given
-- row and column keys, if exists. If @f@ returns 'Nothing', the value
-- associated with those keys, if exists is deleted.
--
-- > let f x = if x == "b" then Just "new b" else Nothing in do
-- >   update f 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"new b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   update f 1 'a' (fromList [(1,'a',"a"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
update :: (Ord r, Ord c) => (a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
update :: (a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
update = (r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
updateWithKeys ((r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a)
-> ((a -> Maybe a) -> r -> c -> a -> Maybe a)
-> (a -> Maybe a)
-> r
-> c
-> Table r c a
-> Table r c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> Maybe a) -> r -> c -> a -> Maybe a
forall a b. a -> b -> a
const ((c -> a -> Maybe a) -> r -> c -> a -> Maybe a)
-> ((a -> Maybe a) -> c -> a -> Maybe a)
-> (a -> Maybe a)
-> r
-> c
-> a
-> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe a) -> c -> a -> Maybe a
forall a b. a -> b -> a
const

-- | /O(log k)/, assuming the function @r -> c -> a -> 'Maybe' a@ takes /O(1)/.
-- The expression (@'updateWithKeys' f r c table@) updates the value at the given
-- row and column keys, if exists. If @f@ returns 'Nothing', the value
-- associated with those keys, if exists is deleted.
--
-- > let f r c x = if x == "b" then Just (show r ++ ":" ++ show c ++ ":new b") else Nothing in do
-- >   updateWithKeys f 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"1:'a':new b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   updateWithKeys f 1 'a' (fromList [(1,'a',"a"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
updateWithKeys
  :: (Ord r, Ord c)
  => (r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
updateWithKeys :: (r -> c -> a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
updateWithKeys r -> c -> a -> Maybe a
f = (r -> c -> Maybe a -> Maybe a)
-> r -> c -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> Maybe a -> Maybe a)
-> r -> c -> Table r c a -> Table r c a
alterWithKeys (\r
r c
c -> (Maybe a -> (a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= r -> c -> a -> Maybe a
f r
r c
c))

-- | /O(log k)/, assuming the function @'Maybe' a -> 'Maybe' a@ takes /O(1)/.
-- The expression (@'alter' f r c table@) alters the value at the given
-- row and column keys, if exists. It can be used to insert, delete
-- or update a value.
--
-- > let (f,g,h) = (const Nothing, const (Just "hello"), fmap ('z':)) in do
-- >   alter f 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alter f 4 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alter f 2 'b' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alter g 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"hello"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alter g 4 'e' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c"),(4,'e',"hello")]
-- >   alter h 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"zb"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alter h 2 'b' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
alter :: (Ord r, Ord c) => (Maybe a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
alter :: (Maybe a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
alter = (r -> c -> Maybe a -> Maybe a)
-> r -> c -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> Maybe a -> Maybe a)
-> r -> c -> Table r c a -> Table r c a
alterWithKeys ((r -> c -> Maybe a -> Maybe a)
 -> r -> c -> Table r c a -> Table r c a)
-> ((Maybe a -> Maybe a) -> r -> c -> Maybe a -> Maybe a)
-> (Maybe a -> Maybe a)
-> r
-> c
-> Table r c a
-> Table r c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> Maybe a -> Maybe a) -> r -> c -> Maybe a -> Maybe a
forall a b. a -> b -> a
const ((c -> Maybe a -> Maybe a) -> r -> c -> Maybe a -> Maybe a)
-> ((Maybe a -> Maybe a) -> c -> Maybe a -> Maybe a)
-> (Maybe a -> Maybe a)
-> r
-> c
-> Maybe a
-> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> Maybe a) -> c -> Maybe a -> Maybe a
forall a b. a -> b -> a
const

-- | /O(log k)/, assuming the function @r -> c -> 'Maybe' a -> 'Maybe' a@ takes /O(1)/.
-- The expression (@'alterWithKeys' f r c table@) alters the value at the given
-- row and column keys, if exists. It can be used to insert, delete
-- or update a value.
--
-- > let (f,g) = (\_ _ _ -> Nothing, \r c -> fmap ((show r ++ ":" ++ show c ++ ":") ++)) in do
-- >   alterWithKeys f 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alterWithKeys f 4 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alterWithKeys f 2 'b' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alterWithKeys g 1 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"1:'a':b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
-- >   alterWithKeys g 2 'b' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]) === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(3,'a',"c")]
alterWithKeys
  :: (Ord r, Ord c)
  => (r -> c -> Maybe a -> Maybe a) -> r -> c -> Table r c a -> Table r c a
alterWithKeys :: (r -> c -> Maybe a -> Maybe a)
-> r -> c -> Table r c a -> Table r c a
alterWithKeys r -> c -> Maybe a -> Maybe a
f r
r c
c tbl :: Table r c a
tbl@(Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_))
  | Just a
a <- r -> c -> Maybe a -> Maybe a
f r
r c
c (r -> c -> Table r c a -> Maybe a
forall r c a. (Ord r, Ord c) => r -> c -> Table r c a -> Maybe a
lookup r
r c
c Table r c a
tbl) =
      let rm' :: Map r (Map c a)
rm' = (Maybe (Map c a) -> Maybe (Map c a))
-> r -> Map r (Map c a) -> Map r (Map c a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter (Map c a -> Maybe (Map c a)
forall a. a -> Maybe a
Just (Map c a -> Maybe (Map c a))
-> (Maybe (Map c a) -> Map c a)
-> Maybe (Map c a)
-> Maybe (Map c a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map c a -> (Map c a -> Map c a) -> Maybe (Map c a) -> Map c a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (c -> a -> Map c a
forall k a. k -> a -> Map k a
Map.singleton c
c a
a) (c -> a -> Map c a -> Map c a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert c
c a
a)) r
r Map r (Map c a)
rm
          cm' :: Map c (Map r a)
cm' = (Maybe (Map r a) -> Maybe (Map r a))
-> c -> Map c (Map r a) -> Map c (Map r a)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Map.alter (Map r a -> Maybe (Map r a)
forall a. a -> Maybe a
Just (Map r a -> Maybe (Map r a))
-> (Maybe (Map r a) -> Map r a)
-> Maybe (Map r a)
-> Maybe (Map r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map r a -> (Map r a -> Map r a) -> Maybe (Map r a) -> Map r a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (r -> a -> Map r a
forall k a. k -> a -> Map k a
Map.singleton r
r a
a) (r -> a -> Map r a -> Map r a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert r
r a
a)) c
c Map c (Map r a)
cm
       in r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
forall r c a.
(Ord r, Ord c) =>
r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps' r
r c
c Map r (Map c a)
rm' Map c (Map r a)
cm'
  | Bool
otherwise = r -> c -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
r -> c -> Table r c a -> Table r c a
delete r
r c
c Table r c a
tbl

------------------------------------------------------------------------------

-- | /O(log k)/. Lookup the values at a row key and column key in the map.
lookup :: (Ord r, Ord c) => r -> c -> Table r c a -> Maybe a
lookup :: r -> c -> Table r c a -> Maybe a
lookup r
r c
c (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = r -> Map r (Map c a) -> Maybe (Map c a)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup r
r Map r (Map c a)
rm Maybe (Map c a) -> (Map c a -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= c -> Map c a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup c
c

-- | /O(log k)/. Lookup the values at a row key and column key in the map.
--
-- > fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")] !? (1,'a') === Just "b"
-- > fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")] !? (1,'c') === Nothing
(!?) :: (Ord r, Ord c) => Table r c a -> (r, c) -> Maybe a
!? :: Table r c a -> (r, c) -> Maybe a
(!?) = ((r, c) -> Table r c a -> Maybe a)
-> Table r c a -> (r, c) -> Maybe a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((r -> c -> Table r c a -> Maybe a)
-> (r, c) -> Table r c a -> Maybe a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry r -> c -> Table r c a -> Maybe a
forall r c a. (Ord r, Ord c) => r -> c -> Table r c a -> Maybe a
lookup)

-- | /O(log k)/. Lookup the values at a row key and column key in the map.
-- Calls 'error' if the value does not exist.
--
-- > fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")] ! (1,'a') === "b"
(!) :: (Ord r, Ord c) => Table r c a -> (r, c) -> a
(!) Table r c a
tbl (r, c)
keys =
  a -> Maybe a -> a
forall a. a -> Maybe a -> a
Maybe.fromMaybe (String -> a
forall a. HasCallStack => String -> a
error String
"Table.!: cell does not exist") (Table r c a
tbl Table r c a -> (r, c) -> Maybe a
forall r c a. (Ord r, Ord c) => Table r c a -> (r, c) -> Maybe a
!? (r, c)
keys)

-- | /O(log k)/. Is there a value associated with the given row and
-- column keys?
--
-- > hasCell (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) (1,'a') === True
-- > hasCell (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) (1,'c') === False
hasCell :: (Ord r, Ord c) => Table r c a -> (r, c) -> Bool
hasCell :: Table r c a -> (r, c) -> Bool
hasCell (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) (r
r, c
c) =
  Bool -> (Map c a -> Bool) -> Maybe (Map c a) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (c -> Map c a -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member c
c) (r -> Map r (Map c a) -> Maybe (Map c a)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup r
r Map r (Map c a)
rm)

-- | /O(log r)/. Is there a row with the given row key that has at least
--  one value?
--
-- > hasRow (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) 1 === True
-- > hasRow (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) 3 === False
hasRow :: Ord r => Table r c a -> r -> Bool
hasRow :: Table r c a -> r -> Bool
hasRow (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) r
r = r -> Map r (Map c a) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member r
r Map r (Map c a)
rm

-- | /O(log c)/. Is there a column with the given column key that has at least
-- one value?
--
-- > hasColumn (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) 'a' === True
-- > hasColumn (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) 'c' === False
hasColumn :: Ord c => Table r c a -> c -> Bool
hasColumn :: Table r c a -> c -> Bool
hasColumn (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) c
c = c -> Map c (Map r a) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member c
c Map c (Map r a)
cm

-- | /O(1)/. Is the table empty?
--
-- > Data.Multimap.Table.null empty === True
-- > Data.Multimap.Table.null (singleton 1 'a' "a") === False
null :: Table r c a -> Bool
null :: Table r c a -> Bool
null (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r (Map c a) -> Bool
forall k a. Map k a -> Bool
Map.null Map r (Map c a)
rm

-- | /O(1)/. Is the table non-empty?
--
-- > notNull empty === False
-- > notNull (singleton 1 'a' "a") === True
notNull :: Table r c a -> Bool
notNull :: Table r c a -> Bool
notNull = Bool -> Bool
not (Bool -> Bool) -> (Table r c a -> Bool) -> Table r c a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Table r c a -> Bool
forall r c a. Table r c a -> Bool
null

-- | The total number of values for all row and column keys.
--
-- @size@ is evaluated lazily. Forcing the size for the first time takes up to
-- /O(n)/ and subsequent forces take /O(1)/.
--
-- > size empty === 0
-- > size (singleton 1 'a' "a") === 1
-- > size (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) === 3
size :: Table r c a -> Int
size :: Table r c a -> Int
size (Table (Map r (Map c a)
_, Map c (Map r a)
_, Int
sz)) = Int
sz

------------------------------------------------------------------------------

-- | Union two tables, preferring values from the first table
-- upon duplicate keys.
--
-- > union (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) (fromList [(1,'a',"c"),(2,'b',"d"),(3,'c',"e")])
-- >   === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(2,'b',"d"),(3,'c',"e")]
union :: (Ord r, Ord c) => Table r c a -> Table r c a -> Table r c a
union :: Table r c a -> Table r c a -> Table r c a
union = (a -> a -> a) -> Table r c a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(a -> a -> a) -> Table r c a -> Table r c a -> Table r c a
unionWith a -> a -> a
forall a b. a -> b -> a
const

-- | Union a number of tables, preferring values from the leftmost table
-- upon duplicate keys.
--
-- > unions [fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")], fromList [(1,'a',"c"),(2,'b',"d"),(3,'c',"e")]]
-- >   === fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b"),(2,'b',"d"),(3,'c',"e")]
unions :: (Foldable f, Ord r, Ord c) => f (Table r c a) -> Table r c a
unions :: f (Table r c a) -> Table r c a
unions = (Table r c a -> Table r c a -> Table r c a)
-> Table r c a -> f (Table r c a) -> Table r c a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr Table r c a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
Table r c a -> Table r c a -> Table r c a
union Table r c a
forall r c a. Table r c a
empty

-- | Union two tables with a combining function for duplicate keys.
--
-- > unionWith (++) (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) (fromList [(1,'a',"c"),(2,'b',"d"),(3,'c',"e")])
-- >   === fromList [(1,'a',"bc"),(1,'b',"c"),(2,'a',"b"),(2,'b',"d"),(3,'c',"e")]
unionWith :: (Ord r, Ord c) => (a -> a -> a) -> Table r c a -> Table r c a -> Table r c a
unionWith :: (a -> a -> a) -> Table r c a -> Table r c a -> Table r c a
unionWith = (r -> c -> a -> a -> a)
-> Table r c a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> a -> a -> a)
-> Table r c a -> Table r c a -> Table r c a
unionWithKeys ((r -> c -> a -> a -> a)
 -> Table r c a -> Table r c a -> Table r c a)
-> ((a -> a -> a) -> r -> c -> a -> a -> a)
-> (a -> a -> a)
-> Table r c a
-> Table r c a
-> Table r c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> a -> a) -> r -> c -> a -> a -> a
forall a b. a -> b -> a
const ((c -> a -> a -> a) -> r -> c -> a -> a -> a)
-> ((a -> a -> a) -> c -> a -> a -> a)
-> (a -> a -> a)
-> r
-> c
-> a
-> a
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> c -> a -> a -> a
forall a b. a -> b -> a
const

-- | Union two tables with a combining function for duplicate keys.
--
-- > let f r c a a' = show r ++ ":" ++ show c ++ ":" ++ a ++ "|" ++ a' in do
-- >   unionWithKeys f (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) (fromList [(1,'a',"c"),(2,'b',"d"),(3,'c',"e")])
-- >     === fromList [(1,'a',"1:'a':b|c"),(1,'b',"c"),(2,'a',"b"),(2,'b',"d"),(3,'c',"e")]
unionWithKeys
  :: (Ord r, Ord c)
  => (r -> c -> a -> a -> a)
  -> Table r c a -> Table r c a -> Table r c a
unionWithKeys :: (r -> c -> a -> a -> a)
-> Table r c a -> Table r c a -> Table r c a
unionWithKeys r -> c -> a -> a -> a
f (Table (Map r (Map c a)
rm1, Map c (Map r a)
cm1, Int
_)) (Table (Map r (Map c a)
rm2, Map c (Map r a)
cm2, Int
_)) = Map r (Map c a) -> Map c (Map r a) -> Table r c a
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c a)
rm Map c (Map r a)
cm
  where
    rm :: Map r (Map c a)
rm = (r -> Map c a -> Map c a -> Map c a)
-> Map r (Map c a) -> Map r (Map c a) -> Map r (Map c a)
forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWithKey ((c -> a -> a -> a) -> Map c a -> Map c a -> Map c a
forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWithKey ((c -> a -> a -> a) -> Map c a -> Map c a -> Map c a)
-> (r -> c -> a -> a -> a) -> r -> Map c a -> Map c a -> Map c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> a -> a
f) Map r (Map c a)
rm1 Map r (Map c a)
rm2
    cm :: Map c (Map r a)
cm = (c -> Map r a -> Map r a -> Map r a)
-> Map c (Map r a) -> Map c (Map r a) -> Map c (Map r a)
forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWithKey ((r -> a -> a -> a) -> Map r a -> Map r a -> Map r a
forall k a.
Ord k =>
(k -> a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWithKey ((r -> a -> a -> a) -> Map r a -> Map r a -> Map r a)
-> (c -> r -> a -> a -> a) -> c -> Map r a -> Map r a -> Map r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> c -> a -> a -> a) -> c -> r -> a -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> c -> a -> a -> a
f) Map c (Map r a)
cm1 Map c (Map r a)
cm2

-- | Union a number of tables with a combining function for duplicate keys.
--
-- > unionsWith (++) [fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")], fromList [(1,'a',"c"),(2,'b',"d"),(3,'c',"e")]]
-- >   === fromList [(1,'a',"bc"),(1,'b',"c"),(2,'a',"b"),(2,'b',"d"),(3,'c',"e")]
unionsWith :: (Foldable f, Ord r, Ord c) => (a -> a -> a) -> f (Table r c a) -> Table r c a
unionsWith :: (a -> a -> a) -> f (Table r c a) -> Table r c a
unionsWith a -> a -> a
f = (Table r c a -> Table r c a -> Table r c a)
-> Table r c a -> f (Table r c a) -> Table r c a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr ((a -> a -> a) -> Table r c a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(a -> a -> a) -> Table r c a -> Table r c a -> Table r c a
unionWith a -> a -> a
f) Table r c a
forall r c a. Table r c a
empty

-- | Union a number of tables with a combining function for duplicate keys.
--
-- > let f r c a a' = show r ++ ":" ++ show c ++ ":" ++ a ++ "|" ++ a' in do
-- >   unionsWithKeys f [fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")], fromList [(1,'a',"c"),(2,'b',"d"),(3,'c',"e")]]
-- >     === fromList [(1,'a',"1:'a':b|c"),(1,'b',"c"),(2,'a',"b"),(2,'b',"d"),(3,'c',"e")]
unionsWithKeys
  :: (Foldable f, Ord r, Ord c)
  => (r -> c -> a -> a -> a)
  -> f (Table r c a) -> Table r c a
unionsWithKeys :: (r -> c -> a -> a -> a) -> f (Table r c a) -> Table r c a
unionsWithKeys r -> c -> a -> a -> a
f = (Table r c a -> Table r c a -> Table r c a)
-> Table r c a -> f (Table r c a) -> Table r c a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr ((r -> c -> a -> a -> a)
-> Table r c a -> Table r c a -> Table r c a
forall r c a.
(Ord r, Ord c) =>
(r -> c -> a -> a -> a)
-> Table r c a -> Table r c a -> Table r c a
unionWithKeys r -> c -> a -> a -> a
f) Table r c a
forall r c a. Table r c a
empty

-- | Difference of two tables. Return values in the first table whose
-- row and column keys do not have an associated value in the second table.
--
-- > difference (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) (fromList [(1,'a',"c"),(1,'b',"d"),(2,'b',"b")])
-- >   === singleton 2 'a' "b"
difference :: (Ord r, Ord c) => Table r c a -> Table r c a -> Table r c a
difference :: Table r c a -> Table r c a -> Table r c a
difference (Table (Map r (Map c a)
rm1, Map c (Map r a)
cm1, Int
_)) (Table (Map r (Map c a)
rm2, Map c (Map r a)
cm2, Int
_)) = Map r (Map c a) -> Map c (Map r a) -> Table r c a
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c a)
rm Map c (Map r a)
cm
  where
    rm :: Map r (Map c a)
rm = (Map c a -> Map c a -> Maybe (Map c a))
-> Map r (Map c a) -> Map r (Map c a) -> Map r (Map c a)
forall k a b.
Ord k =>
(a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
Map.differenceWith ((Map c a -> Maybe (Map c a)
forall a. a -> Maybe a
Just (Map c a -> Maybe (Map c a))
-> (Map c a -> Map c a) -> Map c a -> Maybe (Map c a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Map c a -> Map c a) -> Map c a -> Maybe (Map c a))
-> (Map c a -> Map c a -> Map c a)
-> Map c a
-> Map c a
-> Maybe (Map c a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map c a -> Map c a -> Map c a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.difference) Map r (Map c a)
rm1 Map r (Map c a)
rm2
    cm :: Map c (Map r a)
cm = (Map r a -> Map r a -> Maybe (Map r a))
-> Map c (Map r a) -> Map c (Map r a) -> Map c (Map r a)
forall k a b.
Ord k =>
(a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
Map.differenceWith ((Map r a -> Maybe (Map r a)
forall a. a -> Maybe a
Just (Map r a -> Maybe (Map r a))
-> (Map r a -> Map r a) -> Map r a -> Maybe (Map r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Map r a -> Map r a) -> Map r a -> Maybe (Map r a))
-> (Map r a -> Map r a -> Map r a)
-> Map r a
-> Map r a
-> Maybe (Map r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map r a -> Map r a -> Map r a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.difference) Map c (Map r a)
cm1 Map c (Map r a)
cm2

------------------------------------------------------------------------------

-- | /O(n)/, assuming the function @a -> b@ takes /O(1)/.
-- Map a function over all values in the table.
--
-- > Data.Multimap.Table.map (++ "x") (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) === fromList [(1,'a',"bx"),(1,'b',"cx"),(2,'a',"bx")]
map :: (a -> b) -> Table r c a -> Table r c b
map :: (a -> b) -> Table r c a -> Table r c b
map = (r -> c -> a -> b) -> Table r c a -> Table r c b
forall r c a b. (r -> c -> a -> b) -> Table r c a -> Table r c b
mapWithKeys ((r -> c -> a -> b) -> Table r c a -> Table r c b)
-> ((a -> b) -> r -> c -> a -> b)
-> (a -> b)
-> Table r c a
-> Table r c b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> b) -> r -> c -> a -> b
forall a b. a -> b -> a
const ((c -> a -> b) -> r -> c -> a -> b)
-> ((a -> b) -> c -> a -> b) -> (a -> b) -> r -> c -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> c -> a -> b
forall a b. a -> b -> a
const

-- | /O(n)/, assuming the function @r -> c -> a -> b@ takes /O(1)/.
-- Map a function over all values in the table.
--
-- > mapWithKeys (\r c x -> show r ++ ":" ++ show c ++ ":" ++ x) (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")])
-- >   === fromList [(1,'a',"1:'a':b"),(1,'b',"1:'b':c"),(2,'a',"2:'a':b")]
mapWithKeys :: (r -> c -> a -> b) -> Table r c a -> Table r c b
mapWithKeys :: (r -> c -> a -> b) -> Table r c a -> Table r c b
mapWithKeys r -> c -> a -> b
f (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
sz)) = (Map r (Map c b), Map c (Map r b), Int) -> Table r c b
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c b)
rm', Map c (Map r b)
cm', Int
sz)
  where
    rm' :: Map r (Map c b)
rm' = (r -> Map c a -> Map c b) -> Map r (Map c a) -> Map r (Map c b)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((c -> a -> b) -> Map c a -> Map c b
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((c -> a -> b) -> Map c a -> Map c b)
-> (r -> c -> a -> b) -> r -> Map c a -> Map c b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> b
f) Map r (Map c a)
rm
    cm' :: Map c (Map r b)
cm' = (c -> Map r a -> Map r b) -> Map c (Map r a) -> Map c (Map r b)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((r -> a -> b) -> Map r a -> Map r b
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((r -> a -> b) -> Map r a -> Map r b)
-> (c -> r -> a -> b) -> c -> Map r a -> Map r b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> c -> a -> b) -> c -> r -> a -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> c -> a -> b
f) Map c (Map r a)
cm

-- | Traverse the (row key, column key, value) triples and collect the results.
--
-- > let f r c a = if odd r && c > 'a' then Just (a ++ "x") else Nothing in do
-- >   traverseWithKeys f (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"b")]) === Nothing
-- >   traverseWithKeys f (fromList [(1,'b',"b"),(1,'c',"c"),(3,'d',"b")]) === Just (fromList [(1,'b',"bx"),(1,'c',"cx"),(3,'d',"bx")])
traverseWithKeys
  :: (Applicative t, Ord r, Ord c)
  => (r -> c -> a -> t b)
  -> Table r c a
  -> t (Table r c b)
traverseWithKeys :: (r -> c -> a -> t b) -> Table r c a -> t (Table r c b)
traverseWithKeys r -> c -> a -> t b
f (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r (Map c b) -> Map c (Map r b) -> Table r c b
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps (Map r (Map c b) -> Map c (Map r b) -> Table r c b)
-> t (Map r (Map c b)) -> t (Map c (Map r b) -> Table r c b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (Map r (Map c b))
rm' t (Map c (Map r b) -> Table r c b)
-> t (Map c (Map r b)) -> t (Table r c b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (Map c (Map r b))
cm'
  where
    rm' :: t (Map r (Map c b))
rm' = (r -> Map c a -> t (Map c b))
-> Map r (Map c a) -> t (Map r (Map c b))
forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
Map.traverseWithKey ((c -> a -> t b) -> Map c a -> t (Map c b)
forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
Map.traverseWithKey ((c -> a -> t b) -> Map c a -> t (Map c b))
-> (r -> c -> a -> t b) -> r -> Map c a -> t (Map c b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> t b
f) Map r (Map c a)
rm
    cm' :: t (Map c (Map r b))
cm' = Map r (Map c b) -> Map c (Map r b)
forall r c a. (Ord r, Ord c) => Map r (Map c a) -> Map c (Map r a)
transpose' (Map r (Map c b) -> Map c (Map r b))
-> t (Map r (Map c b)) -> t (Map c (Map r b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (Map r (Map c b))
rm'

-- | Traverse the (row key, column key, value) triples and collect the 'Just' results.
traverseMaybeWithKeys
  :: (Applicative t, Ord r, Ord c)
  => (r -> c -> a -> t (Maybe b))
  -> Table r c a
  -> t (Table r c b)
traverseMaybeWithKeys :: (r -> c -> a -> t (Maybe b)) -> Table r c a -> t (Table r c b)
traverseMaybeWithKeys r -> c -> a -> t (Maybe b)
f (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r (Map c b) -> Map c (Map r b) -> Table r c b
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps (Map r (Map c b) -> Map c (Map r b) -> Table r c b)
-> t (Map r (Map c b)) -> t (Map c (Map r b) -> Table r c b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (Map r (Map c b))
rm' t (Map c (Map r b) -> Table r c b)
-> t (Map c (Map r b)) -> t (Table r c b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> t (Map c (Map r b))
cm'
  where
    rm' :: t (Map r (Map c b))
rm' = (r -> Map c a -> t (Map c b))
-> Map r (Map c a) -> t (Map r (Map c b))
forall (t :: * -> *) k a b.
Applicative t =>
(k -> a -> t b) -> Map k a -> t (Map k b)
Map.traverseWithKey ((c -> a -> t (Maybe b)) -> Map c a -> t (Map c b)
forall (f :: * -> *) k a b.
Applicative f =>
(k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)
Map.traverseMaybeWithKey ((c -> a -> t (Maybe b)) -> Map c a -> t (Map c b))
-> (r -> c -> a -> t (Maybe b)) -> r -> Map c a -> t (Map c b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> t (Maybe b)
f) Map r (Map c a)
rm
    cm' :: t (Map c (Map r b))
cm' = Map r (Map c b) -> Map c (Map r b)
forall r c a. (Ord r, Ord c) => Map r (Map c a) -> Map c (Map r a)
transpose' (Map r (Map c b) -> Map c (Map r b))
-> t (Map r (Map c b)) -> t (Map c (Map r b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (Map r (Map c b))
rm'

------------------------------------------------------------------------------

-- | /O(n)/. Fold the values in the table row by row using the given
-- right-associative binary operator.
--
-- > Data.Multimap.Table.foldr (:) "" (fromList [(1,'a','b'),(1,'b','c'),(2,'a','d')]) === "bcd"
foldr :: (a -> b -> b) -> b -> Table r c a -> b
foldr :: (a -> b -> b) -> b -> Table r c a -> b
foldr = (r -> c -> a -> b -> b) -> b -> Table r c a -> b
forall r c a b. (r -> c -> a -> b -> b) -> b -> Table r c a -> b
foldrWithKeys ((r -> c -> a -> b -> b) -> b -> Table r c a -> b)
-> ((a -> b -> b) -> r -> c -> a -> b -> b)
-> (a -> b -> b)
-> b
-> Table r c a
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> b -> b) -> r -> c -> a -> b -> b
forall a b. a -> b -> a
const ((c -> a -> b -> b) -> r -> c -> a -> b -> b)
-> ((a -> b -> b) -> c -> a -> b -> b)
-> (a -> b -> b)
-> r
-> c
-> a
-> b
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> c -> a -> b -> b
forall a b. a -> b -> a
const

-- | /O(n)/. Fold the values in the table row by row using the given
-- left-associative binary operator.
--
-- > Data.Multimap.Table.foldl (flip (:)) "" (fromList [(1,'a','b'),(1,'b','c'),(2,'a','d')]) === "dcb"
foldl :: (a -> b -> a) -> a -> Table r c b -> a
foldl :: (a -> b -> a) -> a -> Table r c b -> a
foldl a -> b -> a
f = (a -> r -> c -> b -> a) -> a -> Table r c b -> a
forall a r c b. (a -> r -> c -> b -> a) -> a -> Table r c b -> a
foldlWithKeys (\a
a r
_ c
_ -> a -> b -> a
f a
a)

-- | /O(n)/. Fold the (row key, column key value) triplets in the table
--  row by row using the given right-associative binary operator.
--
-- > let f r c a b = show r ++ ":" ++ show c ++ ":" ++ a ++ "|" ++ b in do
-- >   foldrWithKeys f "" (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === "1:'a':b|1:'b':c|2:'a':d|"
foldrWithKeys :: (r -> c -> a -> b -> b) -> b -> Table r c a -> b
foldrWithKeys :: (r -> c -> a -> b -> b) -> b -> Table r c a -> b
foldrWithKeys r -> c -> a -> b -> b
f b
b (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = (r -> Map c a -> b -> b) -> b -> Map r (Map c a) -> b
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey r -> Map c a -> b -> b
f' b
b Map r (Map c a)
rm
  where
    f' :: r -> Map c a -> b -> b
f' = (b -> Map c a -> b) -> Map c a -> b -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((b -> Map c a -> b) -> Map c a -> b -> b)
-> (r -> b -> Map c a -> b) -> r -> Map c a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> b -> b) -> b -> Map c a -> b
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey ((c -> a -> b -> b) -> b -> Map c a -> b)
-> (r -> c -> a -> b -> b) -> r -> b -> Map c a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> b -> b
f

-- | /O(n)/. Fold the (row key, column key, value) triplets in the table
--  row by row using the given left-associative binary operator.
--
-- > let f a r c b = show r ++ ":" ++ show c ++ ":" ++ b ++ "|" ++ a in do
-- >   foldlWithKeys f "" (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === "2:'a':d|1:'b':c|1:'a':b|"
foldlWithKeys :: (a -> r -> c -> b -> a) -> a -> Table r c b -> a
foldlWithKeys :: (a -> r -> c -> b -> a) -> a -> Table r c b -> a
foldlWithKeys a -> r -> c -> b -> a
f a
a (Table (Map r (Map c b)
rm, Map c (Map r b)
_, Int
_)) = (a -> r -> Map c b -> a) -> a -> Map r (Map c b) -> a
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey a -> r -> Map c b -> a
f' a
a Map r (Map c b)
rm
  where
    f' :: a -> r -> Map c b -> a
f' = (r -> a -> Map c b -> a) -> a -> r -> Map c b -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((a -> c -> b -> a) -> a -> Map c b -> a
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey ((a -> c -> b -> a) -> a -> Map c b -> a)
-> (r -> a -> c -> b -> a) -> r -> a -> Map c b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> r -> c -> b -> a) -> r -> a -> c -> b -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> r -> c -> b -> a
f)

-- | /O(n)/. A strict version of 'foldr'. Each application of the
-- operator is evaluated before using the result in the next application.
-- This function is strict in the starting value.
--
-- > Data.Multimap.Table.foldr' (:) "" (fromList [(1,'a','b'),(1,'b','c'),(2,'a','d')]) === "bcd"
foldr' :: (a -> b -> b) -> b -> Table r c a -> b
foldr' :: (a -> b -> b) -> b -> Table r c a -> b
foldr' = (r -> c -> a -> b -> b) -> b -> Table r c a -> b
forall r c a b. (r -> c -> a -> b -> b) -> b -> Table r c a -> b
foldrWithKeys' ((r -> c -> a -> b -> b) -> b -> Table r c a -> b)
-> ((a -> b -> b) -> r -> c -> a -> b -> b)
-> (a -> b -> b)
-> b
-> Table r c a
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> b -> b) -> r -> c -> a -> b -> b
forall a b. a -> b -> a
const ((c -> a -> b -> b) -> r -> c -> a -> b -> b)
-> ((a -> b -> b) -> c -> a -> b -> b)
-> (a -> b -> b)
-> r
-> c
-> a
-> b
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> c -> a -> b -> b
forall a b. a -> b -> a
const

-- | /O(n)/. A strict version of 'foldl'. Each application of the
-- operator is evaluated before using the result in the next application.
-- This function is strict in the starting value.
--
-- > Data.Multimap.Table.foldl' (flip (:)) "" (fromList [(1,'a','b'),(1,'b','c'),(2,'a','d')]) === "dcb"
foldl' :: (a -> b -> a) -> a -> Table r c b -> a
foldl' :: (a -> b -> a) -> a -> Table r c b -> a
foldl' a -> b -> a
f = (a -> r -> c -> b -> a) -> a -> Table r c b -> a
forall a r c b. (a -> r -> c -> b -> a) -> a -> Table r c b -> a
foldlWithKeys' (\a
a r
_ c
_ -> a -> b -> a
f a
a)

-- | /O(n)/. A strict version of 'foldrWithKey'. Each application of the
-- operator is evaluated before using the result in the next application.
-- This function is strict in the starting value.
--
-- > let f r c a b = show r ++ ":" ++ show c ++ ":" ++ a ++ "|" ++ b in do
-- >   foldrWithKeys' f "" (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === "1:'a':b|1:'b':c|2:'a':d|"
foldrWithKeys' :: (r -> c -> a -> b -> b) -> b -> Table r c a -> b
foldrWithKeys' :: (r -> c -> a -> b -> b) -> b -> Table r c a -> b
foldrWithKeys' r -> c -> a -> b -> b
f b
b (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = (r -> Map c a -> b -> b) -> b -> Map r (Map c a) -> b
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey' r -> Map c a -> b -> b
f' b
b Map r (Map c a)
rm
  where
    f' :: r -> Map c a -> b -> b
f' = (b -> Map c a -> b) -> Map c a -> b -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((b -> Map c a -> b) -> Map c a -> b -> b)
-> (r -> b -> Map c a -> b) -> r -> Map c a -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> b -> b) -> b -> Map c a -> b
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey' ((c -> a -> b -> b) -> b -> Map c a -> b)
-> (r -> c -> a -> b -> b) -> r -> b -> Map c a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> b -> b
f

-- | /O(n)/. A strict version of 'foldlWithKey'. Each application of the
-- operator is evaluated before using the result in the next application.
-- This function is strict in the starting value.
--
-- > let f a r c b = show r ++ ":" ++ show c ++ ":" ++ b ++ "|" ++ a in do
-- >   foldlWithKeys' f "" (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === "2:'a':d|1:'b':c|1:'a':b|"
foldlWithKeys' :: (a -> r -> c -> b -> a) -> a -> Table r c b -> a
foldlWithKeys' :: (a -> r -> c -> b -> a) -> a -> Table r c b -> a
foldlWithKeys' a -> r -> c -> b -> a
f a
a (Table (Map r (Map c b)
rm, Map c (Map r b)
_, Int
_)) = (a -> r -> Map c b -> a) -> a -> Map r (Map c b) -> a
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey' a -> r -> Map c b -> a
f' a
a Map r (Map c b)
rm
  where
    f' :: a -> r -> Map c b -> a
f' = (r -> a -> Map c b -> a) -> a -> r -> Map c b -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((a -> c -> b -> a) -> a -> Map c b -> a
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey' ((a -> c -> b -> a) -> a -> Map c b -> a)
-> (r -> a -> c -> b -> a) -> r -> a -> Map c b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> r -> c -> b -> a) -> r -> a -> c -> b -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> r -> c -> b -> a
f)

-- | /O(n)/. Fold the (row key, column key, value) triplets in the map
-- row by row using the given monoid.
--
-- > let f r c a = show r ++ ":" ++ show c ++ ":" ++ a ++ "|" in do
-- >   foldMapWithKeys f (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === "1:'a':b|1:'b':c|2:'a':d|"
foldMapWithKeys :: Monoid m => (r -> c -> a -> m) -> Table r c a -> m
foldMapWithKeys :: (r -> c -> a -> m) -> Table r c a -> m
foldMapWithKeys r -> c -> a -> m
f (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = (r -> Map c a -> m) -> Map r (Map c a) -> m
forall m k a. Monoid m => (k -> a -> m) -> Map k a -> m
Map.foldMapWithKey r -> Map c a -> m
f' Map r (Map c a)
rm
  where
    f' :: r -> Map c a -> m
f' = (c -> a -> m) -> Map c a -> m
forall m k a. Monoid m => (k -> a -> m) -> Map k a -> m
Map.foldMapWithKey ((c -> a -> m) -> Map c a -> m)
-> (r -> c -> a -> m) -> r -> Map c a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> m
f

------------------------------------------------------------------------------

-- | /O(r)/. Return a mapping from column keys to values for the given
-- row key.
--
-- > row 1 (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === Map.fromList [('a',"b"),('b',"c")]
-- > row 3 (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === Map.empty
row :: Ord r => r -> Table r c a -> Map c a
row :: r -> Table r c a -> Map c a
row r
r (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map c a -> r -> Map r (Map c a) -> Map c a
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Map c a
forall k a. Map k a
Map.empty r
r Map r (Map c a)
rm

-- | /O(c)/. Return a mapping from row keys to values for the given
-- column key.
--
-- > column 'a' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === Map.fromList [(1,"b"),(2,"d")]
-- > column 'c' (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === Map.empty
column :: Ord c => c -> Table r c a -> Map r a
column :: c -> Table r c a -> Map r a
column c
c (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) = Map r a -> c -> Map c (Map r a) -> Map r a
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Map r a
forall k a. Map k a
Map.empty c
c Map c (Map r a)
cm

-- | Return a mapping from row keys to maps from column keys to values.
--
-- > rowMap (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")])
-- >   === Map.fromList [(1, Map.fromList [('a',"b"),('b',"c")]),(2, Map.fromList [('a',"d")])]
rowMap :: Table r c a -> Map r (Map c a)
rowMap :: Table r c a -> Map r (Map c a)
rowMap (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r (Map c a)
rm

-- | Return a mapping from column keys to maps from row keys to values.
--
-- > columnMap (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")])
-- >   === Map.fromList [('a', Map.fromList [(1,"b"),(2,"d")]),('b', Map.fromList [(1,"c")])]
columnMap :: Table r c a -> Map c (Map r a)
columnMap :: Table r c a -> Map c (Map r a)
columnMap (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) = Map c (Map r a)
cm

-- | Return, in ascending order, the list of all row keys of that have
-- at least one value in the table.
--
-- > rowKeys (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === [1,2]
rowKeys :: Table r c a -> [r]
rowKeys :: Table r c a -> [r]
rowKeys (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r (Map c a) -> [r]
forall k a. Map k a -> [k]
Map.keys Map r (Map c a)
rm

-- | Return, in ascending order, the list of all column keys of that have
-- at least one value in the table.
--
-- > columnKeys (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === ['a','b']
columnKeys :: Table r c a -> [c]
columnKeys :: Table r c a -> [c]
columnKeys (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) = Map c (Map r a) -> [c]
forall k a. Map k a -> [k]
Map.keys Map c (Map r a)
cm

-- | Return the set of all row keys of that have at least one value
-- in the table.
--
-- > rowKeysSet (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === Set.fromList [1,2]
rowKeysSet :: Table r c a -> Set r
rowKeysSet :: Table r c a -> Set r
rowKeysSet (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r (Map c a) -> Set r
forall k a. Map k a -> Set k
Map.keysSet Map r (Map c a)
rm

-- | Return the set of all column keys of that have at least one value
-- in the table.
--
-- > columnKeysSet (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === Set.fromList ['a','b']
columnKeysSet :: Table r c a -> Set c
columnKeysSet :: Table r c a -> Set c
columnKeysSet (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) = Map c (Map r a) -> Set c
forall k a. Map k a -> Set k
Map.keysSet Map c (Map r a)
cm

-- | Convert the table into a list of (row key, column key, value) triples.
--
-- > toList (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]
toList :: Table r c a -> [(r, c, a)]
toList :: Table r c a -> [(r, c, a)]
toList (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r [(c, a)] -> [(r, [(c, a)])]
forall k a. Map k a -> [(k, a)]
Map.toList (Map c a -> [(c, a)]
forall k a. Map k a -> [(k, a)]
Map.toList (Map c a -> [(c, a)]) -> Map r (Map c a) -> Map r [(c, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map r (Map c a)
rm) [(r, [(c, a)])] -> ((r, [(c, a)]) -> [(r, c, a)]) -> [(r, c, a)]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (r, [(c, a)]) -> [(r, c, a)]
forall a b c. (a, [(b, c)]) -> [(a, b, c)]
distr

-- | Convert the table into a list of (row key, column key, value) triples
-- in ascending order of row keys, and ascending order of column keys
-- with a row.
--
-- > toRowAscList (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]
toRowAscList :: Table r c a -> [(r, c, a)]
toRowAscList :: Table r c a -> [(r, c, a)]
toRowAscList (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r [(c, a)] -> [(r, [(c, a)])]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Map c a -> [(c, a)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Map c a -> [(c, a)]) -> Map r (Map c a) -> Map r [(c, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map r (Map c a)
rm) [(r, [(c, a)])] -> ((r, [(c, a)]) -> [(r, c, a)]) -> [(r, c, a)]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (r, [(c, a)]) -> [(r, c, a)]
forall a b c. (a, [(b, c)]) -> [(a, b, c)]
distr

-- | Convert the table into a list of (column key, row key, value) triples
-- in ascending order of column keys, and ascending order of row keys
-- with a column.
--
-- > toColumnAscList (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === [('a',1,"b"),('a',2,"d"),('b',1,"c")]
toColumnAscList :: Table r c a -> [(c, r, a)]
toColumnAscList :: Table r c a -> [(c, r, a)]
toColumnAscList (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) = Map c [(r, a)] -> [(c, [(r, a)])]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Map r a -> [(r, a)]
forall k a. Map k a -> [(k, a)]
Map.toAscList (Map r a -> [(r, a)]) -> Map c (Map r a) -> Map c [(r, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map c (Map r a)
cm) [(c, [(r, a)])] -> ((c, [(r, a)]) -> [(c, r, a)]) -> [(c, r, a)]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (c, [(r, a)]) -> [(c, r, a)]
forall a b c. (a, [(b, c)]) -> [(a, b, c)]
distr

-- | Convert the table into a list of (row key, column key, value) triples
-- in descending order of row keys, and descending order of column keys
-- with a row.
--
-- > toRowDescList (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === [(2,'a',"d"),(1,'b',"c"),(1,'a',"b")]
toRowDescList :: Table r c a -> [(r, c, a)]
toRowDescList :: Table r c a -> [(r, c, a)]
toRowDescList (Table (Map r (Map c a)
rm, Map c (Map r a)
_, Int
_)) = Map r [(c, a)] -> [(r, [(c, a)])]
forall k a. Map k a -> [(k, a)]
Map.toDescList (Map c a -> [(c, a)]
forall k a. Map k a -> [(k, a)]
Map.toDescList (Map c a -> [(c, a)]) -> Map r (Map c a) -> Map r [(c, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map r (Map c a)
rm) [(r, [(c, a)])] -> ((r, [(c, a)]) -> [(r, c, a)]) -> [(r, c, a)]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (r, [(c, a)]) -> [(r, c, a)]
forall a b c. (a, [(b, c)]) -> [(a, b, c)]
distr

-- | Convert the table into a list of (column key, row key, value) triples
-- in descending order of column keys, and descending order of row keys
-- with a column.
--
-- > toColumnDescList (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === [('b',1,"c"),('a',2,"d"),('a',1,"b")]
toColumnDescList :: Table r c a -> [(c, r, a)]
toColumnDescList :: Table r c a -> [(c, r, a)]
toColumnDescList (Table (Map r (Map c a)
_, Map c (Map r a)
cm, Int
_)) = Map c [(r, a)] -> [(c, [(r, a)])]
forall k a. Map k a -> [(k, a)]
Map.toDescList (Map r a -> [(r, a)]
forall k a. Map k a -> [(k, a)]
Map.toDescList (Map r a -> [(r, a)]) -> Map c (Map r a) -> Map c [(r, a)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map c (Map r a)
cm) [(c, [(r, a)])] -> ((c, [(r, a)]) -> [(c, r, a)]) -> [(c, r, a)]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (c, [(r, a)]) -> [(c, r, a)]
forall a b c. (a, [(b, c)]) -> [(a, b, c)]
distr

------------------------------------------------------------------------------

-- | /O(n)/, assuming the predicate function takes /O(1)/.
-- Retain all values that satisfy the predicate.
--
-- > Data.Multimap.Table.filter (> "c") (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 2 'a' "d"
-- > Data.Multimap.Table.filter (> "d") (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === empty
filter :: (a -> Bool) -> Table r c a -> Table r c a
filter :: (a -> Bool) -> Table r c a -> Table r c a
filter = (r -> c -> a -> Bool) -> Table r c a -> Table r c a
forall r c a. (r -> c -> a -> Bool) -> Table r c a -> Table r c a
filterWithKeys ((r -> c -> a -> Bool) -> Table r c a -> Table r c a)
-> ((a -> Bool) -> r -> c -> a -> Bool)
-> (a -> Bool)
-> Table r c a
-> Table r c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> Bool) -> r -> c -> a -> Bool
forall a b. a -> b -> a
const ((c -> a -> Bool) -> r -> c -> a -> Bool)
-> ((a -> Bool) -> c -> a -> Bool)
-> (a -> Bool)
-> r
-> c
-> a
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Bool) -> c -> a -> Bool
forall a b. a -> b -> a
const

-- | /O(r)/, assuming the predicate function takes /O(1)/.
-- Retain all rows that satisfy the predicate.
--
-- > filterRow even (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 2 'a' "d"
filterRow :: (r -> Bool) -> Table r c a -> Table r c a
filterRow :: (r -> Bool) -> Table r c a -> Table r c a
filterRow r -> Bool
p (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
rm', Map c (Map r a) -> Map c (Map r a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty Map c (Map r a)
cm', Map r (Map c a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map r (Map c a)
rm')
  where
    rm' :: Map r (Map c a)
rm' = (r -> Map c a -> Bool) -> Map r (Map c a) -> Map r (Map c a)
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (Bool -> Map c a -> Bool
forall a b. a -> b -> a
const (Bool -> Map c a -> Bool) -> (r -> Bool) -> r -> Map c a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Bool
p) Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = (Map r a -> Map r a) -> Map c (Map r a) -> Map c (Map r a)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map ((r -> a -> Bool) -> Map r a -> Map r a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (Bool -> a -> Bool
forall a b. a -> b -> a
const (Bool -> a -> Bool) -> (r -> Bool) -> r -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> Bool
p)) Map c (Map r a)
cm

-- | /O(c)/, assuming the predicate function takes /O(1)/.
-- Retain all columns that satisfy the predicate.
--
-- > filterColumn (> 'a') (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 1 'b' "c"
filterColumn :: (c -> Bool) -> Table r c a -> Table r c a
filterColumn :: (c -> Bool) -> Table r c a -> Table r c a
filterColumn c -> Bool
p (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a) -> Map r (Map c a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty Map r (Map c a)
rm', Map c (Map r a)
cm', Map c (Map r a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map c (Map r a)
cm')
  where
    rm' :: Map r (Map c a)
rm' = (Map c a -> Map c a) -> Map r (Map c a) -> Map r (Map c a)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map ((c -> a -> Bool) -> Map c a -> Map c a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (Bool -> a -> Bool
forall a b. a -> b -> a
const (Bool -> a -> Bool) -> (c -> Bool) -> c -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Bool
p)) Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = (c -> Map r a -> Bool) -> Map c (Map r a) -> Map c (Map r a)
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (Bool -> Map r a -> Bool
forall a b. a -> b -> a
const (Bool -> Map r a -> Bool) -> (c -> Bool) -> c -> Map r a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> Bool
p) Map c (Map r a)
cm

-- | /O(c)/, assuming the predicate function takes /O(1)/.
-- Retain all (row key, column key, value) triples that satisfy the predicate.
--
-- > filterWithKeys (\r c a -> odd r && c > 'a' && a > "b") (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 1 'b' "c"
filterWithKeys
  :: (r -> c -> a -> Bool)
  -> Table r c a
  -> Table r c a
filterWithKeys :: (r -> c -> a -> Bool) -> Table r c a -> Table r c a
filterWithKeys r -> c -> a -> Bool
p (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = Map r (Map c a) -> Map c (Map r a) -> Table r c a
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c a)
rm' Map c (Map r a)
cm'
  where
    rm' :: Map r (Map c a)
rm' = (r -> Map c a -> Map c a) -> Map r (Map c a) -> Map r (Map c a)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((c -> a -> Bool) -> Map c a -> Map c a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey ((c -> a -> Bool) -> Map c a -> Map c a)
-> (r -> c -> a -> Bool) -> r -> Map c a -> Map c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> Bool
p) Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = (c -> Map r a -> Map r a) -> Map c (Map r a) -> Map c (Map r a)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((r -> a -> Bool) -> Map r a -> Map r a
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey ((r -> a -> Bool) -> Map r a -> Map r a)
-> (c -> r -> a -> Bool) -> c -> Map r a -> Map r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> c -> a -> Bool) -> c -> r -> a -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> c -> a -> Bool
p) Map c (Map r a)
cm

-- | /O(n)/, assuming the function @a -> 'Maybe' b@ takes /O(1)/.
-- Map values and collect the 'Just' results.
--
-- > mapMaybe (\a -> if a == "a" then Just "new a" else Nothing) (fromList [(1,'a',"a"),(1,'b',"c"),(2,'b',"a")])
-- >   === fromList [(1,'a',"new a"),(2,'b',"new a")]
mapMaybe :: (a -> Maybe b) -> Table r c a -> Table r c b
mapMaybe :: (a -> Maybe b) -> Table r c a -> Table r c b
mapMaybe = (r -> c -> a -> Maybe b) -> Table r c a -> Table r c b
forall r c a b.
(r -> c -> a -> Maybe b) -> Table r c a -> Table r c b
mapMaybeWithKeys ((r -> c -> a -> Maybe b) -> Table r c a -> Table r c b)
-> ((a -> Maybe b) -> r -> c -> a -> Maybe b)
-> (a -> Maybe b)
-> Table r c a
-> Table r c b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> Maybe b) -> r -> c -> a -> Maybe b
forall a b. a -> b -> a
const ((c -> a -> Maybe b) -> r -> c -> a -> Maybe b)
-> ((a -> Maybe b) -> c -> a -> Maybe b)
-> (a -> Maybe b)
-> r
-> c
-> a
-> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe b) -> c -> a -> Maybe b
forall a b. a -> b -> a
const

-- | /O(n)/, assuming the function @r -> c -> a -> 'Maybe' b@ takes /O(1)/.
-- Map (row key, column key, value) triples and collect the 'Just' results.
--
-- > let f r c a = if r == 1 && a == "c" then Just "new c" else Nothing in do
-- >   mapMaybeWithKeys f (fromList [(1,'a',"b"),(1,'b',"c"),(2,'a',"d")]) === singleton 1 'b' "new c"
mapMaybeWithKeys :: (r -> c -> a -> Maybe b) -> Table r c a -> Table r c b
mapMaybeWithKeys :: (r -> c -> a -> Maybe b) -> Table r c a -> Table r c b
mapMaybeWithKeys r -> c -> a -> Maybe b
f (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = Map r (Map c b) -> Map c (Map r b) -> Table r c b
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c b)
rm' Map c (Map r b)
cm'
  where
    rm' :: Map r (Map c b)
rm' = (r -> Map c a -> Map c b) -> Map r (Map c a) -> Map r (Map c b)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((c -> a -> Maybe b) -> Map c a -> Map c b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybeWithKey ((c -> a -> Maybe b) -> Map c a -> Map c b)
-> (r -> c -> a -> Maybe b) -> r -> Map c a -> Map c b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> Maybe b
f) Map r (Map c a)
rm
    cm' :: Map c (Map r b)
cm' = (c -> Map r a -> Map r b) -> Map c (Map r a) -> Map c (Map r b)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((r -> a -> Maybe b) -> Map r a -> Map r b
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
Map.mapMaybeWithKey ((r -> a -> Maybe b) -> Map r a -> Map r b)
-> (c -> r -> a -> Maybe b) -> c -> Map r a -> Map r b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> c -> a -> Maybe b) -> c -> r -> a -> Maybe b
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> c -> a -> Maybe b
f) Map c (Map r a)
cm

-- | /O(n)/, assuming the function @a -> 'Either' a1 a2@ takes /O(1)/.
-- Map values and separate the 'Left' and 'Right' results.
--
-- > mapEither (\a -> if a == "a" then Left a else Right a) (fromList [(1,'a',"a"),(1,'b',"c"),(2,'b',"a")])
-- >   === (fromList [(1,'a',"a"),(2,'b',"a")],fromList [(1,'b',"c")])
mapEither :: (a -> Either a1 a2) -> Table r c a -> (Table r c a1, Table r c a2)
mapEither :: (a -> Either a1 a2) -> Table r c a -> (Table r c a1, Table r c a2)
mapEither = (r -> c -> a -> Either a1 a2)
-> Table r c a -> (Table r c a1, Table r c a2)
forall r c a a1 a2.
(r -> c -> a -> Either a1 a2)
-> Table r c a -> (Table r c a1, Table r c a2)
mapEitherWithKeys ((r -> c -> a -> Either a1 a2)
 -> Table r c a -> (Table r c a1, Table r c a2))
-> ((a -> Either a1 a2) -> r -> c -> a -> Either a1 a2)
-> (a -> Either a1 a2)
-> Table r c a
-> (Table r c a1, Table r c a2)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (c -> a -> Either a1 a2) -> r -> c -> a -> Either a1 a2
forall a b. a -> b -> a
const ((c -> a -> Either a1 a2) -> r -> c -> a -> Either a1 a2)
-> ((a -> Either a1 a2) -> c -> a -> Either a1 a2)
-> (a -> Either a1 a2)
-> r
-> c
-> a
-> Either a1 a2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either a1 a2) -> c -> a -> Either a1 a2
forall a b. a -> b -> a
const

-- | /O(n)/, assuming the function @r -> c -> a -> 'Either' a1 a2@ takes /O(1)/.
-- Map (row key, column key, value) triples and separate the 'Left' and 'Right' results.
--
-- > mapEitherWithKeys (\r c a -> if r == 1 && c == 'a' then Left a else Right a) (fromList [(1,'a',"a"),(1,'b',"c"),(2,'b',"a")])
-- >   === (fromList [(1,'a',"a")],fromList [(1,'b',"c"),(2,'b',"a")])
mapEitherWithKeys :: (r -> c -> a -> Either a1 a2) -> Table r c a -> (Table r c a1, Table r c a2)
mapEitherWithKeys :: (r -> c -> a -> Either a1 a2)
-> Table r c a -> (Table r c a1, Table r c a2)
mapEitherWithKeys r -> c -> a -> Either a1 a2
f (Table (Map r (Map c a)
rm, Map c (Map r a)
cm, Int
_)) = (Map r (Map c a1) -> Map c (Map r a1) -> Table r c a1
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c a1)
rm1 Map c (Map r a1)
cm1, Map r (Map c a2) -> Map c (Map r a2) -> Table r c a2
forall r c a. Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c a2)
rm2 Map c (Map r a2)
cm2)
  where
    (Map r (Map c a1)
rm1, Map r (Map c a2)
rm2) = (((Map c a1, Map c a2) -> Map c a1)
-> Map r (Map c a1, Map c a2) -> Map r (Map c a1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Map c a1, Map c a2) -> Map c a1
forall a b. (a, b) -> a
fst (Map r (Map c a1, Map c a2) -> Map r (Map c a1))
-> (Map r (Map c a1, Map c a2) -> Map r (Map c a2))
-> Map r (Map c a1, Map c a2)
-> (Map r (Map c a1), Map r (Map c a2))
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((Map c a1, Map c a2) -> Map c a2)
-> Map r (Map c a1, Map c a2) -> Map r (Map c a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Map c a1, Map c a2) -> Map c a2
forall a b. (a, b) -> b
snd) (Map r (Map c a1, Map c a2)
 -> (Map r (Map c a1), Map r (Map c a2)))
-> Map r (Map c a1, Map c a2)
-> (Map r (Map c a1), Map r (Map c a2))
forall a b. (a -> b) -> a -> b
$ (r -> Map c a -> (Map c a1, Map c a2))
-> Map r (Map c a) -> Map r (Map c a1, Map c a2)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((c -> a -> Either a1 a2) -> Map c a -> (Map c a1, Map c a2)
forall k a b c.
(k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
Map.mapEitherWithKey ((c -> a -> Either a1 a2) -> Map c a -> (Map c a1, Map c a2))
-> (r -> c -> a -> Either a1 a2)
-> r
-> Map c a
-> (Map c a1, Map c a2)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> c -> a -> Either a1 a2
f) Map r (Map c a)
rm
    (Map c (Map r a1)
cm1, Map c (Map r a2)
cm2) = (((Map r a1, Map r a2) -> Map r a1)
-> Map c (Map r a1, Map r a2) -> Map c (Map r a1)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Map r a1, Map r a2) -> Map r a1
forall a b. (a, b) -> a
fst (Map c (Map r a1, Map r a2) -> Map c (Map r a1))
-> (Map c (Map r a1, Map r a2) -> Map c (Map r a2))
-> Map c (Map r a1, Map r a2)
-> (Map c (Map r a1), Map c (Map r a2))
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((Map r a1, Map r a2) -> Map r a2)
-> Map c (Map r a1, Map r a2) -> Map c (Map r a2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Map r a1, Map r a2) -> Map r a2
forall a b. (a, b) -> b
snd) (Map c (Map r a1, Map r a2)
 -> (Map c (Map r a1), Map c (Map r a2)))
-> Map c (Map r a1, Map r a2)
-> (Map c (Map r a1), Map c (Map r a2))
forall a b. (a -> b) -> a -> b
$ (c -> Map r a -> (Map r a1, Map r a2))
-> Map c (Map r a) -> Map c (Map r a1, Map r a2)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey ((r -> a -> Either a1 a2) -> Map r a -> (Map r a1, Map r a2)
forall k a b c.
(k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)
Map.mapEitherWithKey ((r -> a -> Either a1 a2) -> Map r a -> (Map r a1, Map r a2))
-> (c -> r -> a -> Either a1 a2)
-> c
-> Map r a
-> (Map r a1, Map r a2)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> c -> a -> Either a1 a2) -> c -> r -> a -> Either a1 a2
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> c -> a -> Either a1 a2
f) Map c (Map r a)
cm

------------------------------------------------------------------------------
-- * Non exported functions
------------------------------------------------------------------------------

assoc :: (a, (b, c)) -> (a, b, c)
assoc :: (a, (b, c)) -> (a, b, c)
assoc (a
a, (b
b, c
c)) = (a
a, b
b, c
c)

distr :: (a, [(b, c)]) -> [(a, b, c)]
distr :: (a, [(b, c)]) -> [(a, b, c)]
distr = ((a, (b, c)) -> (a, b, c)) -> [(a, (b, c))] -> [(a, b, c)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, (b, c)) -> (a, b, c)
forall a b c. (a, (b, c)) -> (a, b, c)
assoc ([(a, (b, c))] -> [(a, b, c)])
-> ((a, [(b, c)]) -> [(a, (b, c))]) -> (a, [(b, c)]) -> [(a, b, c)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [(b, c)] -> [(a, (b, c))]) -> (a, [(b, c)]) -> [(a, (b, c))]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ([a] -> [(b, c)] -> [(a, (b, c))]
forall a b. [a] -> [b] -> [(a, b)]
zip ([a] -> [(b, c)] -> [(a, (b, c))])
-> (a -> [a]) -> a -> [(b, c)] -> [(a, (b, c))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. a -> [a]
repeat)

-- | Build a table from a row map and a column map.
fromMaps :: Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps :: Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps Map r (Map c a)
rm Map c (Map r a)
cm = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
rm', Map c (Map r a)
cm', Map r (Map c a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map r (Map c a)
rm')
  where
    rm' :: Map r (Map c a)
rm' = Map r (Map c a) -> Map r (Map c a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = Map c (Map r a) -> Map c (Map r a)
forall k1 k2 a. Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty Map c (Map r a)
cm

fromMaps' :: (Ord r, Ord c) => r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps' :: r -> c -> Map r (Map c a) -> Map c (Map r a) -> Table r c a
fromMaps' r
r c
c Map r (Map c a)
rm Map c (Map r a)
cm = (Map r (Map c a), Map c (Map r a), Int) -> Table r c a
forall r c a.
(Map r (Map c a), Map c (Map r a), Int) -> Table r c a
Table (Map r (Map c a)
rm', Map c (Map r a)
cm', Map r (Map c a) -> Int
forall k1 k2 a. Map k1 (Map k2 a) -> Int
size' Map r (Map c a)
rm')
  where
    rm' :: Map r (Map c a)
rm' = r -> Map r (Map c a) -> Map r (Map c a)
forall k1 k2 a.
Ord k1 =>
k1 -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty' r
r Map r (Map c a)
rm
    cm' :: Map c (Map r a)
cm' = c -> Map c (Map r a) -> Map c (Map r a)
forall k1 k2 a.
Ord k1 =>
k1 -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty' c
c Map c (Map r a)
cm

nonEmpty :: Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty :: Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty = (Map k2 a -> Bool) -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Map k2 a -> Bool) -> Map k2 a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k2 a -> Bool
forall k a. Map k a -> Bool
Map.null)

nonEmpty' :: Ord k1 => k1 -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty' :: k1 -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
nonEmpty' k1
k1 Map k1 (Map k2 a)
m = case k1 -> Map k1 (Map k2 a) -> Maybe (Map k2 a)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k1
k1 Map k1 (Map k2 a)
m of
  Just Map k2 a
m' | Map k2 a -> Bool
forall k a. Map k a -> Bool
Map.null Map k2 a
m' -> k1 -> Map k1 (Map k2 a) -> Map k1 (Map k2 a)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete k1
k1 Map k1 (Map k2 a)
m
  Maybe (Map k2 a)
_ -> Map k1 (Map k2 a)
m

transpose' :: (Ord r, Ord c) => Map r (Map c a) -> Map c (Map r a)
transpose' :: Map r (Map c a) -> Map c (Map r a)
transpose' = (r -> Map c a -> Map c (Map r a) -> Map c (Map r a))
-> Map c (Map r a) -> Map r (Map c a) -> Map c (Map r a)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey' r -> Map c a -> Map c (Map r a) -> Map c (Map r a)
forall k k a.
(Ord k, Ord k) =>
k -> Map k a -> Map k (Map k a) -> Map k (Map k a)
f Map c (Map r a)
forall k a. Map k a
Map.empty
  where
    f :: k -> Map k a -> Map k (Map k a) -> Map k (Map k a)
f k
r = (Map k a -> Map k a -> Map k a)
-> Map k (Map k a) -> Map k (Map k a) -> Map k (Map k a)
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith Map k a -> Map k a -> Map k a
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union (Map k (Map k a) -> Map k (Map k a) -> Map k (Map k a))
-> (Map k a -> Map k (Map k a))
-> Map k a
-> Map k (Map k a)
-> Map k (Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Map k a) -> Map k a -> Map k (Map k a)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map (k -> a -> Map k a
forall k a. k -> a -> Map k a
Map.singleton k
r)

size' :: Map k1 (Map k2 a) -> Int
size' :: Map k1 (Map k2 a) -> Int
size' = Map k1 Int -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (Map k1 Int -> Int)
-> (Map k1 (Map k2 a) -> Map k1 Int) -> Map k1 (Map k2 a) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map k2 a -> Int) -> Map k1 (Map k2 a) -> Map k1 Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Map k2 a -> Int
forall k a. Map k a -> Int
Map.size

uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 a -> b -> c -> d
f ~(a
a, b
b, c
c) = a -> b -> c -> d
f a
a b
b c
c