jdk/src/share/classes/javax/swing/text/GapVector.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.undo.UndoableEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An implementation of a gapped buffer similar to that used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * emacs.  The underlying storage is a java array of some type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * which is known only by the subclass of this class.  The array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * has a gap somewhere.  The gap is moved to the location of changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to take advantage of common behavior where most changes occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * in the same location.  Changes that occur at a gap boundary are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * generally cheap and moving the gap is generally cheaper than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * moving the array contents directly to accomodate the change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see GapContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
abstract class GapVector implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Creates a new GapVector object.  Initial size defaults to 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public GapVector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a new GapVector object, with the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * size specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param initialLength the initial size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public GapVector(int initialLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        array = allocateArray(initialLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        g0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        g1 = initialLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Allocate an array to store items of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * appropriate (which is determined by the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected abstract Object allocateArray(int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Get the length of the allocated array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected abstract int getArrayLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Access to the array.  The actual type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * of the array is known only by the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected final Object getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Access to the start of the gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected final int getGapStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Access to the end of the gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected final int getGapEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // ---- variables -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The array of items.  The type is determined by the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private Object array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * start of gap in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * end of gap in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // --- gap management -------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Replace the given logical position in the storage with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the given new items.  This will move the gap to the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * being changed if the gap is not currently located at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * change location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param position the location to make the replacement.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *  is not the location in the underlying storage array, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *  the location in the contiguous space being modeled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param rmSize the number of items to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param addItems the new items to place in storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected void replace(int position, int rmSize, Object addItems, int addSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int addOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (addSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            close(position, rmSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (rmSize > addSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            /* Shrink the end. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            close(position+addSize, rmSize-addSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            /* Grow the end, do two chunks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int endSize = addSize - rmSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int end = open(position + rmSize, endSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            System.arraycopy(addItems, rmSize, array, end, endSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            addSize = rmSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        System.arraycopy(addItems, addOffset, array, position, addSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Delete nItems at position.  Squeezes any marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * within the deleted area to position.  This moves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the gap to the best place by minimizing it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * overall movement.  The gap must intersect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * target block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    void close(int position, int nItems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (nItems == 0)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int end = position + nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int new_gs = (g1 - g0) + nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (end <= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            // Move gap to end of block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (g0 != end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                shiftGap(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Adjust g0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            shiftGapStartDown(g0 - nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else if (position >= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // Move gap to beginning of block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (g0 != position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                shiftGap(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // Adjust g1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            shiftGapEndUp(g0 + new_gs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // The gap is properly inside the target block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // No data movement necessary, simply move both gap pointers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            shiftGapStartDown(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            shiftGapEndUp(g0 + new_gs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Make space for the given number of items at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return the location that the caller should fill in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    int open(int position, int nItems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int gapSize = g1 - g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (nItems == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (position > g0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                position += gapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // Expand the array if the gap is too small.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        shiftGap(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (nItems >= gapSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // Pre-shift the gap, to reduce total movement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            shiftEnd(getArrayLength() - gapSize + nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            gapSize = g1 - g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        g0 = g0 + nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * resize the underlying storage array to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * given new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    void resize(int nsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Object narray = allocateArray(nsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        System.arraycopy(array, 0, narray, 0, Math.min(nsize, getArrayLength()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        array = narray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Make the gap bigger, moving any necessary data and updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * the appropriate marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    protected void shiftEnd(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int oldSize = getArrayLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int oldGapEnd = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int upperSize = oldSize - oldGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int arrayLength = getNewArraySize(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int newGapEnd = arrayLength - upperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        resize(arrayLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        g1 = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (upperSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // Copy array items to new end of array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            System.arraycopy(array, oldGapEnd, array, newGapEnd, upperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Calculates a new size of the storage array depending on required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param reqSize the size which is necessary for new content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the new size of the storage array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    int getNewArraySize(int reqSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return (reqSize + 1) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Move the start of the gap to a new location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * without changing the size of the gap.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * moves the data in the array and updates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * marks accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    protected void shiftGap(int newGapStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (newGapStart == g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int oldGapStart = g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int dg = newGapStart - oldGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int oldGapEnd = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int newGapEnd = oldGapEnd + dg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int gapSize = oldGapEnd - oldGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        g0 = newGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        g1 = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (dg > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // Move gap up, move data down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            System.arraycopy(array, oldGapEnd, array, oldGapStart, dg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else if (dg < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // Move gap down, move data up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            System.arraycopy(array, newGapStart, array, newGapEnd, -dg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Adjust the gap end downward.  This doesn't move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * any data, but it does update any marks affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * by the boundary change.  All marks from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * gap start down to the new gap start are squeezed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * to the end of the gap (their location has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected void shiftGapStartDown(int newGapStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        g0 = newGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Adjust the gap end upward.  This doesn't move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * any data, but it does update any marks affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * by the boundary change. All marks from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * gap end up to the new gap end are squeezed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * to the end of the gap (their location has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    protected void shiftGapEndUp(int newGapEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        g1 = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
}