Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class Capacity(cores: Int, ram: Int, bandWidth: Int) {
- def +(other: Capacity) = Capacity(cores + other.cores, ram + other.ram, bandWidth + other.bandWidth)
- def -(other: Capacity) = Capacity(cores - other.cores, ram - other.ram, bandWidth - other.bandWidth)
- def >=(other: Capacity) = cores >= other.cores && ram >= other.ram && bandWidth >= other.bandWidth
- }
- case class PhysicalServer(capacity: Capacity)
- case class VirtualServer(demand: Capacity)
- case class ServerMapping(physicalServer: PhysicalServer, virtualServers: List[VirtualServer]) {
- val remainingCapacity = physicalServer.capacity - virtualServers
- .foldLeft(Capacity(0, 0, 0))((sum, next) => sum + next.demand)
- def :+(vServer: VirtualServer) = ServerMapping(physicalServer, virtualServers :+ vServer)
- }
- object FirstFit {
- def apply(serverSpec: PhysicalServer, vServers: List[VirtualServer]): List[ServerMapping] = {
- vServers.foldLeft(Nil: List[ServerMapping]) { (currentMappings, vServer) =>
- // check current mappings, else add new
- currentMappings.view.zipWithIndex.find(_._1.remainingCapacity >= vServer.demand)
- .map(mapping => currentMappings.updated(mapping._2, mapping._1 :+ vServer))
- .getOrElse(currentMappings :+ ServerMapping(serverSpec, List(vServer)))
- }
- }
- }
- object ServerMapperApp extends App {
- val virtualServers = List(
- VirtualServer(Capacity(2, 3, 2)),
- VirtualServer(Capacity(2, 4, 2)),
- VirtualServer(Capacity(2, 3, 2))
- )
- val physicalServerSpec = PhysicalServer(Capacity(4, 6, 4))
- FirstFit(physicalServerSpec, virtualServers)
- .foreach(println)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement