public void run() { Deque<VisitingContext> stack = new ArrayDeque<>(); stack.add(new VisitingContext(rootNode)); boolean goOn = stack.peek().hasNext(); if (goOn) { do { Map.Entry<String, JsonValue> current = stack.peek().nextElement(); if (!(current.getValue() instanceof JsonStructure)) { String key = stack.peek().getNSPrefix() + current.getKey(); String value = null; JsonValue jsonValue = current.getValue(); switch (jsonValue.getValueType()) { case NULL: value = null; break; case FALSE: value = Boolean.FALSE.toString(); break; case TRUE: Boolean.TRUE.toString(); break; case NUMBER: value = jsonValue.toString(); break; case STRING: value = ((JsonString) jsonValue).getString(); break; default: throw new ConfigException("Internal failure while processing JSON document."); } targetStore.put(key, value); } else if (current.getValue() instanceof JsonObject) { String key = stack.peek().getNSPrefix() + current.getKey(); JsonObject node = (JsonObject) current.getValue(); stack.push(new VisitingContext(node, key)); } else if (current.getValue() instanceof JsonArray) { throw new ConfigException("Arrays are not supported at the moment."); } else { throw new ConfigException("Internal failure while processing JSON document."); } goOn = stack.peek().hasNext(); while (!goOn && stack.size() > 0) { stack.remove(); goOn = (stack.size() > 0) && stack.peek().hasNext(); } } while (goOn); } }
@Override protected AFCalculationResult computeLog10PNonRef( final VariantContext vc, final int defaultPloidy, final double[] log10AlleleFrequencyPriors, final StateTracker stateTracker) { Utils.nonNull(vc, "vc is null"); Utils.nonNull(log10AlleleFrequencyPriors, "log10AlleleFrequencyPriors is null"); Utils.nonNull(stateTracker, "stateTracker is null"); final int numAlternateAlleles = vc.getNAlleles() - 1; final List<double[]> genotypeLikelihoods = getGLs(vc.getGenotypes(), true); final int numSamples = genotypeLikelihoods.size() - 1; final int numChr = 2 * numSamples; // queue of AC conformations to process final Deque<ExactACset> ACqueue = new LinkedList<>(); // mapping of ExactACset indexes to the objects final Map<ExactACcounts, ExactACset> indexesToACset = new HashMap<>(numChr + 1); // add AC=0 to the queue final int[] zeroCounts = new int[numAlternateAlleles]; final ExactACset zeroSet = new ExactACset(numSamples + 1, new ExactACcounts(zeroCounts)); ACqueue.add(zeroSet); indexesToACset.put(zeroSet.getACcounts(), zeroSet); while (!ACqueue.isEmpty()) { // compute log10Likelihoods final ExactACset set = ACqueue.remove(); calculateAlleleCountConformation( set, genotypeLikelihoods, numChr, ACqueue, indexesToACset, log10AlleleFrequencyPriors, stateTracker); // clean up memory indexesToACset.remove(set.getACcounts()); } return getResultFromFinalState(vc, log10AlleleFrequencyPriors, stateTracker); }
int countMachines(int start) { int count = 0; List<Edge> E2 = new ArrayList<Edge>(); Deque<Integer> D = new ArrayDeque<>(); boolean[] V = new boolean[G.size()]; V[start] = true; D.add(start); if (K[start]) { count++; } while (!D.isEmpty()) { int a = D.remove(); for (Edge e : G.get(a)) { if (e.w == 0) { continue; } ; E2.add(e); int b = (e.a == a) ? e.b : e.a; if (!V[b]) { V[b] = true; D.add(b); if (K[b]) { count++; if (count > 1) { return count; } } } } } if (count <= 1) { for (Edge e : E2) { e.w = 0; } } return count; }
/** * Runs the server. The server will wait for some match info (which specifies the teams and set of * maps to run) and then begin running matches. */ public void run() { int aWins = 0, bWins = 0; while (!matches.isEmpty()) { Match match = matches.peek(); if (!finished.isEmpty()) match.setInitialTeamMemory(finished.getLast().getComputedTeamMemory()); try { debug("running match " + match); match.initialize(); runMatch(match, proxyWriter); finished.add(match); matches.remove(match); if (match.getWinner() == Team.A) aWins++; else if (match.getWinner() == Team.B) bWins++; match.finish(); // Allow best of three scrimmages -- single game scrims should still work fine // TODO:This "win mode" should probably be something from the database if (mode == Mode.TOURNAMENT || mode == Mode.SCRIMMAGE) { if (aWins == 2 || bWins == 2) break; } } catch (Exception e) { ErrorReporter.report(e); error("couldn't run match: "); this.state = State.ERROR; } } proxyWriter.terminate(); }
public CommentsCollection parse(final InputStream in, final String charsetName) throws IOException, UnsupportedEncodingException { boolean lastWasASlashR = false; BufferedReader br = new BufferedReader(new InputStreamReader(in, charsetName)); CommentsCollection comments = new CommentsCollection(); int r; Deque prevTwoChars = new LinkedList<Character>(Arrays.asList('z', 'z')); State state = State.CODE; LineComment currentLineComment = null; BlockComment currentBlockComment = null; StringBuffer currentContent = null; int currLine = 1; int currCol = 1; while ((r = br.read()) != -1) { char c = (char) r; if (c == '\r') { lastWasASlashR = true; } else if (c == '\n' && lastWasASlashR) { lastWasASlashR = false; continue; } else { lastWasASlashR = false; } switch (state) { case CODE: if (prevTwoChars.peekLast().equals('/') && c == '/') { currentLineComment = new LineComment(); currentLineComment.setBeginLine(currLine); currentLineComment.setBeginColumn(currCol - 1); state = State.IN_LINE_COMMENT; currentContent = new StringBuffer(); } else if (prevTwoChars.peekLast().equals('/') && c == '*') { currentBlockComment = new BlockComment(); currentBlockComment.setBeginLine(currLine); currentBlockComment.setBeginColumn(currCol - 1); state = State.IN_BLOCK_COMMENT; currentContent = new StringBuffer(); } else if (c == '"') { state = State.IN_STRING; } else { // nothing to do } break; case IN_LINE_COMMENT: if (c == '\n' || c == '\r') { currentLineComment.setContent(currentContent.toString()); currentLineComment.setEndLine(currLine); currentLineComment.setEndColumn(currCol); comments.addComment(currentLineComment); state = State.CODE; } else { currentContent.append(c); } break; case IN_BLOCK_COMMENT: if (prevTwoChars.peekLast().equals('*') && c == '/' && !prevTwoChars.peekFirst().equals('/')) { // delete last character String content = currentContent.deleteCharAt(currentContent.toString().length() - 1).toString(); if (content.startsWith("*")) { JavadocComment javadocComment = new JavadocComment(); javadocComment.setContent(content.substring(1)); javadocComment.setBeginLine(currentBlockComment.getBeginLine()); javadocComment.setBeginColumn(currentBlockComment.getBeginColumn()); javadocComment.setEndLine(currLine); javadocComment.setEndColumn(currCol + 1); comments.addComment(javadocComment); } else { currentBlockComment.setContent(content); currentBlockComment.setEndLine(currLine); currentBlockComment.setEndColumn(currCol + 1); comments.addComment(currentBlockComment); } state = State.CODE; } else { currentContent.append(c == '\r' ? '\n' : c); } break; case IN_STRING: if (!prevTwoChars.peekLast().equals('\\') && c == '"') { state = State.CODE; } break; default: throw new RuntimeException("Unexpected"); } switch (c) { case '\n': case '\r': currLine += 1; currCol = 1; break; case '\t': currCol += COLUMNS_PER_TAB; break; default: currCol += 1; } prevTwoChars.remove(); prevTwoChars.add(c); } if (state == State.IN_LINE_COMMENT) { currentLineComment.setContent(currentContent.toString()); currentLineComment.setEndLine(currLine); currentLineComment.setEndColumn(currCol); comments.addComment(currentLineComment); } return comments; }
private MapLocation findGoodConstructionLocation() throws GameActionException { // want a location near the HQ // that doesn't have many voids around it // idea; consider all locations near us // do a fancy sort // sort by distance to enemy HQ // prefer bigger distances Direction[] sortedDirections = allDirections.clone(); sort( sortedDirections, new Comparator<Direction>() { public int compare(Direction o1, Direction o2) { return new Integer(myLocation.add(o2).distanceSquaredTo(enemyHqLocation)) .compareTo(myLocation.add(o1).distanceSquaredTo(enemyHqLocation)); } }); Deque<MapLocation> possibleLocations = new ArrayDeque<MapLocation>(); Set<MapLocation> visitedLocations = new HashSet<MapLocation>(); visitedLocations.add(myLocation); for (Direction direction : sortedDirections) { MapLocation possibleLocation = myLocation.add(direction); if (gameMap.isTraversable(possibleLocation.x, possibleLocation.y)) { possibleLocations.add(possibleLocation); visitedLocations.add(possibleLocation); } } MapLocation bestLocation = null; int bestFreeTiles = 0; boolean found = false; int i = 0; while (!found && !possibleLocations.isEmpty()) { MapLocation currentLocation = possibleLocations.remove(); int freeTiles = 0; for (Direction direction : sortedDirections) { MapLocation possibleLocation = currentLocation.add(direction); if (gameMap.isTraversable(possibleLocation.x, possibleLocation.y)) { freeTiles++; if (!visitedLocations.contains(possibleLocation)) { possibleLocations.add(possibleLocation); visitedLocations.add(possibleLocation); } } } if (freeTiles > 4) { boolean canBuild = true; // check if there is a construction there Robot robot = (Robot) rc.senseObjectAtLocation(currentLocation); if (robot != null) { RobotInfo info = rc.senseRobotInfo(robot); if (info.type != SOLDIER) { canBuild = false; } } if (canBuild && freeTiles > bestFreeTiles) { bestLocation = currentLocation; bestFreeTiles = freeTiles; } } i++; if (bestFreeTiles > 6 || i > 16) { break; } } return bestLocation; }