module HGeometry.Set.Util
( toggle
, splitOn, splitBy
, fromListBy
, join
, insertBy
, deleteAllBy
, queryBy
, toggleBy
) where
import Data.Functor.Identity
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Set.Internal as Internal
import HGeometry.Ord.Dynamic
toggle :: Ord a => a -> Set a -> Set a
toggle :: forall a. Ord a => a -> Set a -> Set a
toggle a
x = Identity (Set a) -> Set a
forall a. Identity a -> a
runIdentity (Identity (Set a) -> Set a)
-> (Set a -> Identity (Set a)) -> Set a -> Set a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> Identity Bool) -> a -> Set a -> Identity (Set a)
forall a (f :: * -> *).
(Ord a, Functor f) =>
(Bool -> f Bool) -> a -> Set a -> f (Set a)
Set.alterF (Bool -> Identity Bool
forall a. a -> Identity a
Identity (Bool -> Identity Bool) -> (Bool -> Bool) -> Bool -> Identity Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Bool
not) a
x
splitOn :: Ord b => (a -> b) -> b -> Set a -> (Set a, Set a, Set a)
splitOn :: forall b a.
Ord b =>
(a -> b) -> b -> Set a -> (Set a, Set a, Set a)
splitOn a -> b
f b
x Set a
s = let (Set a
l,Set a
s') = (a -> Bool) -> Set a -> (Set a, Set a)
forall a. (a -> Bool) -> Set a -> (Set a, Set a)
Set.spanAntitone (Ordering -> b -> Bool
g Ordering
LT (b -> Bool) -> (a -> b) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) Set a
s
(Set a
m,Set a
r) = (a -> Bool) -> Set a -> (Set a, Set a)
forall a. (a -> Bool) -> Set a -> (Set a, Set a)
Set.spanAntitone (Ordering -> b -> Bool
g Ordering
EQ (b -> Bool) -> (a -> b) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) Set a
s'
g :: Ordering -> b -> Bool
g Ordering
c b
y = b
y b -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` b
x Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
c
in (Set a
l,Set a
m,Set a
r)
splitBy :: (a -> Ordering) -> Set a -> (Set a, Set a, Set a)
splitBy :: forall a. (a -> Ordering) -> Set a -> (Set a, Set a, Set a)
splitBy a -> Ordering
f Set a
s = let (Set a
l,Set a
s') = (a -> Bool) -> Set a -> (Set a, Set a)
forall a. (a -> Bool) -> Set a -> (Set a, Set a)
Set.spanAntitone (Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
(==) Ordering
LT (Ordering -> Bool) -> (a -> Ordering) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Ordering
f) Set a
s
(Set a
m,Set a
r) = (a -> Bool) -> Set a -> (Set a, Set a)
forall a. (a -> Bool) -> Set a -> (Set a, Set a)
Set.spanAntitone (Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
(==) Ordering
EQ (Ordering -> Bool) -> (a -> Ordering) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Ordering
f) Set a
s'
in (Set a
l,Set a
m,Set a
r)
fromListBy :: (a -> a -> Ordering) -> [a] -> Set a
fromListBy :: forall a. (a -> a -> Ordering) -> [a] -> Set a
fromListBy a -> a -> Ordering
cmp [a]
xs = (a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b.
(a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s b) -> b
withOrd a -> a -> Ordering
cmp (Set (O s a) -> O s (Set a)
forall (f :: * -> *) s a. f (O s a) -> O s (f a)
extractOrd1 (Set (O s a) -> O s (Set a))
-> ([a] -> Set (O s a)) -> [a] -> O s (Set a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [O s a] -> Set (O s a)
forall a. Ord a => [a] -> Set a
Set.fromList ([O s a] -> Set (O s a)) -> ([a] -> [O s a]) -> [a] -> Set (O s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> O s a) -> [a] -> [O s a]
forall a b. (a -> b) -> [a] -> [b]
map a -> O s a
forall s a. a -> O s a
O ([a] -> O s (Set a)) -> [a] -> O s (Set a)
forall a b. (a -> b) -> a -> b
$ [a]
xs)
join :: Set a -> Set a -> Set a
join :: forall a. Set a -> Set a -> Set a
join = Set a -> Set a -> Set a
forall a. Set a -> Set a -> Set a
Internal.merge
insertBy :: (a -> a -> Ordering) -> a -> Set a -> Set a
insertBy :: forall a. (a -> a -> Ordering) -> a -> Set a -> Set a
insertBy a -> a -> Ordering
cmp a
x Set a
s = (a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b.
(a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s b) -> b
withOrd a -> a -> Ordering
cmp ((forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b. (a -> b) -> a -> b
$ (Set (O s a) -> Set (O s a)) -> Set a -> O s (Set a)
forall (f :: * -> *) s a (g :: * -> *).
(f (O s a) -> g (O s a)) -> f a -> O s (g a)
liftOrd1 (O s a -> Set (O s a) -> Set (O s a)
forall a. Ord a => a -> Set a -> Set a
Set.insert (O s a -> Set (O s a) -> Set (O s a))
-> O s a -> Set (O s a) -> Set (O s a)
forall a b. (a -> b) -> a -> b
$ a -> O s a
forall s a. a -> O s a
O a
x) Set a
s
deleteAllBy :: (a -> a -> Ordering) -> a -> Set a -> Set a
deleteAllBy :: forall a. (a -> a -> Ordering) -> a -> Set a -> Set a
deleteAllBy a -> a -> Ordering
cmp a
x Set a
s = (a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b.
(a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s b) -> b
withOrd a -> a -> Ordering
cmp ((forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b. (a -> b) -> a -> b
$ (Set (O s a) -> Set (O s a)) -> Set a -> O s (Set a)
forall (f :: * -> *) s a (g :: * -> *).
(f (O s a) -> g (O s a)) -> f a -> O s (g a)
liftOrd1 (O s a -> Set (O s a) -> Set (O s a)
forall a. Ord a => a -> Set a -> Set a
Set.delete (O s a -> Set (O s a) -> Set (O s a))
-> O s a -> Set (O s a) -> Set (O s a)
forall a b. (a -> b) -> a -> b
$ a -> O s a
forall s a. a -> O s a
O a
x) Set a
s
toggleBy :: (a -> a -> Ordering) -> a -> Set a -> Set a
toggleBy :: forall a. (a -> a -> Ordering) -> a -> Set a -> Set a
toggleBy a -> a -> Ordering
cmp a
x Set a
s = (a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b.
(a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s b) -> b
withOrd a -> a -> Ordering
cmp ((forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a)
-> (forall s. Reifies s (OrdDict a) => O s (Set a)) -> Set a
forall a b. (a -> b) -> a -> b
$ (Set (O s a) -> Set (O s a)) -> Set a -> O s (Set a)
forall (f :: * -> *) s a (g :: * -> *).
(f (O s a) -> g (O s a)) -> f a -> O s (g a)
liftOrd1 (O s a -> Set (O s a) -> Set (O s a)
forall a. Ord a => a -> Set a -> Set a
toggle (O s a -> Set (O s a) -> Set (O s a))
-> O s a -> Set (O s a) -> Set (O s a)
forall a b. (a -> b) -> a -> b
$ a -> O s a
forall s a. a -> O s a
O a
x) Set a
s
queryBy :: (a -> a -> Ordering)
-> (forall b. Ord b => b -> Set b -> t b)
-> a -> Set a -> t a
queryBy :: forall a (t :: * -> *).
(a -> a -> Ordering)
-> (forall b. Ord b => b -> Set b -> t b) -> a -> Set a -> t a
queryBy a -> a -> Ordering
cmp forall b. Ord b => b -> Set b -> t b
fs a
q Set a
s = (a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s (t a)) -> t a
forall a b.
(a -> a -> Ordering)
-> (forall s. Reifies s (OrdDict a) => O s b) -> b
withOrd a -> a -> Ordering
cmp ((forall s. Reifies s (OrdDict a) => O s (t a)) -> t a)
-> (forall s. Reifies s (OrdDict a) => O s (t a)) -> t a
forall a b. (a -> b) -> a -> b
$ (Set (O s a) -> t (O s a)) -> Set a -> O s (t a)
forall (f :: * -> *) s a (g :: * -> *).
(f (O s a) -> g (O s a)) -> f a -> O s (g a)
liftOrd1 (O s a -> Set (O s a) -> t (O s a)
forall b. Ord b => b -> Set b -> t b
fs (O s a -> Set (O s a) -> t (O s a))
-> O s a -> Set (O s a) -> t (O s a)
forall a b. (a -> b) -> a -> b
$ a -> O s a
forall s a. a -> O s a
O a
q) Set a
s