1 27 package org.htmlparser.lexer; 28 29 import java.io.Serializable ; 30 import org.htmlparser.util.sort.Ordered; 31 import org.htmlparser.util.sort.Sort; 32 import org.htmlparser.util.sort.Sortable; 33 34 41 public class PageIndex 42 implements 43 Serializable , 44 Sortable 45 { 46 49 protected static final int mStartIncrement = 100; 50 51 54 protected int mIncrement; 55 56 59 protected int mCount; 60 61 64 protected int[] mIndices; 65 66 69 protected Page mPage; 70 71 75 public PageIndex (Page page) 76 { 77 mPage = page; 78 mIndices = new int[mIncrement]; 79 mCount = 0; 80 mIncrement = mStartIncrement * 2; 81 } 82 83 88 public PageIndex (Page page, int cursor) 89 { 90 this (page); 91 mIndices[0] = cursor; 92 mCount = 1; 93 } 94 95 101 public PageIndex (Page page, int[] cursors) 102 { 103 mPage = page; 104 mIndices = cursors; 105 mCount = cursors.length; 106 } 107 108 112 public Page getPage () 113 { 114 return (mPage); 115 } 116 117 121 public int size () 122 { 123 return (mCount); 124 } 125 126 130 public int capacity () 131 { 132 return (mIndices.length); 133 } 134 135 141 public int add (Cursor cursor) 142 { 143 int position; 144 int last; 145 int ret; 146 147 position = cursor.getPosition (); 148 if (0 == mCount) 149 { 150 ret = 0; 151 insertElementAt (position, ret); 152 } 153 else 154 { 155 last = mIndices[mCount - 1]; 156 if (position == last) 157 ret = mCount - 1; 158 else 159 if (position > last) 160 { 161 ret = mCount; 162 insertElementAt (position, ret); 163 } 164 else 165 { 166 ret = Sort.bsearch (this, cursor); 168 169 if (!((ret < size ()) && (position == mIndices[ret]))) 171 insertElementAt (position, ret); 172 } 173 } 174 175 return (ret); 176 } 177 178 184 public int add (int cursor) 185 { 186 return (add (new Cursor (getPage (), cursor))); 187 } 188 189 193 public void remove (Cursor cursor) 194 { 195 int i; 196 197 i = Sort.bsearch (this, cursor); 199 200 if ((i < size ()) && (cursor.getPosition () == mIndices[i])) 202 removeElementAt (i); 203 } 204 205 209 public void remove (int cursor) 210 { 211 remove (new Cursor (getPage (), cursor)); 212 } 213 214 219 public int elementAt (int index) 220 { 221 if (index >= mCount) throw new IndexOutOfBoundsException ("index " + index + " beyond current limit"); 223 else 224 return (mIndices[index]); 225 } 226 227 232 public int row (Cursor cursor) 233 { 234 int ret; 235 236 ret = Sort.bsearch (this, cursor); 237 if ((ret < mCount) && (cursor.getPosition () == mIndices[ret])) 242 ret++; 243 244 return (ret); 245 } 246 247 252 public int row (int cursor) 253 { 254 return (row (new Cursor (getPage (), cursor))); 255 } 256 257 262 public int column (Cursor cursor) 263 { 264 int row; 265 int previous; 266 267 row = row (cursor); 268 if (0 != row) 269 previous = this.elementAt (row - 1); 270 else 271 previous = 0; 272 273 return (cursor.getPosition () - previous); 274 } 275 276 281 public int column (int cursor) 282 { 283 return (column (new Cursor (getPage (), cursor))); 284 } 285 286 291 public int[] get () 292 { 293 int[] ret = new int[size ()]; 294 System.arraycopy (mIndices, 0, ret, 0, size ()); 295 296 return (ret); 297 } 298 299 304 protected int bsearch (int cursor) 305 { 306 return (Sort.bsearch (this, new Cursor (getPage (), cursor))); 307 } 308 309 316 protected int bsearch (int cursor, int first, int last) 317 { 318 return (Sort.bsearch (this, new Cursor (getPage (), cursor), first, last)); 319 } 320 321 328 protected void insertElementAt (int cursor, int index) 329 { 330 if ((index >= capacity ()) || (size () == capacity ())) 331 { int new_values[] = new int[Math.max (capacity () + mIncrement, index + 1)]; 333 mIncrement *= 2; 334 if (index < capacity ()) 335 { 336 System.arraycopy (mIndices, 0, new_values, 0, index); 338 System.arraycopy (mIndices, index, new_values, index + 1, capacity () - index); 339 } 340 else 341 System.arraycopy (mIndices, 0, new_values, 0, capacity ()); 342 mIndices = new_values; 343 } 344 else if (index < size ()) 345 System.arraycopy (mIndices, index, mIndices, index + 1, capacity () - (index + 1)); 347 mIndices[index] = cursor; 348 mCount++; 349 } 350 351 355 protected void removeElementAt (int index) 356 { 357 System.arraycopy (mIndices, index + 1, mIndices, index, capacity () - (index + 1)); 359 mIndices[capacity() - 1] = 0; 360 mCount--; 361 } 362 363 367 371 public int first () 372 { 373 return (0); 374 } 375 376 382 public int last () 383 { 384 return (mCount - 1); 385 } 386 387 398 public Ordered fetch (int index, Ordered reuse) 399 { 400 Cursor ret; 401 402 if (null != reuse) 403 { 404 ret = (Cursor)reuse; 405 ret.mPosition = mIndices[index]; 406 ret.mPage = getPage (); } 408 else 409 ret = new Cursor (getPage (), mIndices[index]); 410 411 return (ret); 412 } 413 414 419 public void swap (int i, int j) 420 { 421 int temp = mIndices[i]; 422 mIndices[i] = mIndices[j]; 423 mIndices[j] = temp; 424 } 425 } 426 | Popular Tags |