1. /*
  2. * @(#)GapVector.java 1.7 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text;
  11. import java.util.Vector;
  12. import java.io.Serializable;
  13. import javax.swing.undo.UndoableEdit;
  14. /**
  15. * An implementation of a gapped buffer similar to that used by
  16. * emacs. The underlying storage is a java array of some type,
  17. * which is known only by the subclass of this class. The array
  18. * has a gap somewhere. The gap is moved to the location of changes
  19. * to take advantage of common behavior where most changes occur
  20. * in the same location. Changes that occur at a gap boundary are
  21. * generally cheap and moving the gap is generally cheaper than
  22. * moving the array contents directly to accomodate the change.
  23. *
  24. * @author Timothy Prinzing
  25. * @version 1.7 02/02/00
  26. * @see GapContent
  27. */
  28. abstract class GapVector implements Serializable {
  29. /**
  30. * Creates a new GapVector object. Initial size defaults to 10.
  31. */
  32. public GapVector() {
  33. this(10);
  34. }
  35. /**
  36. * Creates a new GapVector object, with the initial
  37. * size specified.
  38. *
  39. * @param initialLength the initial size
  40. */
  41. public GapVector(int initialLength) {
  42. array = allocateArray(initialLength);
  43. g0 = 0;
  44. g1 = initialLength;
  45. }
  46. /**
  47. * Allocate an array to store items of the type
  48. * appropriate (which is determined by the subclass).
  49. */
  50. protected abstract Object allocateArray(int len);
  51. /**
  52. * Get the length of the allocated array
  53. */
  54. protected abstract int getArrayLength();
  55. /**
  56. * Access to the array. The actual type
  57. * of the array is known only by the subclass.
  58. */
  59. protected final Object getArray() {
  60. return array;
  61. }
  62. /**
  63. * Access to the start of the gap.
  64. */
  65. protected final int getGapStart() {
  66. return g0;
  67. }
  68. /**
  69. * Access to the end of the gap.
  70. */
  71. protected final int getGapEnd() {
  72. return g1;
  73. }
  74. // ---- variables -----------------------------------
  75. /**
  76. * The array of items. The type is determined by the subclass.
  77. */
  78. private Object array;
  79. /**
  80. * start of gap in the array
  81. */
  82. private int g0;
  83. /**
  84. * end of gap in the array
  85. */
  86. private int g1;
  87. // --- gap management -------------------------------
  88. /**
  89. * Replace the given logical position in the storage with
  90. * the given new items. This will move the gap to the area
  91. * being changed if the gap is not currently located at the
  92. * change location.
  93. *
  94. * @param position the location to make the replacement. This
  95. * is not the location in the underlying storage array, but
  96. * the location in the contiguous space being modeled.
  97. * @param rmSize the number of items to remove
  98. * @param addItems the new items to place in storage.
  99. */
  100. protected void replace(int position, int rmSize, Object addItems, int addSize) {
  101. int addOffset = 0;
  102. if (addSize == 0) {
  103. close(position, rmSize);
  104. return;
  105. } else if (rmSize > addSize) {
  106. /* Shrink the end. */
  107. close(position+addSize, rmSize-addSize);
  108. } else {
  109. /* Grow the end, do two chunks. */
  110. int endSize = addSize - rmSize;
  111. int end = open(position + rmSize, endSize);
  112. System.arraycopy(addItems, rmSize, array, end, endSize);
  113. addSize = rmSize;
  114. }
  115. System.arraycopy(addItems, addOffset, array, position, addSize);
  116. }
  117. /**
  118. * Delete nItems at position. Squeezes any marks
  119. * within the deleted area to position. This moves
  120. * the gap to the best place by minimizing it's
  121. * overall movement. The gap must intersect the
  122. * target block.
  123. */
  124. void close(int position, int nItems) {
  125. if (nItems == 0) return;
  126. int end = position + nItems;
  127. int new_gs = (g1 - g0) + nItems;
  128. if (end <= g0) {
  129. // Move gap to end of block.
  130. if (g0 != end) {
  131. shiftGap(end);
  132. }
  133. // Adjust g0.
  134. shiftGapStartDown(g0 - nItems);
  135. } else if (position >= g0) {
  136. // Move gap to beginning of block.
  137. if (g0 != position) {
  138. shiftGap(position);
  139. }
  140. // Adjust g1.
  141. shiftGapEndUp(g0 + new_gs);
  142. } else {
  143. // The gap is properly inside the target block.
  144. // No data movement necessary, simply move both gap pointers.
  145. shiftGapStartDown(position);
  146. shiftGapEndUp(g0 + new_gs);
  147. }
  148. }
  149. /**
  150. * Make space for the given number of items at the given
  151. * location.
  152. *
  153. * @returns the location that the caller should fill in.
  154. */
  155. int open(int position, int nItems) {
  156. int gapSize = g1 - g0;
  157. if (nItems == 0) {
  158. if (position > g0)
  159. position += gapSize;
  160. return position;
  161. }
  162. // Expand the array if the gap is too small.
  163. shiftGap(position);
  164. if (nItems >= gapSize) {
  165. // Pre-shift the gap, to reduce total movement.
  166. shiftEnd(getArrayLength() - gapSize + nItems);
  167. gapSize = g1 - g0;
  168. }
  169. g0 = g0 + nItems;
  170. return position;
  171. }
  172. /**
  173. * resize the underlying storage array to the
  174. * given new size
  175. */
  176. void resize(int nsize) {
  177. Object narray = allocateArray(nsize);
  178. System.arraycopy(array, 0, narray, 0, Math.min(nsize, getArrayLength()));
  179. array = narray;
  180. }
  181. /**
  182. * Make the gap bigger, moving any necessary data and updating
  183. * the appropriate marks
  184. */
  185. protected void shiftEnd(int newSize) {
  186. int oldSize = getArrayLength();
  187. int oldGapEnd = g1;
  188. int upperSize = oldSize - oldGapEnd;
  189. int arrayLength = (newSize + 1) * 2;
  190. int newGapEnd = arrayLength - upperSize;
  191. resize(arrayLength);
  192. g1 = newGapEnd;
  193. if (upperSize != 0) {
  194. // Copy array items to new end of array.
  195. System.arraycopy(array, oldGapEnd, array, newGapEnd, upperSize);
  196. }
  197. }
  198. /**
  199. * Move the start of the gap to a new location,
  200. * without changing the size of the gap. This
  201. * moves the data in the array and updates the
  202. * marks accordingly.
  203. */
  204. protected void shiftGap(int newGapStart) {
  205. if (newGapStart == g0) {
  206. return;
  207. }
  208. int oldGapStart = g0;
  209. int dg = newGapStart - oldGapStart;
  210. int oldGapEnd = g1;
  211. int newGapEnd = oldGapEnd + dg;
  212. int gapSize = oldGapEnd - oldGapStart;
  213. g0 = newGapStart;
  214. g1 = newGapEnd;
  215. if (dg > 0) {
  216. // Move gap up, move data down.
  217. System.arraycopy(array, oldGapEnd, array, oldGapStart, dg);
  218. } else if (dg < 0) {
  219. // Move gap down, move data up.
  220. System.arraycopy(array, newGapStart, array, newGapEnd, -dg);
  221. }
  222. }
  223. /**
  224. * Adjust the gap end downward. This doesn't move
  225. * any data, but it does update any marks affected
  226. * by the boundary change. All marks from the old
  227. * gap start down to the new gap start are squeezed
  228. * to the end of the gap (their location has been
  229. * removed).
  230. */
  231. protected void shiftGapStartDown(int newGapStart) {
  232. g0 = newGapStart;
  233. }
  234. /**
  235. * Adjust the gap end upward. This doesn't move
  236. * any data, but it does update any marks affected
  237. * by the boundary change. All marks from the old
  238. * gap end up to the new gap end are squeezed
  239. * to the end of the gap (their location has been
  240. * removed).
  241. */
  242. protected void shiftGapEndUp(int newGapEnd) {
  243. g1 = newGapEnd;
  244. }
  245. }