You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
1.2 KiB
Haskell
21 lines
1.2 KiB
Haskell
2 years ago
|
module Algo.MaxRate where
|
||
|
|
||
|
import Types
|
||
|
import Heap
|
||
|
import Algo.Common
|
||
|
|
||
|
createHeap :: [Time] -> Heap Unit
|
||
|
createHeap [] = Nil
|
||
|
createHeap (t:ts) = insert (Unit (1 / (fromIntegral t)) t 1 0) $ createHeap ts -- we use speed as metric
|
||
|
|
||
|
runUntilCycle :: Int -> [Int] -> Heap Unit
|
||
|
runUntilCycle p ts = fst $ run 0 (h, emptyHeap) -- maintain two heaps - one for clients with remaining packets, one for clients without available packets
|
||
|
where h = createHeap ts
|
||
|
run :: Time -> (Heap Unit, Heap Unit) -> (Heap Unit, Heap Unit)
|
||
|
run curr_t (h, exh_h) | (mod curr_t p == 0) && (curr_t /= 0) = (h, exh_h) -- cycle found
|
||
|
| otherwise = let (Just m,h') = deleteMax h in
|
||
|
run (curr_t + period m) $ updateHeap curr_t (curr_t + period m) $ insertDecreased m h' exh_h where
|
||
|
insertDecreased el h exh_h = if (rem_p el == 1) then
|
||
|
(h, insert el {sent_p = sent_p el + 1, rem_p = 0} exh_h) -- move best client to the "exhausted" heap if his rem_p is equal to zero
|
||
|
else (insert el {sent_p = sent_p el + 1, rem_p = rem_p el - 1} h, exh_h)
|