/**
   * Returns the NameSurferEntry associated with this name, if one exists. If the name does not
   * appear in the database, this method returns null.
   */
  public NameSurferEntry findEntry(String name) {

    char firstLetter = name.charAt(0);
    if (Character.isLowerCase(firstLetter) == true) {
      firstLetter = Character.toUpperCase(firstLetter);
    }
    String remainingLetters = name.substring(1);
    remainingLetters = remainingLetters.toLowerCase();
    name = firstLetter + remainingLetters;

    if (namesDataBase.containsKey(name)) {
      return namesDataBase.get(name);
    }
    return null;
  }
Пример #2
0
 public void keyTyped(char ch) {
   if (this.emuSys != null) {
     if (this.emuSys.getSwapKeyCharCase()) {
       if (Character.isUpperCase(ch)) {
         ch = Character.toLowerCase(ch);
       } else if (Character.isLowerCase(ch)) {
         ch = Character.toUpperCase(ch);
       }
     }
     if (this.emuSys.getConvertKeyCharToISO646DE()) {
       this.emuSys.keyTyped(TextUtil.toISO646DE(ch));
     } else {
       this.emuSys.keyTyped(ch);
     }
   }
 }
  /**
   * Compare the calculate source list, with an explicit list, usually supplied from the makefile.
   * Used to detect bugs where the makefile and sjavac have different opinions on which files should
   * be compiled.
   */
  public void compareWithMakefileList(File makefileSourceList) throws ProblemException {
    // If we are building on win32 using for example cygwin the paths in the makefile source list
    // might be /cygdrive/c/.... which does not match c:\....
    // We need to adjust our calculated sources to be identical, if necessary.
    boolean mightNeedRewriting = File.pathSeparatorChar == ';';

    if (makefileSourceList == null) return;

    Set<String> calculatedSources = new HashSet<>();
    Set<String> listedSources = new HashSet<>();

    // Create a set of filenames with full paths.
    for (Source s : now.sources().values()) {
      // Don't include link only sources when comparing sources to compile
      if (!s.isLinkedOnly()) {
        calculatedSources.add(s.file().getPath());
      }
    }
    // Read in the file and create another set of filenames with full paths.
    try {
      BufferedReader in = new BufferedReader(new FileReader(makefileSourceList));
      for (; ; ) {
        String l = in.readLine();
        if (l == null) break;
        l = l.trim();
        if (mightNeedRewriting) {
          if (l.indexOf(":") == 1 && l.indexOf("\\") == 2) {
            // Everything a-ok, the format is already C:\foo\bar
          } else if (l.indexOf(":") == 1 && l.indexOf("/") == 2) {
            // The format is C:/foo/bar, rewrite into the above format.
            l = l.replaceAll("/", "\\\\");
          } else if (l.charAt(0) == '/' && l.indexOf("/", 1) != -1) {
            // The format might be: /cygdrive/c/foo/bar, rewrite into the above format.
            // Do not hardcode the name cygdrive here.
            int slash = l.indexOf("/", 1);
            l = l.replaceAll("/", "\\\\");
            l = "" + l.charAt(slash + 1) + ":" + l.substring(slash + 2);
          }
          if (Character.isLowerCase(l.charAt(0))) {
            l = Character.toUpperCase(l.charAt(0)) + l.substring(1);
          }
        }
        listedSources.add(l);
      }
    } catch (FileNotFoundException e) {
      throw new ProblemException(
          "Could not open " + makefileSourceList.getPath() + " since it does not exist!");
    } catch (IOException e) {
      throw new ProblemException("Could not read " + makefileSourceList.getPath());
    }

    for (String s : listedSources) {
      if (!calculatedSources.contains(s)) {
        throw new ProblemException(
            "The makefile listed source " + s + " was not calculated by the smart javac wrapper!");
      }
    }

    for (String s : calculatedSources) {
      if (!listedSources.contains(s)) {
        throw new ProblemException(
            "The smart javac wrapper calculated source " + s + " was not listed by the makefiles!");
      }
    }
  }
Пример #4
0
  public static void main(String args[]) throws FileNotFoundException, IOException {
    System.out.println("Gathering data...");
    // Use char[] for ease in building strings, despite only using 8 bits.
    int numElements = 65536;
    char[] info = new char[numElements];
    for (int i = 0; i < info.length; i++) {
      if (i == '\n' || i == '\r') info[i] = NEWLINE_CODE;
      else if (i == ' ' || i == '\t' || i == '\f') info[i] = SPACE_CODE;
      else if (i < 128 && Character.isLowerCase((char) i)) info[i] = LOWER_CODE; // Ascii lower case
      else if (i < 128 && Character.isUpperCase((char) i)) info[i] = UPPER_CODE; // Ascii upper case
      else if (i < 128 && Character.isDigit((char) i)) info[i] = DIGIT_CODE; // Ascii digit
      else if (Character.isJavaIdentifierStart((char) i)) info[i] = OTHER_LETTER_CODE;
      else if (Character.isJavaIdentifierPart((char) i)) info[i] = OTHER_DIGIT_CODE;
      else {
        info[i] = BAD_CODE;
        numElements--;
      }
    }

    System.out.println("Compressing tables...");
    int bestShift = 0;
    int bestEst = info.length;
    String bestBlkStr = null;

    for (int i = 3; i < 11; i++) {
      int blkSize = 1 << i;
      Map blocks = new HashMap();
      List blkArray = new ArrayList();
      System.out.print("shift: " + i);

      for (int j = 0; j < info.length; j += blkSize) {
        String key = new String(info, j, blkSize);
        if (blocks.get(key) == null) {
          blkArray.add(key);
          blocks.put(key, new Integer(blkArray.size()));
        }
      }
      int blkNum = blkArray.size();
      int blockLen = blkNum * blkSize;
      System.out.print(" before " + blockLen);

      //
      // Try to pack blkArray, by finding successively smaller matches
      // between heads and tails of blocks.
      //
      for (int j = blkSize - 1; j > 0; j--) {
        Map tails = new HashMap();
        for (int k = 0; k < blkArray.size(); k++) {
          String str = (String) blkArray.get(k);
          if (str == null) continue;
          String tail = str.substring(str.length() - j);
          List l = (List) tails.get(tail);
          if (l == null) tails.put(tail, new LinkedList(Collections.singleton(new Integer(k))));
          else l.add(new Integer(k));
        }

        //
        // Now calculate the heads, and merge overlapping blocks
        //
        block:
        for (int k = 0; k < blkArray.size(); k++) {
          String tomerge = (String) blkArray.get(k);
          if (tomerge == null) continue;
          while (true) {
            String head = tomerge.substring(0, j);
            LinkedList entry = (LinkedList) tails.get(head);
            if (entry == null) continue block;
            Integer other = (Integer) entry.removeFirst();
            if (other.intValue() == k) {
              if (entry.size() > 0) {
                entry.add(other);
                other = (Integer) entry.removeFirst();
              } else {
                entry.add(other);
                continue block;
              }
            }
            if (entry.size() == 0) tails.remove(head);

            //
            // A match was found.
            //
            String merge = blkArray.get(other.intValue()) + tomerge.substring(j);
            blockLen -= j;
            blkNum--;
            if (other.intValue() < k) {
              blkArray.set(k, null);
              blkArray.set(other.intValue(), merge);
              String tail = merge.substring(merge.length() - j);
              List l = (List) tails.get(tail);
              Collections.replaceAll(l, new Integer(k), other);
              continue block;
            }
            blkArray.set(k, merge);
            blkArray.set(other.intValue(), null);
            tomerge = merge;
          }
        }
      }
      StringBuffer blockStr = new StringBuffer(blockLen);
      for (int k = 0; k < blkArray.size(); k++) {
        String str = (String) blkArray.get(k);
        if (str != null) blockStr.append(str);
      }
      if (blockStr.length() != blockLen) throw new Error("Unexpected blockLen " + blockLen);
      int estimate = blockLen + (info.length >> (i - 1));
      System.out.println(" after merge " + blockLen + ": " + estimate + " bytes");
      if (estimate < bestEst) {
        bestEst = estimate;
        bestShift = i;
        bestBlkStr = blockStr.toString();
      }
    }

    int blkSize = 1 << bestShift;
    char[] blocks = new char[info.length / blkSize];
    for (int j = 0; j < info.length; j += blkSize) {
      String key = new String(info, j, blkSize);
      int index = bestBlkStr.indexOf(key);
      if (index == -1) throw new Error("Unexpected index for " + j);
      blocks[j >> bestShift] = (char) (index - j);
    }

    //
    // Process the code.h file
    //
    System.out.println("Generating code.h with shift of " + bestShift);
    PrintStream hfile = new PrintStream(new FileOutputStream("code.h"));
    printHeader(hfile, new String[] {"\"platform.h\""});
    hfile.println("#ifndef code_INCLUDED");
    hfile.println("#define code_INCLUDED");
    hfile.println();
    hfile.println("class Code");
    hfile.println("{");
    hfile.println("    //");
    hfile.println("    // To facilitate the scanning, the character set is partitioned into");
    hfile.println("    // 8 categories using the array CODE. These are described below");
    hfile.println("    // together with some self-explanatory functions defined on CODE.");
    hfile.println("    //");
    hfile.println("    enum {");
    hfile.println("        SHIFT = " + bestShift + ",");
    hfile.println("        NEWLINE_CODE = " + NEWLINE_CODE + ',');
    hfile.println("        SPACE_CODE = " + SPACE_CODE + ',');
    hfile.println("        BAD_CODE = " + BAD_CODE + ',');
    hfile.println("        DIGIT_CODE = " + DIGIT_CODE + ',');
    hfile.println("        OTHER_DIGIT_CODE = " + OTHER_DIGIT_CODE + ',');
    hfile.println("        LOWER_CODE = " + LOWER_CODE + ',');
    hfile.println("        UPPER_CODE = " + UPPER_CODE + ',');
    hfile.println("        OTHER_LETTER_CODE = " + OTHER_LETTER_CODE);
    hfile.println("    };");
    hfile.println();
    hfile.println("    static char codes[" + bestBlkStr.length() + "];");
    hfile.println("    static u2 blocks[" + blocks.length + "];");
    hfile.println();
    hfile.println();
    hfile.println("public:");
    hfile.println();
    hfile.println("    static inline void SetBadCode(wchar_t c)");
    hfile.println("    {");
    hfile.println("        codes[(u2) (blocks[c >> SHIFT] + c)] = BAD_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline void CodeCheck(wchar_t c)");
    hfile.println("    {");
    hfile.println("        assert((u2) (blocks[c >> SHIFT] + c) < " + bestBlkStr.length() + ");");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool CodeCheck(void)");
    hfile.println("    {");
    hfile.println("        for (int i = 0; i <= 0xffff; i++)");
    hfile.println("            CodeCheck((wchar_t) i);");
    hfile.println("        return true;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    //");
    hfile.println("    // \\r characters are replaced by \\x0a in Stream::ProcessInput().");
    hfile.println("    //");
    hfile.println("    static inline bool IsNewline(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return c == '\\x0a';");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsSpaceButNotNewline(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == SPACE_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsSpace(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] <= SPACE_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsDigit(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == DIGIT_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsOctalDigit(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return c >= U_0 && c <= U_7;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsHexDigit(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return c <= U_f && (c >= U_a ||");
    hfile.println("                            (c >= U_A && c <= U_F) ||");
    hfile.println("                            (c >= U_0 && c <= U_9));");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsUpper(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == UPPER_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsLower(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == LOWER_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsAlpha(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] >= LOWER_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsAlnum(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] >= DIGIT_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("};");
    hfile.println();
    hfile.println("#endif // code_INCLUDED");
    printFooter(hfile);
    hfile.close();

    //
    // Process the code.cpp file
    //
    System.out.println("Generating code.cpp");
    PrintStream cfile = new PrintStream(new FileOutputStream("code.cpp"));
    printHeader(cfile, new String[] {"\"code.h\""});
    cfile.println("char Code::codes[" + bestBlkStr.length() + "] =");
    cfile.println("{");
    for (int j = 0; j < bestBlkStr.length(); j += 4) {
      for (int k = 0; k < 4; k++) {
        if (k + j >= bestBlkStr.length()) break;
        if (k == 0) cfile.print("   ");
        cfile.print(" " + CODE_NAMES[bestBlkStr.charAt(k + j)] + ",");
      }
      cfile.println();
    }
    cfile.println("};");
    cfile.println();
    cfile.println();
    cfile.println("//");
    cfile.println("// The Blocks vector:");
    cfile.println("//");
    cfile.println("u2 Code::blocks[" + blocks.length + "] =");
    cfile.println("{");
    for (int k = 0; k < blocks.length; k += 9) {
      for (int i = 0; i < 9; i++) {
        if (k + i >= blocks.length) break;
        if (i == 0) cfile.print("   ");
        cfile.print(" 0x" + Integer.toHexString(blocks[k + i]) + ",");
      }
      cfile.println();
    }
    cfile.println("};");
    printFooter(cfile);
    cfile.close();

    //
    // Print statistics.
    //
    System.out.println(
        "Total static storage utilization is " + blocks.length * 2 + " bytes for block lookup");
    System.out.println("   plus " + bestBlkStr.length() + " bytes for the encodings");
    System.out.println(
        "The number of unicode characters legal in Java sourcecode is " + numElements);
  }
Пример #5
0
  public static void main(String[] args) throws Exception {

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    String line;

    HashMap<String, HashSet<String>> projectToStudents;
    HashMap<String, Integer> projectToCount;

    String currProject = null;
    StringTokenizer st;
    StringBuilder sb = new StringBuilder();

    ArrayList<String> studentsLastRound = new ArrayList<String>(2);
    // HashSet<String> votingStudents;
    HashSet<String> participatedStudents;
    HashSet<String> illegal;

    int voteCount;
    // long start = System.currentTimeMillis();

    boolean startProject = true;
    while ((line = input.readLine()) != null && line.charAt(0) != '0') {

      projectToCount = new HashMap<String, Integer>(100);
      projectToStudents = new HashMap<String, HashSet<String>>(100);
      participatedStudents = new HashSet<String>(10000);
      illegal = new HashSet<String>(10);

      while (line.charAt(0) != '1') {
        if (Character.isUpperCase(line.charAt(0))) {
          // if (currProject != null) {
          //	projectToCount.put(currProject, projectToCount.get(currProject) +
          //		votingStudents.size());
          // }

          // for (String voter : votingStudents) {
          //	participatedStudents.add(voter);
          // }

          projectToStudents.put(line, new HashSet<String>());
          // projectToCount.put(line, 0);
          // votingStudents = new HashMap<String>();
          currProject = line;
          startProject = true;
        } else if (Character.isLowerCase(line.charAt(0))) {
          if (startProject) {
            for (String voter : studentsLastRound) {
              participatedStudents.add(voter);
            }

            studentsLastRound.clear();
            startProject = false;
          }

          if (!participatedStudents.contains(line)) {
            projectToStudents.get(currProject).add(line);
            // votingStudents.add(line);
            studentsLastRound.add(line);
          } else {
            illegal.add(line);
          }
        } else {
          break;
        }

        line = input.readLine();
      }

      for (Map.Entry<String, HashSet<String>> entry : projectToStudents.entrySet()) {
        voteCount = entry.getValue().size();
        for (String disqualified : illegal) {
          if (entry.getValue().contains(disqualified)) {
            voteCount--;
          }
        }
        projectToCount.put(entry.getKey(), voteCount);
      }

      Set<Map.Entry<String, Integer>> countEntries = /*new ArrayList<Map.Entry<String, Integer>>(*/
          projectToCount.entrySet();
      Collections.sort(
          countEntries,
          new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
              if (e1.getValue() == e2.getValue()) {
                return e1.getKey().compareTo(e2.getKey());
              } else {
                return -1 * e1.getValue().compareTo(e2.getValue());
              }
            }
          });

      for (Map.Entry<String, Integer> projectCount : countEntries) {
        sb.append(projectCount.toString() + "\n");
      }
    }

    System.out.println(sb.toString());
  }