public class GapTextStore extends java.lang.Object implements ITextStore
Performance: Typing-style changes perform in constant time
unless re-allocation becomes necessary. Generally, a change that does not
cause re-allocation will cause at most one
arraycopy
operation of a length of about d, where d is the
distance from the previous change. Let a(x) be the algorithmic
performance of an arraycopy
operation of the length x
, then such a change then performs in O(a(x)),
get(int, length) performs in
O(a(length)), get(int)
in O(1).
How frequently the array needs re-allocation is controlled by the constructor parameters.
This class is not intended to be subclassed.
Modifier and Type | Field and Description |
---|---|
private char[] |
fContent
The store's content
|
private int |
fGapEnd
End index of the gap
|
private int |
fGapStart
Starting index of the gap
|
private int |
fMaxGapSize
The maximum gap size allocated when re-allocation occurs.
|
private int |
fMinGapSize
The minimum gap size allocated when re-allocation occurs.
|
private float |
fSizeMultiplier
The multiplier to compute the array size from the content length
(1 <= fSizeMultiplier <= 2).
|
private int |
fThreshold
The current high water mark.
|
Constructor and Description |
---|
GapTextStore()
Equivalent to new
GapTextStore(256, 4096, 0.1f).
|
GapTextStore(int lowWatermark,
int highWatermark)
Creates a new empty text store using the specified low and high
watermarks.
|
GapTextStore(int minSize,
int maxSize,
float maxGapFactor)
Creates an empty text store that uses re-allocation thresholds relative
to the content length.
|
Modifier and Type | Method and Description |
---|---|
private void |
adjustGap(int offset,
int remove,
int add)
Moves the gap to
offset + add , moving any content after
offset + remove behind the gap. |
private char[] |
allocate(int size)
Allocates a new
char[size] . |
private void |
arrayCopy(int srcPos,
char[] dest,
int destPos,
int length) |
private int |
gapSize()
Returns the gap size.
|
char |
get(int offset)
Returns the character at the specified offset.
|
java.lang.String |
get(int offset,
int length)
Returns the text of the specified character range.
|
protected java.lang.String |
getContentAsString()
Returns a copy of the content of this text store.
|
protected int |
getGapEndIndex()
Returns the end index of the gap managed by this text store.
|
protected int |
getGapStartIndex()
Returns the start index of the gap managed by this text store.
|
int |
getLength()
Returns number of characters stored in this text store.
|
private int |
moveGap(int offset,
int remove,
int oldGapSize,
int newGapSize,
int newGapStart)
Moves the gap to
newGapStart . |
private int |
reallocate(int offset,
int remove,
int oldGapSize,
int newGapSize,
int newGapStart)
Reallocates a new array and copies the data from the previous one.
|
void |
replace(int offset,
int length,
java.lang.String text)
Replaces the specified character range with the given text.
|
void |
set(java.lang.String text)
Replace the content of the text store with the given text.
|
private final int fMinGapSize
private final int fMaxGapSize
private final float fSizeMultiplier
private char[] fContent
private int fGapStart
private int fGapEnd
private int fThreshold
public GapTextStore(int lowWatermark, int highWatermark)
lowWatermark
- unused - at the lower bound, the array is only
resized when the content does not fithighWatermark
- if the gap is ever larger than this, it will
automatically be shrunken (>= 0) @deprecated use
GapTextStore(int, int, float)
insteadpublic GapTextStore()
public GapTextStore(int minSize, int maxSize, float maxGapFactor)
[0, maxGapFactor]
. When re-allocation occurs, the array
is sized such that the gap factor is 0.5 * maxGapFactor
. The
gap size computed in this manner is bounded by the minSize
and maxSize
parameters.
A maxGapFactor
of 0
creates a text store that
never has a gap at all (if minSize
is 0); a
maxGapFactor
of 1
creates a text store that
doubles its size with every re-allocation and that never shrinks.
The minSize
and maxSize
parameters are absolute
bounds to the allocated gap size. Use minSize
to avoid
frequent re-allocation for small documents. Use maxSize
to
avoid a huge gap being allocated for large documents.
minSize
- the minimum gap size to allocate (>= 0; use 0 for
no minimum)maxSize
- the maximum gap size to allocate (>= minSize; use
Integer.MAX_VALUE
for no maximum)maxGapFactor
- is the maximum fraction of the array that is occupied
by the gap (
0 <= maxGapFactor <= 1
) @since 3.3public final char get(int offset)
ITextStore
get
in interface ITextStore
offset
- the offset in this text storepublic final java.lang.String get(int offset, int length)
ITextStore
get
in interface ITextStore
offset
- the offset of the rangelength
- the length of the rangepublic final int getLength()
ITextStore
getLength
in interface ITextStore
public final void set(java.lang.String text)
ITextStore
replace(0, getLength(), text
.set
in interface ITextStore
text
- the new content of the text storepublic final void replace(int offset, int length, java.lang.String text)
ITextStore
replace(getLength(), 0, "some text")
is a valid call and
appends text to the end of the text store.replace
in interface ITextStore
offset
- the offset of the range to be replacedlength
- the number of characters to be replacedtext
- the substitution textprivate void adjustGap(int offset, int remove, int add)
offset + add
, moving any content after
offset + remove
behind the gap. The gap size is kept between
0 and fThreshold
, leading to re-allocation if needed. The
content between offset
and offset + add
is
undefined after this operation.offset
- the offset at which a change happensremove
- the number of character which are removed or overwritten at
offset
add
- the number of character which are inserted or overwriting at
offset
private int moveGap(int offset, int remove, int oldGapSize, int newGapSize, int newGapStart)
newGapStart
.offset
- the change offsetremove
- the number of removed / overwritten charactersoldGapSize
- the old gap sizenewGapSize
- the gap size after the changenewGapStart
- the offset in the array to move the gap toprivate int reallocate(int offset, int remove, int oldGapSize, int newGapSize, int newGapStart)
offset
- the change offsetremove
- the number of removed / overwritten charactersoldGapSize
- the old gap sizenewGapSize
- the gap size after the change if no re-allocation would
occur (can be negative)newGapStart
- the offset in the array to move the gap toprivate char[] allocate(int size)
char[size]
.size
- the length of the new array.private void arrayCopy(int srcPos, char[] dest, int destPos, int length)
private int gapSize()
protected java.lang.String getContentAsString()
protected int getGapStartIndex()
protected int getGapEndIndex()