{- |
Module      : Singular.State.Batch
Description : Order public requests and find their greatest buildable prefix
License     : Apache-2.0

Selection reads only the supplied values. Every larger prefix is tried before
the selected one, so a later success cannot be missed by assuming a cost law.
Only a typed capacity refusal permits a shorter attempt.
-}
module Singular.State.Batch
    ( BatchSelection (..)
    , orderByTake
    , largestFittingPrefix
    ) where

import Data.List (sortOn)
import Data.Ord (Down (..))
import Data.Word (Word32)

import Singular.State.Refusal (CapacityLimit, Refusal (..))

-- | The selected prefix, the rest counted, and the actual builder result.
data BatchSelection a b = BatchSelection
    { forall a b. BatchSelection a b -> [a]
selectedRequests :: [a]
    -- ^ Every request included in the successful probe, in its build order.
    , forall a b. BatchSelection a b -> Int
requestsLeft :: Int
    -- ^ All supplied requests beyond the selected prefix, including the cap.
    , forall a b. BatchSelection a b -> b
selectedBuild :: b
    -- ^ The result returned by that successful probe.
    , forall a b. BatchSelection a b -> Maybe CapacityLimit
nextCapacity :: Maybe CapacityLimit
    -- ^ The actual refusal of the next prefix, absent at the whole set or cap.
    }
    deriving stock (BatchSelection a b -> BatchSelection a b -> Bool
(BatchSelection a b -> BatchSelection a b -> Bool)
-> (BatchSelection a b -> BatchSelection a b -> Bool)
-> Eq (BatchSelection a b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b.
(Eq a, Eq b) =>
BatchSelection a b -> BatchSelection a b -> Bool
$c== :: forall a b.
(Eq a, Eq b) =>
BatchSelection a b -> BatchSelection a b -> Bool
== :: BatchSelection a b -> BatchSelection a b -> Bool
$c/= :: forall a b.
(Eq a, Eq b) =>
BatchSelection a b -> BatchSelection a b -> Bool
/= :: BatchSelection a b -> BatchSelection a b -> Bool
Eq, Int -> BatchSelection a b -> ShowS
[BatchSelection a b] -> ShowS
BatchSelection a b -> String
(Int -> BatchSelection a b -> ShowS)
-> (BatchSelection a b -> String)
-> ([BatchSelection a b] -> ShowS)
-> Show (BatchSelection a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> BatchSelection a b -> ShowS
forall a b. (Show a, Show b) => [BatchSelection a b] -> ShowS
forall a b. (Show a, Show b) => BatchSelection a b -> String
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> BatchSelection a b -> ShowS
showsPrec :: Int -> BatchSelection a b -> ShowS
$cshow :: forall a b. (Show a, Show b) => BatchSelection a b -> String
show :: BatchSelection a b -> String
$cshowList :: forall a b. (Show a, Show b) => [BatchSelection a b] -> ShowS
showList :: [BatchSelection a b] -> ShowS
Show)

-- | Greatest take first; equal takes use the output reference ascending.
orderByTake
    :: (Ord bid, Ord reference)
    => (a -> bid) -> (a -> reference) -> [a] -> [a]
orderByTake :: forall bid reference a.
(Ord bid, Ord reference) =>
(a -> bid) -> (a -> reference) -> [a] -> [a]
orderByTake a -> bid
takeOf a -> reference
referenceOf = (a -> (Down bid, reference)) -> [a] -> [a]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (\a
request -> (bid -> Down bid
forall a. a -> Down a
Down (a -> bid
takeOf a
request), a -> reference
referenceOf a
request))

{- | Try prefixes in descending size, stopping on the first success or any
noncapacity refusal. The next-prefix boundary is the immediately preceding
failed probe; all still larger prefixes have also been refused for capacity.
-}
largestFittingPrefix
    :: (Monad m)
    => ([a] -> m (Either Refusal b))
    -> Maybe Word32
    -> [a]
    -> m (Either Refusal (BatchSelection a b))
largestFittingPrefix :: forall (m :: * -> *) a b.
Monad m =>
([a] -> m (Either Refusal b))
-> Maybe Word32 -> [a] -> m (Either Refusal (BatchSelection a b))
largestFittingPrefix [a] -> m (Either Refusal b)
build Maybe Word32
cap [a]
requests = Int
-> Maybe CapacityLimit -> m (Either Refusal (BatchSelection a b))
go Int
bounded Maybe CapacityLimit
forall a. Maybe a
Nothing
  where
    count :: Int
count = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
requests
    bounded :: Int
bounded =
        Integer -> Int
forall a. Num a => Integer -> a
fromInteger
            (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
count) (Integer -> (Word32 -> Integer) -> Maybe Word32 -> Integer
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
count) Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Maybe Word32
cap))
    go :: Int
-> Maybe CapacityLimit -> m (Either Refusal (BatchSelection a b))
go Int
0 Maybe CapacityLimit
boundary = Either Refusal (BatchSelection a b)
-> m (Either Refusal (BatchSelection a b))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Refusal -> Either Refusal (BatchSelection a b)
forall a b. a -> Either a b
Left (Refusal
-> (CapacityLimit -> Refusal) -> Maybe CapacityLimit -> Refusal
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Refusal
NothingToFold CapacityLimit -> Refusal
CapacityExceeded Maybe CapacityLimit
boundary))
    go Int
size Maybe CapacityLimit
boundary = do
        let prefix :: [a]
prefix = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
size [a]
requests
        result <- [a] -> m (Either Refusal b)
build [a]
prefix
        case result of
            Left (CapacityExceeded CapacityLimit
limit) -> Int
-> Maybe CapacityLimit -> m (Either Refusal (BatchSelection a b))
go (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (CapacityLimit -> Maybe CapacityLimit
forall a. a -> Maybe a
Just CapacityLimit
limit)
            Left Refusal
refusal -> Either Refusal (BatchSelection a b)
-> m (Either Refusal (BatchSelection a b))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Refusal -> Either Refusal (BatchSelection a b)
forall a b. a -> Either a b
Left Refusal
refusal)
            Right b
built -> Either Refusal (BatchSelection a b)
-> m (Either Refusal (BatchSelection a b))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BatchSelection a b -> Either Refusal (BatchSelection a b)
forall a b. b -> Either a b
Right ([a] -> Int -> b -> Maybe CapacityLimit -> BatchSelection a b
forall a b.
[a] -> Int -> b -> Maybe CapacityLimit -> BatchSelection a b
BatchSelection [a]
prefix (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
size) b
built Maybe CapacityLimit
boundary))