| 1 4 package gnu.lists; 5 6 17 18 public class TreePosition extends SeqPosition implements Cloneable  19 { 20 22 private Object xpos; 23 24 25 AbstractSequence[] sstack; 26 27 28 int[] istack; 29 30 32 int depth; 33 34 36 int start; 37 38 public TreePosition() 39 { 40 depth = -1; 41 } 42 43 44 public TreePosition(Object root) 45 { 46 xpos = root; 47 depth = -1; 48 } 49 50 public TreePosition(AbstractSequence seq, int index) 51 { 52 super(seq, index, false); 53 } 54 55 public TreePosition (TreePosition pos) 56 { 57 set(pos); 58 } 59 60 public Object clone () 61 { 62 return new TreePosition(this); 63 } 64 65 public void set (TreePosition position) 66 { 67 release(); 68 int d = position.depth; 69 depth = d; 70 if (d < 0) 71 { 72 xpos = position.xpos; 73 return; 74 } 75 if (sstack == null || sstack.length <= d) 76 sstack = new AbstractSequence[d + 10]; 77 if (istack == null || istack.length <= d) 78 istack = new int[d + 10]; 79 AbstractSequence seq; 80 int i; 81 for (i = 0; i < depth; i++) 82 { 83 int j = i + position.start; 84 seq = position.sstack[j]; 85 sstack[depth-1] = seq; 86 istack[depth - i] = seq.copyPos(position.istack[j]); 87 } 88 seq = position.sequence; 89 sequence = seq; 90 ipos = seq.copyPos(position.ipos); 91 } 92 93 94 public int getDepth() 95 { 96 return depth + 1; 97 } 98 99 100 public AbstractSequence getRoot() 101 { 102 return depth == 0 ? sequence : sstack[start]; 103 } 104 105 public Object getPosNext() 106 { 107 return sequence == null ? xpos : sequence.getPosNext(ipos); 108 } 109 110 public void push(AbstractSequence child, int iposChild) 111 { 112 int d = depth + start; 113 if (d >= 0) 114 { 115 if (d == 0) 116 { 117 istack = new int[8]; 118 sstack = new AbstractSequence[8]; 119 } 120 else if (d >= istack.length) 121 { 122 int ndepth = 2 * d; 123 int[] itemp = new int[ndepth]; 124 Object [] xtemp = new Object [ndepth]; 125 AbstractSequence[] stemp = new AbstractSequence[ndepth]; 126 System.arraycopy(istack, 0, itemp, 0, depth); 127 System.arraycopy(sstack, 0, stemp, 0, depth); 128 istack = itemp; 129 sstack = stemp; 130 } 131 sstack[d] = sequence; 132 istack[d] = ipos; 133 } 134 depth++; 135 sequence = child; 136 ipos = iposChild; 137 } 138 139 public void pop() 140 { 141 sequence.releasePos(ipos); 142 popNoRelease(); 143 } 144 145 public void popNoRelease() 146 { 147 if (--depth < 0) 148 { 149 xpos = sequence; 150 sequence = null; 151 } 152 else 153 { 154 sequence = sstack[start+depth]; 155 ipos = istack[start+depth]; 156 } 157 } 158 159 public final boolean gotoParent() 160 { 161 return sequence == null ? false : sequence.gotoParent(this); 162 } 163 164 169 public boolean gotoChildrenStart() 170 { 171 if (sequence == null) 172 { 173 if (! (xpos instanceof AbstractSequence)) 174 return false; 175 depth = 0; 176 sequence = (AbstractSequence) xpos; 177 setPos(sequence.startPos()); 178 } 179 else 180 { 181 if (! sequence.gotoChildrenStart(this)) 182 return false; 183 } 184 return true; 185 } 186 187 190 public boolean gotoAttributesStart() 191 { 192 if (sequence == null) 193 { 194 if (xpos instanceof AbstractSequence) 195 { 196 } 198 return false; 199 } 200 return sequence.gotoAttributesStart(this); 201 } 202 203 209 210 215 public Object getAncestor(int up) 216 { 217 if (up == 0) 218 return sequence.getPosNext(ipos); 219 int i = depth - up; 220 if (i <= 0) 221 return getRoot(); 222 i += start; 223 return sstack[i].getPosNext(istack[i]); 224 } 225 226 public void release() 227 { 228 while (sequence != null) 229 { 230 sequence.releasePos(ipos); 231 pop(); 232 } 233 xpos = null; 234 } 235 236 237 250 251 public void dump() 252 { 253 System.err.println("TreePosition dump depth:"+depth+" start:"+start); 254 for (int i = 0; i <= depth; i++) 255 { 256 AbstractSequence seq = i==0 ? sequence : sstack[depth-i]; 257 System.err.print("#"+i+" seq:"+seq); 258 System.err.println(" ipos:" + (i == 0 ? ipos : istack[depth-i])); 259 } 260 } 261 } 262 | Popular Tags |