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 (..))
data BatchSelection a b = BatchSelection
{ forall a b. BatchSelection a b -> [a]
selectedRequests :: [a]
, forall a b. BatchSelection a b -> Int
requestsLeft :: Int
, forall a b. BatchSelection a b -> b
selectedBuild :: b
, forall a b. BatchSelection a b -> Maybe CapacityLimit
nextCapacity :: Maybe CapacityLimit
}
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)
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))
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))