From a4bd1159c2e14696d3fc8f937d82b460d8e8b5ac Mon Sep 17 00:00:00 2001 From: ros Date: Thu, 13 May 2021 16:18:59 +0200 Subject: [PATCH] refactor(utils.set): new utils.set folder with set-functions added --- src/Utils.hs | 27 +---------------------- src/Utils/Set.hs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 src/Utils/Set.hs diff --git a/src/Utils.hs b/src/Utils.hs index ed364adc1..cdd3ee440 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -40,6 +40,7 @@ import Utils.HttpConditional as Utils import Utils.Persist as Utils import Utils.ARC as Utils import Utils.LRU as Utils +import Utils.Set as Utils import Text.Blaze (Markup, ToMarkup(..)) @@ -562,32 +563,6 @@ withoutSubsequenceBy cmp = go [] | otherwise = go (y:acc) a b ----------- --- Sets -- ----------- - --- | Intersection of multiple sets. Returns empty set for empty input list -setIntersections :: Ord a => [Set a] -> Set a -setIntersections [] = Set.empty -setIntersections (h:t) = foldl' Set.intersection h t - -setMapMaybe :: Ord b => (a -> Maybe b) -> Set a -> Set b -setMapMaybe f = Set.fromList . mapMaybe f . Set.toList - --- | Symmetric difference of two sets. -setSymmDiff :: Ord a => Set a -> Set a -> Set a -setSymmDiff x y = (x `Set.difference` y) `Set.union` (y `Set.difference` x) - -setProduct :: Set a -> Set b -> Set (a, b) --- ^ Depends on the valid internal structure of the given sets -setProduct (Set.toAscList -> as) (Set.toAscList -> bs) = Set.fromDistinctAscList $ (,) <$> as <*> bs - -setPartitionEithers :: (Ord a, Ord b) => Set (Either a b) -> (Set a, Set b) -setPartitionEithers = (,) <$> setMapMaybe (preview _Left) <*> setMapMaybe (preview _Right) - -setFromFunc :: (Finite k, Ord k) => (k -> Bool) -> Set k -setFromFunc = Set.fromList . flip filter universeF - ---------- -- Maps -- ---------- diff --git a/src/Utils/Set.hs b/src/Utils/Set.hs new file mode 100644 index 000000000..1926c51b9 --- /dev/null +++ b/src/Utils/Set.hs @@ -0,0 +1,56 @@ +module Utils.Set +( setIntersectAll +, setIntersectNotOne +, setIntersections +, setMapMaybe +, setSymmDiff +, setProduct +, setPartitionEithers +, setFromFunc +) where + +import qualified Data.Set as Set +import qualified Data.Map.Strict() +import ClassyPrelude +import Data.Universe +import Control.Lens.Prism +import Control.Lens + +-- Mächtigkeit Schnittmenge +setIntersectAll :: Ord a => [Set.Set a] -> Int +setIntersectAll [] = 0 +setIntersectAll [x] = Set.size x +setIntersectAll (x:y:z) = setIntersectAll (xy:z) where xy = Set.intersection x y + +-- Mächtigkeit Schnittmenge Set, Liste von Sets +setIntersectNotOne :: Ord a => Set.Set a -> [Set.Set a] -> Int +setIntersectNotOne _ [] = 0 +setIntersectNotOne k r = Set.size $ Set.intersection k others where others = setUnionOthers r + +-- Vereinigung von Sets +setUnionOthers :: Ord a => [Set.Set a] -> Set.Set a +setUnionOthers [] = Set.empty +setUnionOthers [x] = x +setUnionOthers (x:y:z) = setUnionOthers (xy:z) where xy = Set.union x y + +-- | Intersection of multiple sets. Returns empty set for empty input list +setIntersections :: Ord a => [Set a] -> Set a +setIntersections [] = Set.empty +setIntersections (h:t) = foldl' Set.intersection h t + +setMapMaybe :: Ord b => (a -> Maybe b) -> Set a -> Set b +setMapMaybe f = Set.fromList . mapMaybe f . Set.toList + +-- | Symmetric difference of two sets. +setSymmDiff :: Ord a => Set a -> Set a -> Set a +setSymmDiff x y = (x `Set.difference` y) `Set.union` (y `Set.difference` x) + +setProduct :: Set a -> Set b -> Set (a, b) +-- ^ Depends on the valid internal structure of the given sets +setProduct (Set.toAscList -> as) (Set.toAscList -> bs) = Set.fromDistinctAscList $ (,) <$> as <*> bs + +setPartitionEithers :: (Ord a, Ord b) => Set (Either a b) -> (Set a, Set b) +setPartitionEithers = (,) <$> setMapMaybe (preview _Left) <*> setMapMaybe (preview _Right) + +setFromFunc :: (Finite k, Ord k) => (k -> Bool) -> Set k +setFromFunc = Set.fromList . flip filter universeF \ No newline at end of file