Class GapTextStore
- All Implemented Interfaces:
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate char[]
The store's contentprivate int
End index of the gapprivate int
Starting index of the gapprivate final int
The maximum gap size allocated when re-allocation occurs.private final int
The minimum gap size allocated when re-allocation occurs.private final float
The multiplier to compute the array size from the content length (1 <= fSizeMultiplier <= 2).private int
The current high water mark. -
Constructor Summary
ConstructorsConstructorDescriptionEquivalent 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. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
adjustGap
(int offset, int remove, int add) Moves the gap tooffset + add
, moving any content afteroffset + remove
behind the gap.private char[]
allocate
(int size) Allocates a newchar[size]
.private void
arrayCopy
(int srcPos, char[] dest, int destPos, int length) private int
gapSize()
Returns the gap size.final char
get
(int offset) Returns the character at the specified offset.final String
get
(int offset, int length) Returns the text of the specified character range.protected String
Returns a copy of the content of this text store.protected int
Returns the end index of the gap managed by this text store.protected int
Returns the start index of the gap managed by this text store.final int
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 tonewGapStart
.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.final void
Replaces the specified character range with the given text.final void
Replace the content of the text store with the given text.
-
Field Details
-
fMinGapSize
private final int fMinGapSizeThe minimum gap size allocated when re-allocation occurs. @since 3.3 -
fMaxGapSize
private final int fMaxGapSizeThe maximum gap size allocated when re-allocation occurs. @since 3.3 -
fSizeMultiplier
private final float fSizeMultiplierThe multiplier to compute the array size from the content length (1 <= fSizeMultiplier <= 2). @since 3.3 -
fContent
private char[] fContentThe store's content -
fGapStart
private int fGapStartStarting index of the gap -
fGapEnd
private int fGapEndEnd index of the gap -
fThreshold
private int fThresholdThe current high water mark. If a change would cause the gap to grow larger than this, the array is re-allocated. @since 3.3
-
-
Constructor Details
-
GapTextStore
public GapTextStore(int lowWatermark, int highWatermark) Creates a new empty text store using the specified low and high watermarks.- Parameters:
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 useGapTextStore(int, int, float)
instead
-
GapTextStore
public GapTextStore()Equivalent to new GapTextStore(256, 4096, 0.1f). @since 3.3 -
GapTextStore
public GapTextStore(int minSize, int maxSize, float maxGapFactor) Creates an empty text store that uses re-allocation thresholds relative to the content length. Re-allocation is controlled by the gap factor, which is the quotient of the gap size and the array size. Re-allocation occurs if a change causes the gap factor to go outside[0, maxGapFactor]
. When re-allocation occurs, the array is sized such that the gap factor is0.5 * maxGapFactor
. The gap size computed in this manner is bounded by theminSize
andmaxSize
parameters.A
maxGapFactor
of0
creates a text store that never has a gap at all (ifminSize
is 0); amaxGapFactor
of1
creates a text store that doubles its size with every re-allocation and that never shrinks.The
minSize
andmaxSize
parameters are absolute bounds to the allocated gap size. UseminSize
to avoid frequent re-allocation for small documents. UsemaxSize
to avoid a huge gap being allocated for large documents.- Parameters:
minSize
- the minimum gap size to allocate (>= 0; use 0 for no minimum)maxSize
- the maximum gap size to allocate (>= minSize; useInteger.MAX_VALUE
for no maximum)maxGapFactor
- is the maximum fraction of the array that is occupied by the gap (0 <= maxGapFactor <= 1
) @since 3.3
-
-
Method Details
-
get
public final char get(int offset) Description copied from interface:ITextStore
Returns the character at the specified offset.- Specified by:
get
in interfaceITextStore
- Parameters:
offset
- the offset in this text store- Returns:
- the character at this offset
-
get
Description copied from interface:ITextStore
Returns the text of the specified character range.- Specified by:
get
in interfaceITextStore
- Parameters:
offset
- the offset of the rangelength
- the length of the range- Returns:
- the text of the range
-
getLength
public final int getLength()Description copied from interface:ITextStore
Returns number of characters stored in this text store.- Specified by:
getLength
in interfaceITextStore
- Returns:
- the number of characters stored in this text store
-
set
Description copied from interface:ITextStore
Replace the content of the text store with the given text. Convenience method forreplace(0, getLength(), text
.- Specified by:
set
in interfaceITextStore
- Parameters:
text
- the new content of the text store
-
replace
Description copied from interface:ITextStore
Replaces the specified character range with the given text.replace(getLength(), 0, "some text")
is a valid call and appends text to the end of the text store.- Specified by:
replace
in interfaceITextStore
- Parameters:
offset
- the offset of the range to be replacedlength
- the number of characters to be replacedtext
- the substitution text
-
adjustGap
private void adjustGap(int offset, int remove, int add) Moves the gap tooffset + add
, moving any content afteroffset + remove
behind the gap. The gap size is kept between 0 andfThreshold
, leading to re-allocation if needed. The content betweenoffset
andoffset + add
is undefined after this operation.- Parameters:
offset
- the offset at which a change happensremove
- the number of character which are removed or overwritten atoffset
add
- the number of character which are inserted or overwriting atoffset
-
moveGap
private int moveGap(int offset, int remove, int oldGapSize, int newGapSize, int newGapStart) Moves the gap tonewGapStart
.- Parameters:
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 to- Returns:
- the new gap end @since 3.3
-
reallocate
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.- Parameters:
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 to- Returns:
- the new gap end @since 3.3
-
allocate
private char[] allocate(int size) Allocates a newchar[size]
.- Parameters:
size
- the length of the new array.- Returns:
- a newly allocated char array @since 3.3
-
arrayCopy
private void arrayCopy(int srcPos, char[] dest, int destPos, int length) -
gapSize
private int gapSize()Returns the gap size.- Returns:
- the gap size @since 3.3
-
getContentAsString
Returns a copy of the content of this text store. For internal use only.- Returns:
- a copy of the content of this text store
-
getGapStartIndex
protected int getGapStartIndex()Returns the start index of the gap managed by this text store. For internal use only.- Returns:
- the start index of the gap managed by this text store
-
getGapEndIndex
protected int getGapEndIndex()Returns the end index of the gap managed by this text store. For internal use only.- Returns:
- the end index of the gap managed by this text store
-