/** * Returns the branch state for a given branch id * (the appropriate ancestor branch state is returned if * there is no branch state associated with the given branch id) * @param bid branch id * @return BranchState associated with the given branch id */ protected BranchState getBranchState(int bid) { BranchState bs = branchStates.get(bid); if (bs == null) { BranchState pbs = null; int id = bid; while (pbs == null && id >= 0) { Pair<Integer, Integer> p = bidIndex.get(id); id = p.first; pbs = branchStates.get(id); } bs = pbs; } return bs; }
/** * This saves the contents of this index into string form, as part of a larger * text-serialization. This is not intended to act as a standalone routine, * instead being called from the text-serialization routine for a component * that makes use of an Index, so everything can be stored in one file. This is * similar to {@code saveToFileName}. * @param bw Writer to save to. * @throws IOException Exception thrown if cannot save. */ @Override public void saveToWriter(Writer bw) throws IOException { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } }
/** A safe version of {@link SequenceMatcher.BranchStates#getParents(int, Integer[])} */ private List<Integer> getParents(int bid) { List<Integer> pids = new ArrayList<>(); Pair<Integer,Integer> p = bidIndex.get(bid); while (p != null && p.first() >= 0) { pids.add(p.first()); p = bidIndex.get(p.first()); } Collections.reverse(pids); return pids; }
@Override public void saveToFilename(String file) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } } catch (IOException e) { throw new RuntimeIOException(e); } // give up }
/** * Given a branch id, return a list of parent branches * @param bid branch id * @return list of parent branch ids */ private List<Integer> getParents(int bid, Integer[] buffer) { int index = buffer.length - 1; buffer[index] = bid; index -= 1; Pair<Integer,Integer> p = bidIndex.get(bid); while (p != null && p.first() >= 0) { buffer[index] = p.first; index -= 1; if (index < 0) { return getParents(bid); // optimization failed -- back off to the old version } p = bidIndex.get(p.first()); } return Arrays.asList(buffer).subList(index + 1, buffer.length); }
/** * Returns the branch state for a given branch id * (the appropriate ancestor branch state is returned if * there is no branch state associated with the given branch id) * @param bid branch id * @return BranchState associated with the given branch id */ protected BranchState getBranchState(int bid) { BranchState bs = branchStates.get(bid); if (bs == null) { BranchState pbs = null; int id = bid; while (pbs == null && id >= 0) { Pair<Integer, Integer> p = bidIndex.get(id); id = p.first; pbs = branchStates.get(id); } bs = pbs; } return bs; }
/** * Returns the branch state for a given branch id * (the appropriate ancestor branch state is returned if * there is no branch state associated with the given branch id) * @param bid branch id * @return BranchState associated with the given branch id */ protected BranchState getBranchState(int bid) { BranchState bs = branchStates.get(bid); if (bs == null) { BranchState pbs = null; int id = bid; while (pbs == null && id >= 0) { Pair<Integer, Integer> p = bidIndex.get(id); id = p.first; pbs = branchStates.get(id); } bs = pbs; } return bs; }
/** A safe version of {@link SequenceMatcher.BranchStates#getParents(int, Integer[])} */ private List<Integer> getParents(int bid) { List<Integer> pids = new ArrayList<>(); Pair<Integer,Integer> p = bidIndex.get(bid); while (p != null && p.first() >= 0) { pids.add(p.first()); p = bidIndex.get(p.first()); } Collections.reverse(pids); return pids; }
/** * This saves the contents of this index into string form, as part of a larger * text-serialization. This is not intended to act as a standalone routine, * instead being called from the text-serialization routine for a component * that makes use of an Index, so everything can be stored in one file. This is * similar to {@code saveToFileName}. * @param bw Writer to save to. * @throws IOException Exception thrown if cannot save. */ @Override public void saveToWriter(Writer bw) throws IOException { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } }
/** * This saves the contents of this index into string form, as part of a larger * text-serialization. This is not intended to act as a standalone routine, * instead being called from the text-serialization routine for a component * that makes use of an Index, so everything can be stored in one file. This is * similar to {@code saveToFileName}. * @param bw Writer to save to. * @throws IOException Exception thrown if cannot save. */ @Override public void saveToWriter(Writer bw) throws IOException { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } }
/** * This saves the contents of this index into string form, as part of a larger * text-serialization. This is not intended to act as a standalone routine, * instead being called from the text-serialization routine for a component * that makes use of an Index, so everything can be stored in one file. This is * similar to <code>saveToFileName</code>. * NOTE: adds an extra newline at the end of the sequence. * @param bw Writer to save to. * @throws IOException Exception thrown if cannot save. */ public void saveToWriter(Writer bw) throws IOException { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } bw.write("\n"); }
/** * This saves the contents of this index into string form, as part of a larger * text-serialization. This is not intended to act as a standalone routine, * instead being called from the text-serialization routine for a component * that makes use of an Index, so everything can be stored in one file. This is * similar to {@code saveToFileName}. * @param bw Writer to save to. * @throws IOException Exception thrown if cannot save. */ @Override public void saveToWriter(Writer bw) throws IOException { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } }
@Override public void saveToFilename(String file) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } } catch (IOException e) { throw new RuntimeIOException(e); } // give up }
/** A safe version of {@link SequenceMatcher.BranchStates#getParents(int, Integer[])} */ private List<Integer> getParents(int bid) { List<Integer> pids = new ArrayList<>(); Pair<Integer,Integer> p = bidIndex.get(bid); while (p != null && p.first() >= 0) { pids.add(p.first()); p = bidIndex.get(p.first()); } Collections.reverse(pids); return pids; }
@Override public void saveToFilename(String file) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } } catch (IOException e) { throw new RuntimeIOException(e); } // give up }
public void saveToFilename(String file) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(file)); for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } bw.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException ioe) { // give up } } } }
@Override public void saveToFilename(String file) { BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(file)); for (int i = 0, sz = size(); i < sz; i++) { bw.write(i + "=" + get(i) + '\n'); } bw.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException ioe) { // give up } } } }
/** * Given a branch id, return a list of parent branches * @param bid branch id * @return list of parent branch ids */ private List<Integer> getParents(int bid, Integer[] buffer) { int index = buffer.length - 1; buffer[index] = bid; index -= 1; Pair<Integer,Integer> p = bidIndex.get(bid); while (p != null && p.first() >= 0) { buffer[index] = p.first; index -= 1; if (index < 0) { return getParents(bid); // optimization failed -- back off to the old version } p = bidIndex.get(p.first()); } return Arrays.asList(buffer).subList(index + 1, buffer.length); }
/** * Given a branch id, return a list of parent branches * @param bid branch id * @return list of parent branch ids */ private List<Integer> getParents(int bid, Integer[] buffer) { int index = buffer.length - 1; buffer[index] = bid; index -= 1; Pair<Integer,Integer> p = bidIndex.get(bid); while (p != null && p.first() >= 0) { buffer[index] = p.first; index -= 1; if (index < 0) { return getParents(bid); // optimization failed -- back off to the old version } p = bidIndex.get(p.first()); } return Arrays.asList(buffer).subList(index + 1, buffer.length); }