/** * Get dependencies of a source file. * * @param path The canonical path of source file. * @return Path of dependencies. */ private ArrayList<String> getDependencies(String path) { if (!dependenceMap.containsKey(path)) { ArrayList<String> dependencies = new ArrayList<String>(); Matcher m = PATTERN_REQUIRE.matcher(read(path, charset)); while (m.find()) { // Decide which root path to use. // Path wrapped in <> is related to root path. // Path wrapped in "" is related to parent folder of the source file. String root = null; if (m.group(1).equals("<")) { root = this.root; } else { root = new File(path).getParent(); } // Get path of required file. String required = m.group(2); File f = new File(root, required); if (f.exists()) { dependencies.add(canonize(f)); } else { App.exit("Cannot find required file " + required + " in " + path); } } dependenceMap.put(path, dependencies); } return dependenceMap.get(path); }
/** * Combine seed file with its' dependencies. * * @param seed The seed file. * @return Output queue with correct dependencies order. */ public ArrayList<String> combo(File seed) { Stack<ArrayList<String>> tree = new Stack<ArrayList<String>>(); ArrayList<String> root = new ArrayList<String>(); ArrayList<String> output = new ArrayList<String>(); // Construct the initial tree. root.add(canonize(seed)); tree.add(root); // Travel the dependencies tree from the seed file. travel(tree, new Stack<String>(), output); return output; }
/** * Checks for the next item. * * @return result of check * @throws IOException I/O exception */ public boolean more() throws IOException { if (cache == null) { out.write(4); send(id); cache = new ArrayList<byte[]>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); while (in.read() > 0) { receive(in, os); cache.add(os.toByteArray()); os.reset(); } if (!ok()) throw new IOException(receive()); pos = 0; } if (pos < cache.size()) return true; cache = null; return false; }
/** * Travel dependencies tree by DFS and Post-Order algorithm. * * @param tree The initial tree which contains root node only. * @param footprint The footprint of the traversal. * @param output Output queue of combined files. */ private void travel( Stack<ArrayList<String>> tree, Stack<String> footprint, ArrayList<String> output) { for (String node : tree.peek()) { // Detect circular dependences by looking back footprint. if (footprint.contains(node)) { String msg = "Circular dependences was found\n"; for (String path : footprint) { msg += " " + path + " ->\n"; } msg += " " + node; App.exit(msg); } // Skip visited node. if (output.contains(node)) { continue; } // Move forward. footprint.push(node); // Add sub nodes. tree.push(getDependencies(node)); // Travel sub nodes. travel(tree, footprint, output); // Clean visited nodes. tree.pop(); // Move backward. footprint.pop(); // Add first visited node to output queue. output.add(node); } }
public static void main(String[] args) throws Throwable { final int itrs = Integer.getInteger("iterations", 100000); // final int itrs = Integer.getInteger("iterations", 12); final int size = Integer.getInteger("size", 2048); final int subsize = Integer.getInteger("subsize", 128); final int maxchar = Integer.getInteger("maxchar", 128); final String regex = System.getProperty("filter"); final Pattern filter = (regex == null) ? null : Pattern.compile(regex); final boolean useSecurityManager = Boolean.getBoolean("SecurityManager"); if (useSecurityManager) System.setSecurityManager(new PermissiveSecurityManger()); final Random rnd = new Random(); String[] csns = new String[] { "Big5", "Johab", "EUC_CN", "EUC_KR", "MS932", "MS936", "MS949", "MS950", "GBK", "Big5_HKSCS", "Big5_HKSCS_2001", "Big5_Solaris", "MS950_HKSCS", "MS950_HKSCS_XP", "IBM1364", "IBM1381", "IBM1383", "IBM930", "IBM933", "IBM935", "IBM937", "IBM939", "IBM942", "IBM943", "IBM948", "IBM949", "IBM950", "IBM970", }; ArrayList<long[]> sum = new ArrayList<>(); for (final String csn : csns) { final Charset cs = Charset.forName(csn); List<Integer> cps = new ArrayList<>(0x4000); int off = 0; int cp = 0; int n = 0; CharsetEncoder enc = cs.newEncoder(); while (cp < 0x10000 && n < cps.size()) { if (enc.canEncode((char) cp)) { cps.add(cp); n++; } cp++; } Collections.shuffle(cps); char[] ca = new char[cps.size()]; for (int i = 0; i < cps.size(); i++) ca[i] = (char) (int) cps.get(i); System.out.printf("%n--------%s---------%n", csn); for (int sz = 8; sz <= 2048; sz *= 2) { System.out.printf(" [len=%d]%n", sz); final char[] chars = Arrays.copyOf(ca, sz); final String str = new String(chars); final byte[] bs = str.getBytes(cs); Job[] jobs = { new Job("String decode: csn") { public void work() throws Throwable { for (int i = 0; i < itrs; i++) new String(bs, csn); } }, new Job("String decode: cs") { public void work() throws Throwable { for (int i = 0; i < itrs; i++) new String(bs, cs); } }, new Job("String encode: csn") { public void work() throws Throwable { for (int i = 0; i < itrs; i++) str.getBytes(csn); } }, new Job("String encode: cs") { public void work() throws Throwable { for (int i = 0; i < itrs; i++) str.getBytes(cs); } }, }; sum.add(time(jobs)); } } }
/** * Returns the next item. * * @return item string * @throws IOException I/O Exception */ public String next() throws IOException { return more() ? new String(cache.set(pos++, null), UTF8) : null; }