{-# LANGUAGE FlexibleContexts #-}
module Q.SortedVector
  (
    fromList
  , fromVector
  , fromSortedList
  , SortedVector(..)
  , minElement
  , maxElement
  ) where

import qualified Data.Vector.Algorithms.Merge  as V (sort)

import           Data.Vector.Storable  (Storable)
import qualified Data.Vector.Storable as V (Vector (..), fromList, length, head, last, modify)
import           Q.Types

newtype SortedVector a = SortedVector (V.Vector a)

fromList :: [a] -> SortedVector a
fromList [a]
as = Vector a -> SortedVector a
forall a. Vector a -> SortedVector a
SortedVector ((forall s. MVector s a -> ST s ()) -> Vector a -> Vector a
forall a.
Storable a =>
(forall s. MVector s a -> ST s ()) -> Vector a -> Vector a
V.modify forall s. MVector s a -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> m ()
V.sort (Vector a -> Vector a) -> Vector a -> Vector a
forall a b. (a -> b) -> a -> b
$ [a] -> Vector a
forall a. Storable a => [a] -> Vector a
V.fromList [a]
as)
fromVector :: Vector a -> SortedVector a
fromVector Vector a
v = Vector a -> SortedVector a
forall a. Vector a -> SortedVector a
SortedVector ((forall s. MVector s a -> ST s ()) -> Vector a -> Vector a
forall a.
Storable a =>
(forall s. MVector s a -> ST s ()) -> Vector a -> Vector a
V.modify forall s. MVector s a -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e, Ord e) =>
v (PrimState m) e -> m ()
V.sort Vector a
v)
fromSortedList :: [a] -> SortedVector a
fromSortedList [a]
xs = Vector a -> SortedVector a
forall a. Vector a -> SortedVector a
SortedVector (Vector a -> SortedVector a) -> Vector a -> SortedVector a
forall a b. (a -> b) -> a -> b
$ [a] -> Vector a
forall a. Storable a => [a] -> Vector a
V.fromList [a]
xs


minElement :: SortedVector a -> a
minElement (SortedVector Vector a
v) = Vector a -> a
forall a. Storable a => Vector a -> a
V.head Vector a
v
maxElement :: SortedVector a -> a
maxElement (SortedVector Vector a
v) = Vector a -> a
forall a. Storable a => Vector a -> a
V.last Vector a
v