分享代码系列――vlist (六)

2014-11-24 07:40:12 · 作者: · 浏览: 5
lean hasNext() {
323: return mCurrCell != null;
324: }
325:
326: /**
327: * Advances the iterator and returns the element it used to be over.
328: */
329: public T next() {
330: /* Bounds-check. */
331: if (!hasNext())
332: throw new NoSuchElementException();
333:
334: /* Cache the return value; we'll be moving off of it soon. */
335: T result = mCurrCell.mElems[mCurrIndex];
336:
337: /* Back up one step. */
338: --mCurrIndex;
339:
340: /*
341: * If we walked off the end of the buffer, advance to the next
342: * element of the list.
343: */
344: if (mCurrIndex < mCurrCell.mFreeSpace) {
345: mCurrCell = mCurrCell.mPrev;
346:
347: /*
348: * Update the next get location, provided of course that we
349: * didn't just walk off the end of the list.
350: */
351: if (mCurrCell != null)
352: mCurrIndex = mCurrCell.mElems.length - 1;
353: }
354:
355: /* Since there was indeed an element, we can remove it. */
356: mCanRemove = true;
357:
358: return result;
359: }
360:
361: /**
362: * Removes the last element we visited.
363: */
364: public void remove() {
365: /* Check whether there's something to remove. */
366: if (!mCanRemove)
367: throw new IllegalStateException(
368: "remove() without next(), or double remove().");
369:
370: /* Clear the flag saying we can do this. */
371: mCanRemove = false;
372:
373: /*
374: * There are several cases to consider. If the current cell is null,
375: * we've walked off the end of the array, so we want to remove the
376: * very last element. If the current cell isn't null and the cursor
377: * is in the middle, remove the previous element and back up a step.
378: * If the current cell isn't null and the cursor is at the front,
379: * remove the element one step before us and back up a step.
380: */
381:
382: /* Case 1. */
383: if (mCurrCell == null)
384: VList.this.remove(size() - 1);
385: /* Case 2. */
386: else if (mCurrIndex != mCurrCell.mElems.length - 1) {
387: /*
388: * Back up a step, and remove the element at this position.
389: * After the remove completes, the element here should be the
390: * next element to visit.
391: */
392: ++mCurrIndex;
393: removeAtPosition(new VListLocation(mCurrCell, mCurrIndex));
394: }
395: /* Case 3. */
396: else {
397: /*
398: * Back up a step to the top of the previous list. We know that
399: * the top will be at position 0, since all internal blocks are
400: * completely full. We also know that we aren't at the very
401: * front of the list, since if we were, then the call to next()