1 /* 2 * Copyright (C) 2021 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 package androidx.constraintlayout.core.motion.utils; 17 18 import androidx.constraintlayout.core.motion.CustomAttribute; 19 import androidx.constraintlayout.core.motion.CustomVariable; 20 21 import java.util.Arrays; 22 23 public class KeyFrameArray { 24 25 // =================================== CustomAttribute ================================= 26 public static class CustomArray { 27 int[] mKeys = new int[101]; 28 CustomAttribute[] mValues = new CustomAttribute[101]; 29 int mCount; 30 private static final int EMPTY = 999; 31 CustomArray()32 public CustomArray() { 33 clear(); 34 } 35 36 // @TODO: add description clear()37 public void clear() { 38 Arrays.fill(mKeys, EMPTY); 39 Arrays.fill(mValues, null); 40 mCount = 0; 41 } 42 43 // @TODO: add description dump()44 public void dump() { 45 System.out.println("V: " + Arrays.toString(Arrays.copyOf(mKeys, mCount))); 46 System.out.print("K: ["); 47 for (int i = 0; i < mCount; i++) { 48 System.out.print((i == 0 ? "" : ", ") + valueAt(i)); 49 } 50 System.out.println("]"); 51 } 52 53 // @TODO: add description size()54 public int size() { 55 return mCount; 56 } 57 58 // @TODO: add description valueAt(int i)59 public CustomAttribute valueAt(int i) { 60 return mValues[mKeys[i]]; 61 } 62 63 // @TODO: add description keyAt(int i)64 public int keyAt(int i) { 65 return mKeys[i]; 66 } 67 68 // @TODO: add description append(int position, CustomAttribute value)69 public void append(int position, CustomAttribute value) { 70 if (mValues[position] != null) { 71 remove(position); 72 } 73 mValues[position] = value; 74 mKeys[mCount++] = position; 75 Arrays.sort(mKeys); 76 } 77 78 // @TODO: add description remove(int position)79 public void remove(int position) { 80 mValues[position] = null; 81 for (int j = 0, i = 0; i < mCount; i++) { 82 if (position == mKeys[i]) { 83 mKeys[i] = EMPTY; 84 j++; 85 } 86 if (i != j) { 87 mKeys[i] = mKeys[j]; 88 } 89 j++; 90 91 } 92 mCount--; 93 } 94 } 95 96 // =================================== CustomVar ================================= 97 public static class CustomVar { 98 int[] mKeys = new int[101]; 99 CustomVariable[] mValues = new CustomVariable[101]; 100 int mCount; 101 private static final int EMPTY = 999; 102 CustomVar()103 public CustomVar() { 104 clear(); 105 } 106 107 // @TODO: add description clear()108 public void clear() { 109 Arrays.fill(mKeys, EMPTY); 110 Arrays.fill(mValues, null); 111 mCount = 0; 112 } 113 114 // @TODO: add description dump()115 public void dump() { 116 System.out.println("V: " + Arrays.toString(Arrays.copyOf(mKeys, mCount))); 117 System.out.print("K: ["); 118 for (int i = 0; i < mCount; i++) { 119 System.out.print((i == 0 ? "" : ", ") + valueAt(i)); 120 } 121 System.out.println("]"); 122 } 123 124 // @TODO: add description size()125 public int size() { 126 return mCount; 127 } 128 129 // @TODO: add description valueAt(int i)130 public CustomVariable valueAt(int i) { 131 return mValues[mKeys[i]]; 132 } 133 134 // @TODO: add description keyAt(int i)135 public int keyAt(int i) { 136 return mKeys[i]; 137 } 138 139 // @TODO: add description append(int position, CustomVariable value)140 public void append(int position, CustomVariable value) { 141 if (mValues[position] != null) { 142 remove(position); 143 } 144 mValues[position] = value; 145 mKeys[mCount++] = position; 146 Arrays.sort(mKeys); 147 } 148 149 // @TODO: add description remove(int position)150 public void remove(int position) { 151 mValues[position] = null; 152 for (int j = 0, i = 0; i < mCount; i++) { 153 if (position == mKeys[i]) { 154 mKeys[i] = EMPTY; 155 j++; 156 } 157 if (i != j) { 158 mKeys[i] = mKeys[j]; 159 } 160 j++; 161 162 } 163 mCount--; 164 } 165 } 166 167 // =================================== FloatArray ====================================== 168 static class FloatArray { 169 int[] mKeys = new int[101]; 170 float[][] mValues = new float[101][]; 171 int mCount; 172 private static final int EMPTY = 999; 173 FloatArray()174 FloatArray() { 175 clear(); 176 } 177 clear()178 public void clear() { 179 Arrays.fill(mKeys, EMPTY); 180 Arrays.fill(mValues, null); 181 mCount = 0; 182 } 183 dump()184 public void dump() { 185 System.out.println("V: " + Arrays.toString(Arrays.copyOf(mKeys, mCount))); 186 System.out.print("K: ["); 187 for (int i = 0; i < mCount; i++) { 188 System.out.print((i == 0 ? "" : ", ") + Arrays.toString(valueAt(i))); 189 } 190 System.out.println("]"); 191 } 192 size()193 public int size() { 194 return mCount; 195 } 196 valueAt(int i)197 public float[] valueAt(int i) { 198 return mValues[mKeys[i]]; 199 } 200 keyAt(int i)201 public int keyAt(int i) { 202 return mKeys[i]; 203 } 204 append(int position, float[] value)205 public void append(int position, float[] value) { 206 if (mValues[position] != null) { 207 remove(position); 208 } 209 mValues[position] = value; 210 mKeys[mCount++] = position; 211 Arrays.sort(mKeys); 212 } 213 remove(int position)214 public void remove(int position) { 215 mValues[position] = null; 216 for (int j = 0, i = 0; i < mCount; i++) { 217 if (position == mKeys[i]) { 218 mKeys[i] = EMPTY; 219 j++; 220 } 221 if (i != j) { 222 mKeys[i] = mKeys[j]; 223 } 224 j++; 225 226 } 227 mCount--; 228 } 229 } 230 } 231