comparison dwtx/jface/text/GapTextStore.d @ 156:a9566845f1cb

...
author Frank Benoit <benoit@tionex.de>
date Mon, 25 Aug 2008 19:03:46 +0200
parents 75302ef3f92f
children 1a5b8f8129df
comparison
equal deleted inserted replaced
155:8442b6b2da2d 156:a9566845f1cb
176 * How frequently the array needs re-allocation is controlled by the constructor parameters. 176 * How frequently the array needs re-allocation is controlled by the constructor parameters.
177 * </p> 177 * </p>
178 * <p> 178 * <p>
179 * This class is not intended to be subclassed. 179 * This class is not intended to be subclassed.
180 * </p> 180 * </p>
181 * 181 *
182 * @see CopyOnWriteTextStore for a copy-on-write text store wrapper 182 * @see CopyOnWriteTextStore for a copy-on-write text store wrapper
183 * @noextend This class is not intended to be subclassed by clients. 183 * @noextend This class is not intended to be subclassed by clients.
184 */ 184 */
185 public class GapTextStore : ITextStore { 185 public class GapTextStore : ITextStore {
186 /** 186 /**
194 */ 194 */
195 private const int fMaxGapSize; 195 private const int fMaxGapSize;
196 /** 196 /**
197 * The multiplier to compute the array size from the content length 197 * The multiplier to compute the array size from the content length
198 * (1&nbsp;&lt;=&nbsp;fSizeMultiplier&nbsp;&lt;=&nbsp;2). 198 * (1&nbsp;&lt;=&nbsp;fSizeMultiplier&nbsp;&lt;=&nbsp;2).
199 * 199 *
200 * @since 3.3 200 * @since 3.3
201 */ 201 */
202 private const float fSizeMultiplier; 202 private const float fSizeMultiplier;
203 203
204 /** The store's content */ 204 /** The store's content */
214 */ 214 */
215 private int fThreshold= 0; 215 private int fThreshold= 0;
216 216
217 /** 217 /**
218 * Creates a new empty text store using the specified low and high watermarks. 218 * Creates a new empty text store using the specified low and high watermarks.
219 * 219 *
220 * @param lowWatermark unused - at the lower bound, the array is only resized when the content 220 * @param lowWatermark unused - at the lower bound, the array is only resized when the content
221 * does not fit 221 * does not fit
222 * @param highWatermark if the gap is ever larger than this, it will automatically be shrunken 222 * @param highWatermark if the gap is ever larger than this, it will automatically be shrunken
223 * (&gt;=&nbsp;0) 223 * (&gt;=&nbsp;0)
224 * @deprecated use {@link GapTextStore#GapTextStore(int, int, float)} instead 224 * @deprecated use {@link GapTextStore#GapTextStore(int, int, float)} instead
237 * lowWatermark, which doesn't make any sense: that area of the gap was simply never ever 237 * lowWatermark, which doesn't make any sense: that area of the gap was simply never ever
238 * used. 238 * used.
239 */ 239 */
240 this(highWatermark / 2, highWatermark / 2, 0f); 240 this(highWatermark / 2, highWatermark / 2, 0f);
241 } 241 }
242 242
243 /** 243 /**
244 * Equivalent to 244 * Equivalent to
245 * {@linkplain GapTextStore#GapTextStore(int, int, float) new GapTextStore(256, 4096, 0.1f)}. 245 * {@linkplain GapTextStore#GapTextStore(int, int, float) new GapTextStore(256, 4096, 0.1f)}.
246 * 246 *
247 * @since 3.3 247 * @since 3.3
248 */ 248 */
249 public this() { 249 public this() {
250 this(256, 4096, 0.1f); 250 this(256, 4096, 0.1f);
251 } 251 }
266 * The <code>minSize</code> and <code>maxSize</code> parameters are absolute bounds to the 266 * The <code>minSize</code> and <code>maxSize</code> parameters are absolute bounds to the
267 * allocated gap size. Use <code>minSize</code> to avoid frequent re-allocation for small 267 * allocated gap size. Use <code>minSize</code> to avoid frequent re-allocation for small
268 * documents. Use <code>maxSize</code> to avoid a huge gap being allocated for large 268 * documents. Use <code>maxSize</code> to avoid a huge gap being allocated for large
269 * documents. 269 * documents.
270 * </p> 270 * </p>
271 * 271 *
272 * @param minSize the minimum gap size to allocate (&gt;=&nbsp;0; use 0 for no minimum) 272 * @param minSize the minimum gap size to allocate (&gt;=&nbsp;0; use 0 for no minimum)
273 * @param maxSize the maximum gap size to allocate (&gt;=&nbsp;minSize; use 273 * @param maxSize the maximum gap size to allocate (&gt;=&nbsp;minSize; use
274 * {@link Integer#MAX_VALUE} for no maximum) 274 * {@link Integer#MAX_VALUE} for no maximum)
275 * @param maxGapFactor is the maximum fraction of the array that is occupied by the gap (<code>0&nbsp;&lt;=&nbsp;maxGapFactor&nbsp;&lt;=&nbsp;1</code>) 275 * @param maxGapFactor is the maximum fraction of the array that is occupied by the gap (<code>0&nbsp;&lt;=&nbsp;maxGapFactor&nbsp;&lt;=&nbsp;1</code>)
276 * @since 3.3 276 * @since 3.3
348 /** 348 /**
349 * Moves the gap to <code>offset + add</code>, moving any content after 349 * Moves the gap to <code>offset + add</code>, moving any content after
350 * <code>offset + remove</code> behind the gap. The gap size is kept between 0 and 350 * <code>offset + remove</code> behind the gap. The gap size is kept between 0 and
351 * {@link #fThreshold}, leading to re-allocation if needed. The content between 351 * {@link #fThreshold}, leading to re-allocation if needed. The content between
352 * <code>offset</code> and <code>offset + add</code> is undefined after this operation. 352 * <code>offset</code> and <code>offset + add</code> is undefined after this operation.
353 * 353 *
354 * @param offset the offset at which a change happens 354 * @param offset the offset at which a change happens
355 * @param remove the number of character which are removed or overwritten at <code>offset</code> 355 * @param remove the number of character which are removed or overwritten at <code>offset</code>
356 * @param add the number of character which are inserted or overwriting at <code>offset</code> 356 * @param add the number of character which are inserted or overwriting at <code>offset</code>
357 */ 357 */
358 private void adjustGap(int offset, int remove, int add) { 358 private void adjustGap(int offset, int remove, int add) {
372 fGapEnd= newGapEnd; 372 fGapEnd= newGapEnd;
373 } 373 }
374 374
375 /** 375 /**
376 * Moves the gap to <code>newGapStart</code>. 376 * Moves the gap to <code>newGapStart</code>.
377 * 377 *
378 * @param offset the change offset 378 * @param offset the change offset
379 * @param remove the number of removed / overwritten characters 379 * @param remove the number of removed / overwritten characters
380 * @param oldGapSize the old gap size 380 * @param oldGapSize the old gap size
381 * @param newGapSize the gap size after the change 381 * @param newGapSize the gap size after the change
382 * @param newGapStart the offset in the array to move the gap to 382 * @param newGapStart the offset in the array to move the gap to
404 return newGapEnd; 404 return newGapEnd;
405 } 405 }
406 406
407 /** 407 /**
408 * Reallocates a new array and copies the data from the previous one. 408 * Reallocates a new array and copies the data from the previous one.
409 * 409 *
410 * @param offset the change offset 410 * @param offset the change offset
411 * @param remove the number of removed / overwritten characters 411 * @param remove the number of removed / overwritten characters
412 * @param oldGapSize the old gap size 412 * @param oldGapSize the old gap size
413 * @param newGapSize the gap size after the change if no re-allocation would occur (can be negative) 413 * @param newGapSize the gap size after the change if no re-allocation would occur (can be negative)
414 * @param newGapStart the offset in the array to move the gap to 414 * @param newGapStart the offset in the array to move the gap to
415 * @return the new gap end 415 * @return the new gap end
416 * @since 3.3 416 * @since 3.3
417 */ 417 */
418 private int reallocate(int offset, int remove, final int oldGapSize, int newGapSize, final int newGapStart) { 418 private int reallocate(int offset, int remove, int oldGapSize, int newGapSize, int newGapStart) {
419 // the new content length (without any gap) 419 // the new content length (without any gap)
420 final int newLength= fContent.length - newGapSize; 420 final int newLength= fContent.length - newGapSize;
421 // the new array size based on the gap factor 421 // the new array size based on the gap factor
422 int newArraySize= cast(int) (newLength * fSizeMultiplier); 422 int newArraySize= cast(int) (newLength * fSizeMultiplier);
423 newGapSize= newArraySize - newLength; 423 newGapSize= newArraySize - newLength;
474 return newGapEnd; 474 return newGapEnd;
475 } 475 }
476 476
477 /** 477 /**
478 * Allocates a new <code>char[size]</code>. 478 * Allocates a new <code>char[size]</code>.
479 * 479 *
480 * @param size the length of the new array. 480 * @param size the length of the new array.
481 * @return a newly allocated char array 481 * @return a newly allocated char array
482 * @since 3.3 482 * @since 3.3
483 */ 483 */
484 private char[] allocate(int size) { 484 private char[] allocate(int size) {
495 System.arraycopy(fContent, srcPos, dest, destPos, length); 495 System.arraycopy(fContent, srcPos, dest, destPos, length);
496 } 496 }
497 497
498 /** 498 /**
499 * Returns the gap size. 499 * Returns the gap size.
500 * 500 *
501 * @return the gap size 501 * @return the gap size
502 * @since 3.3 502 * @since 3.3
503 */ 503 */
504 private int gapSize() { 504 private int gapSize() {
505 return fGapEnd - fGapStart; 505 return fGapEnd - fGapStart;