I improved the GapContentPosition stuff a little, now lumping marks together into one mark when they become equal. This does still not 100% solve the bug 26368, but we get nearer.
2006-05-18 Roman Kennke <[EMAIL PROTECTED]>
PR 26368
* javax/swing/text/GapContent.java
(GapContentPosition(int)): Use adapted binarySearch method to
allow for having a greater array than number of entries.
(numMarks): New field, holds the end of the marks list.
(GapContent): Initialize positionMarks with size of 10 instead
of 0.
(shiftGapStartDown): Adjusted for new setPositionsInRange
signature.
(shiftGapEndUp): Adjusted for new setPositionsInRange signature.
(setPositionsInRange): Changed signature to narrow the purpose
and
special cases inside. Reimplemented to crunch together equal
marks.
(adjustPositionsInRange): Added assertion to make sure we do
not accidentally change the order of the mark. Added some debug
output for a special case of which I don't know if it even
exists.
(resetMarksAtZero): Made impl simpler.
(dumpMarks): New debug helper method.
(insertMark): Grow array in bigger chunks to avoid excessive
copying.
(binarySearch): New method. An adaption of Arrays.binarySearch()
that allows for an maxIndex parameter.
/Roman
--
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: javax/swing/text/GapContent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/GapContent.java,v
retrieving revision 1.47
diff -u -1 -0 -r1.47 GapContent.java
--- javax/swing/text/GapContent.java 17 May 2006 16:28:34 -0000 1.47
+++ javax/swing/text/GapContent.java 18 May 2006 13:56:37 -0000
@@ -32,21 +32,20 @@
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.text;
import java.io.Serializable;
-import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import java.util.WeakHashMap;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoableEdit;
@@ -80,21 +79,21 @@
* Creates a new GapContentPosition object.
*
* @param mark the mark of this Position
*/
GapContentPosition(int mark)
{
// Try to find the mark in the positionMarks array, and store the index
// to it.
synchronized (GapContent.this)
{
- int i = Arrays.binarySearch(positionMarks, mark);
+ int i = binarySearch(positionMarks, mark, numMarks);
if (i >= 0) // mark found
{
index = i;
}
else
{
index = -i - 1;
insertMark(index, mark);
}
}
@@ -228,20 +227,26 @@
// corresponding marks, or alternativly, perform some regular cleanup of
// the positionMarks array.
/**
* Holds the marks for positions. These marks are referenced by the
* GapContentPosition instances by an index into this array.
*/
int[] positionMarks;
/**
+ * The number of elements in the positionMarks array. The positionMarks array
+ * might be bigger than the actual number of elements.
+ */
+ int numMarks;
+
+ /**
* (Weakly) Stores the GapContentPosition instances.
*/
WeakHashMap positions;
/**
* Creates a new GapContent object.
*/
public GapContent()
{
this(DEFAULT_BUFSIZE);
@@ -253,21 +258,22 @@
* @param size the initial size of the buffer
*/
public GapContent(int size)
{
size = Math.max(size, 2);
buffer = (char[]) allocateArray(size);
gapStart = 1;
gapEnd = size;
buffer[0] = '\n';
positions = new WeakHashMap();
- positionMarks = new int[0];
+ positionMarks = new int[10];
+ numMarks = 0;
}
/**
* Allocates an array of the specified length that can then be used as
* buffer.
*
* @param size the size of the array to be allocated
*
* @return the allocated array
*/
@@ -545,40 +551,40 @@
*
* @param newGapStart the new gap start
*/
protected void shiftGapStartDown(int newGapStart)
{
if (newGapStart == gapStart)
return;
assert newGapStart < gapStart : "The new gap start must be less than the "
+ "old gap start.";
- setPositionsInRange(newGapStart, gapStart - newGapStart, gapStart);
+ setPositionsInRange(newGapStart, gapStart, false);
gapStart = newGapStart;
}
/**
* Shifts the gap end upwards. This does not affect the content of the
* buffer. This only updates the gap end and all the marks that are between
* the old gap end and the new end start. They all are squeezed to the end
* of the gap, because their location has been removed.
*
* @param newGapEnd the new gap start
*/
protected void shiftGapEndUp(int newGapEnd)
{
if (newGapEnd == gapEnd)
return;
assert newGapEnd > gapEnd : "The new gap end must be greater than the "
+ "old gap end.";
- setPositionsInRange(gapEnd, newGapEnd - gapEnd, newGapEnd);
+ setPositionsInRange(gapEnd, newGapEnd, false);
gapEnd = newGapEnd;
}
/**
* Returns the allocated buffer array.
*
* @return the allocated buffer array
*/
protected final Object getArray()
{
@@ -665,85 +671,143 @@
GapContentPosition p = (GapContentPosition) i.next();
int offs = p.getOffset();
if (offs >= offset && offs < endOffs)
res.add(p);
}
return res;
}
/**
- * Sets the mark of all <code>Position</code>s that are in the range
- * specified by <code>offset</code> and </code>length</code> within
- * the buffer array to <code>value</code>
+ * Crunches all positions in the specified range to either the start or
+ * end of that interval. The interval boundaries are meant to be inclusive
+ * [start, end].
*
- * @param offset the start offset of the range to search
- * @param length the length of the range to search
- * @param value the new value for each mark
- */
- private void setPositionsInRange(int offset, int length, int value)
- {
- int endMark = offset + length;
+ * @param start the start offset of the range
+ * @param end the end offset of the range
+ * @param toStart a boolean indicating if the positions should be crunched
+ * to the start (true) or to the end (false)
+ */
+ private void setPositionsInRange(int start, int end, boolean toStart)
+ {
+ // We slump together all the GapContentPositions to point to
+ // one mark. So this is implemented as follows:
+ // 1. Remove all the marks in the specified range.
+ // 2. Insert one new mark at the correct location.
+ // 3. Adjust all affected GapContentPosition instances to point to
+ // this new mark.
synchronized (this)
{
- int startIndex = Arrays.binarySearch(positionMarks, offset);
+ int startIndex = binarySearch(positionMarks, start, numMarks);
if (startIndex < 0) // Translate to insertion index, if not found.
startIndex = - startIndex - 1;
- int endIndex = Arrays.binarySearch(positionMarks, endMark);
+ int endIndex = binarySearch(positionMarks, end, numMarks);
if (endIndex < 0) // Translate to insertion index - 1, if not found.
endIndex = - endIndex - 2;
- for (int i = startIndex; i <= endIndex; i++)
- positionMarks[i] = value;
- }
+ // Update the marks.
+ // We have inclusive interval bounds, but let one element over for
+ // filling in the new value.
+ int removed = endIndex - startIndex;
+ if (removed <= 0)
+ return;
+ System.arraycopy(positionMarks, endIndex + 1, positionMarks,
+ startIndex + 1, positionMarks.length - endIndex - 1);
+ numMarks -= removed;
+ if (toStart)
+ {
+ positionMarks[startIndex] = start;
+ }
+ else
+ {
+ positionMarks[startIndex] = end;
+ }
+
+ // Update all affected GapContentPositions to point to the new index
+ // and all GapContentPositions that come after the interval to
+ // have their index moved by -removed.
+ Set positionSet = positions.keySet();
+ for (Iterator i = positionSet.iterator(); i.hasNext();)
+ {
+ GapContentPosition p = (GapContentPosition) i.next();
+ if (p.index > start || p.index <= end)
+ p.index = start;
+ else if (p.index > end)
+ p.index -= removed;
+ }
+ }
}
/**
* Adjusts the mark of all <code>Position</code>s that are in the range
* specified by <code>offset</code> and </code>length</code> within
* the buffer array by <code>increment</code>
*
* @param offset the start offset of the range to search
* @param length the length of the range to search
* @param incr the increment
*/
private void adjustPositionsInRange(int offset, int length, int incr)
{
int endMark = offset + length;
synchronized (this)
{
- int startIndex = Arrays.binarySearch(positionMarks, offset);
+ // Find the start and end indices in the positionMarks array.
+ int startIndex = binarySearch(positionMarks, offset, numMarks);
if (startIndex < 0) // Translate to insertion index, if not found.
startIndex = - startIndex - 1;
- int endIndex = Arrays.binarySearch(positionMarks, endMark);
+ int endIndex = binarySearch(positionMarks, endMark, numMarks);
if (endIndex < 0) // Translate to insertion index - 1, if not found.
endIndex = - endIndex - 2;
+ // We must not change the order of the marks, this would have
+ // unpredictable results while binary-searching the marks.
+ assert (startIndex <= 0
+ || positionMarks[startIndex - 1]
+ <= positionMarks [startIndex] + incr)
+ && (endIndex >= numMarks - 1
+ || positionMarks[endIndex + 1]
+ >= positionMarks[endIndex] + incr)
+ : "Adjusting the marks must not change their order";
+
+ // Some debug helper output to determine if the start or end of the
+ // should ever be coalesced together with adjecent marks.
+ if (startIndex > 0 && positionMarks[startIndex - 1]
+ == positionMarks[startIndex] + incr)
+ System.err.println("DEBUG: We could coalesce the start of the region"
+ + " in GapContent.adjustPositionsInRange()");
+ if (endIndex < numMarks - 1 && positionMarks[endIndex + 1]
+ == positionMarks[endIndex] + incr)
+ System.err.println("DEBUG: We could coalesce the end of the region"
+ + " in GapContent.adjustPositionsInRange()");
+
+ // Actually adjust the marks.
for (int i = startIndex; i <= endIndex; i++)
positionMarks[i] += incr;
}
+
}
/**
* Resets all <code>Position</code> that have an offset of <code>0</code>,
* to also have an array index of <code>0</code>. This might be necessary
* after a call to <code>shiftGap(0)</code>, since then the marks at offset
* <code>0</code> get shifted to <code>gapEnd</code>.
*/
protected void resetMarksAtZero()
{
if (gapStart != 0)
return;
- setPositionsInRange(gapEnd, 0, 0);
+ positionMarks[0] = 0;
}
/**
* @specnote This method is not very well specified and the positions vector
* is implementation specific. The undo positions are managed
* differently in this implementation, this method is only here
* for binary compatibility.
*/
protected void updateUndoPositions(Vector positions, int offset, int length)
{
@@ -770,20 +834,31 @@
if (!Character.isISOControl(buffer[i]))
System.err.print(buffer[i]);
else
System.err.print('.');
}
System.err.println();
}
/**
+ * Prints out the position marks.
+ */
+ private void dumpMarks()
+ {
+ System.err.print("positionMarks: ");
+ for (int i = 0; i < positionMarks.length; i++)
+ System.err.print(positionMarks[i] + ", ");
+ System.err.println();
+ }
+
+ /**
* Inserts a mark into the positionMarks array. This must update all the
* GapContentPosition instances in positions that come after insertionPoint.
*
* This is package private to avoid synthetic accessor methods.
*
* @param insertionPoint the index at which to insert the mark
* @param mark the mark to insert
*/
void insertMark(int insertionPoint, int mark)
{
@@ -792,22 +867,67 @@
// Update the positions.
Set positionSet = positions.keySet();
for (Iterator i = positionSet.iterator(); i.hasNext();)
{
GapContentPosition p = (GapContentPosition) i.next();
if (p.index >= insertionPoint)
p.index++;
}
// Update the position marks.
- // TODO: We might want to do this more efficiently by enlarging the
- // array in bigger chunks than 1.
- int[] newMarks = new int[positionMarks.length + 1];
- System.arraycopy(positionMarks, 0, newMarks, 0, insertionPoint);
- newMarks[insertionPoint] = mark;
- System.arraycopy(positionMarks, insertionPoint, newMarks,
- insertionPoint + 1,
- positionMarks.length - insertionPoint);
- positionMarks = newMarks;
+ if (positionMarks.length <= numMarks)
+ {
+ int[] newMarks = new int[positionMarks.length + 10];
+ System.arraycopy(positionMarks, 0, newMarks, 0, insertionPoint);
+ newMarks[insertionPoint] = mark;
+ System.arraycopy(positionMarks, insertionPoint, newMarks,
+ insertionPoint + 1,
+ numMarks - insertionPoint);
+ positionMarks = newMarks;
+ }
+ else
+ {
+ System.arraycopy(positionMarks, insertionPoint, positionMarks,
+ insertionPoint + 1,
+ numMarks - insertionPoint);
+ positionMarks[insertionPoint] = mark;
+ }
+ numMarks++;
+ }
+ }
+
+ /**
+ * An adaption of [EMAIL PROTECTED] java.util.Arrays#binarySearch(int[], int)} to
+ * specify a maximum index up to which the array is searched. This allows
+ * us to have some trailing entries that we ignore.
+ *
+ * This is package private to avoid synthetic accessor methods.
+ *
+ * @param a the array
+ * @param key the key to search for
+ * @param maxIndex the maximum index up to which the search is performed
+ *
+ * @return the index of the found entry, or (-(index)-1) for the
+ * insertion point when not found
+ *
+ * @see java.util.Arrays#binarySearch(int[], int)
+ */
+ int binarySearch(int[] a, int key, int maxIndex)
+ {
+ int low = 0;
+ int hi = maxIndex - 1;
+ int mid = 0;
+ while (low <= hi)
+ {
+ mid = (low + hi) >> 1;
+ final int d = a[mid];
+ if (d == key)
+ return mid;
+ else if (d > key)
+ hi = mid - 1;
+ else
+ // This gets the insertion point right on the last loop.
+ low = ++mid;
}
+ return -mid - 1;
}
}
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
