Skip to content

Commit

Permalink
add ZDD.fromSetOfIntSets
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Oct 18, 2021
1 parent fc10963 commit 7fb68fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Data/DecisionDiagram/ZDD.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Data.DecisionDiagram.ZDD
-- * Construction
, empty
, base
, fromSetOfIntSets

-- * Query
, null
Expand Down Expand Up @@ -79,6 +80,7 @@ import qualified Data.HashTable.Class as H
import qualified Data.HashTable.ST.Cuckoo as C
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.List (sortBy)
import Data.Proxy
import Data.Set (Set)
import qualified Data.Set as Set
Expand Down Expand Up @@ -415,6 +417,15 @@ isProperSubsetOf a b = a `isSubsetOf` b && a /= b
disjoint :: ItemOrder a => ZDD a -> ZDD a -> Bool
disjoint a b = null (a `intersection` b)

-- | Create a ZDD from a set of 'IntSet'
fromSetOfIntSets :: forall a. ItemOrder a => Set IntSet -> ZDD a
fromSetOfIntSets xss = unions
[ ZDD $ foldr (\x node -> Branch x F node) T
$ sortBy (compareItem (Proxy :: Proxy a))
$ IntSet.toList xs
| xs <- Set.toList xss
]

-- | Convert the family to a set of 'IntSet'.
toSetOfIntSets :: ZDD a -> Set IntSet
toSetOfIntSets (ZDD node) = runST $ do
Expand Down
15 changes: 15 additions & 0 deletions test/TestZDD.hs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ prop_base =
withDefaultOrder $ \(_ :: Proxy o) ->
ZDD.toSetOfIntSets (ZDD.base :: ZDD o) === Set.singleton IntSet.empty

prop_toSetOfIntSets_fromSetOfIntSets :: Property
prop_toSetOfIntSets_fromSetOfIntSets =
withDefaultOrder $ \(_ :: Proxy o) ->
forAll (liftM (Set.fromList . map IntSet.fromList) arbitrary) $ \xss ->
let a :: ZDD o
a = ZDD.fromSetOfIntSets xss
in counterexample (show a) $ ZDD.toSetOfIntSets a === xss

prop_fromSetOfIntSets_toSetOfIntSets :: Property
prop_fromSetOfIntSets_toSetOfIntSets =
withDefaultOrder $ \(_ :: Proxy o) ->
forAll arbitrary $ \(a :: ZDD o) ->
let xss = ZDD.toSetOfIntSets a
in counterexample (show xss) $ ZDD.fromSetOfIntSets xss === a

prop_change :: Property
prop_change =
withDefaultOrder $ \(_ :: Proxy o) ->
Expand Down

0 comments on commit 7fb68fc

Please sign in to comment.