1 5 package org.h2.util; 6 7 import java.util.Collection ; 8 import java.util.Comparator ; 9 import java.util.Iterator ; 10 11 import org.h2.engine.Constants; 12 13 16 17 public class ObjectArray { 18 private static final int SIZE_INIT = 4, SIZE_SHRINK = 256; 19 20 private Object [] data; 21 private int size; 22 23 public ObjectArray() { 24 this(SIZE_INIT); 25 } 26 27 public ObjectArray(int size) { 28 data = new Object [size > 1 ? size : 1]; 29 } 30 31 public ObjectArray(Object [] data) { 32 this.data = data; 33 size = data.length; 34 } 35 36 public ObjectArray(Collection collection) { 37 size = collection.size(); 39 data = new Object [size]; 40 Iterator it = collection.iterator(); 41 for(int i=0; i<size; i++) { 42 data[i] = it.next(); 43 } 44 } 45 46 public void add(Object value) { 47 ensureCapacity(size); 48 data[size++] = value; 49 } 50 51 public Object get(int i) { 52 if (Constants.CHECK && i >= size) { 53 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 54 } 55 return data[i]; 56 } 57 58 public Object remove(int i) { 59 if (Constants.CHECK && i >= size) { 61 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 62 } 63 Object value = data[i]; 64 System.arraycopy(data, i + 1, data, i, size - i - 1); 65 size--; 66 data[size] = null; 67 return value; 69 } 70 71 public void removeRange(int from, int to) { 72 if (Constants.CHECK && (to > size || from > to)) { 73 throw new ArrayIndexOutOfBoundsException ("to=" + to + " from="+from+" size=" + size); 74 } 75 System.arraycopy(data, to, data, from, size - to); 76 size -= to - from; 77 for(int i=size + (to-from) - 1; i>=size; i--) { 78 data[i] = null; 79 } 80 } 81 82 public void setSize(int i) { 83 ensureCapacity(i); 84 this.size = i; 85 } 86 87 private void ensureCapacity(int i) { 88 while (i >= data.length) { 89 Object [] d = new Object [data.length * 2]; 90 System.arraycopy(data, 0, d, 0, data.length); 91 data = d; 92 } 93 } 94 95 public void add(int i, Object value) { 96 if (Constants.CHECK && i > size) { 97 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 98 } 99 ensureCapacity(size); 100 if (i == size) { 101 add(value); 102 } else { 103 System.arraycopy(data, i, data, i + 1, size - i); 104 data[i] = value; 105 size++; 106 } 107 } 108 109 public void set(int i, Object value) { 110 if (Constants.CHECK && i >= size) { 111 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 112 } 113 data[i] = value; 114 } 115 116 public int size() { 117 return size; 118 } 119 120 public void toArray(Object [] array) { 121 for(int i=0; i<size; i++) { 122 array[i] = data[i]; 123 } 124 } 125 126 public void clear() { 127 if(data.length > SIZE_SHRINK) { 128 data = new Object [SIZE_INIT]; 129 } else { 130 for(int i=0; i<size; i++) { 131 data[i] = null; 132 } 133 } 134 size = 0; 135 } 136 137 public int indexOf(Object o) { 138 for(int i=0; i<size; i++) { 139 if(data[i] == o) { 140 return i; 141 } 142 } 143 return -1; 144 } 145 146 public void addAll(ObjectArray list) { 147 for(int i=0; i<list.size; i++) { 148 add(list.get(i)); 149 } 150 } 151 152 private void swap(int l, int r) { 153 Object t = data[r]; 154 data[r] = data[l]; 155 data[l] = t; 156 } 157 158 public void sort(Comparator comp) { 159 sort(comp, 0, size-1); 160 } 161 162 private void sort(Comparator comp, int l, int r) { 163 int i, j; 164 while (r - l > 10) { 165 i = (r + l) >> 1; 166 if (comp.compare(get(l), get(r)) > 0) { 167 swap(l, r); 168 } 169 if (comp.compare(get(i), get(l)) < 0) { 170 swap(l, i); 171 } else if (comp.compare(get(i), get(r)) > 0) { 172 swap(i, r); 173 } 174 j = r - 1; 175 swap(i, j); 176 Object p = get(j); 177 i = l; 178 while (true) { 179 do { 180 ++i; 181 } while (comp.compare(get(i), p) < 0); 182 do { 183 --j; 184 } while (comp.compare(get(j), p) > 0); 185 if (i >= j) { 186 break; 187 } 188 swap(i, j); 189 } 190 swap(i, r - 1); 191 sort(comp, l, i - 1); 192 l = i + 1; 193 } 194 for (i = l + 1; i <= r; i++) { 195 Object t = get(i); 196 for (j = i - 1; j >= l && (comp.compare(get(j), t) > 0); j--) { 197 set(j + 1, get(j)); 198 } 199 set(j + 1, t); 200 } 201 } 202 203 213 } 214 | Popular Tags |