SizeSequence object
efficiently maintains an ordered list
of sizes and corresponding positions.
One situation for which SizeSequence
might be appropriate is in a component
that displays multiple rows of unequal size.
In this case, a single SizeSequence
object could be used to track the heights
and Y positions of all rows.
Another example would be a multi-column component,
such as a JTable,
in which the column sizes are not all equal.
The JTable might use a single
SizeSequence object
to store the widths and X positions of all the columns.
The JTable could then use the
SizeSequence object
to find the column corresponding to a certain position.
The JTable could update the
SizeSequence object
whenever one or more column sizes changed.
The following figure shows the relationship between size and position data for a multi-column component.
In the figure, the first index (0) corresponds to the first column,
the second index (1) to the second column, and so on.
The first column's position starts at 0,
and the column occupies size0 pixels,
where size0 is the value returned by
getSize(0).
Thus, the first column ends at size0 - 1.
The second column then begins at
the position size0
and occupies size1 (getSize(1)) pixels.
Note that a SizeSequence object simply represents intervals
along an axis.
In our examples, the intervals represent height or width in pixels.
However, any other unit of measure (for example, time in days)
could be just as valid.
getIndex(position)
and setSize(index, size).
Whichever choice of internal format is made one of these
operations is costly when the number of entries becomes large.
If sizes are stored, finding the index of the entry
that encloses a particular position is linear in the
number of entries. If positions are stored instead, setting
the size of an entry at a particular index requires updating
the positions of the affected entries, which is also a linear
calculation.
Like the above techniques this class holds an array of N integers internally but uses a hybrid encoding, which is halfway between the size-based and positional-based approaches. The result is a data structure that takes the same space to store the information but can perform most operations in Log(N) time instead of O(N), where N is the number of entries in the list.
Two operations that remain O(N) in the number of entries are
the insertEntries
and removeEntries methods, both
of which are implemented by converting the internal array to
a set of integer sizes, copying it into the new array, and then
reforming the hybrid representation in place.
| Modifier and Type | Field and Description |
|---|---|
| private int[] | |
| private static int[] |
| Access | Constructor and Description |
|---|---|
| public | |
| public | SizeSequence(int
the number of sizes to track numEntries)Creates a new |
| public | SizeSequence(int
the number of sizes to track numEntries, int the initial value of each size value)Creates a new |
| public | SizeSequence(int[]
the array of sizes to be contained in
the sizes)SizeSequenceCreates a new |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public int | Returns: the index of the entry that occupies the specified positionthe position of the entry position)Returns the index of the entry that corresponds to the specified position. |
| private int | |
| public int | Returns: the starting position of the specified entrythe index of the entry whose position is desired index)Returns the start position for the specified entry. |
| private int | |
| public int | Returns: the size of the entrythe index corresponding to the entry index)Returns the size of the specified entry. |
| public int[] | |
| private int | |
| public void | insertEntries(int
the index to be assigned to the first entry
in the group start, int the number of entries in the group length, int the size to be assigned to each new entry value)Adds a contiguous group of entries to this |
| public void | removeEntries(int
the index of the first entry to be removed start, int the number of entries to be removed length)Removes a contiguous group of entries
from this |
| public void | setSize(int
the index corresponding to the entry index, int the size of the entry size)Sets the size of the specified entry. |
| pack-priv void | setSizes(int length, int size)
Resets the size sequence to contain |
| private int | |
| public void | setSizes(int[]
the array of sizes to be contained in
this sizes)SizeSequenceResets this |
| private int |
| a | back to summary |
|---|---|
| private int[] a | |
| emptyArray | back to summary |
|---|---|
| private static int[] emptyArray | |
| SizeSequence | back to summary |
|---|---|
| public SizeSequence() Creates a new
| |
| SizeSequence | back to summary |
|---|---|
| public SizeSequence(int numEntries) Creates a new
| |
| SizeSequence | back to summary |
|---|---|
| public SizeSequence(int numEntries, int value) Creates a new
| |
| SizeSequence | back to summary |
|---|---|
| public SizeSequence(int[] sizes) Creates a new
| |
| changeSize | back to summary |
|---|---|
| private void changeSize(int from, int to, int index, int delta) | |
| getIndex | back to summary |
|---|---|
| public int getIndex(int position) Returns the index of the entry
that corresponds to the specified position.
For example,
| |
| getIndex | back to summary |
|---|---|
| private int getIndex(int from, int to, int position) | |
| getPosition | back to summary |
|---|---|
| public int getPosition(int index) Returns the start position for the specified entry.
For example, Note that if
| |
| getPosition | back to summary |
|---|---|
| private int getPosition(int from, int to, int index) | |
| getSize | back to summary |
|---|---|
| public int getSize(int index) Returns the size of the specified entry.
If
| |
| getSizes | back to summary |
|---|---|
| public int[] getSizes() Returns the size of all entries.
| |
| getSizes | back to summary |
|---|---|
| private int getSizes(int from, int to, int[] sizes) | |
| insertEntries | back to summary |
|---|---|
| public void insertEntries(int start, int length, int value) Adds a contiguous group of entries to this
| |
| removeEntries | back to summary |
|---|---|
| public void removeEntries(int start, int length) Removes a contiguous group of entries
from this
| |
| setSize | back to summary |
|---|---|
| public void setSize(int index, int size) Sets the size of the specified entry.
Note that if the value of
| |
| setSizes | back to summary |
|---|---|
| pack-priv void setSizes(int length, int size) Resets the size sequence to contain | |
| setSizes | back to summary |
|---|---|
| private int setSizes(int from, int to, int size) | |
| setSizes | back to summary |
|---|---|
| public void setSizes(int[] sizes) Resets this
| |
| setSizes | back to summary |
|---|---|
| private int setSizes(int from, int to, int[] sizes) | |