예제 #1
0
 /**
  * Delete children from start to stop and replace with t even if t is a list (nil-root tree). num
  * of children can increase or decrease. For huge child lists, inserting children can force
  * walking rest of children to set their childindex; could be slow.
  */
 public void replaceChildren(int startChildIndex, int stopChildIndex, Object t) {
   /*
   System.out.println("replaceChildren "+startChildIndex+", "+stopChildIndex+
   				   " with "+((PythonTree)t).toStringTree());
   System.out.println("in="+toStringTree());
   */
   if (children == null) {
     throw new IllegalArgumentException("indexes invalid; no children in list");
   }
   int replacingHowMany = stopChildIndex - startChildIndex + 1;
   int replacingWithHowMany;
   PythonTree newTree = (PythonTree) t;
   List newChildren = null;
   // normalize to a list of children to add: newChildren
   if (newTree.isNil()) {
     newChildren = newTree.children;
   } else {
     newChildren = new ArrayList(1);
     newChildren.add(newTree);
   }
   replacingWithHowMany = newChildren.size();
   int numNewChildren = newChildren.size();
   int delta = replacingHowMany - replacingWithHowMany;
   // if same number of nodes, do direct replace
   if (delta == 0) {
     int j = 0; // index into new children
     for (int i = startChildIndex; i <= stopChildIndex; i++) {
       PythonTree child = (PythonTree) newChildren.get(j);
       children.set(i, child);
       child.setParent(this);
       child.setChildIndex(i);
       j++;
     }
   } else if (delta > 0) { // fewer new nodes than there were
     // set children and then delete extra
     for (int j = 0; j < numNewChildren; j++) {
       children.set(startChildIndex + j, newChildren.get(j));
     }
     int indexToDelete = startChildIndex + numNewChildren;
     for (int c = indexToDelete; c <= stopChildIndex; c++) {
       // delete same index, shifting everybody down each time
       PythonTree killed = (PythonTree) children.remove(indexToDelete);
     }
     freshenParentAndChildIndexes(startChildIndex);
   } else { // more new nodes than were there before
     // fill in as many children as we can (replacingHowMany) w/o moving data
     for (int j = 0; j < replacingHowMany; j++) {
       children.set(startChildIndex + j, newChildren.get(j));
     }
     int numToInsert = replacingWithHowMany - replacingHowMany;
     for (int j = replacingHowMany; j < replacingWithHowMany; j++) {
       children.add(startChildIndex + j, newChildren.get(j));
     }
     freshenParentAndChildIndexes(startChildIndex);
   }
   // System.out.println("out="+toStringTree());
 }
예제 #2
0
 /** Set the parent and child index values for all child of t */
 public void freshenParentAndChildIndexes() {
   freshenParentAndChildIndexes(0);
 }