@@ -142,17 +142,15 @@ import Control.Monad.Primitive
142142#endif
143143import Control.Monad.ST
144144import qualified Data.Foldable as Foldable
145- import Data.Function (on )
146145import Data.Hashable
147146import Data.HashMap.Lazy (HashMap )
148147import qualified Data.HashMap.Lazy as HashMap
149148import qualified Data.HashTable.Class as H
150149import qualified Data.HashTable.ST.Cuckoo as C
151150import Data.IntMap (IntMap )
152- import qualified Data.IntMap as IntMap
153151import Data.IntSet (IntSet )
154152import qualified Data.IntSet as IntSet
155- import Data.List (foldl' , sortBy )
153+ import Data.List (foldl' )
156154import Data.Map.Lazy (Map )
157155import qualified Data.Map.Lazy as Map
158156import Data.Maybe
@@ -235,7 +233,7 @@ instance ItemOrder a => Exts.IsList (ZDD a) where
235233 fromList = fromListOfSortedList . map f
236234 where
237235 f :: IntSet -> [Int ]
238- f = sortBy (compareItem ( Proxy :: Proxy a )) . IntSet. toList
236+ f = setToList ( Proxy :: Proxy a )
239237
240238 toList = toListOfIntSets
241239
@@ -283,7 +281,7 @@ singleton xs = insert xs empty
283281
284282-- | Set of all subsets, i.e. powerset
285283subsets :: forall a . ItemOrder a => IntSet -> ZDD a
286- subsets = foldl' f Base . sortBy ( flip (compareItem (Proxy :: Proxy a ))) . IntSet. toList
284+ subsets = foldl' f Base . reverse . setToList (Proxy :: Proxy a )
287285 where
288286 f zdd x = Branch x zdd zdd
289287
@@ -293,7 +291,7 @@ combinations xs k
293291 | k < 0 = error " Data.DecisionDiagram.ZDD.combinations: negative size"
294292 | otherwise = unfoldOrd f (0 , k)
295293 where
296- table = V. fromList $ sortBy (compareItem ( Proxy :: Proxy a )) $ IntSet. toList xs
294+ table = V. fromList $ setToList ( Proxy :: Proxy a ) xs
297295 n = V. length table
298296
299297 f :: (Int , Int ) -> Sig (Int , Int )
@@ -307,7 +305,7 @@ subsetsAtLeast :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> ZDD a
307305subsetsAtLeast xs k0 = unfoldOrd f (0 , k0)
308306 where
309307 xs' :: V. Vector (Int , w )
310- xs' = V. fromList $ sortBy (compareItem ( Proxy :: Proxy a ) `on` fst ) $ IntMap. toList xs
308+ xs' = V. fromList $ mapToList ( Proxy :: Proxy a ) xs
311309 ys :: V. Vector (w , w )
312310 ys = V. scanr (\ (_, w) (lb,ub) -> if w >= 0 then (lb, ub+ w) else (lb+ w, ub)) (0 ,0 ) xs'
313311
@@ -326,7 +324,7 @@ subsetsAtMost :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> ZDD a
326324subsetsAtMost xs k0 = unfoldOrd f (0 , k0)
327325 where
328326 xs' :: V. Vector (Int , w )
329- xs' = V. fromList $ sortBy (compareItem ( Proxy :: Proxy a ) `on` fst ) $ IntMap. toList xs
327+ xs' = V. fromList $ mapToList ( Proxy :: Proxy a ) xs
330328 ys :: V. Vector (w , w )
331329 ys = V. scanr (\ (_, w) (lb,ub) -> if w >= 0 then (lb, ub+ w) else (lb+ w, ub)) (0 ,0 ) xs'
332330
@@ -349,7 +347,7 @@ subsetsExactly :: forall a w. (ItemOrder a, Real w) => IntMap w -> w -> ZDD a
349347subsetsExactly xs k0 = unfoldOrd f (0 , k0)
350348 where
351349 xs' :: V. Vector (Int , w )
352- xs' = V. fromList $ sortBy (compareItem ( Proxy :: Proxy a ) `on` fst ) $ IntMap. toList xs
350+ xs' = V. fromList $ mapToList ( Proxy :: Proxy a ) xs
353351 ys :: V. Vector (w , w )
354352 ys = V. scanr (\ (_, w) (lb,ub) -> if w >= 0 then (lb, ub+ w) else (lb+ w, ub)) (0 ,0 ) xs'
355353
@@ -367,7 +365,7 @@ subsetsExactlyIntegral :: forall a w. (ItemOrder a, Real w, Integral w) => IntMa
367365subsetsExactlyIntegral xs k0 = unfoldOrd f (0 , k0)
368366 where
369367 xs' :: V. Vector (Int , w )
370- xs' = V. fromList $ sortBy (compareItem ( Proxy :: Proxy a ) `on` fst ) $ IntMap. toList xs
368+ xs' = V. fromList $ mapToList ( Proxy :: Proxy a ) xs
371369 ys :: V. Vector (w , w )
372370 ys = V. scanr (\ (_, w) (lb,ub) -> if w >= 0 then (lb, ub+ w) else (lb+ w, ub)) (0 ,0 ) xs'
373371 ds :: V. Vector w
@@ -433,7 +431,7 @@ subset0 var zdd = runST $ do
433431-- >>> toSetOfIntSets (insert (IntSet.fromList [1,2,3]) (fromListOfIntSets (map IntSet.fromList [[1,3], [2,4]])) :: ZDD AscOrder)
434432-- fromList [fromList [1,2,3],fromList [1,3],fromList [2,4]]
435433insert :: forall a . ItemOrder a => IntSet -> ZDD a -> ZDD a
436- insert xs = f (sortBy (compareItem ( Proxy :: Proxy a )) ( IntSet. toList xs) )
434+ insert xs = f (setToList ( Proxy :: Proxy a ) xs )
437435 where
438436 f [] (Leaf _) = Base
439437 f [] (Branch top p0 p1) = Branch top (f [] p0) p1
@@ -450,7 +448,7 @@ insert xs = f (sortBy (compareItem (Proxy :: Proxy a)) (IntSet.toList xs))
450448-- >>> toSetOfIntSets (delete (IntSet.fromList [1,3]) (fromListOfIntSets (map IntSet.fromList [[1,2,3], [1,3], [2,4]])) :: ZDD AscOrder)
451449-- fromList [fromList [1,2,3],fromList [2,4]]
452450delete :: forall a . ItemOrder a => IntSet -> ZDD a -> ZDD a
453- delete xs = f (sortBy (compareItem ( Proxy :: Proxy a )) ( IntSet. toList xs) )
451+ delete xs = f (setToList ( Proxy :: Proxy a ) xs )
454452 where
455453 f [] (Leaf _) = Empty
456454 f [] (Branch top p0 p1) = Branch top (f [] p0) p1
@@ -731,7 +729,7 @@ minimalHittingSets = minimalHittingSetsToda
731729member :: forall a . (ItemOrder a ) => IntSet -> ZDD a -> Bool
732730member xs = member' xs'
733731 where
734- xs' = sortBy (compareItem ( Proxy :: Proxy a )) $ IntSet. toList xs
732+ xs' = setToList ( Proxy :: Proxy a ) xs
735733
736734member' :: forall a . (ItemOrder a ) => [Int ] -> ZDD a -> Bool
737735member' [] Base = True
@@ -808,7 +806,7 @@ fromListOfIntSets :: forall a. ItemOrder a => [IntSet] -> ZDD a
808806fromListOfIntSets = fromListOfSortedList . map f
809807 where
810808 f :: IntSet -> [Int ]
811- f = sortBy (compareItem ( Proxy :: Proxy a )) . IntSet. toList
809+ f = setToList ( Proxy :: Proxy a )
812810
813811-- | Convert the family to a list of 'IntSet'.
814812toListOfIntSets :: ZDD a -> [IntSet ]
@@ -962,6 +960,7 @@ findMinSum weight =
962960-- \max_{X\in S} \sum_{x\in X} w(x)
963961-- \]
964962--
963+ -- >>> import qualified Data.IntMap as IntMap
965964-- >>> findMaxSum (IntMap.fromList [(1,2),(2,4),(3,-3)] IntMap.!) (fromListOfIntSets (map IntSet.fromList [[1], [2], [3], [1,2,3]]) :: ZDD AscOrder)
966965-- (4,fromList [2])
967966findMaxSum :: forall a w . (ItemOrder a , Num w , Ord w , HasCallStack ) => (Int -> w ) -> ZDD a -> (w , IntSet )
0 commit comments