{-# LANGUAGE CPP #-}
module GHC.Data.List.SetOps (
unionLists, unionListsOrd, minusList,
Assoc, assoc, assocMaybe, assocUsing, assocDefault, assocDefaultUsing,
hasNoDups, removeDups, nubOrdBy, findDupsEq,
equivClasses,
getNth,
isIn, isn'tIn,
) where
import GHC.Prelude
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Misc
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Set as S
getNth :: Outputable a => [a] -> Int -> a
getNth :: forall a. Outputable a => [a] -> Int -> a
getNth [a]
xs Int
n = forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr ([a]
xs forall a. [a] -> Int -> Bool
`lengthExceeds` Int
n) (forall a. Outputable a => a -> SDoc
ppr Int
n forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr [a]
xs) forall a b. (a -> b) -> a -> b
$
[a]
xs forall a. [a] -> Int -> a
!! Int
n
unionListsOrd :: (HasDebugCallStack, Outputable a, Ord a) => [a] -> [a] -> [a]
unionListsOrd :: forall a.
(HasDebugCallStack, Outputable a, Ord a) =>
[a] -> [a] -> [a]
unionListsOrd [a]
xs [a]
ys
= let set_ys :: Set a
set_ys = forall a. Ord a => [a] -> Set a
S.fromList [a]
ys
in (forall a. (a -> Bool) -> [a] -> [a]
filter (\a
e -> Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> Set a -> Bool
S.member a
e Set a
set_ys) [a]
xs) forall a. [a] -> [a] -> [a]
++ [a]
ys
unionLists :: (HasDebugCallStack, Outputable a, Eq a) => [a] -> [a] -> [a]
unionLists :: forall a.
(HasDebugCallStack, Outputable a, Eq a) =>
[a] -> [a] -> [a]
unionLists [a]
xs [] = [a]
xs
unionLists [] [a]
ys = [a]
ys
unionLists [a
x] [a]
ys
| forall a. Eq a => String -> a -> [a] -> Bool
isIn String
"unionLists" a
x [a]
ys = [a]
ys
| Bool
otherwise = a
xforall a. a -> [a] -> [a]
:[a]
ys
unionLists [a]
xs [a
y]
| forall a. Eq a => String -> a -> [a] -> Bool
isIn String
"unionLists" a
y [a]
xs = [a]
xs
| Bool
otherwise = a
yforall a. a -> [a] -> [a]
:[a]
xs
unionLists [a]
xs [a]
ys
= forall a. HasCallStack => Bool -> String -> SDoc -> a -> a
warnPprTrace (forall a. [a] -> Int -> Bool
lengthExceeds [a]
xs Int
100 Bool -> Bool -> Bool
|| forall a. [a] -> Int -> Bool
lengthExceeds [a]
ys Int
100) String
"unionLists" (forall a. Outputable a => a -> SDoc
ppr [a]
xs forall doc. IsDoc doc => doc -> doc -> doc
$$ forall a. Outputable a => a -> SDoc
ppr [a]
ys) forall a b. (a -> b) -> a -> b
$
[a
x | a
x <- [a]
xs, forall a. Eq a => String -> a -> [a] -> Bool
isn'tIn String
"unionLists" a
x [a]
ys] forall a. [a] -> [a] -> [a]
++ [a]
ys
minusList :: Ord a => [a] -> [a] -> [a]
minusList :: forall a. Ord a => [a] -> [a] -> [a]
minusList [] [a]
_ = []
minusList xs :: [a]
xs@[a
x] [a]
ys
| a
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
ys = []
| Bool
otherwise = [a]
xs
minusList [a]
xs [] = [a]
xs
minusList [a]
xs [a
y] = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/= a
y) [a]
xs
minusList [a]
xs [a]
ys = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Ord a => a -> Set a -> Bool
`S.notMember` Set a
yss) [a]
xs
where
yss :: Set a
yss = forall a. Ord a => [a] -> Set a
S.fromList [a]
ys
type Assoc a b = [(a,b)]
assoc :: (Eq a) => String -> Assoc a b -> a -> b
assocDefault :: (Eq a) => b -> Assoc a b -> a -> b
assocUsing :: (a -> a -> Bool) -> String -> Assoc a b -> a -> b
assocMaybe :: (Eq a) => Assoc a b -> a -> Maybe b
assocDefaultUsing :: (a -> a -> Bool) -> b -> Assoc a b -> a -> b
assocDefaultUsing :: forall a b. (a -> a -> Bool) -> b -> Assoc a b -> a -> b
assocDefaultUsing a -> a -> Bool
_ b
deflt [] a
_ = b
deflt
assocDefaultUsing a -> a -> Bool
eq b
deflt ((a
k,b
v) : [(a, b)]
rest) a
key
| a
k a -> a -> Bool
`eq` a
key = b
v
| Bool
otherwise = forall a b. (a -> a -> Bool) -> b -> Assoc a b -> a -> b
assocDefaultUsing a -> a -> Bool
eq b
deflt [(a, b)]
rest a
key
assoc :: forall a b. Eq a => String -> Assoc a b -> a -> b
assoc String
crash_msg Assoc a b
list a
key = forall a b. (a -> a -> Bool) -> b -> Assoc a b -> a -> b
assocDefaultUsing forall a. Eq a => a -> a -> Bool
(==) (forall a. HasCallStack => String -> a
panic (String
"Failed in assoc: " forall a. [a] -> [a] -> [a]
++ String
crash_msg)) Assoc a b
list a
key
assocDefault :: forall a b. Eq a => b -> Assoc a b -> a -> b
assocDefault b
deflt Assoc a b
list a
key = forall a b. (a -> a -> Bool) -> b -> Assoc a b -> a -> b
assocDefaultUsing forall a. Eq a => a -> a -> Bool
(==) b
deflt Assoc a b
list a
key
assocUsing :: forall a b. (a -> a -> Bool) -> String -> Assoc a b -> a -> b
assocUsing a -> a -> Bool
eq String
crash_msg Assoc a b
list a
key = forall a b. (a -> a -> Bool) -> b -> Assoc a b -> a -> b
assocDefaultUsing a -> a -> Bool
eq (forall a. HasCallStack => String -> a
panic (String
"Failed in assoc: " forall a. [a] -> [a] -> [a]
++ String
crash_msg)) Assoc a b
list a
key
assocMaybe :: forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe Assoc a b
alist a
key
= Assoc a b -> Maybe b
lookup Assoc a b
alist
where
lookup :: Assoc a b -> Maybe b
lookup [] = forall a. Maybe a
Nothing
lookup ((a
tv,b
ty):Assoc a b
rest) = if a
key forall a. Eq a => a -> a -> Bool
== a
tv then forall a. a -> Maybe a
Just b
ty else Assoc a b -> Maybe b
lookup Assoc a b
rest
hasNoDups :: (Eq a) => [a] -> Bool
hasNoDups :: forall a. Eq a => [a] -> Bool
hasNoDups [a]
xs = [a] -> [a] -> Bool
f [] [a]
xs
where
f :: [a] -> [a] -> Bool
f [a]
_ [] = Bool
True
f [a]
seen_so_far (a
x:[a]
xs) = if a
x a -> [a] -> Bool
`is_elem` [a]
seen_so_far
then Bool
False
else [a] -> [a] -> Bool
f (a
xforall a. a -> [a] -> [a]
:[a]
seen_so_far) [a]
xs
is_elem :: a -> [a] -> Bool
is_elem = forall a. Eq a => String -> a -> [a] -> Bool
isIn String
"hasNoDups"
equivClasses :: (a -> a -> Ordering)
-> [a]
-> [NonEmpty a]
equivClasses :: forall a. (a -> a -> Ordering) -> [a] -> [NonEmpty a]
equivClasses a -> a -> Ordering
_ [] = []
equivClasses a -> a -> Ordering
_ [a
stuff] = [a
stuff forall a. a -> [a] -> NonEmpty a
:| []]
equivClasses a -> a -> Ordering
cmp [a]
items = forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
NE.groupBy a -> a -> Bool
eq (forall a. (a -> a -> Ordering) -> [a] -> [a]
L.sortBy a -> a -> Ordering
cmp [a]
items)
where
eq :: a -> a -> Bool
eq a
a a
b = case a -> a -> Ordering
cmp a
a a
b of { Ordering
EQ -> Bool
True; Ordering
_ -> Bool
False }
removeDups :: (a -> a -> Ordering)
-> [a]
-> ([a],
[NonEmpty a])
removeDups :: forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups a -> a -> Ordering
_ [] = ([], [])
removeDups a -> a -> Ordering
_ [a
x] = ([a
x],[])
removeDups a -> a -> Ordering
cmp [a]
xs
= case forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
L.mapAccumR forall a. [NonEmpty a] -> NonEmpty a -> ([NonEmpty a], a)
collect_dups [] (forall a. (a -> a -> Ordering) -> [a] -> [NonEmpty a]
equivClasses a -> a -> Ordering
cmp [a]
xs) of { ([NonEmpty a]
dups, [a]
xs') ->
([a]
xs', [NonEmpty a]
dups) }
where
collect_dups :: [NonEmpty a] -> NonEmpty a -> ([NonEmpty a], a)
collect_dups :: forall a. [NonEmpty a] -> NonEmpty a -> ([NonEmpty a], a)
collect_dups [NonEmpty a]
dups_so_far (a
x :| []) = ([NonEmpty a]
dups_so_far, a
x)
collect_dups [NonEmpty a]
dups_so_far dups :: NonEmpty a
dups@(a
x :| [a]
_) = (NonEmpty a
dupsforall a. a -> [a] -> [a]
:[NonEmpty a]
dups_so_far, a
x)
nubOrdBy :: (a -> a -> Ordering) -> [a] -> [a]
nubOrdBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
nubOrdBy a -> a -> Ordering
cmp [a]
xs = forall a b. (a, b) -> a
fst (forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups a -> a -> Ordering
cmp [a]
xs)
findDupsEq :: (a->a->Bool) -> [a] -> [NonEmpty a]
findDupsEq :: forall a. (a -> a -> Bool) -> [a] -> [NonEmpty a]
findDupsEq a -> a -> Bool
_ [] = []
findDupsEq a -> a -> Bool
eq (a
x:[a]
xs) | forall (t :: * -> *) a. Foldable t => t a -> Bool
L.null [a]
eq_xs = forall a. (a -> a -> Bool) -> [a] -> [NonEmpty a]
findDupsEq a -> a -> Bool
eq [a]
xs
| Bool
otherwise = (a
x forall a. a -> [a] -> NonEmpty a
:| [a]
eq_xs) forall a. a -> [a] -> [a]
: forall a. (a -> a -> Bool) -> [a] -> [NonEmpty a]
findDupsEq a -> a -> Bool
eq [a]
neq_xs
where ([a]
eq_xs, [a]
neq_xs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
L.partition (a -> a -> Bool
eq a
x) [a]
xs
# if !defined(DEBUG)
isIn, isn'tIn :: Eq a => String -> a -> [a] -> Bool
isIn :: forall a. Eq a => String -> a -> [a] -> Bool
isIn String
_msg a
x [a]
ys = a
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a]
ys
isn'tIn :: forall a. Eq a => String -> a -> [a] -> Bool
isn'tIn String
_msg a
x [a]
ys = a
x forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [a]
ys
# else /* DEBUG */
isIn, isn'tIn :: (HasDebugCallStack, Eq a) => String -> a -> [a] -> Bool
isIn msg x ys
= elem100 0 x ys
where
elem100 :: Eq a => Int -> a -> [a] -> Bool
elem100 _ _ [] = False
elem100 i x (y:ys)
| i > 100 = warnPprTrace True ("Over-long elem in " ++ msg) empty (x `elem` (y:ys))
| otherwise = x == y || elem100 (i + 1) x ys
isn'tIn msg x ys
= notElem100 0 x ys
where
notElem100 :: Eq a => Int -> a -> [a] -> Bool
notElem100 _ _ [] = True
notElem100 i x (y:ys)
| i > 100 = warnPprTrace True ("Over-long notElem in " ++ msg) empty (x `notElem` (y:ys))
| otherwise = x /= y && notElem100 (i + 1) x ys
# endif /* DEBUG */