--------------------------------------------------------------------------------
-- |
-- Module      :  HGeometry.Set.NonEmpty
-- Copyright   :  (C) Frank Staals
-- License     :  see the LICENSE file
-- Maintainer  :  Frank Staals
--
-- Utilities for working with NonEmpty sets
--
--------------------------------------------------------------------------------
module HGeometry.Set.NonEmpty
  ( extractMinimaBy

  ) where

import HGeometry.NonEmpty.Util qualified as NonEmpty
import Data.Set.NonEmpty qualified as NESet
import HGeometry.Ext
import Data.Set qualified as Set
import Data.Bifunctor

--------------------------------------------------------------------------------

-- | Extract the smallest element according to the given ordering form the set.
extractMinimaBy     :: (a -> a -> Ordering) -> NESet.NESet a -> NESet.NESet a :+ Set.Set a
extractMinimaBy :: forall a. (a -> a -> Ordering) -> NESet a -> NESet a :+ Set a
extractMinimaBy a -> a -> Ordering
cmp = (NonEmpty a -> NESet a)
-> ([a] -> Set a) -> (NonEmpty a :+ [a]) -> NESet a :+ Set a
forall a b c d. (a -> b) -> (c -> d) -> (a :+ c) -> b :+ d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap NonEmpty a -> NESet a
forall a. NonEmpty a -> NESet a
NESet.fromDistinctAscList [a] -> Set a
forall a. [a] -> Set a
Set.fromDistinctAscList
                    ((NonEmpty a :+ [a]) -> NESet a :+ Set a)
-> (NESet a -> NonEmpty a :+ [a]) -> NESet a -> NESet a :+ Set a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a :+ [a]
forall a. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a :+ [a]
NonEmpty.extractMinimaBy a -> a -> Ordering
cmp (NonEmpty a -> NonEmpty a :+ [a])
-> (NESet a -> NonEmpty a) -> NESet a -> NonEmpty a :+ [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NESet a -> NonEmpty a
forall a. NESet a -> NonEmpty a
NESet.toAscList