Commit 0f6ad3c5 authored by Peter Achten's avatar Peter Achten

missing Data.MapCollection added

missing Data.MapCollection added
missing import of 'Data.Generics.GenEq' added in Graphics.Scalable.Types.icl
parent 17ea06b0
Pipeline #9175 failed with stage
in 1 minute and 39 seconds
definition module Data.MapCollection
from Data.Map import :: Map
from Data.Set import :: Set
import StdOverloaded
/** singletonMapSet key value:
generates a singleton Map of @key to the singleton Set containing @value.
*/
singletonMapSet :: !k !v -> Map k (Set v)
/** addToMapSet key value m = m`:
if @key is already member of @m, then @value is added to the values associated with @key in @m;
if @key is not a member of @m, then @key and @value are added as new element to @m.
*/
addToMapSet :: !k !v !(Map k (Set v)) -> Map k (Set v) | < k & <, == v
/** mergeMapToSets m1 m2 = m:
for every key k for which ('Data.Map'.member k @m1 && 'Data.Map'.member k @m2), @m has the 'Data.Set'.union of their sets
('Data.Map'.find k @m1 and 'Data.Map'.find k @m2);
for every key k in @m1 but not in @m2, @m has 'Data.Map'.find k @m1;
for every key k in @m2 but not in @m1, @m has 'Data.Map'.find k @m2.
*/
mergeMapToSets :: !(Map k (Set v)) !(Map k (Set v)) -> Map k (Set v) | < k & <, == v
/** mergeMapToSetss ms:
merges all maps in @ms via mergeMapToSets.
*/
mergeMapToSetss :: ![Map k (Set v)] -> Map k (Set v) | < k & <, == v
/** invertToMapSet m = m`:
all (k_1,v) ... (k_n,v) pairs in @m are transformed to (v,{k_1,...,k_n}) in @m`.
*/
invertToMapSet :: !(Map k v) -> Map v (Set k) | < v & <, == k
/** singletonMapList key value:
generates a singleton Map of @key to the singleton list containing @value.
*/
singletonMapList :: !k v -> Map k [v]
/** mergeMapToListss m1 m2 = m:
for every key k for which ('Data.Map'.member k @m1 && 'Data.Map'.member k @m2), @m has the (++) of their lists
('Data.Map'.find k @m1 and 'Data.Map'.find k @m2);
for every key k in @m1 but not in @m2, @m has 'Data.Map'.find k @m1;
for every key k in @m2 but not in @m1, @m has 'Data.Map'.find k @m2.
*/
mergeMapToLists :: !(Map k [v]) !(Map k [v]) -> Map k [v] | < k
/** mergeMapToListss ms:
merges all maps in @ms via mergeMapToLists.
*/
mergeMapToListss :: ![Map k [v]] -> Map k [v] | < k
/** invertToMapList m = m`:
all (k_1,v) ... (k_n,v) in @m are transformed to (v,[k_1,...,k_n]) in @m` (actual order not defined).
*/
invertToMapList :: !(Map k v) -> Map v [k] | < v & <, == k
implementation module Data.MapCollection
import Data.List
from Data.Maybe import :: Maybe (..), fromJust, maybeToList
from Data.Map import :: Map
from Data.Set import :: Set
import qualified Data.Map as DM
import qualified Data.Set as DS
singletonMapSet :: !k !v -> Map k (Set v)
singletonMapSet k v = 'DM'.singleton k ('DS'.singleton v)
addToMapSet :: !k !v !(Map k (Set v)) -> Map k (Set v) | < k & <, == v
addToMapSet k v m = 'DM'.alter (merge_kv v) k m
where
merge_kv :: !v !(Maybe (Set v)) -> Maybe (Set v) | < v
merge_kv v (Just vs) = Just ('DS'.insert v vs)
merge_kv v nothing = Just ('DS'.singleton v)
mergeMapToSets :: !(Map k (Set v)) !(Map k (Set v)) -> Map k (Set v) | < k & <, == v
mergeMapToSets a b
| 'DM'.mapSize a <= 'DM'.mapSize b = foldl merge b ('DM'.toList a)
| otherwise = foldl merge a ('DM'.toList b)
where
merge :: !(Map k (Set v)) !(!k,!Set v) -> Map k (Set v) | < k & <, == v
merge a (k,addSet) = 'DM'.put k newSet a
where
newSet = case 'DM'.get k a of
Just oldSet = 'DS'.union addSet oldSet
nothing = addSet
mergeMapToSetss :: ![Map k (Set v)] -> Map k (Set v) | < k & <, == v
mergeMapToSetss [ts : tss] = foldl mergeMapToSets ts tss
mergeMapToSetss _ = 'DM'.newMap
invertToMapSet :: !(Map k v) -> Map v (Set k) | < v & <, == k
invertToMapSet m = mergeMapToSetss [singletonMapSet v k \\ (k,v) <- 'DM'.toList m]
singletonMapList :: !k v -> Map k [v]
singletonMapList k v = 'DM'.singleton k [v]
mergeMapToLists :: !(Map k [v]) !(Map k [v]) -> Map k [v] | < k
mergeMapToLists a b
| 'DM'.mapSize a <= 'DM'.mapSize b = foldl merge b ('DM'.toList a)
| otherwise = foldl merge a ('DM'.toList b)
where
merge :: !(Map k [v]) !(k,[v]) -> Map k [v] | < k
merge a (k,add) = 'DM'.put k new a
where
new = case 'DM'.get k a of
Just old = add ++ old
nothing = add
mergeMapToListss :: ![Map k [v]] -> Map k [v] | < k
mergeMapToListss [hs : hss] = foldl mergeMapToLists hs hss
mergeMapToListss _ = 'DM'.newMap
invertToMapList :: !(Map k v) -> Map v [k] | < v & <, == k
invertToMapList m = mergeMapToListss [singletonMapList v k \\ (k,v) <- 'DM'.toList m]
......@@ -2,6 +2,7 @@ implementation module Graphics.Scalable.Types
import Data.Maybe
import Data.List
import Data.Generics.GenEq
from Text.HTML import :: SVGColor (..)
from Data.Set import :: Set, instance == (Set a), instance < (Set a)
import StdBool, StdInt, StdReal, StdString
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment