| 1 |
package structures; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.LinkedList; |
| 5 |
|
| 6 |
import shared.KillSwitch; |
| 7 |
import shared.Shared; |
| 8 |
import shared.Timer; |
| 9 |
import shared.Tools; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
public final class FloatList{ |
| 14 |
|
| 15 |
public static void main(String[] args){ |
| 16 |
Timer t=new Timer(); |
| 17 |
int length=args.length>0 ? Integer.parseInt(args[0]) : 100000000; |
| 18 |
|
| 19 |
System.gc(); |
| 20 |
|
| 21 |
{ |
| 22 |
System.err.println("\nFloatList:"); |
| 23 |
Shared.printMemory(); |
| 24 |
t.start(); |
| 25 |
FloatList list=new FloatList(); |
| 26 |
for(int i=0; i<length; i++){ |
| 27 |
list.add(i); |
| 28 |
} |
| 29 |
t.stop("Time: \t"); |
| 30 |
System.gc(); |
| 31 |
Shared.printMemory(); |
| 32 |
list=null; |
| 33 |
System.gc(); |
| 34 |
} |
| 35 |
|
| 36 |
{ |
| 37 |
System.err.println("\nArrayList:"); |
| 38 |
Shared.printMemory(); |
| 39 |
t.start(); |
| 40 |
ArrayList<Float> list=new ArrayList<Float>(); |
| 41 |
for(int i=0; i<length; i++){ |
| 42 |
list.add((float)i); |
| 43 |
} |
| 44 |
t.stop("Time: \t"); |
| 45 |
System.gc(); |
| 46 |
Shared.printMemory(); |
| 47 |
list=null; |
| 48 |
System.gc(); |
| 49 |
} |
| 50 |
|
| 51 |
{ |
| 52 |
System.err.println("\nLinkedList:"); |
| 53 |
Shared.printMemory(); |
| 54 |
t.start(); |
| 55 |
LinkedList<Float> list=new LinkedList<Float>(); |
| 56 |
for(int i=0; i<length; i++){ |
| 57 |
list.add((float)i); |
| 58 |
} |
| 59 |
t.stop("Time: \t"); |
| 60 |
System.gc(); |
| 61 |
Shared.printMemory(); |
| 62 |
list=null; |
| 63 |
System.gc(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public FloatList(){this(256);} |
| 68 |
|
| 69 |
public FloatList(int initial){ |
| 70 |
|
| 71 |
initial=Tools.max(initial, 1); |
| 72 |
array=KillSwitch.allocFloat1D(initial); |
| 73 |
} |
| 74 |
|
| 75 |
public FloatList copy() { |
| 76 |
FloatList copy=new FloatList(size); |
| 77 |
copy.addAll(this); |
| 78 |
return copy; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
public void clear(){size=0;} |
| 83 |
|
| 84 |
public final void set(int loc, float value){ |
| 85 |
if(loc>=array.length){ |
| 86 |
resize(loc*2L+1); |
| 87 |
} |
| 88 |
array[loc]=value; |
| 89 |
size=max(size, loc+1); |
| 90 |
} |
| 91 |
|
| 92 |
public final void setLast(float value){ |
| 93 |
assert(size>0); |
| 94 |
array[size-1]=value; |
| 95 |
} |
| 96 |
|
| 97 |
public final void increment(int loc, float value){ |
| 98 |
if(loc>=array.length){ |
| 99 |
resize(loc*2L+1); |
| 100 |
} |
| 101 |
array[loc]+=value; |
| 102 |
size=max(size, loc+1); |
| 103 |
} |
| 104 |
|
| 105 |
public final void increment(int loc){ |
| 106 |
increment(loc, 1); |
| 107 |
} |
| 108 |
|
| 109 |
public final void incrementBy(FloatList b){ |
| 110 |
for(int i=b.size-1; i>=0; i--){ |
| 111 |
increment(i, b.get(i)); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public final void incrementBy(float[] b){ |
| 116 |
for(int i=b.length-1; i>=0; i--){ |
| 117 |
increment(i, b[i]); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public final void append(FloatList b){ |
| 122 |
for(int i=0; i<b.size; i++){ |
| 123 |
add(b.get(i)); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public final void append(float[] b){ |
| 128 |
for(int i=0; i<b.length; i++){ |
| 129 |
add(b[i]); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public void subtractFrom(float value){ |
| 134 |
for(int i=0; i<size; i++){ |
| 135 |
array[i]=value-array[i]; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public final float get(int loc){ |
| 140 |
return(loc>=size ? 0 : array[loc]); |
| 141 |
} |
| 142 |
|
| 143 |
public float lastElement() { |
| 144 |
assert(size>0); |
| 145 |
return array[size-1]; |
| 146 |
} |
| 147 |
|
| 148 |
public final void add(float x){ |
| 149 |
if(size>=array.length){ |
| 150 |
resize(size*2L+1); |
| 151 |
} |
| 152 |
array[size]=x; |
| 153 |
size++; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
public boolean containsDuplicates(){ |
| 158 |
for(int i=0; i<size; i++){ |
| 159 |
for(int j=i+1; j<size; j++){ |
| 160 |
if(array[i]==array[j]){return true;} |
| 161 |
} |
| 162 |
} |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
public void addAll(FloatList counts) { |
| 167 |
final float[] array2=counts.array; |
| 168 |
final int size2=counts.size; |
| 169 |
for(int i=0; i<size2; i++){add(array2[i]);} |
| 170 |
} |
| 171 |
|
| 172 |
public boolean contains(float x) { |
| 173 |
for(int i=0; i<size; i++){ |
| 174 |
if(array[i]==x){return true;} |
| 175 |
} |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
public final void setSize(final int size2) { |
| 180 |
if(size2<array.length){resize(size2);} |
| 181 |
size=size2; |
| 182 |
} |
| 183 |
|
| 184 |
private final void resize(final long size2){ |
| 185 |
assert(size2>size) : size+", "+size2; |
| 186 |
final int size3=(int)Tools.min(Shared.MAX_ARRAY_LEN, size2); |
| 187 |
assert(size3>size) : "Overflow: "+size+", "+size2+" -> "+size3; |
| 188 |
array=KillSwitch.copyOf(array, size3); |
| 189 |
} |
| 190 |
|
| 191 |
public int maxIdx() { |
| 192 |
if(size<1) {return -1;} |
| 193 |
float max=array[0]; |
| 194 |
int maxIdx=0; |
| 195 |
for(int i=1; i<size; i++) { |
| 196 |
if(array[i]>max) { |
| 197 |
max=array[i]; |
| 198 |
maxIdx=i; |
| 199 |
} |
| 200 |
max=max(max, array[i]); |
| 201 |
} |
| 202 |
return maxIdx; |
| 203 |
} |
| 204 |
|
| 205 |
public float max() { |
| 206 |
if(size<1) {return 0;} |
| 207 |
float max=array[0]; |
| 208 |
for(int i=1; i<size; i++) {max=max(max, array[i]);} |
| 209 |
return max; |
| 210 |
} |
| 211 |
|
| 212 |
public final float stdev(){ |
| 213 |
if(size<2){return 0;} |
| 214 |
double sum=sum(); |
| 215 |
double avg=sum/size; |
| 216 |
double sumdev2=0; |
| 217 |
for(int i=0; i<size; i++){ |
| 218 |
double x=array[i]; |
| 219 |
double dev=avg-x; |
| 220 |
sumdev2+=(dev*dev); |
| 221 |
} |
| 222 |
return (float)Math.sqrt(sumdev2/size); |
| 223 |
} |
| 224 |
|
| 225 |
public final double sumLong(){ |
| 226 |
double sum=0; |
| 227 |
for(int i=0; i<size; i++){ |
| 228 |
sum+=array[i]; |
| 229 |
} |
| 230 |
return sum; |
| 231 |
} |
| 232 |
|
| 233 |
public final double sum(){ |
| 234 |
double sum=0; |
| 235 |
for(int i=0; i<size; i++){ |
| 236 |
sum+=array[i]; |
| 237 |
} |
| 238 |
return sum; |
| 239 |
} |
| 240 |
|
| 241 |
public final double mean(){ |
| 242 |
return size<1 ? 0 : sum()/size; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
public final double median(){ |
| 247 |
if(size<1){return 0;} |
| 248 |
int idx=percentileIndex(0.5f); |
| 249 |
return array[idx]; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
public final float mode(){ |
| 254 |
if(size<1){return 0;} |
| 255 |
assert(sorted()); |
| 256 |
int streak=1, bestStreak=0; |
| 257 |
float prev=array[0]; |
| 258 |
float best=prev; |
| 259 |
for(int i=0; i<size; i++){ |
| 260 |
float x=array[i]; |
| 261 |
if(x==prev){streak++;} |
| 262 |
else{ |
| 263 |
if(streak>bestStreak){ |
| 264 |
bestStreak=streak; |
| 265 |
best=prev; |
| 266 |
} |
| 267 |
streak=1; |
| 268 |
prev=x; |
| 269 |
} |
| 270 |
} |
| 271 |
if(streak>bestStreak){ |
| 272 |
bestStreak=streak; |
| 273 |
best=prev; |
| 274 |
} |
| 275 |
return best; |
| 276 |
} |
| 277 |
|
| 278 |
public float percentile(double fraction){ |
| 279 |
if(size<1){return 0;} |
| 280 |
int idx=percentileIndex(fraction); |
| 281 |
return array[idx]; |
| 282 |
} |
| 283 |
|
| 284 |
public int percentileIndex(double fraction){ |
| 285 |
if(size<2){return size-1;} |
| 286 |
assert(sorted()); |
| 287 |
double target=(sum()*fraction); |
| 288 |
double sum=0; |
| 289 |
for(int i=0; i<size; i++){ |
| 290 |
sum+=array[i]; |
| 291 |
if(sum>=target){ |
| 292 |
return i; |
| 293 |
} |
| 294 |
} |
| 295 |
return size-1; |
| 296 |
} |
| 297 |
|
| 298 |
public final FloatList shrink(){ |
| 299 |
if(size==array.length){return this;} |
| 300 |
array=KillSwitch.copyOf(array, size); |
| 301 |
return this; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public final void shrinkToUnique(){ |
| 307 |
condense(); |
| 308 |
shrink(); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public final void condense(){ |
| 314 |
if(size<=1){return;} |
| 315 |
|
| 316 |
int i=0, j=1; |
| 317 |
for(; j<size && array[i]<array[j]; i++, j++){} |
| 318 |
|
| 319 |
int dupes=0; |
| 320 |
for(; j<size; j++){ |
| 321 |
float a=array[i], b=array[j]; |
| 322 |
assert(a<=b) : "Unsorted: "+i+", "+j+", "+a+", "+b; |
| 323 |
if(b>a){ |
| 324 |
i++; |
| 325 |
array[i]=b; |
| 326 |
}else{ |
| 327 |
|
| 328 |
dupes++; |
| 329 |
assert(a==b); |
| 330 |
} |
| 331 |
} |
| 332 |
assert(dupes==(size-(i+1))); |
| 333 |
assert(size>=(i+1)); |
| 334 |
size=i+1; |
| 335 |
} |
| 336 |
|
| 337 |
public float[] toArray(){ |
| 338 |
return KillSwitch.copyOf(array, size); |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public String toString(){ |
| 343 |
return toStringListView(); |
| 344 |
} |
| 345 |
|
| 346 |
public String toStringSetView(){ |
| 347 |
StringBuilder sb=new StringBuilder(); |
| 348 |
sb.append('['); |
| 349 |
String comma=""; |
| 350 |
for(int i=0; i<size; i++){ |
| 351 |
if(array[i]!=0){ |
| 352 |
sb.append(comma+"("+i+", "+array[i]+")"); |
| 353 |
comma=", "; |
| 354 |
} |
| 355 |
} |
| 356 |
sb.append(']'); |
| 357 |
return sb.toString(); |
| 358 |
} |
| 359 |
|
| 360 |
public String toStringListView(){ |
| 361 |
StringBuilder sb=new StringBuilder(); |
| 362 |
sb.append('['); |
| 363 |
String comma=""; |
| 364 |
for(int i=0; i<size; i++){ |
| 365 |
sb.append(comma+array[i]); |
| 366 |
comma=", "; |
| 367 |
} |
| 368 |
sb.append(']'); |
| 369 |
return sb.toString(); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public void getUniqueCounts(FloatList counts) { |
| 376 |
counts.size=0; |
| 377 |
if(size<=0){return;} |
| 378 |
|
| 379 |
int unique=1; |
| 380 |
int count=1; |
| 381 |
|
| 382 |
for(int i=1; i<size; i++){ |
| 383 |
assert(array[i]>=array[i-1]); |
| 384 |
if(array[i]==array[i-1]){ |
| 385 |
count++; |
| 386 |
}else{ |
| 387 |
array[unique]=array[i]; |
| 388 |
unique++; |
| 389 |
counts.add(count); |
| 390 |
count=1; |
| 391 |
} |
| 392 |
} |
| 393 |
if(count>0){ |
| 394 |
counts.add(count); |
| 395 |
} |
| 396 |
size=unique; |
| 397 |
assert(counts.size==size); |
| 398 |
} |
| 399 |
|
| 400 |
public void sort() { |
| 401 |
if(size>1){Shared.sort(array, 0, size);} |
| 402 |
} |
| 403 |
|
| 404 |
public void reverse() { |
| 405 |
if(size>1){Tools.reverseInPlace(array, 0, size);} |
| 406 |
} |
| 407 |
|
| 408 |
public boolean sorted(){ |
| 409 |
for(int i=1; i<size; i++){ |
| 410 |
if(array[i]<array[i-1]){return false;} |
| 411 |
} |
| 412 |
return true; |
| 413 |
} |
| 414 |
|
| 415 |
public int size() { |
| 416 |
return size; |
| 417 |
} |
| 418 |
|
| 419 |
public boolean isEmpty() { |
| 420 |
return size<1; |
| 421 |
} |
| 422 |
|
| 423 |
public int capacity() { |
| 424 |
return array.length; |
| 425 |
} |
| 426 |
|
| 427 |
public int freeSpace() { |
| 428 |
return array.length-size; |
| 429 |
} |
| 430 |
|
| 431 |
private static final int min(int x, int y){return x<y ? x : y;} |
| 432 |
private static final int max(int x, int y){return x>y ? x : y;} |
| 433 |
private static final float max(float x, float y){return x>y ? x : y;} |
| 434 |
|
| 435 |
public float[] array; |
| 436 |
public int size=0; |
| 437 |
|
| 438 |
} |