private Children(SpanNodeParent parent, List<SpanNode> children, double probability) { super(children); setParent(parent); if (children != null) { for (SpanNode node : children) { node.setParent(AlternateSpanList.this); } } this.probability = probability; }
@Override public void remove() { if (returnedFromNext != null) { returnedFromNext.setInvalid(); returnedFromNext.setParent(null); owner.resetCachedFromAndTo(); } returnedFromNext = null; base.remove(); }
@Override public void set(SpanNode spanNode) { if (returnedFromNext != null) { returnedFromNext.setInvalid(); returnedFromNext.setParent(null); } owner.resetCachedFromAndTo(); returnedFromNext = null; base.set(spanNode); }
/** * Removes and invalidates the SpanNode at the given index from this. * * @param i the index of the node to remove. * @return this, for chaining. */ public SpanList remove(int i) { SpanNode node = children().remove(i); if (node != null) { node.setParent(null); node.setInvalid(); } return this; }
/** * Moves a child of this SpanList to another SpanList. * * @param node the node to move * @param target the SpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList */ public void move(SpanNode node, SpanList target) { boolean removed = children().remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param node the node to move * @param target the SpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList */ public void move(int i, SpanNode node, SpanList target) { boolean removed = children(i).remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Convenience method to add a span node to the child tree at index i. This is equivalent to calling * <code> * AlternateSpanList.children(i).add(node); * </code> * * @param i index * @param node span node */ public AlternateSpanList add(int i, SpanNode node) { checkValidity(node, children(i)); node.setParent(this); children(i).add(node); return this; }
/** * Removes and invalidates the given SpanNode from this. * * @param node the node to remove. * @return this, for chaining. */ public SpanList remove(SpanNode node) { boolean removed = children().remove(node); if (removed) { node.setParent(null); resetCachedFromAndTo(); node.setInvalid(); } return this; }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @throws IndexOutOfBoundsException if the given index is out of range */ public void move(int i, int nodeNum, SpanList target) { SpanNode node = children(i).remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } }
/** * Moves a child of this SpanList to another SpanList. * * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @throws IndexOutOfBoundsException if the given index is out of range */ public void move(int nodeNum, SpanList target) { SpanNode node = children().remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } }
/** Removes and invalidates all references to child nodes. */ public void clearChildren() { for (SpanNode node : children()) { node.setInvalid(); node.setParent(null); } children().clear(); resetCachedFromAndTo(); }
/** * WARNING! Only to be used by deserializers! Sets the root of this SpanTree instance. * * @param root the root to set for this SpanTree instance. */ public void setRoot(SpanNode root) { if (!root.isValid()) { throw new IllegalStateException("Cannot use invalid node " + root + " as root node."); } if (root.getParent() != null) { if (root.getParent() != this) { throw new IllegalStateException(root + " is already a child of " + root.getParent() + ", cannot be root of " + this); } } this.root = root; root.setParent(this); }
/** * Moves a child of this SpanList to another SpanList. * * @param node the node to move * @param target the SpanList to add the node to * @param targetSubTree the index of the subtree of the given AlternateSpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList * @throws IndexOutOfBoundsException if the target subtree index is out of range */ public void move(SpanNode node, AlternateSpanList target, int targetSubTree) { if (targetSubTree < 0 || targetSubTree >= target.getNumSubTrees()) { throw new IndexOutOfBoundsException(target + " has no subtree at index " + targetSubTree); } boolean removed = children().remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @param targetSubTree the index of the subtree of the given AlternateSpanList to add the node to * @throws IndexOutOfBoundsException if any of the given indeces are out of range, or the target subtree index is out of range */ public void move(int i, int nodeNum, AlternateSpanList target, int targetSubTree) { if (targetSubTree < 0 || targetSubTree >= target.getNumSubTrees()) { throw new IndexOutOfBoundsException(target + " has no subtree at index " + targetSubTree); } SpanNode node = children(i).remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param node the node to move * @param target the SpanList to add the node to * @param targetSubTree the index of the subtree of the given AlternateSpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList * @throws IndexOutOfBoundsException if the given index is out of range, or if the target subtree index is out of range */ public void move(int i, SpanNode node, AlternateSpanList target, int targetSubTree) { if (targetSubTree < 0 || targetSubTree >= target.getNumSubTrees()) { throw new IndexOutOfBoundsException(target + " has no subtree at index " + targetSubTree); } boolean removed = children(i).remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Adds a child node to this SpanList. * * @param node the node to add. * @return this, for call chaining * @throws IllegalStateException if SpanNode.isValid() returns false. */ public SpanList add(SpanNode node) { checkValidity(node, children()); node.setParent(this); resetCachedFromAndTo(); children().add(node); return this; }
/** * Moves a child of this SpanList to another SpanList. * * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @param targetSubTree the index of the subtree of the given AlternateSpanList to add the node to * @throws IndexOutOfBoundsException if the given index is out of range, or the target subtree index is out of range */ public void move(int nodeNum, AlternateSpanList target, int targetSubTree) { if (targetSubTree < 0 || targetSubTree >= target.getNumSubTrees()) { throw new IndexOutOfBoundsException(target + " has no subtree at index " + targetSubTree); } SpanNode node = children().remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(targetSubTree, node); } }
/** * Removes the subtree at index i (both the subtree itself and its contents, which become invalidated). * Note that if this AlternateSpanList has only one subtree and index 0 is given, * a new empty subtree is automatically added, since an AlternateSpanList always has at least one subtree. * * @param i the index of the subtree to remove * @return the subtree removed, if any (note: invalidated) */ public List<SpanNode> removeChildren(int i) { Children retval = childTrees.remove(i); ensureAtLeastOneSubTree(); resetCachedFromAndTo(); if (retval != null) { retval.setInvalid(); retval.setParent(null); for (SpanNode node : retval.children()) { node.setParent(null); } return retval.children(); } return null; }
/** * Sets the subtree at index i. * * @param i the index of where to set the subtree * @param subtree the subtree to set * @param probability the probability to set * @return the overwritten subtree, if any */ public List<SpanNode> setChildren(int i, List<SpanNode> subtree, double probability) { resetCachedFromAndTo(); if (childTrees.size() == 1 && i == 0) { //replace the first subtree Children sub = new Children(getParent(), subtree, probability); Children retval = childTrees.set(i, sub); if (retval == null) { return null; } else { retval.setParent(null); for (SpanNode node : retval.children()) { node.setParent(null); } return retval.children(); } } List<SpanNode> retval = removeChildren(i); addChildren(i, subtree, probability); return retval; }