module PlutusCore.Name.UniqueMap (
UniqueMap (..),
insertByUnique,
insertByName,
singletonByName,
insertNamed,
insertByNameIndex,
fromFoldable,
fromUniques,
fromNames,
lookupUnique,
lookupName,
restrictKeys,
lookupNameIndex,
) where
import Control.Lens (view)
import Control.Lens.Getter ((^.))
import Data.Coerce (Coercible, coerce)
import Data.IntMap.Strict qualified as IM
import Data.List as List (foldl')
import PlutusCore.Name.Unique (HasText (..), HasUnique (..), Named (Named), Unique (Unique))
import PlutusCore.Name.UniqueSet (UniqueSet (UniqueSet))
import Prelude hiding (foldr)
newtype UniqueMap unique a = UniqueMap
{ forall unique a. UniqueMap unique a -> IntMap a
unUniqueMap :: IM.IntMap a
}
deriving stock (Int -> UniqueMap unique a -> ShowS
[UniqueMap unique a] -> ShowS
UniqueMap unique a -> String
(Int -> UniqueMap unique a -> ShowS)
-> (UniqueMap unique a -> String)
-> ([UniqueMap unique a] -> ShowS)
-> Show (UniqueMap unique a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall unique a. Show a => Int -> UniqueMap unique a -> ShowS
forall unique a. Show a => [UniqueMap unique a] -> ShowS
forall unique a. Show a => UniqueMap unique a -> String
$cshowsPrec :: forall unique a. Show a => Int -> UniqueMap unique a -> ShowS
showsPrec :: Int -> UniqueMap unique a -> ShowS
$cshow :: forall unique a. Show a => UniqueMap unique a -> String
show :: UniqueMap unique a -> String
$cshowList :: forall unique a. Show a => [UniqueMap unique a] -> ShowS
showList :: [UniqueMap unique a] -> ShowS
Show, UniqueMap unique a -> UniqueMap unique a -> Bool
(UniqueMap unique a -> UniqueMap unique a -> Bool)
-> (UniqueMap unique a -> UniqueMap unique a -> Bool)
-> Eq (UniqueMap unique a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall unique a.
Eq a =>
UniqueMap unique a -> UniqueMap unique a -> Bool
$c== :: forall unique a.
Eq a =>
UniqueMap unique a -> UniqueMap unique a -> Bool
== :: UniqueMap unique a -> UniqueMap unique a -> Bool
$c/= :: forall unique a.
Eq a =>
UniqueMap unique a -> UniqueMap unique a -> Bool
/= :: UniqueMap unique a -> UniqueMap unique a -> Bool
Eq)
deriving newtype (NonEmpty (UniqueMap unique a) -> UniqueMap unique a
UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
(UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a)
-> (NonEmpty (UniqueMap unique a) -> UniqueMap unique a)
-> (forall b.
Integral b =>
b -> UniqueMap unique a -> UniqueMap unique a)
-> Semigroup (UniqueMap unique a)
forall b.
Integral b =>
b -> UniqueMap unique a -> UniqueMap unique a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall unique a.
NonEmpty (UniqueMap unique a) -> UniqueMap unique a
forall unique a.
UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
forall unique a b.
Integral b =>
b -> UniqueMap unique a -> UniqueMap unique a
$c<> :: forall unique a.
UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
<> :: UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
$csconcat :: forall unique a.
NonEmpty (UniqueMap unique a) -> UniqueMap unique a
sconcat :: NonEmpty (UniqueMap unique a) -> UniqueMap unique a
$cstimes :: forall unique a b.
Integral b =>
b -> UniqueMap unique a -> UniqueMap unique a
stimes :: forall b.
Integral b =>
b -> UniqueMap unique a -> UniqueMap unique a
Semigroup, Semigroup (UniqueMap unique a)
UniqueMap unique a
Semigroup (UniqueMap unique a) =>
UniqueMap unique a
-> (UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a)
-> ([UniqueMap unique a] -> UniqueMap unique a)
-> Monoid (UniqueMap unique a)
[UniqueMap unique a] -> UniqueMap unique a
UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall unique a. Semigroup (UniqueMap unique a)
forall unique a. UniqueMap unique a
forall unique a. [UniqueMap unique a] -> UniqueMap unique a
forall unique a.
UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
$cmempty :: forall unique a. UniqueMap unique a
mempty :: UniqueMap unique a
$cmappend :: forall unique a.
UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
mappend :: UniqueMap unique a -> UniqueMap unique a -> UniqueMap unique a
$cmconcat :: forall unique a. [UniqueMap unique a] -> UniqueMap unique a
mconcat :: [UniqueMap unique a] -> UniqueMap unique a
Monoid, (forall a b. (a -> b) -> UniqueMap unique a -> UniqueMap unique b)
-> (forall a b. a -> UniqueMap unique b -> UniqueMap unique a)
-> Functor (UniqueMap unique)
forall a b. a -> UniqueMap unique b -> UniqueMap unique a
forall a b. (a -> b) -> UniqueMap unique a -> UniqueMap unique b
forall unique a b. a -> UniqueMap unique b -> UniqueMap unique a
forall unique a b.
(a -> b) -> UniqueMap unique a -> UniqueMap unique b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall unique a b.
(a -> b) -> UniqueMap unique a -> UniqueMap unique b
fmap :: forall a b. (a -> b) -> UniqueMap unique a -> UniqueMap unique b
$c<$ :: forall unique a b. a -> UniqueMap unique b -> UniqueMap unique a
<$ :: forall a b. a -> UniqueMap unique b -> UniqueMap unique a
Functor, (forall m. Monoid m => UniqueMap unique m -> m)
-> (forall m a. Monoid m => (a -> m) -> UniqueMap unique a -> m)
-> (forall m a. Monoid m => (a -> m) -> UniqueMap unique a -> m)
-> (forall a b. (a -> b -> b) -> b -> UniqueMap unique a -> b)
-> (forall a b. (a -> b -> b) -> b -> UniqueMap unique a -> b)
-> (forall b a. (b -> a -> b) -> b -> UniqueMap unique a -> b)
-> (forall b a. (b -> a -> b) -> b -> UniqueMap unique a -> b)
-> (forall a. (a -> a -> a) -> UniqueMap unique a -> a)
-> (forall a. (a -> a -> a) -> UniqueMap unique a -> a)
-> (forall a. UniqueMap unique a -> [a])
-> (forall a. UniqueMap unique a -> Bool)
-> (forall a. UniqueMap unique a -> Int)
-> (forall a. Eq a => a -> UniqueMap unique a -> Bool)
-> (forall a. Ord a => UniqueMap unique a -> a)
-> (forall a. Ord a => UniqueMap unique a -> a)
-> (forall a. Num a => UniqueMap unique a -> a)
-> (forall a. Num a => UniqueMap unique a -> a)
-> Foldable (UniqueMap unique)
forall a. Eq a => a -> UniqueMap unique a -> Bool
forall a. Num a => UniqueMap unique a -> a
forall a. Ord a => UniqueMap unique a -> a
forall m. Monoid m => UniqueMap unique m -> m
forall a. UniqueMap unique a -> Bool
forall a. UniqueMap unique a -> Int
forall a. UniqueMap unique a -> [a]
forall a. (a -> a -> a) -> UniqueMap unique a -> a
forall unique a. Eq a => a -> UniqueMap unique a -> Bool
forall unique a. Num a => UniqueMap unique a -> a
forall unique a. Ord a => UniqueMap unique a -> a
forall m a. Monoid m => (a -> m) -> UniqueMap unique a -> m
forall unique m. Monoid m => UniqueMap unique m -> m
forall unique a. UniqueMap unique a -> Bool
forall unique a. UniqueMap unique a -> Int
forall unique a. UniqueMap unique a -> [a]
forall b a. (b -> a -> b) -> b -> UniqueMap unique a -> b
forall a b. (a -> b -> b) -> b -> UniqueMap unique a -> b
forall unique a. (a -> a -> a) -> UniqueMap unique a -> a
forall unique m a. Monoid m => (a -> m) -> UniqueMap unique a -> m
forall unique b a. (b -> a -> b) -> b -> UniqueMap unique a -> b
forall unique a b. (a -> b -> b) -> b -> UniqueMap unique a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall unique m. Monoid m => UniqueMap unique m -> m
fold :: forall m. Monoid m => UniqueMap unique m -> m
$cfoldMap :: forall unique m a. Monoid m => (a -> m) -> UniqueMap unique a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> UniqueMap unique a -> m
$cfoldMap' :: forall unique m a. Monoid m => (a -> m) -> UniqueMap unique a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> UniqueMap unique a -> m
$cfoldr :: forall unique a b. (a -> b -> b) -> b -> UniqueMap unique a -> b
foldr :: forall a b. (a -> b -> b) -> b -> UniqueMap unique a -> b
$cfoldr' :: forall unique a b. (a -> b -> b) -> b -> UniqueMap unique a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> UniqueMap unique a -> b
$cfoldl :: forall unique b a. (b -> a -> b) -> b -> UniqueMap unique a -> b
foldl :: forall b a. (b -> a -> b) -> b -> UniqueMap unique a -> b
$cfoldl' :: forall unique b a. (b -> a -> b) -> b -> UniqueMap unique a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> UniqueMap unique a -> b
$cfoldr1 :: forall unique a. (a -> a -> a) -> UniqueMap unique a -> a
foldr1 :: forall a. (a -> a -> a) -> UniqueMap unique a -> a
$cfoldl1 :: forall unique a. (a -> a -> a) -> UniqueMap unique a -> a
foldl1 :: forall a. (a -> a -> a) -> UniqueMap unique a -> a
$ctoList :: forall unique a. UniqueMap unique a -> [a]
toList :: forall a. UniqueMap unique a -> [a]
$cnull :: forall unique a. UniqueMap unique a -> Bool
null :: forall a. UniqueMap unique a -> Bool
$clength :: forall unique a. UniqueMap unique a -> Int
length :: forall a. UniqueMap unique a -> Int
$celem :: forall unique a. Eq a => a -> UniqueMap unique a -> Bool
elem :: forall a. Eq a => a -> UniqueMap unique a -> Bool
$cmaximum :: forall unique a. Ord a => UniqueMap unique a -> a
maximum :: forall a. Ord a => UniqueMap unique a -> a
$cminimum :: forall unique a. Ord a => UniqueMap unique a -> a
minimum :: forall a. Ord a => UniqueMap unique a -> a
$csum :: forall unique a. Num a => UniqueMap unique a -> a
sum :: forall a. Num a => UniqueMap unique a -> a
$cproduct :: forall unique a. Num a => UniqueMap unique a -> a
product :: forall a. Num a => UniqueMap unique a -> a
Foldable)
insertByUnique ::
(Coercible unique Unique) =>
unique ->
a ->
UniqueMap unique a ->
UniqueMap unique a
insertByUnique :: forall unique a.
Coercible unique Unique =>
unique -> a -> UniqueMap unique a -> UniqueMap unique a
insertByUnique unique
uniq = (IntMap a -> IntMap a) -> UniqueMap unique a -> UniqueMap unique a
forall a b. Coercible a b => a -> b
coerce ((IntMap a -> IntMap a)
-> UniqueMap unique a -> UniqueMap unique a)
-> (a -> IntMap a -> IntMap a)
-> a
-> UniqueMap unique a
-> UniqueMap unique a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
IM.insert (unique -> Int
forall a b. Coercible a b => a -> b
coerce unique
uniq)
insertByName :: (HasUnique name unique) => name -> a -> UniqueMap unique a -> UniqueMap unique a
insertByName :: forall name unique a.
HasUnique name unique =>
name -> a -> UniqueMap unique a -> UniqueMap unique a
insertByName = unique -> a -> UniqueMap unique a -> UniqueMap unique a
forall unique a.
Coercible unique Unique =>
unique -> a -> UniqueMap unique a -> UniqueMap unique a
insertByUnique (unique -> a -> UniqueMap unique a -> UniqueMap unique a)
-> (name -> unique)
-> name
-> a
-> UniqueMap unique a
-> UniqueMap unique a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting unique name unique -> name -> unique
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting unique name unique
forall a unique. HasUnique a unique => Lens' a unique
Lens' name unique
unique
singletonByName :: (HasUnique name unique) => name -> a -> UniqueMap unique a
singletonByName :: forall name unique a.
HasUnique name unique =>
name -> a -> UniqueMap unique a
singletonByName name
n a
a = name -> a -> UniqueMap unique a -> UniqueMap unique a
forall name unique a.
HasUnique name unique =>
name -> a -> UniqueMap unique a -> UniqueMap unique a
insertByName name
n a
a UniqueMap unique a
forall a. Monoid a => a
mempty
insertNamed ::
(HasText name, HasUnique name unique) =>
name ->
a ->
UniqueMap unique (Named a) ->
UniqueMap unique (Named a)
insertNamed :: forall name unique a.
(HasText name, HasUnique name unique) =>
name
-> a -> UniqueMap unique (Named a) -> UniqueMap unique (Named a)
insertNamed name
name = name
-> Named a
-> UniqueMap unique (Named a)
-> UniqueMap unique (Named a)
forall name unique a.
HasUnique name unique =>
name -> a -> UniqueMap unique a -> UniqueMap unique a
insertByName name
name (Named a
-> UniqueMap unique (Named a) -> UniqueMap unique (Named a))
-> (a -> Named a)
-> a
-> UniqueMap unique (Named a)
-> UniqueMap unique (Named a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> a -> Named a
forall a. Text -> a -> Named a
Named (name
name name -> Getting Text name Text -> Text
forall s a. s -> Getting a s a -> a
^. Getting Text name Text
forall a. HasText a => Lens' a Text
Lens' name Text
theText)
insertByNameIndex ::
(HasUnique name unique1, Coercible unique2 Unique) =>
name ->
a ->
UniqueMap unique2 a ->
UniqueMap unique2 a
insertByNameIndex :: forall name unique1 unique2 a.
(HasUnique name unique1, Coercible unique2 Unique) =>
name -> a -> UniqueMap unique2 a -> UniqueMap unique2 a
insertByNameIndex = unique2 -> a -> UniqueMap unique2 a -> UniqueMap unique2 a
forall unique a.
Coercible unique Unique =>
unique -> a -> UniqueMap unique a -> UniqueMap unique a
insertByUnique (unique2 -> a -> UniqueMap unique2 a -> UniqueMap unique2 a)
-> (name -> unique2)
-> name
-> a
-> UniqueMap unique2 a
-> UniqueMap unique2 a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. unique1 -> unique2
forall a b. Coercible a b => a -> b
coerce (unique1 -> unique2) -> (name -> unique1) -> name -> unique2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting unique1 name unique1 -> name -> unique1
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting unique1 name unique1
forall a unique. HasUnique a unique => Lens' a unique
Lens' name unique1
unique
fromFoldable ::
(Foldable f) =>
(i -> a -> UniqueMap unique a -> UniqueMap unique a) ->
f (i, a) ->
UniqueMap unique a
fromFoldable :: forall (f :: * -> *) i a unique.
Foldable f =>
(i -> a -> UniqueMap unique a -> UniqueMap unique a)
-> f (i, a) -> UniqueMap unique a
fromFoldable i -> a -> UniqueMap unique a -> UniqueMap unique a
ins = (UniqueMap unique a -> (i, a) -> UniqueMap unique a)
-> UniqueMap unique a -> f (i, a) -> UniqueMap unique a
forall b a. (b -> a -> b) -> b -> f a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (((i, a) -> UniqueMap unique a -> UniqueMap unique a)
-> UniqueMap unique a -> (i, a) -> UniqueMap unique a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (((i, a) -> UniqueMap unique a -> UniqueMap unique a)
-> UniqueMap unique a -> (i, a) -> UniqueMap unique a)
-> ((i, a) -> UniqueMap unique a -> UniqueMap unique a)
-> UniqueMap unique a
-> (i, a)
-> UniqueMap unique a
forall a b. (a -> b) -> a -> b
$ (i -> a -> UniqueMap unique a -> UniqueMap unique a)
-> (i, a) -> UniqueMap unique a -> UniqueMap unique a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry i -> a -> UniqueMap unique a -> UniqueMap unique a
ins) UniqueMap unique a
forall a. Monoid a => a
mempty
fromUniques :: (Foldable f) => (Coercible Unique unique) => f (unique, a) -> UniqueMap unique a
fromUniques :: forall (f :: * -> *) unique a.
(Foldable f, Coercible Unique unique) =>
f (unique, a) -> UniqueMap unique a
fromUniques = (unique -> a -> UniqueMap unique a -> UniqueMap unique a)
-> f (unique, a) -> UniqueMap unique a
forall (f :: * -> *) i a unique.
Foldable f =>
(i -> a -> UniqueMap unique a -> UniqueMap unique a)
-> f (i, a) -> UniqueMap unique a
fromFoldable unique -> a -> UniqueMap unique a -> UniqueMap unique a
forall unique a.
Coercible unique Unique =>
unique -> a -> UniqueMap unique a -> UniqueMap unique a
insertByUnique
fromNames :: (Foldable f) => (HasUnique name unique) => f (name, a) -> UniqueMap unique a
fromNames :: forall (f :: * -> *) name unique a.
(Foldable f, HasUnique name unique) =>
f (name, a) -> UniqueMap unique a
fromNames = (name -> a -> UniqueMap unique a -> UniqueMap unique a)
-> f (name, a) -> UniqueMap unique a
forall (f :: * -> *) i a unique.
Foldable f =>
(i -> a -> UniqueMap unique a -> UniqueMap unique a)
-> f (i, a) -> UniqueMap unique a
fromFoldable name -> a -> UniqueMap unique a -> UniqueMap unique a
forall name unique a.
HasUnique name unique =>
name -> a -> UniqueMap unique a -> UniqueMap unique a
insertByName
lookupUnique :: (Coercible unique Unique) => unique -> UniqueMap unique a -> Maybe a
lookupUnique :: forall unique a.
Coercible unique Unique =>
unique -> UniqueMap unique a -> Maybe a
lookupUnique unique
uniq = Int -> IntMap a -> Maybe a
forall a. Int -> IntMap a -> Maybe a
IM.lookup (unique -> Int
forall a b. Coercible a b => a -> b
coerce unique
uniq) (IntMap a -> Maybe a)
-> (UniqueMap unique a -> IntMap a)
-> UniqueMap unique a
-> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UniqueMap unique a -> IntMap a
forall unique a. UniqueMap unique a -> IntMap a
unUniqueMap
lookupName :: (HasUnique name unique) => name -> UniqueMap unique a -> Maybe a
lookupName :: forall name unique a.
HasUnique name unique =>
name -> UniqueMap unique a -> Maybe a
lookupName = unique -> UniqueMap unique a -> Maybe a
forall unique a.
Coercible unique Unique =>
unique -> UniqueMap unique a -> Maybe a
lookupUnique (unique -> UniqueMap unique a -> Maybe a)
-> (name -> unique) -> name -> UniqueMap unique a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting unique name unique -> name -> unique
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting unique name unique
forall a unique. HasUnique a unique => Lens' a unique
Lens' name unique
unique
restrictKeys :: UniqueMap unique v -> UniqueSet unique -> UniqueMap unique v
restrictKeys :: forall unique v.
UniqueMap unique v -> UniqueSet unique -> UniqueMap unique v
restrictKeys (UniqueMap IntMap v
m) (UniqueSet IntSet
s) =
IntMap v -> UniqueMap unique v
forall unique a. IntMap a -> UniqueMap unique a
UniqueMap (IntMap v -> UniqueMap unique v) -> IntMap v -> UniqueMap unique v
forall a b. (a -> b) -> a -> b
$ IntMap v -> IntSet -> IntMap v
forall a. IntMap a -> IntSet -> IntMap a
IM.restrictKeys IntMap v
m IntSet
s
lookupNameIndex ::
(HasUnique name unique1, Coercible unique2 Unique) =>
name ->
UniqueMap unique2 a ->
Maybe a
lookupNameIndex :: forall name unique1 unique2 a.
(HasUnique name unique1, Coercible unique2 Unique) =>
name -> UniqueMap unique2 a -> Maybe a
lookupNameIndex = unique2 -> UniqueMap unique2 a -> Maybe a
forall unique a.
Coercible unique Unique =>
unique -> UniqueMap unique a -> Maybe a
lookupUnique (unique2 -> UniqueMap unique2 a -> Maybe a)
-> (name -> unique2) -> name -> UniqueMap unique2 a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. unique1 -> unique2
forall a b. Coercible a b => a -> b
coerce (unique1 -> unique2) -> (name -> unique1) -> name -> unique2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting unique1 name unique1 -> name -> unique1
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting unique1 name unique1
forall a unique. HasUnique a unique => Lens' a unique
Lens' name unique1
unique