module Data.Set.Util where
import Data.DynamicOrd
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Set.Internal as Internal
import Data.Ord (comparing)
data S = S String deriving Int -> S -> ShowS
[S] -> ShowS
S -> String
(Int -> S -> ShowS) -> (S -> String) -> ([S] -> ShowS) -> Show S
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [S] -> ShowS
$cshowList :: [S] -> ShowS
show :: S -> String
$cshow :: S -> String
showsPrec :: Int -> S -> ShowS
$cshowsPrec :: Int -> S -> ShowS
Show
cmpS :: S -> S -> Ordering
cmpS :: S -> S -> Ordering
cmpS = (S -> Int) -> S -> S -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (\(S String
s) -> String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s)
splitOn :: Ord b => (a -> b) -> b -> Set a -> (Set a, Set a, Set a)
splitOn :: (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 :: (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 :: (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 :: 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 :: (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 :: (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
queryBy :: (a -> a -> Ordering)
-> (forall b. Ord b => b -> Set b -> t b)
-> a -> Set a -> t a
queryBy :: (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
test :: Maybe S
test = (S -> S -> Ordering)
-> (forall b. Ord b => b -> Set b -> Maybe b)
-> S
-> Set S
-> Maybe S
forall a (t :: * -> *).
(a -> a -> Ordering)
-> (forall b. Ord b => b -> Set b -> t b) -> a -> Set a -> t a
queryBy S -> S -> Ordering
cmpS forall b. Ord b => b -> Set b -> Maybe b
Set.lookupGE (String -> S
S String
"22") (Set S -> Maybe S) -> Set S -> Maybe S
forall a b. (a -> b) -> a -> b
$ (S -> S -> Ordering) -> [S] -> Set S
forall a. (a -> a -> Ordering) -> [a] -> Set a
fromListBy S -> S -> Ordering
cmpS [String -> S
S String
"a" , String -> S
S String
"bbb" , String -> S
S String
"ddddddd"]