/** * Creates a new Index and adds every member of c to it. * @param c A collection of objects */ public HashIndex(Collection<? extends E> c) { this(); addAll(c); }
/** * 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'); } }
/** * Takes an Object and returns the integer index of the Object, * perhaps adding it to the index first. * Returns -1 if the Object is not in the Index. * <p> * <i>Notes:</i> The method indexOf(x, true) is the direct replacement for * the number(x) method in the old Numberer class. This method now uses a * Semaphore object to make the index safe for concurrent multithreaded * usage. (CDM: Is this better than using a synchronized block?) * * @param o the Object whose index is desired. * @param add Whether it is okay to add new items to the index * @return The index of the Object argument. Returns -1 if the object is not in the index. */ @Override @Deprecated public int indexOf(E o, boolean add) { if (add) { return addToIndex(o); } else { return indexOf(o); } }
protected int getBranchId(int bid, int nextBranchIndex, int nextTotal) { if (nextBranchIndex <= 0 || nextBranchIndex > nextTotal) { throw new IllegalArgumentException("Invalid nextBranchIndex=" + nextBranchIndex + ", nextTotal=" + nextTotal); } if (nextTotal == 1) { return bid; } else { Pair<Integer,Integer> p = new Pair<>(bid, nextBranchIndex); int i = bidIndex.indexOf(p); if (i < 0) { for (int j = 0; j < nextTotal; j++) { bidIndex.add(new Pair<>(bid, j + 1)); } i = bidIndex.indexOf(p); } return i; } }
/** * This is the analogue of {@code loadFromFilename}, and is intended to be included in a routine * that unpacks a text-serialized form of an object that incorporates an Index. * NOTE: presumes that the next readLine() will read in the first line of the * portion of the text file representing the saved Index. Currently reads until it * encounters a blank line, consuming that line and returning the Index. * TODO: figure out how best to terminate: currently a blank line is considered to be a terminator. * @param br The Reader to read the index from * @return An Index read from a file */ public static Index<String> loadFromReader(BufferedReader br) throws IOException { HashIndex<String> index = new HashIndex<>(); String line = br.readLine(); // terminate if EOF reached, or if a blank line is encountered. while ((line != null) && (line.length() > 0)) { int start = line.indexOf('='); if (start == -1 || start == line.length() - 1) { continue; } index.add(line.substring(start + 1)); line = br.readLine(); } return index; }
/** * Returns the index of each elem in a List. * @param elements The list of items * @return An array of indices */ public int[] indices(Collection<E> elements) { int[] indices = new int[elements.size()]; int i = 0; for (E elem : elements) { indices[i++] = indexOf(elem); } return indices; }
/** * 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; }
Index<String> labelIndex = HashIndex.loadFromReader(in); Index<String> featureIndex = HashIndex.loadFromReader(in); double[][] weights = new double[featureIndex.size()][labelIndex.size()]; int currLine = 1;
/** Returns a readable version of the Index contents * * @return A String showing the full index contents */ @Override public String toString() { return toString(Integer.MAX_VALUE); }
public String toStringOneEntryPerLine() { return toStringOneEntryPerLine(Integer.MAX_VALUE); }
/** * Adds every member of Collection to the Index. Does nothing for members already in the Index. * * @return true if some item was added to the index and false if no * item was already in the index or if the index is locked */ @Override public boolean addAll(Collection<? extends E> c) { boolean changed = false; for (E element: c) { changed |= add(element); //changed &= add(element); } return changed; }
/** * Returns an unmodifiable view of the Index. It is just * a locked index that cannot be unlocked, so if you * try to add something, nothing will happen (it won't throw * an exception). Trying to unlock it will throw an * UnsupportedOperationException. If the * underlying Index is modified, the change will * "write-through" to the view. * * @return An unmodifiable view of the Index */ public HashIndex<E> unmodifiableView() { HashIndex<E> newIndex = new HashIndex<E>(objects, indexes) { @Override public void unlock() { throw new UnsupportedOperationException("This is an unmodifiable view!"); } private static final long serialVersionUID = 3415903369787491736L; }; newIndex.lock(); return newIndex; }
/** * Gets the object whose index is the integer argument. * * @param i the integer index to be queried for the corresponding argument * @return the object whose index is the integer argument. */ @Override public E get(int i) { if (i < 0 || i >= objects.size()) throw new ArrayIndexOutOfBoundsException("Index " + i + " outside the bounds [0," + size() + ")"); return objects.get(i); }
public AmbiguityClasses(TTags ttags) { classes = new HashIndex<>(); // naClass.init(naWord, ttags); }
/** * This is the analogue of {@code loadFromFilename}, and is intended to be included in a routine * that unpacks a text-serialized form of an object that incorporates an Index. * NOTE: presumes that the next readLine() will read in the first line of the * portion of the text file representing the saved Index. Currently reads until it * encounters a blank line, consuming that line and returning the Index. * TODO: figure out how best to terminate: currently a blank line is considered to be a terminator. * @param br The Reader to read the index from * @return An Index read from a file */ public static Index<String> loadFromReader(BufferedReader br) throws IOException { HashIndex<String> index = new HashIndex<>(); String line = br.readLine(); // terminate if EOF reached, or if a blank line is encountered. while ((line != null) && (line.length() > 0)) { int start = line.indexOf('='); if (start == -1 || start == line.length() - 1) { continue; } index.add(line.substring(start + 1)); line = br.readLine(); } return index; }
protected int getBranchId(int bid, int nextBranchIndex, int nextTotal) { if (nextBranchIndex <= 0 || nextBranchIndex > nextTotal) { throw new IllegalArgumentException("Invalid nextBranchIndex=" + nextBranchIndex + ", nextTotal=" + nextTotal); } if (nextTotal == 1) { return bid; } else { Pair<Integer,Integer> p = new Pair<>(bid, nextBranchIndex); int i = bidIndex.indexOf(p); if (i < 0) { for (int j = 0; j < nextTotal; j++) { bidIndex.add(new Pair<>(bid, j + 1)); } i = bidIndex.indexOf(p); } return i; } }
protected int getBid(int parent, int child) { return bidIndex.indexOf(new Pair<>(parent,child)); }
/** 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; }