41 |
import java.io.Serializable; |
import java.io.Serializable; |
42 |
import java.util.ArrayList; |
import java.util.ArrayList; |
43 |
import java.util.Collections; |
import java.util.Collections; |
|
import java.util.Iterator; |
|
44 |
import java.util.ListIterator; |
import java.util.ListIterator; |
45 |
import java.util.Vector; |
import java.util.Vector; |
46 |
|
|
397 |
|
|
398 |
int delta = newSize - gapEnd + gapStart; |
int delta = newSize - gapEnd + gapStart; |
399 |
// Update the marks after the gapEnd. |
// Update the marks after the gapEnd. |
400 |
Vector v = getPositionsInRange(null, gapEnd, buffer.length - gapEnd); |
adjustPositionsInRange(gapEnd, buffer.length - gapEnd, delta); |
|
for (Iterator i = v.iterator(); i.hasNext();) |
|
|
{ |
|
|
GapContentPosition p = (GapContentPosition) i.next(); |
|
|
p.mark += delta; |
|
|
} |
|
401 |
|
|
402 |
// Copy the data around. |
// Copy the data around. |
403 |
char[] newBuf = (char[]) allocateArray(length() + newSize); |
char[] newBuf = (char[]) allocateArray(length() + newSize); |
424 |
{ |
{ |
425 |
// Update the positions between newGapStart and (old) gapStart. The marks |
// Update the positions between newGapStart and (old) gapStart. The marks |
426 |
// must be shifted by (gapEnd - gapStart). |
// must be shifted by (gapEnd - gapStart). |
427 |
Vector v = getPositionsInRange(null, newGapStart, |
adjustPositionsInRange(newGapStart, gapStart - newGapStart, gapEnd - gapStart); |
|
gapStart - newGapStart); |
|
|
for (Iterator i = v.iterator(); i.hasNext();) |
|
|
{ |
|
|
GapContentPosition p = (GapContentPosition) i.next(); |
|
|
p.mark += gapEnd - gapStart; |
|
|
} |
|
428 |
System.arraycopy(buffer, newGapStart, buffer, newGapEnd, gapStart |
System.arraycopy(buffer, newGapStart, buffer, newGapEnd, gapStart |
429 |
- newGapStart); |
- newGapStart); |
430 |
gapStart = newGapStart; |
gapStart = newGapStart; |
434 |
{ |
{ |
435 |
// Update the positions between newGapEnd and (old) gapEnd. The marks |
// Update the positions between newGapEnd and (old) gapEnd. The marks |
436 |
// must be shifted by (gapEnd - gapStart). |
// must be shifted by (gapEnd - gapStart). |
437 |
Vector v = getPositionsInRange(null, gapEnd, |
adjustPositionsInRange(gapEnd, newGapEnd - gapEnd, -(gapEnd - gapStart)); |
|
newGapEnd - gapEnd); |
|
|
for (Iterator i = v.iterator(); i.hasNext();) |
|
|
{ |
|
|
GapContentPosition p = (GapContentPosition) i.next(); |
|
|
p.mark -= gapEnd - gapStart; |
|
|
} |
|
438 |
System.arraycopy(buffer, gapEnd, buffer, gapStart, newGapStart |
System.arraycopy(buffer, gapEnd, buffer, gapStart, newGapStart |
439 |
- gapStart); |
- gapStart); |
440 |
gapStart = newGapStart; |
gapStart = newGapStart; |
459 |
|
|
460 |
assert newGapStart < gapStart : "The new gap start must be less than the " |
assert newGapStart < gapStart : "The new gap start must be less than the " |
461 |
+ "old gap start."; |
+ "old gap start."; |
462 |
Vector v = getPositionsInRange(null, newGapStart, gapStart - newGapStart); |
setPositionsInRange(newGapStart, gapStart - newGapStart, gapStart); |
|
for (Iterator i = v.iterator(); i.hasNext();) |
|
|
{ |
|
|
GapContentPosition p = (GapContentPosition) i.next(); |
|
|
p.mark = gapStart; |
|
|
} |
|
463 |
gapStart = newGapStart; |
gapStart = newGapStart; |
464 |
} |
} |
465 |
|
|
478 |
|
|
479 |
assert newGapEnd > gapEnd : "The new gap end must be greater than the " |
assert newGapEnd > gapEnd : "The new gap end must be greater than the " |
480 |
+ "old gap end."; |
+ "old gap end."; |
481 |
Vector v = getPositionsInRange(null, gapEnd, newGapEnd - gapEnd); |
setPositionsInRange(gapEnd, newGapEnd - gapEnd, newGapEnd + 1); |
|
for (Iterator i = v.iterator(); i.hasNext();) |
|
|
{ |
|
|
GapContentPosition p = (GapContentPosition) i.next(); |
|
|
p.mark = newGapEnd + 1; |
|
|
} |
|
482 |
gapEnd = newGapEnd; |
gapEnd = newGapEnd; |
483 |
} |
} |
484 |
|
|
580 |
} |
} |
581 |
return res; |
return res; |
582 |
} |
} |
583 |
|
|
584 |
|
/** |
585 |
|
* Sets the mark of all <code>Position</code>s that are in the range |
586 |
|
* specified by <code>offset</code> and </code>length</code> within |
587 |
|
* the buffer array to <code>value</code> |
588 |
|
* |
589 |
|
* @param offset the start offset of the range to search |
590 |
|
* @param length the length of the range to search |
591 |
|
* @param value the new value for each mark |
592 |
|
*/ |
593 |
|
void setPositionsInRange(int offset, int length, int value) |
594 |
|
{ |
595 |
|
int endOffset = offset + length; |
596 |
|
|
597 |
|
int index1 = Collections.binarySearch(positions, |
598 |
|
new GapContentPosition(offset)); |
599 |
|
int index2 = Collections.binarySearch(positions, |
600 |
|
new GapContentPosition(endOffset)); |
601 |
|
if (index1 < 0) |
602 |
|
index1 = -(index1 + 1); |
603 |
|
if (index2 < 0) |
604 |
|
index2 = -(index2 + 1); |
605 |
|
for (ListIterator i = positions.listIterator(index1); i.hasNext();) |
606 |
|
{ |
607 |
|
if (i.nextIndex() > index2) |
608 |
|
break; |
609 |
|
|
610 |
|
GapContentPosition p = (GapContentPosition) i.next(); |
611 |
|
if (p.mark >= offset && p.mark <= endOffset) |
612 |
|
p.mark = value; |
613 |
|
} |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
|
* Adjusts the mark of all <code>Position</code>s that are in the range |
618 |
|
* specified by <code>offset</code> and </code>length</code> within |
619 |
|
* the buffer array by <code>increment</code> |
620 |
|
* |
621 |
|
* @param offset the start offset of the range to search |
622 |
|
* @param length the length of the range to search |
623 |
|
* @param incr the increment |
624 |
|
*/ |
625 |
|
void adjustPositionsInRange(int offset, int length, int incr) |
626 |
|
{ |
627 |
|
int endOffset = offset + length; |
628 |
|
|
629 |
|
int index1 = Collections.binarySearch(positions, |
630 |
|
new GapContentPosition(offset)); |
631 |
|
int index2 = Collections.binarySearch(positions, |
632 |
|
new GapContentPosition(endOffset)); |
633 |
|
if (index1 < 0) |
634 |
|
index1 = -(index1 + 1); |
635 |
|
if (index2 < 0) |
636 |
|
index2 = -(index2 + 1); |
637 |
|
for (ListIterator i = positions.listIterator(index1); i.hasNext();) |
638 |
|
{ |
639 |
|
if (i.nextIndex() > index2) |
640 |
|
break; |
641 |
|
|
642 |
|
GapContentPosition p = (GapContentPosition) i.next(); |
643 |
|
if (p.mark >= offset && p.mark <= endOffset) |
644 |
|
p.mark += incr; |
645 |
|
} |
646 |
|
} |
647 |
|
|
648 |
/** |
/** |
649 |
* Resets all <code>Position</code> that have an offset of <code>0</code>, |
* Resets all <code>Position</code> that have an offset of <code>0</code>, |
656 |
if (gapStart != 0) |
if (gapStart != 0) |
657 |
return; |
return; |
658 |
|
|
659 |
Vector zeroMarks = getPositionsInRange(null, gapEnd, 0); |
setPositionsInRange(gapEnd, 0, 0); |
|
for (Iterator i = zeroMarks.iterator(); i.hasNext();) |
|
|
{ |
|
|
GapContentPosition pos = (GapContentPosition) i.next(); |
|
|
pos.mark = 0; |
|
|
} |
|
660 |
} |
} |
661 |
} |
} |