jdk/src/share/classes/javax/swing/SizeSequence.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23010 6dadb192ad81
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21982
diff changeset
     2
 * Copyright (c) 1999, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * efficiently maintains an ordered list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * of sizes and corresponding positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * One situation for which <code>SizeSequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * might be appropriate is in a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * that displays multiple rows of unequal size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * In this case, a single <code>SizeSequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * object could be used to track the heights
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and Y positions of all rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Another example would be a multi-column component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * such as a <code>JTable</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * in which the column sizes are not all equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The <code>JTable</code> might use a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to store the widths and X positions of all the columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The <code>JTable</code> could then use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to find the column corresponding to a certain position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The <code>JTable</code> could update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * whenever one or more column sizes changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The following figure shows the relationship between size and position data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * for a multi-column component.
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 20157
diff changeset
    55
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <img src="doc-files/SizeSequence-1.gif" width=384 height = 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * alt="The first item begins at position 0, the second at the position equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 to the size of the previous item, and so on.">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * In the figure, the first index (0) corresponds to the first column,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * the second index (1) to the second column, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The first column's position starts at 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * and the column occupies <em>size<sub>0</sub></em> pixels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * where <em>size<sub>0</sub></em> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>getSize(0)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Thus, the first column ends at <em>size<sub>0</sub></em> - 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The second column then begins at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the position <em>size<sub>0</sub></em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * and occupies <em>size<sub>1</sub></em> (<code>getSize(1)</code>) pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Note that a <code>SizeSequence</code> object simply represents intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * In our examples, the intervals represent height or width in pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * However, any other unit of measure (for example, time in days)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * could be just as valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
    80
 * <h3>Implementation Notes</h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Normally when storing the size and position of entries,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * one would choose between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * storing the sizes or storing their positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * instead. The two common operations that are needed during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * rendering are: <code>getIndex(position)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * and <code>setSize(index, size)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * Whichever choice of internal format is made one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * operations is costly when the number of entries becomes large.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * If sizes are stored, finding the index of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * that encloses a particular position is linear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * number of entries. If positions are stored instead, setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * the size of an entry at a particular index requires updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * the positions of the affected entries, which is also a linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Like the above techniques this class holds an array of N integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * internally but uses a hybrid encoding, which is halfway
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * between the size-based and positional-based approaches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * The result is a data structure that takes the same space to store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * the information but can perform most operations in Log(N) time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * instead of O(N), where N is the number of entries in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Two operations that remain O(N) in the number of entries are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * the <code>insertEntries</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * and <code>removeEntries</code> methods, both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * of which are implemented by converting the internal array to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * a set of integer sizes, copying it into the new array, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * reforming the hybrid representation in place.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *   Each method is implemented by taking the minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *   maximum of the range of integers that need to be operated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *   upon. All the algorithms work by dividing this range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *   into two smaller ranges and recursing. The recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *   is terminated when the upper and lower bounds are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
public class SizeSequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static int[] emptyArray = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private int a[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Creates a new <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * that contains no entries.  To add entries, you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * can use <code>insertEntries</code> or <code>setSizes</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @see #insertEntries
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   134
     * @see #setSizes(int[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public SizeSequence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        a = emptyArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Creates a new <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * that contains the specified number of entries,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * all initialized to have size 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param numEntries  the number of sizes to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @exception NegativeArraySizeException if
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   147
     *    <code>numEntries &lt; 0</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public SizeSequence(int numEntries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this(numEntries, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Creates a new <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * that contains the specified number of entries,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * all initialized to have size <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param numEntries  the number of sizes to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param value       the initial value of each size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public SizeSequence(int numEntries, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        insertEntries(0, numEntries, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Creates a new <code>SizeSequence</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * that contains the specified sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param sizes  the array of sizes to be contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *               the <code>SizeSequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public SizeSequence(int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        setSizes(sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Resets the size sequence to contain <code>length</code> items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * all with a size of <code>size</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    void setSizes(int length, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (a.length != length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            a = new int[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        setSizes(0, length, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private int setSizes(int from, int to, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (to <= from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int m = (from + to)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        a[m] = size + setSizes(from, m, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return a[m] + setSizes(m + 1, to, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Resets this <code>SizeSequence</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * using the data in the <code>sizes</code> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * This method reinitializes this object so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * contains as many entries as the <code>sizes</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Each entry's size is initialized to the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * corresponding item in <code>sizes</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param sizes  the array of sizes to be contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *               this <code>SizeSequence</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void setSizes(int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (a.length != sizes.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            a = new int[sizes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        setSizes(0, a.length, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private int setSizes(int from, int to, int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (to <= from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int m = (from + to)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        a[m] = sizes[m] + setSizes(from, m, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return a[m] + setSizes(m + 1, to, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns the size of all entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return  a new array containing the sizes in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public int[] getSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int n = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int[] sizes = new int[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        getSizes(0, n, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return sizes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private int getSizes(int from, int to, int[] sizes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (to <= from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int m = (from + to)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        sizes[m] = a[m] - getSizes(from, m, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return a[m] + getSizes(m + 1, to, sizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Returns the start position for the specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * For example, <code>getPosition(0)</code> returns 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <code>getPosition(1)</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *   <code>getSize(0)</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <code>getPosition(2)</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *   <code>getSize(0)</code> + <code>getSize(1)</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>Note that if <code>index</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <code>length</code> the value returned may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * be meaningless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param index  the index of the entry whose position is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return       the starting position of the specified entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public int getPosition(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return getPosition(0, a.length, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private int getPosition(int from, int to, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (to <= from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int m = (from + to)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (index <= m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            return getPosition(from, m, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return a[m] + getPosition(m + 1, to, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Returns the index of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * that corresponds to the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * For example, <code>getIndex(0)</code> is 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * since the first entry always starts at position 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param position  the position of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return  the index of the entry that occupies the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public int getIndex(int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return getIndex(0, a.length, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private int getIndex(int from, int to, int position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (to <= from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int m = (from + to)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int pivot = a[m];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (position < pivot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
           return getIndex(from, m, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return getIndex(m + 1, to, position - pivot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns the size of the specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * If <code>index</code> is out of the range
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   308
     * <code>(0 &lt;= index &lt; getSizes().length)</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * the behavior is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param index  the index corresponding to the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return  the size of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public int getSize(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return getPosition(index + 1) - getPosition(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Sets the size of the specified entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Note that if the value of <code>index</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * does not fall in the range:
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   322
     * <code>(0 &lt;= index &lt; getSizes().length)</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the behavior is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param index  the index corresponding to the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param size   the size of the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void setSize(int index, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        changeSize(0, a.length, index, size - getSize(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private void changeSize(int from, int to, int index, int delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (to <= from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int m = (from + to)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (index <= m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            a[m] += delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            changeSize(from, m, index, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            changeSize(m + 1, to, index, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Adds a contiguous group of entries to this <code>SizeSequence</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Note that the values of <code>start</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <code>length</code> must satisfy the following
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   350
     * conditions:  <code>(0 &lt;= start &lt; getSizes().length)
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   351
     * AND (length &gt;= 0)</code>.  If these conditions are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * not met, the behavior is unspecified and an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * may be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param start   the index to be assigned to the first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *                in the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param length  the number of entries in the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param value   the size to be assigned to each new entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @exception ArrayIndexOutOfBoundsException if the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *   are outside of the range:
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   361
     *   (<code>0 &lt;= start &lt; (getSizes().length)) AND (length &gt;= 0)</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public void insertEntries(int start, int length, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int sizes[] = getSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        int end = start + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int n = a.length + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        a = new int[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        for (int i = 0; i < start; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            a[i] = sizes[i] ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            a[i] = value ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        for (int i = end; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            a[i] = sizes[i-length] ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        setSizes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Removes a contiguous group of entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * from this <code>SizeSequence</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Note that the values of <code>start</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <code>length</code> must satisfy the following
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   385
     * conditions:  <code>(0 &lt;= start &lt; getSizes().length)
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 9035
diff changeset
   386
     * AND (length &gt;= 0)</code>.  If these conditions are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * not met, the behavior is unspecified and an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * may be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param start   the index of the first entry to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param length  the number of entries to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public void removeEntries(int start, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int sizes[] = getSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        int end = start + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int n = a.length - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        a = new int[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        for (int i = 0; i < start; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            a[i] = sizes[i] ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        for (int i = start; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            a[i] = sizes[i+length] ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        setSizes(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}