From: adam Date: Mon, 3 Nov 2008 10:17:01 +0000 (+0100) Subject: add Context.allocateShip() X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=1bd87628c05e90b85a212816bfd7c33c5dbc2eb2;p=fleet.git add Context.allocateShip() --- diff --git a/src/edu/berkeley/fleet/ir/Context.java b/src/edu/berkeley/fleet/ir/Context.java index 83a2806..616d25b 100644 --- a/src/edu/berkeley/fleet/ir/Context.java +++ b/src/edu/berkeley/fleet/ir/Context.java @@ -46,6 +46,23 @@ public class Context { public final Fleet fleet; public Context(Fleet fleet) { this.fleet = fleet; } + // FIXME: ability for a group of Contexts to share an "allocation + // pool" because they will run concurrently. + public HashSet allocatedShips = new HashSet(); + + public Ship allocateShip(String name) { + for(int i=0; ; i++) { + Ship ship = fleet.getShip(name, i); + if (ship==null) + throw new RuntimeException("no more ships of type " + name); + if (allocatedShips.contains(ship)) continue; + allocatedShips.add(ship); + return ship; + } + } + + // FIXME: is it legitimate to send a torpedo to a count==1 loop? + // FIXME: is it legitimate to send a torpedo to a count>1 loop? /** * * A helper class for building loops of instructions.