1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.text;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
/**
* PackedIntVector stores a two-dimensional array of integers,
* optimized for inserting and deleting rows and for
* offsetting the values in segments of a given column.
*
* @hide
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public class PackedIntVector {
private final int mColumns;
private int mRows;
private int mRowGapStart;
private int mRowGapLength;
private int[] mValues;
private int[] mValueGap; // starts, followed by lengths
/**
* Creates a new PackedIntVector with the specified width and
* a height of 0.
*
* @param columns the width of the PackedIntVector.
*/
public PackedIntVector(int columns) {
mColumns = columns;
mRows = 0;
mRowGapStart = 0;
mRowGapLength = mRows;
mValues = null;
mValueGap = new int[2 * columns];
}
/**
* Returns the value at the specified row and column.
*
* @param row the index of the row to return.
* @param column the index of the column to return.
*
* @return the value stored at the specified position.
*
* @throws IndexOutOfBoundsException if the row is out of range
* (row < 0 || row >= size()) or the column is out of range
* (column < 0 || column >= width()).
*/
public int getValue(int row, int column) {
final int columns = mColumns;
if (((row | column) < 0) || (row >= size()) || (column >= columns)) {
throw new IndexOutOfBoundsException(row + ", " + column);
}
if (row >= mRowGapStart) {
row += mRowGapLength;
}
int value = mValues[row * columns + column];
int[] valuegap = mValueGap;
if (row >= valuegap[column]) {
value += valuegap[column + columns];
}
return value;
}
/**
* Sets the value at the specified row and column.
*
* @param row the index of the row to set.
* @param column the index of the column to set.
*
* @throws IndexOutOfBoundsException if the row is out of range
* (row < 0 || row >= size()) or the column is out of range
* (column < 0 || column >= width()).
*/
public void setValue(int row, int column, int value) {
if (((row | column) < 0) || (row >= size()) || (column >= mColumns)) {
throw new IndexOutOfBoundsException(row + ", " + column);
}
if (row >= mRowGapStart) {
row += mRowGapLength;
}
int[] valuegap = mValueGap;
if (row >= valuegap[column]) {
value -= valuegap[column + mColumns];
}
mValues[row * mColumns + column] = value;
}
/**
* Sets the value at the specified row and column.
* Private internal version: does not check args.
*
* @param row the index of the row to set.
* @param column the index of the column to set.
*
*/
private void setValueInternal(int row, int column, int value) {
if (row >= mRowGapStart) {
row += mRowGapLength;
}
int[] valuegap = mValueGap;
if (row >= valuegap[column]) {
value -= valuegap[column + mColumns];
}
mValues[row * mColumns + column] = value;
}
/**
* Increments all values in the specified column whose row >= the
* specified row by the specified delta.
*
* @param startRow the row at which to begin incrementing.
* This may be == size(), which case there is no effect.
* @param column the index of the column to set.
*
* @throws IndexOutOfBoundsException if the row is out of range
* (startRow < 0 || startRow > size()) or the column
* is out of range (column < 0 || column >= width()).
*/
public void adjustValuesBelow(int startRow, int column, int delta) {
if (((startRow | column) < 0) || (startRow > size()) ||
(column >= width())) {
throw new IndexOutOfBoundsException(startRow + ", " + column);
}
if (startRow >= mRowGapStart) {
startRow += mRowGapLength;
}
moveValueGapTo(column, startRow);
mValueGap[column + mColumns] += delta;
}
/**
* Inserts a new row of values at the specified row offset.
*
* @param row the row above which to insert the new row.
* This may be == size(), which case the new row is added
* at the end.
* @param values the new values to be added. If this is null,
* a row of zeroes is added.
*
* @throws IndexOutOfBoundsException if the row is out of range
* (row < 0 || row > size()) or if the length of the
* values array is too small (values.length < width()).
*/
public void insertAt(int row, int[] values) {
if ((row < 0) || (row > size())) {
throw new IndexOutOfBoundsException("row " + row);
}
if ((values != null) && (values.length < width())) {
throw new IndexOutOfBoundsException("value count " + values.length);
}
moveRowGapTo(row);
if (mRowGapLength == 0) {
growBuffer();
}
mRowGapStart++;
mRowGapLength--;
if (values == null) {
for (int i = mColumns - 1; i >= 0; i--) {
setValueInternal(row, i, 0);
}
} else {
for (int i = mColumns - 1; i >= 0; i--) {
setValueInternal(row, i, values[i]);
}
}
}
/**
* Deletes the specified number of rows starting with the specified
* row.
*
* @param row the index of the first row to be deleted.
* @param count the number of rows to delete.
*
* @throws IndexOutOfBoundsException if any of the rows to be deleted
* are out of range (row < 0 || count < 0 ||
* row + count > size()).
*/
public void deleteAt(int row, int count) {
if (((row | count) < 0) || (row + count > size())) {
throw new IndexOutOfBoundsException(row + ", " + count);
}
moveRowGapTo(row + count);
mRowGapStart -= count;
mRowGapLength += count;
// TODO: Reclaim memory when the new height is much smaller
// than the allocated size.
}
/**
* Returns the number of rows in the PackedIntVector. This number
* will change as rows are inserted and deleted.
*
* @return the number of rows.
*/
public int size() {
return mRows - mRowGapLength;
}
/**
* Returns the width of the PackedIntVector. This number is set
* at construction and will not change.
*
* @return the number of columns.
*/
public int width() {
return mColumns;
}
/**
* Grows the value and gap arrays to be large enough to store at least
* one more than the current number of rows.
*/
private final void growBuffer() {
final int columns = mColumns;
int[] newvalues = ArrayUtils.newUnpaddedIntArray(
GrowingArrayUtils.growSize(size()) * columns);
int newsize = newvalues.length / columns;
final int[] valuegap = mValueGap;
final int rowgapstart = mRowGapStart;
int after = mRows - (rowgapstart + mRowGapLength);
if (mValues != null) {
System.arraycopy(mValues, 0, newvalues, 0, columns * rowgapstart);
System.arraycopy(mValues, (mRows - after) * columns,
newvalues, (newsize - after) * columns,
after * columns);
}
for (int i = 0; i < columns; i++) {
if (valuegap[i] >= rowgapstart) {
valuegap[i] += newsize - mRows;
if (valuegap[i] < rowgapstart) {
valuegap[i] = rowgapstart;
}
}
}
mRowGapLength += newsize - mRows;
mRows = newsize;
mValues = newvalues;
}
/**
* Moves the gap in the values of the specified column to begin at
* the specified row.
*/
private final void moveValueGapTo(int column, int where) {
final int[] valuegap = mValueGap;
final int[] values = mValues;
final int columns = mColumns;
if (where == valuegap[column]) {
return;
} else if (where > valuegap[column]) {
for (int i = valuegap[column]; i < where; i++) {
values[i * columns + column] += valuegap[column + columns];
}
} else /* where < valuegap[column] */ {
for (int i = where; i < valuegap[column]; i++) {
values[i * columns + column] -= valuegap[column + columns];
}
}
valuegap[column] = where;
}
/**
* Moves the gap in the row indices to begin at the specified row.
*/
private final void moveRowGapTo(int where) {
if (where == mRowGapStart) {
return;
} else if (where > mRowGapStart) {
int moving = where + mRowGapLength - (mRowGapStart + mRowGapLength);
final int columns = mColumns;
final int[] valuegap = mValueGap;
final int[] values = mValues;
final int gapend = mRowGapStart + mRowGapLength;
for (int i = gapend; i < gapend + moving; i++) {
int destrow = i - gapend + mRowGapStart;
for (int j = 0; j < columns; j++) {
int val = values[i * columns+ j];
if (i >= valuegap[j]) {
val += valuegap[j + columns];
}
if (destrow >= valuegap[j]) {
val -= valuegap[j + columns];
}
values[destrow * columns + j] = val;
}
}
} else /* where < mRowGapStart */ {
int moving = mRowGapStart - where;
final int columns = mColumns;
final int[] valuegap = mValueGap;
final int[] values = mValues;
final int gapend = mRowGapStart + mRowGapLength;
for (int i = where + moving - 1; i >= where; i--) {
int destrow = i - where + gapend - moving;
for (int j = 0; j < columns; j++) {
int val = values[i * columns+ j];
if (i >= valuegap[j]) {
val += valuegap[j + columns];
}
if (destrow >= valuegap[j]) {
val -= valuegap[j + columns];
}
values[destrow * columns + j] = val;
}
}
}
mRowGapStart = where;
}
}
|