1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text; 18 19 import com.android.internal.util.ArrayUtils; 20 import com.android.internal.util.GrowingArrayUtils; 21 22 import libcore.util.EmptyArray; 23 24 @android.ravenwood.annotation.RavenwoodKeepWholeClass 25 class PackedObjectVector<E> 26 { 27 private int mColumns; 28 private int mRows; 29 30 private int mRowGapStart; 31 private int mRowGapLength; 32 33 private Object[] mValues; 34 35 public PackedObjectVector(int columns)36 PackedObjectVector(int columns) 37 { 38 mColumns = columns; 39 mValues = EmptyArray.OBJECT; 40 mRows = 0; 41 42 mRowGapStart = 0; 43 mRowGapLength = mRows; 44 } 45 46 public E getValue(int row, int column)47 getValue(int row, int column) 48 { 49 if (row >= mRowGapStart) 50 row += mRowGapLength; 51 52 Object value = mValues[row * mColumns + column]; 53 54 return (E) value; 55 } 56 57 public void setValue(int row, int column, E value)58 setValue(int row, int column, E value) 59 { 60 if (row >= mRowGapStart) 61 row += mRowGapLength; 62 63 mValues[row * mColumns + column] = value; 64 } 65 66 public void insertAt(int row, E[] values)67 insertAt(int row, E[] values) 68 { 69 moveRowGapTo(row); 70 71 if (mRowGapLength == 0) 72 growBuffer(); 73 74 mRowGapStart++; 75 mRowGapLength--; 76 77 if (values == null) 78 for (int i = 0; i < mColumns; i++) 79 setValue(row, i, null); 80 else 81 for (int i = 0; i < mColumns; i++) 82 setValue(row, i, values[i]); 83 } 84 85 public void deleteAt(int row, int count)86 deleteAt(int row, int count) 87 { 88 moveRowGapTo(row + count); 89 90 mRowGapStart -= count; 91 mRowGapLength += count; 92 93 if (mRowGapLength > size() * 2) 94 { 95 // dump(); 96 // growBuffer(); 97 } 98 } 99 100 public int size()101 size() 102 { 103 return mRows - mRowGapLength; 104 } 105 106 public int width()107 width() 108 { 109 return mColumns; 110 } 111 112 private void growBuffer()113 growBuffer() 114 { 115 Object[] newvalues = ArrayUtils.newUnpaddedObjectArray( 116 GrowingArrayUtils.growSize(size()) * mColumns); 117 int newsize = newvalues.length / mColumns; 118 int after = mRows - (mRowGapStart + mRowGapLength); 119 120 System.arraycopy(mValues, 0, newvalues, 0, mColumns * mRowGapStart); 121 System.arraycopy(mValues, (mRows - after) * mColumns, newvalues, (newsize - after) * mColumns, after * mColumns); 122 123 mRowGapLength += newsize - mRows; 124 mRows = newsize; 125 mValues = newvalues; 126 } 127 128 private void moveRowGapTo(int where)129 moveRowGapTo(int where) 130 { 131 if (where == mRowGapStart) 132 return; 133 134 if (where > mRowGapStart) 135 { 136 int moving = where + mRowGapLength - (mRowGapStart + mRowGapLength); 137 138 for (int i = mRowGapStart + mRowGapLength; i < mRowGapStart + mRowGapLength + moving; i++) 139 { 140 int destrow = i - (mRowGapStart + mRowGapLength) + mRowGapStart; 141 142 for (int j = 0; j < mColumns; j++) 143 { 144 Object val = mValues[i * mColumns + j]; 145 146 mValues[destrow * mColumns + j] = val; 147 } 148 } 149 } 150 else /* where < mRowGapStart */ 151 { 152 int moving = mRowGapStart - where; 153 154 for (int i = where + moving - 1; i >= where; i--) 155 { 156 int destrow = i - where + mRowGapStart + mRowGapLength - moving; 157 158 for (int j = 0; j < mColumns; j++) 159 { 160 Object val = mValues[i * mColumns + j]; 161 162 mValues[destrow * mColumns + j] = val; 163 } 164 } 165 } 166 167 mRowGapStart = where; 168 } 169 170 public void // XXX dump()171 dump() 172 { 173 for (int i = 0; i < mRows; i++) 174 { 175 for (int j = 0; j < mColumns; j++) 176 { 177 Object val = mValues[i * mColumns + j]; 178 179 if (i < mRowGapStart || i >= mRowGapStart + mRowGapLength) 180 System.out.print(val + " "); 181 else 182 System.out.print("(" + val + ") "); 183 } 184 185 System.out.print(" << \n"); 186 } 187 188 System.out.print("-----\n\n"); 189 } 190 } 191