11
11
{-# LANGUAGE PatternSynonyms #-}
12
12
{-# LANGUAGE RoleAnnotations #-}
13
13
{-# LANGUAGE TypeFamilies #-}
14
+ {-# LANGUAGE ScopedTypeVariables #-}
14
15
#endif
15
16
16
17
{-# OPTIONS_HADDOCK not-home #-}
@@ -147,23 +148,23 @@ module Data.Set.Internal (
147
148
, lookupGE , lookupGENE
148
149
, isSubsetOf , isSubsetOfNE
149
150
, isProperSubsetOf , isProperSubsetOfNE
150
- , disjoint , disjointNE
151
+ , disjoint , disjointNE , disjointNEX
151
152
152
153
-- * Construction
153
154
, empty
154
155
, singleton , singletonNE
155
156
, insert , insertNE
156
157
, delete , deleteNE
157
158
, alterF
158
- , powerSet
159
+ , powerSet , powerSetNE
159
160
160
161
-- * Combine
161
162
, union , unionNE
162
163
, unions
163
164
, difference , differenceNE
164
165
, intersection , intersectionNE
165
- , cartesianProduct
166
- , disjointUnion
166
+ , cartesianProduct , cartesianProductNE
167
+ , disjointUnion , disjointUnionNE , disjointUnionNEX , disjointUnionXNE
167
168
168
169
-- * Filter
169
170
, filter , filterNE
@@ -173,7 +174,7 @@ module Data.Set.Internal (
173
174
, partition , partitionNE
174
175
, split , splitNE
175
176
, splitMember , splitMemberNE
176
- , splitRoot
177
+ , splitRoot , splitRootNE , splitNERootNE
177
178
178
179
-- * Indexed
179
180
, lookupIndex , lookupIndexNE
@@ -185,13 +186,12 @@ module Data.Set.Internal (
185
186
, splitAt , splitAtNE
186
187
187
188
-- * Map
188
- , map
189
+ , map , mapNE
189
190
, mapMonotonic , mapMonotonicNE
190
191
191
192
-- * Folds
192
193
, foldr , foldr1
193
194
, foldl , foldl1
194
- -- ** Strict folds
195
195
, foldr' , foldr1'
196
196
, foldl' , foldl1'
197
197
-- ** Legacy folds
@@ -385,6 +385,47 @@ instance Foldable.Foldable Set where
385
385
{-# INLINABLE product #-}
386
386
#endif
387
387
388
+ instance Foldable. Foldable NonEmptySet where
389
+ fold = goNE
390
+ where goNE (Bin' 1 k _ _) = k
391
+ goNE (Bin' _ k l r) = go l `mappend` (k `mappend` go r)
392
+ go Tip = mempty
393
+ go (NE s) = goNE s
394
+ {-# INLINABLE fold #-}
395
+ -- foldr f z s = foldr
396
+ -- {-# INLINE foldr #-}
397
+ -- foldl = foldl
398
+ -- {-# INLINE foldl #-}
399
+ foldMap f t = goNE t
400
+ where goNE (Bin' 1 k _ _) = f k
401
+ goNE (Bin' _ k l r) = go l `mappend` (f k `mappend` go r)
402
+ go Tip = mempty
403
+ go (NE s) = goNE s
404
+ {-# INLINE foldMap #-}
405
+ -- foldl' = foldl'
406
+ -- {-# INLINE foldl' #-}
407
+ -- foldr' = foldr'
408
+ -- {-# INLINE foldr' #-}
409
+ #if MIN_VERSION_base(4,8,0)
410
+ length = sizeNE
411
+ {-# INLINE length #-}
412
+ null _ = False
413
+ {-# INLINE null #-}
414
+ #if MIN_VERSION_base(4,9,0)
415
+ toList = NEL. toList . toListNE
416
+ {-# INLINE toList #-}
417
+ #endif
418
+ elem x xs = elem x $ NE xs
419
+ {-# INLINABLE elem #-}
420
+ minimum = lookupMinNE
421
+ {-# INLINE minimum #-}
422
+ maximum = lookupMaxNE
423
+ {-# INLINE maximum #-}
424
+ -- sum = foldl' (+) 0
425
+ -- {-# INLINABLE sum #-}
426
+ -- product = foldl' (*) 1
427
+ -- {-# INLINABLE product #-}
428
+ #endif
388
429
389
430
#if __GLASGOW_HASKELL__
390
431
@@ -798,8 +839,8 @@ alteredSet :: Ord a => a -> Set a -> AlteredSet a
798
839
alteredSet x0 s0 = go x0 s0
799
840
where
800
841
go :: Ord a => a -> Set a -> AlteredSet a
801
- go x Tip = Inserted (singleton x)
802
- go x (Bin _ y l r) = case compare x y of
842
+ go x Tip = Inserted (singleton x)
843
+ go x (NE ( Bin' _ y l r) ) = case compare x y of
803
844
LT -> case go x l of
804
845
Deleted d -> Deleted (balanceR y d r)
805
846
Inserted i -> Inserted (balanceL y i r)
@@ -1170,6 +1211,9 @@ map f = fromList . List.map f . toList
1170
1211
{-# INLINABLE map #-}
1171
1212
#endif
1172
1213
1214
+ mapNE :: Ord b => (a -> b ) -> NonEmptySet a -> NonEmptySet b
1215
+ mapNE f = fromListNE . fmap f . toListNE
1216
+
1173
1217
-- | /O(n)/. The
1174
1218
--
1175
1219
-- @'mapMonotonic' f s == 'map' f s@, but works only when @f@ is strictly increasing.
@@ -2314,6 +2358,14 @@ splitRoot orig =
2314
2358
NE (Bin' _ v l r) -> [l, singleton v, r]
2315
2359
{-# INLINE splitRoot #-}
2316
2360
2361
+ splitRootNE :: NonEmptySet a -> NEL. NonEmpty (Set a )
2362
+ splitRootNE (Bin' _ v l r) = l NEL. :| [singleton v, r]
2363
+
2364
+ splitNERootNE :: NonEmptySet a -> NEL. NonEmpty (NonEmptySet a )
2365
+ splitNERootNE (Bin' _ v Tip Tip ) = pure $ singletonNE v
2366
+ splitNERootNE (Bin' _ v (NE l) Tip ) = l NEL. :| [singletonNE v]
2367
+ splitNERootNE (Bin' _ v Tip (NE r)) = singletonNE v NEL. :| [r]
2368
+ splitNERootNE (Bin' _ v (NE l) (NE r)) = l NEL. :| [singletonNE v, r]
2317
2369
2318
2370
-- | Calculate the power set of a set: the set of all its subsets.
2319
2371
--
@@ -2333,6 +2385,24 @@ powerSet :: Set a -> Set (Set a)
2333
2385
powerSet xs0 = insertMin empty (foldr' step Tip xs0) where
2334
2386
step x pxs = insertMin (singleton x) (insertMin x `mapMonotonic` pxs) `glue` pxs
2335
2387
2388
+ powerSetNE :: NonEmptySet a -> NonEmptySet (Set a )
2389
+ powerSetNE xs = insertMinNE empty . NE . mapMonotonicNE NE $ nePowerSetNE xs
2390
+
2391
+ nePowerSetNE :: forall a . NonEmptySet a -> NonEmptySet (NonEmptySet a )
2392
+ nePowerSetNE xs = foldr1By f (singletonNE. singletonNE) xs
2393
+ where
2394
+ f :: a -> NonEmptySet (NonEmptySet a ) -> NonEmptySet (NonEmptySet a )
2395
+ f v acc = insertMinNE (singletonNE v) (NE $ mapMonotonicNE (insertMinNE v . NE ) acc) `glueNE` acc
2396
+
2397
+ foldr1By :: forall a b . (a -> b -> b ) -> (a -> b ) -> NonEmptySet a -> b
2398
+ foldr1By f g = go
2399
+ where
2400
+ finish :: Set a -> b -> b
2401
+ finish l acc = foldr f acc l
2402
+ go :: NonEmptySet a -> b
2403
+ go (Bin' _ v l (NE r)) = finish l (f v (go r))
2404
+ go (Bin' _ v l Tip ) = finish l (g v)
2405
+
2336
2406
-- | /O(m*n)/ (conjectured). Calculate the Cartesian product of two sets.
2337
2407
--
2338
2408
-- @
@@ -2371,6 +2441,16 @@ cartesianProduct as (NE (Bin' 1 b _ _)) = mapMonotonic (flip (,) b) as
2371
2441
cartesianProduct as bs =
2372
2442
getMergeSet $ foldMap (\ a -> MergeSet $ mapMonotonic ((,) a) bs) as
2373
2443
2444
+ cartesianProductNE :: NonEmptySet a -> NonEmptySet b -> NonEmptySet (a , b )
2445
+ cartesianProductNE as (Bin' 1 b _ _) = mapMonotonicNE (flip (,) b) as
2446
+ cartesianProductNE as bs = goFoldMapNE as
2447
+ where
2448
+ f a = mapMonotonicNE ((,) a) bs
2449
+ goFoldMapNE (Bin' 1 k _ _) = f k
2450
+ goFoldMapNE (Bin' _ k l r) = goFoldMap l `mergeXNE` (f k `mergeNEX` goFoldMap r)
2451
+ goFoldMap Tip = empty
2452
+ goFoldMap (NE s) = NE $ goFoldMapNE s
2453
+
2374
2454
-- A version of Set with peculiar Semigroup and Monoid instances.
2375
2455
-- The result of xs <> ys will only be a valid set if the greatest
2376
2456
-- element of xs is strictly less than the least element of ys.
@@ -2406,6 +2486,14 @@ instance Monoid (MergeSet a) where
2406
2486
disjointUnion :: Set a -> Set b -> Set (Either a b )
2407
2487
disjointUnion as bs = merge (mapMonotonic Left as) (mapMonotonic Right bs)
2408
2488
2489
+ disjointUnionNE :: NonEmptySet a -> NonEmptySet b -> NonEmptySet (Either a b )
2490
+ disjointUnionNE as bs = mergeNE (mapMonotonicNE Left as) (mapMonotonicNE Right bs)
2491
+
2492
+ disjointUnionNEX :: NonEmptySet a -> Set b -> NonEmptySet (Either a b )
2493
+ disjointUnionNEX as bs = mergeNEX (mapMonotonicNE Left as) (mapMonotonic Right bs)
2494
+
2495
+ disjointUnionXNE :: Set a -> NonEmptySet b -> NonEmptySet (Either a b )
2496
+ disjointUnionXNE as bs = mergeXNE (mapMonotonic Left as) (mapMonotonicNE Right bs)
2409
2497
{- -------------------------------------------------------------------
2410
2498
Debugging
2411
2499
--------------------------------------------------------------------}
0 commit comments