{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
module Data.Set.NonEmpty (
NESet
, pattern IsNonEmpty
, pattern IsEmpty
, nonEmptySet
, toSet
, withNonEmpty
, insertSet
, insertSetMin
, insertSetMax
, unsafeFromSet
, singleton
, fromList
, fromAscList
, fromDescList
, fromDistinctAscList
, fromDistinctDescList
, powerSet
, insert
, delete
, member
, notMember
, lookupLT
, lookupGT
, lookupLE
, lookupGE
, size
, isSubsetOf
, isProperSubsetOf
, disjoint
, union
, unions
, difference
, (\\)
, intersection
, cartesianProduct
, disjointUnion
, filter
, takeWhileAntitone
, dropWhileAntitone
, spanAntitone
, partition
, split
, splitMember
, splitRoot
, lookupIndex
, findIndex
, elemAt
, deleteAt
, take
, drop
, splitAt
, map
, mapMonotonic
, foldr
, foldl
, F.foldr1
, F.foldl1
, foldr'
, foldl'
, foldr1'
, foldl1'
, findMin
, findMax
, deleteMin
, deleteMax
, deleteFindMin
, deleteFindMax
, elems
, toList
, toAscList
, toDescList
, valid
) where
import Control.Applicative
import Data.Bifunctor
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe
import Data.Set (Set)
import Data.Set.NonEmpty.Internal
import Data.These
import Prelude hiding (Foldable(..), filter, map, take, drop, splitAt)
import qualified Data.Foldable as F
import qualified Data.List.NonEmpty as NE
import qualified Data.Semigroup.Foldable as F1
import qualified Data.Set as S
pattern IsNonEmpty :: NESet a -> Set a
pattern $bIsNonEmpty :: forall a. NESet a -> Set a
$mIsNonEmpty :: forall {r} {a}. Set a -> (NESet a -> r) -> ((# #) -> r) -> r
IsNonEmpty n <- (nonEmptySet->Just n)
where
IsNonEmpty NESet a
n = forall a. NESet a -> Set a
toSet NESet a
n
pattern IsEmpty :: Set a
pattern $bIsEmpty :: forall a. Set a
$mIsEmpty :: forall {r} {a}. Set a -> ((# #) -> r) -> ((# #) -> r) -> r
IsEmpty <- (S.null->True)
where
IsEmpty = forall a. Set a
S.empty
{-# COMPLETE IsNonEmpty, IsEmpty #-}
unsafeFromSet
:: Set a
-> NESet a
unsafeFromSet :: forall a. Set a -> NESet a
unsafeFromSet = forall r a. r -> (NESet a -> r) -> Set a -> r
withNonEmpty forall {a}. a
e forall a. a -> a
id
where
e :: a
e = forall a. [Char] -> a
errorWithoutStackTrace [Char]
"NESet.unsafeFromSet: empty set"
{-# INLINE unsafeFromSet #-}
insertSet :: Ord a => a -> Set a -> NESet a
insertSet :: forall a. Ord a => a -> Set a -> NESet a
insertSet a
x = forall r a. r -> (NESet a -> r) -> Set a -> r
withNonEmpty (forall a. a -> NESet a
singleton a
x) (forall a. Ord a => a -> NESet a -> NESet a
insert a
x)
{-# INLINE insertSet #-}
insertSetMin :: a -> Set a -> NESet a
insertSetMin :: forall a. a -> Set a -> NESet a
insertSetMin = forall a. a -> Set a -> NESet a
NESet
{-# INLINE insertSetMin #-}
insertSetMax :: a -> Set a -> NESet a
insertSetMax :: forall a. a -> Set a -> NESet a
insertSetMax a
x = forall r a. r -> (NESet a -> r) -> Set a -> r
withNonEmpty (forall a. a -> NESet a
singleton a
x) NESet a -> NESet a
go
where
go :: NESet a -> NESet a
go (NESet a
x0 Set a
s0) = forall a. a -> Set a -> NESet a
NESet a
x0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Set a -> Set a
insertMaxSet a
x forall a b. (a -> b) -> a -> b
$ Set a
s0
{-# INLINE insertSetMax #-}
fromAscList :: Eq a => NonEmpty a -> NESet a
fromAscList :: forall a. Eq a => NonEmpty a -> NESet a
fromAscList = forall a. NonEmpty a -> NESet a
fromDistinctAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => NonEmpty a -> NonEmpty a
combineEq
{-# INLINE fromAscList #-}
fromDistinctAscList :: NonEmpty a -> NESet a
fromDistinctAscList :: forall a. NonEmpty a -> NESet a
fromDistinctAscList (a
x :| [a]
xs) = forall a. a -> Set a -> NESet a
insertSetMin a
x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Set a
S.fromDistinctAscList
forall a b. (a -> b) -> a -> b
$ [a]
xs
{-# INLINE fromDistinctAscList #-}
fromDescList :: Eq a => NonEmpty a -> NESet a
fromDescList :: forall a. Eq a => NonEmpty a -> NESet a
fromDescList = forall a. NonEmpty a -> NESet a
fromDistinctDescList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => NonEmpty a -> NonEmpty a
combineEq
{-# INLINE fromDescList #-}
fromDistinctDescList :: NonEmpty a -> NESet a
fromDistinctDescList :: forall a. NonEmpty a -> NESet a
fromDistinctDescList (a
x :| [a]
xs) = forall a. a -> Set a -> NESet a
insertSetMax a
x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Set a
S.fromDistinctDescList
forall a b. (a -> b) -> a -> b
$ [a]
xs
{-# INLINE fromDistinctDescList #-}
powerSet
:: forall a. ()
=> NESet a
-> NESet (NESet a)
powerSet :: forall a. NESet a -> NESet (NESet a)
powerSet (NESet a
x Set a
s0) = case forall a. Set a -> Maybe (NESet a)
nonEmptySet Set (NESet a)
p1 of
Maybe (NESet (NESet a))
Nothing -> forall a. a -> NESet a
singleton (forall a. a -> NESet a
singleton a
x)
Just NESet (NESet a)
p2 -> forall a b. (a -> b) -> NESet a -> NESet b
mapMonotonic (forall a. a -> Set a -> NESet a
insertSetMin a
x) NESet (Set a)
p0
forall a. NESet a -> NESet a -> NESet a
`merge` NESet (NESet a)
p2
where
p0 :: NESet (Set a)
p0 :: NESet (Set a)
p0@(NESet Set a
_ Set (Set a)
p0s) = forall a. Set a -> NESet a
forSure forall a b. (a -> b) -> a -> b
$ forall a. Set a -> Set (Set a)
powerSetSet Set a
s0
p1 :: Set (NESet a)
p1 :: Set (NESet a)
p1 = forall a b. (a -> b) -> Set a -> Set b
S.mapMonotonic forall a. Set a -> NESet a
forSure Set (Set a)
p0s
forSure :: Set a -> NESet a
forSure = forall r a. r -> (NESet a -> r) -> Set a -> r
withNonEmpty (forall a. [Char] -> a
errorWithoutStackTrace [Char]
"NESet.powerSet: internal error")
forall a. a -> a
id
{-# INLINABLE powerSet #-}
insert :: Ord a => a -> NESet a -> NESet a
insert :: forall a. Ord a => a -> NESet a -> NESet a
insert a
x n :: NESet a
n@(NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. a -> Set a -> NESet a
NESet a
x forall a b. (a -> b) -> a -> b
$ forall a. NESet a -> Set a
toSet NESet a
n
Ordering
EQ -> forall a. a -> Set a -> NESet a
NESet a
x Set a
s
Ordering
GT -> forall a. a -> Set a -> NESet a
NESet a
x0 forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> Set a -> Set a
S.insert a
x Set a
s
{-# INLINE insert #-}
delete :: Ord a => a -> NESet a -> Set a
delete :: forall a. Ord a => a -> NESet a -> Set a
delete a
x n :: NESet a
n@(NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. NESet a -> Set a
toSet NESet a
n
Ordering
EQ -> Set a
s
Ordering
GT -> forall a. a -> Set a -> Set a
insertMinSet a
x0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => a -> Set a -> Set a
S.delete a
x forall a b. (a -> b) -> a -> b
$ Set a
s
{-# INLINE delete #-}
member :: Ord a => a -> NESet a -> Bool
member :: forall a. Ord a => a -> NESet a -> Bool
member a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> Bool
False
Ordering
EQ -> Bool
True
Ordering
GT -> forall a. Ord a => a -> Set a -> Bool
S.member a
x Set a
s
{-# INLINE member #-}
notMember :: Ord a => a -> NESet a -> Bool
notMember :: forall a. Ord a => a -> NESet a -> Bool
notMember a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> Bool
True
Ordering
EQ -> Bool
False
Ordering
GT -> forall a. Ord a => a -> Set a -> Bool
S.notMember a
x Set a
s
{-# INLINE notMember #-}
lookupLT :: Ord a => a -> NESet a -> Maybe a
lookupLT :: forall a. Ord a => a -> NESet a -> Maybe a
lookupLT a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. Maybe a
Nothing
Ordering
EQ -> forall a. Maybe a
Nothing
Ordering
GT -> forall a. Ord a => a -> Set a -> Maybe a
S.lookupLT a
x Set a
s forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just a
x0
{-# INLINE lookupLT #-}
lookupGT :: Ord a => a -> NESet a -> Maybe a
lookupGT :: forall a. Ord a => a -> NESet a -> Maybe a
lookupGT a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. a -> Maybe a
Just a
x0
Ordering
EQ -> forall a. Set a -> Maybe a
S.lookupMin Set a
s
Ordering
GT -> forall a. Ord a => a -> Set a -> Maybe a
S.lookupGT a
x Set a
s
{-# INLINE lookupGT #-}
lookupLE :: Ord a => a -> NESet a -> Maybe a
lookupLE :: forall a. Ord a => a -> NESet a -> Maybe a
lookupLE a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. Maybe a
Nothing
Ordering
EQ -> forall a. a -> Maybe a
Just a
x0
Ordering
GT -> forall a. Ord a => a -> Set a -> Maybe a
S.lookupLE a
x Set a
s forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. a -> Maybe a
Just a
x0
{-# INLINE lookupLE #-}
lookupGE :: Ord a => a -> NESet a -> Maybe a
lookupGE :: forall a. Ord a => a -> NESet a -> Maybe a
lookupGE a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. a -> Maybe a
Just a
x0
Ordering
EQ -> forall a. a -> Maybe a
Just a
x0
Ordering
GT -> forall a. Ord a => a -> Set a -> Maybe a
S.lookupGE a
x Set a
s
{-# INLINE lookupGE #-}
isSubsetOf
:: Ord a
=> NESet a
-> NESet a
-> Bool
isSubsetOf :: forall a. Ord a => NESet a -> NESet a -> Bool
isSubsetOf (NESet a
x Set a
s0) (forall a. NESet a -> Set a
toSet->Set a
s1) = a
x forall a. Ord a => a -> Set a -> Bool
`S.member` Set a
s1
Bool -> Bool -> Bool
&& Set a
s0 forall a. Ord a => Set a -> Set a -> Bool
`S.isSubsetOf` Set a
s1
{-# INLINE isSubsetOf #-}
isProperSubsetOf
:: Ord a
=> NESet a
-> NESet a
-> Bool
isProperSubsetOf :: forall a. Ord a => NESet a -> NESet a -> Bool
isProperSubsetOf NESet a
s0 NESet a
s1 = forall a. Set a -> Int
S.size (forall a. NESet a -> Set a
nesSet NESet a
s0) forall a. Ord a => a -> a -> Bool
< forall a. Set a -> Int
S.size (forall a. NESet a -> Set a
nesSet NESet a
s1)
Bool -> Bool -> Bool
&& NESet a
s0 forall a. Ord a => NESet a -> NESet a -> Bool
`isSubsetOf` NESet a
s1
{-# INLINE isProperSubsetOf #-}
disjoint
:: Ord a
=> NESet a
-> NESet a
-> Bool
disjoint :: forall a. Ord a => NESet a -> NESet a -> Bool
disjoint n1 :: NESet a
n1@(NESet a
x1 Set a
s1) n2 :: NESet a
n2@(NESet a
x2 Set a
s2) = case forall a. Ord a => a -> a -> Ordering
compare a
x1 a
x2 of
Ordering
LT -> Set a
s1 forall a. Ord a => Set a -> Set a -> Bool
`disjointSet` forall a. NESet a -> Set a
toSet NESet a
n2
Ordering
EQ -> Bool
False
Ordering
GT -> forall a. NESet a -> Set a
toSet NESet a
n1 forall a. Ord a => Set a -> Set a -> Bool
`disjointSet` Set a
s2
{-# INLINE disjoint #-}
difference
:: Ord a
=> NESet a
-> NESet a
-> Set a
difference :: forall a. Ord a => NESet a -> NESet a -> Set a
difference n1 :: NESet a
n1@(NESet a
x1 Set a
s1) n2 :: NESet a
n2@(NESet a
x2 Set a
s2) = case forall a. Ord a => a -> a -> Ordering
compare a
x1 a
x2 of
Ordering
LT -> forall a. a -> Set a -> Set a
insertMinSet a
x1 forall a b. (a -> b) -> a -> b
$ Set a
s1 forall a. Ord a => Set a -> Set a -> Set a
`S.difference` forall a. NESet a -> Set a
toSet NESet a
n2
Ordering
EQ -> Set a
s1 forall a. Ord a => Set a -> Set a -> Set a
`S.difference` Set a
s2
Ordering
GT -> forall a. NESet a -> Set a
toSet NESet a
n1 forall a. Ord a => Set a -> Set a -> Set a
`S.difference` Set a
s2
{-# INLINE difference #-}
(\\)
:: Ord a
=> NESet a
-> NESet a
-> Set a
\\ :: forall a. Ord a => NESet a -> NESet a -> Set a
(\\) = forall a. Ord a => NESet a -> NESet a -> Set a
difference
{-# INLINE (\\) #-}
intersection
:: Ord a
=> NESet a
-> NESet a
-> Set a
intersection :: forall a. Ord a => NESet a -> NESet a -> Set a
intersection n1 :: NESet a
n1@(NESet a
x1 Set a
s1) n2 :: NESet a
n2@(NESet a
x2 Set a
s2) = case forall a. Ord a => a -> a -> Ordering
compare a
x1 a
x2 of
Ordering
LT -> Set a
s1 forall a. Ord a => Set a -> Set a -> Set a
`S.intersection` forall a. NESet a -> Set a
toSet NESet a
n2
Ordering
EQ -> forall a. a -> Set a -> Set a
insertMinSet a
x1 forall a b. (a -> b) -> a -> b
$ Set a
s1 forall a. Ord a => Set a -> Set a -> Set a
`S.intersection` Set a
s2
Ordering
GT -> forall a. NESet a -> Set a
toSet NESet a
n1 forall a. Ord a => Set a -> Set a -> Set a
`S.intersection` Set a
s2
{-# INLINE intersection #-}
cartesianProduct
:: NESet a
-> NESet b
-> NESet (a, b)
cartesianProduct :: forall a b. NESet a -> NESet b -> NESet (a, b)
cartesianProduct NESet a
n1 NESet b
n2 = forall a. MergeNESet a -> NESet a
getMergeNESet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
F1.foldMap1 (\a
x -> forall a. NESet a -> MergeNESet a
MergeNESet forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> NESet a -> NESet b
mapMonotonic (a
x,) NESet b
n2)
forall a b. (a -> b) -> a -> b
$ NESet a
n1
{-# INLINE cartesianProduct #-}
disjointUnion
:: NESet a
-> NESet b
-> NESet (Either a b)
disjointUnion :: forall a b. NESet a -> NESet b -> NESet (Either a b)
disjointUnion (NESet a
x1 Set a
s1) NESet b
n2 = forall a. a -> Set a -> NESet a
NESet (forall a b. a -> Either a b
Left a
x1)
(Set a
s1 forall a b. Set a -> Set b -> Set (Either a b)
`disjointUnionSet` forall a. NESet a -> Set a
toSet NESet b
n2)
{-# INLINE disjointUnion #-}
filter
:: (a -> Bool)
-> NESet a
-> Set a
filter :: forall a. (a -> Bool) -> NESet a -> Set a
filter a -> Bool
f (NESet a
x Set a
s1)
| a -> Bool
f a
x = forall a. a -> Set a -> Set a
insertMinSet a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> Set a -> Set a
S.filter a -> Bool
f forall a b. (a -> b) -> a -> b
$ Set a
s1
| Bool
otherwise = forall a. (a -> Bool) -> Set a -> Set a
S.filter a -> Bool
f Set a
s1
{-# INLINE filter #-}
takeWhileAntitone
:: (a -> Bool)
-> NESet a
-> Set a
takeWhileAntitone :: forall a. (a -> Bool) -> NESet a -> Set a
takeWhileAntitone a -> Bool
f (NESet a
x Set a
s)
| a -> Bool
f a
x = forall a. a -> Set a -> Set a
insertMinSet a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> Set a -> Set a
S.takeWhileAntitone a -> Bool
f forall a b. (a -> b) -> a -> b
$ Set a
s
| Bool
otherwise = forall a. Set a
S.empty
{-# INLINE takeWhileAntitone #-}
dropWhileAntitone
:: (a -> Bool)
-> NESet a
-> Set a
dropWhileAntitone :: forall a. (a -> Bool) -> NESet a -> Set a
dropWhileAntitone a -> Bool
f n :: NESet a
n@(NESet a
x Set a
s)
| a -> Bool
f a
x = forall a. (a -> Bool) -> Set a -> Set a
S.dropWhileAntitone a -> Bool
f Set a
s
| Bool
otherwise = forall a. NESet a -> Set a
toSet NESet a
n
{-# INLINE dropWhileAntitone #-}
spanAntitone
:: (a -> Bool)
-> NESet a
-> These (NESet a) (NESet a)
spanAntitone :: forall a. (a -> Bool) -> NESet a -> These (NESet a) (NESet a)
spanAntitone a -> Bool
f n :: NESet a
n@(NESet a
x Set a
s0)
| a -> Bool
f a
x = case (forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s1, forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s2) of
(Maybe (NESet a)
Nothing, Maybe (NESet a)
Nothing) -> forall a b. a -> These a b
This NESet a
n
(Just NESet a
_ , Maybe (NESet a)
Nothing) -> forall a b. a -> These a b
This NESet a
n
(Maybe (NESet a)
Nothing, Just NESet a
n2) -> forall a b. a -> b -> These a b
These (forall a. a -> NESet a
singleton a
x) NESet a
n2
(Just NESet a
_ , Just NESet a
n2) -> forall a b. a -> b -> These a b
These (forall a. a -> Set a -> NESet a
insertSetMin a
x Set a
s1) NESet a
n2
| Bool
otherwise = forall a b. b -> These a b
That NESet a
n
where
(Set a
s1, Set a
s2) = forall a. (a -> Bool) -> Set a -> (Set a, Set a)
S.spanAntitone a -> Bool
f Set a
s0
{-# INLINABLE spanAntitone #-}
partition
:: (a -> Bool)
-> NESet a
-> These (NESet a) (NESet a)
partition :: forall a. (a -> Bool) -> NESet a -> These (NESet a) (NESet a)
partition a -> Bool
f n :: NESet a
n@(NESet a
x Set a
s0) = case (forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s1, forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s2) of
(Maybe (NESet a)
Nothing, Maybe (NESet a)
Nothing)
| a -> Bool
f a
x -> forall a b. a -> These a b
This NESet a
n
| Bool
otherwise -> forall a b. b -> These a b
That NESet a
n
(Just NESet a
n1, Maybe (NESet a)
Nothing)
| a -> Bool
f a
x -> forall a b. a -> These a b
This NESet a
n
| Bool
otherwise -> forall a b. a -> b -> These a b
These NESet a
n1 (forall a. a -> NESet a
singleton a
x)
(Maybe (NESet a)
Nothing, Just NESet a
n2)
| a -> Bool
f a
x -> forall a b. a -> b -> These a b
These (forall a. a -> NESet a
singleton a
x) NESet a
n2
| Bool
otherwise -> forall a b. b -> These a b
That NESet a
n
(Just NESet a
n1, Just NESet a
n2)
| a -> Bool
f a
x -> forall a b. a -> b -> These a b
These (forall a. a -> Set a -> NESet a
insertSetMin a
x Set a
s1) NESet a
n2
| Bool
otherwise -> forall a b. a -> b -> These a b
These NESet a
n1 (forall a. a -> Set a -> NESet a
insertSetMin a
x Set a
s2)
where
(Set a
s1, Set a
s2) = forall a. (a -> Bool) -> Set a -> (Set a, Set a)
S.partition a -> Bool
f Set a
s0
{-# INLINABLE partition #-}
split
:: Ord a
=> a
-> NESet a
-> Maybe (These (NESet a) (NESet a))
split :: forall a.
Ord a =>
a -> NESet a -> Maybe (These (NESet a) (NESet a))
split a
x n :: NESet a
n@(NESet a
x0 Set a
s0) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. b -> These a b
That NESet a
n
Ordering
EQ -> forall a b. b -> These a b
That forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s0
Ordering
GT -> case (forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s1, forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s2) of
(Maybe (NESet a)
Nothing, Maybe (NESet a)
Nothing) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> These a b
This (forall a. a -> NESet a
singleton a
x0)
(Just NESet a
_ , Maybe (NESet a)
Nothing) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> These a b
This (forall a. a -> Set a -> NESet a
insertSetMin a
x0 Set a
s1)
(Maybe (NESet a)
Nothing, Just NESet a
n2) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> These a b
These (forall a. a -> NESet a
singleton a
x0) NESet a
n2
(Just NESet a
_ , Just NESet a
n2) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> These a b
These (forall a. a -> Set a -> NESet a
insertSetMin a
x0 Set a
s1) NESet a
n2
where
(Set a
s1, Set a
s2) = forall a. Ord a => a -> Set a -> (Set a, Set a)
S.split a
x Set a
s0
{-# INLINABLE split #-}
splitMember
:: Ord a
=> a
-> NESet a
-> (Bool, Maybe (These (NESet a) (NESet a)))
splitMember :: forall a.
Ord a =>
a -> NESet a -> (Bool, Maybe (These (NESet a) (NESet a)))
splitMember a
x n :: NESet a
n@(NESet a
x0 Set a
s0) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> (Bool
False, forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. b -> These a b
That NESet a
n)
Ordering
EQ -> (Bool
True , forall a b. b -> These a b
That forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s0)
Ordering
GT -> (Bool
mem ,) forall a b. (a -> b) -> a -> b
$ case (forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s1, forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s2) of
(Maybe (NESet a)
Nothing, Maybe (NESet a)
Nothing) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> These a b
This (forall a. a -> NESet a
singleton a
x0)
(Just NESet a
_ , Maybe (NESet a)
Nothing) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> These a b
This (forall a. a -> Set a -> NESet a
insertSetMin a
x0 Set a
s1)
(Maybe (NESet a)
Nothing, Just NESet a
n2) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> These a b
These (forall a. a -> NESet a
singleton a
x0) NESet a
n2
(Just NESet a
_ , Just NESet a
n2) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> These a b
These (forall a. a -> Set a -> NESet a
insertSetMin a
x0 Set a
s1) NESet a
n2
where
(Set a
s1, Bool
mem, Set a
s2) = forall a. Ord a => a -> Set a -> (Set a, Bool, Set a)
S.splitMember a
x Set a
s0
{-# INLINABLE splitMember #-}
splitRoot
:: NESet a
-> NonEmpty (NESet a)
splitRoot :: forall a. NESet a -> NonEmpty (NESet a)
splitRoot (NESet a
x Set a
s) = forall a. a -> NESet a
singleton a
x
forall a. a -> [a] -> NonEmpty a
:| forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe forall a. Set a -> Maybe (NESet a)
nonEmptySet (forall a. Set a -> [Set a]
S.splitRoot Set a
s)
{-# INLINE splitRoot #-}
lookupIndex
:: Ord a
=> a
-> NESet a
-> Maybe Int
lookupIndex :: forall a. Ord a => a -> NESet a -> Maybe Int
lookupIndex a
x (NESet a
x0 Set a
s) = case forall a. Ord a => a -> a -> Ordering
compare a
x a
x0 of
Ordering
LT -> forall a. Maybe a
Nothing
Ordering
EQ -> forall a. a -> Maybe a
Just Int
0
Ordering
GT -> (forall a. Num a => a -> a -> a
+ Int
1) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Ord a => a -> Set a -> Maybe Int
S.lookupIndex a
x Set a
s
{-# INLINE lookupIndex #-}
findIndex
:: Ord a
=> a
-> NESet a
-> Int
findIndex :: forall a. Ord a => a -> NESet a -> Int
findIndex a
k = forall a. a -> Maybe a -> a
fromMaybe forall {a}. a
e forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Ord a => a -> NESet a -> Maybe Int
lookupIndex a
k
where
e :: a
e = forall a. HasCallStack => [Char] -> a
error [Char]
"NESet.findIndex: element is not in the set"
{-# INLINE findIndex #-}
elemAt
:: Int
-> NESet a
-> a
elemAt :: forall a. Int -> NESet a -> a
elemAt Int
0 (NESet a
x Set a
_) = a
x
elemAt Int
i (NESet a
_ Set a
s) = forall a. Int -> Set a -> a
S.elemAt (Int
i forall a. Num a => a -> a -> a
- Int
1) Set a
s
{-# INLINE elemAt #-}
deleteAt
:: Int
-> NESet a
-> Set a
deleteAt :: forall a. Int -> NESet a -> Set a
deleteAt Int
0 (NESet a
_ Set a
s) = Set a
s
deleteAt Int
i (NESet a
x Set a
s) = forall a. a -> Set a -> Set a
insertMinSet a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> Set a -> Set a
S.deleteAt (Int
i forall a. Num a => a -> a -> a
- Int
1) forall a b. (a -> b) -> a -> b
$ Set a
s
{-# INLINABLE deleteAt #-}
take
:: Int
-> NESet a
-> Set a
take :: forall a. Int -> NESet a -> Set a
take Int
0 (NESet a
_ Set a
_) = forall a. Set a
S.empty
take Int
i (NESet a
x Set a
s) = forall a. a -> Set a -> Set a
insertMinSet a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> Set a -> Set a
S.take (Int
i forall a. Num a => a -> a -> a
- Int
1) forall a b. (a -> b) -> a -> b
$ Set a
s
{-# INLINABLE take #-}
drop
:: Int
-> NESet a
-> Set a
drop :: forall a. Int -> NESet a -> Set a
drop Int
0 NESet a
n = forall a. NESet a -> Set a
toSet NESet a
n
drop Int
n (NESet a
_ Set a
s) = forall a. Int -> Set a -> Set a
S.drop (Int
n forall a. Num a => a -> a -> a
- Int
1) Set a
s
{-# INLINABLE drop #-}
splitAt
:: Int
-> NESet a
-> These (NESet a) (NESet a)
splitAt :: forall a. Int -> NESet a -> These (NESet a) (NESet a)
splitAt Int
0 NESet a
n = forall a b. b -> These a b
That NESet a
n
splitAt Int
i n :: NESet a
n@(NESet a
x Set a
s0) = case (forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s1, forall a. Set a -> Maybe (NESet a)
nonEmptySet Set a
s2) of
(Maybe (NESet a)
Nothing, Maybe (NESet a)
Nothing) -> forall a b. a -> These a b
This (forall a. a -> NESet a
singleton a
x)
(Just NESet a
_ , Maybe (NESet a)
Nothing) -> forall a b. a -> These a b
This NESet a
n
(Maybe (NESet a)
Nothing, Just NESet a
n2) -> forall a b. a -> b -> These a b
These (forall a. a -> NESet a
singleton a
x) NESet a
n2
(Just NESet a
_ , Just NESet a
n2) -> forall a b. a -> b -> These a b
These (forall a. a -> Set a -> NESet a
insertSetMin a
x Set a
s1) NESet a
n2
where
(Set a
s1, Set a
s2) = forall a. Int -> Set a -> (Set a, Set a)
S.splitAt (Int
i forall a. Num a => a -> a -> a
- Int
1) Set a
s0
{-# INLINABLE splitAt #-}
map :: Ord b
=> (a -> b)
-> NESet a
-> NESet b
map :: forall b a. Ord b => (a -> b) -> NESet a -> NESet b
map a -> b
f (NESet a
x0 Set a
s) = forall a. Ord a => NonEmpty a -> NESet a
fromList
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b
f a
x0 forall a. a -> [a] -> NonEmpty a
:|)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b -> b) -> b -> Set a -> b
S.foldr (\a
x [b]
xs -> a -> b
f a
x forall a. a -> [a] -> [a]
: [b]
xs) []
forall a b. (a -> b) -> a -> b
$ Set a
s
{-# INLINE map #-}
mapMonotonic
:: (a -> b)
-> NESet a
-> NESet b
mapMonotonic :: forall a b. (a -> b) -> NESet a -> NESet b
mapMonotonic a -> b
f (NESet a
x Set a
s) = forall a. a -> Set a -> NESet a
NESet (a -> b
f a
x) (forall a b. (a -> b) -> Set a -> Set b
S.mapMonotonic a -> b
f Set a
s)
{-# INLINE mapMonotonic #-}
foldr1' :: (a -> a -> a) -> NESet a -> a
foldr1' :: forall a. (a -> a -> a) -> NESet a -> a
foldr1' a -> a -> a
f (NESet a
x Set a
s) = case forall a. Set a -> Maybe (a, Set a)
S.maxView Set a
s of
Maybe (a, Set a)
Nothing -> a
x
Just (a
y, Set a
s') -> let !z :: a
z = forall a b. (a -> b -> b) -> b -> Set a -> b
S.foldr' a -> a -> a
f a
y Set a
s' in a
x a -> a -> a
`f` a
z
{-# INLINE foldr1' #-}
foldl1' :: (a -> a -> a) -> NESet a -> a
foldl1' :: forall a. (a -> a -> a) -> NESet a -> a
foldl1' a -> a -> a
f (NESet a
x Set a
s) = forall a b. (a -> b -> a) -> a -> Set b -> a
S.foldl' a -> a -> a
f a
x Set a
s
{-# INLINE foldl1' #-}
findMin :: NESet a -> a
findMin :: forall a. NESet a -> a
findMin (NESet a
x Set a
_) = a
x
{-# INLINE findMin #-}
findMax :: NESet a -> a
findMax :: forall a. NESet a -> a
findMax (NESet a
x Set a
s) = forall a. a -> Maybe a -> a
fromMaybe a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> Maybe a
S.lookupMax forall a b. (a -> b) -> a -> b
$ Set a
s
{-# INLINE findMax #-}
deleteMin :: NESet a -> Set a
deleteMin :: forall a. NESet a -> Set a
deleteMin (NESet a
_ Set a
s) = Set a
s
{-# INLINE deleteMin #-}
deleteMax :: NESet a -> Set a
deleteMax :: forall a. NESet a -> Set a
deleteMax (NESet a
x Set a
s) = case forall a. Set a -> Maybe (a, Set a)
S.maxView Set a
s of
Maybe (a, Set a)
Nothing -> forall a. Set a
S.empty
Just (a
_, Set a
s') -> forall a. a -> Set a -> Set a
insertMinSet a
x Set a
s'
{-# INLINE deleteMax #-}
deleteFindMin :: NESet a -> (a, Set a)
deleteFindMin :: forall a. NESet a -> (a, Set a)
deleteFindMin (NESet a
x Set a
s) = (a
x, Set a
s)
{-# INLINE deleteFindMin #-}
deleteFindMax :: NESet a -> (a, Set a)
deleteFindMax :: forall a. NESet a -> (a, Set a)
deleteFindMax (NESet a
x Set a
s) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (a
x, forall a. Set a
S.empty) (forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (forall a. a -> Set a -> Set a
insertMinSet a
x))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Set a -> Maybe (a, Set a)
S.maxView
forall a b. (a -> b) -> a -> b
$ Set a
s
{-# INLINE deleteFindMax #-}
elems :: NESet a -> NonEmpty a
elems :: forall a. NESet a -> NonEmpty a
elems = forall a. NESet a -> NonEmpty a
toList
{-# INLINE elems #-}
toAscList :: NESet a -> NonEmpty a
toAscList :: forall a. NESet a -> NonEmpty a
toAscList = forall a. NESet a -> NonEmpty a
toList
{-# INLINE toAscList #-}
toDescList :: NESet a -> NonEmpty a
toDescList :: forall a. NESet a -> NonEmpty a
toDescList (NESet a
x Set a
s) = forall a b. (a -> b -> a) -> a -> Set b -> a
S.foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. a -> NonEmpty a -> NonEmpty a
(NE.<|)) (a
x forall a. a -> [a] -> NonEmpty a
:| []) Set a
s
{-# INLINE toDescList #-}
combineEq :: Eq a => NonEmpty a -> NonEmpty a
combineEq :: forall a. Eq a => NonEmpty a -> NonEmpty a
combineEq (a
x :| [a]
xs) = forall {t}. Eq t => t -> [t] -> NonEmpty t
go a
x [a]
xs
where
go :: t -> [t] -> NonEmpty t
go t
z [] = t
z forall a. a -> [a] -> NonEmpty a
:| []
go t
z (t
y:[t]
ys)
| t
z forall a. Eq a => a -> a -> Bool
== t
y = t -> [t] -> NonEmpty t
go t
z [t]
ys
| Bool
otherwise = t
z forall a. a -> NonEmpty a -> NonEmpty a
NE.<| t -> [t] -> NonEmpty t
go t
y [t]
ys