| 1 |
package structures; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import shared.KillSwitch; |
| 6 |
import shared.Shared; |
| 7 |
import shared.Tools; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
public final class LongList{ |
| 12 |
|
| 13 |
public static void main(String[] args){ |
| 14 |
LongList list=new LongList(); |
| 15 |
list.add(3); |
| 16 |
list.add(1); |
| 17 |
list.add(2); |
| 18 |
list.add(5); |
| 19 |
list.add(2); |
| 20 |
list.add(1); |
| 21 |
list.add(7); |
| 22 |
list.add(3); |
| 23 |
list.add(3); |
| 24 |
System.err.println(list); |
| 25 |
list.sort(); |
| 26 |
System.err.println(list); |
| 27 |
list.condense(); |
| 28 |
System.err.println(list); |
| 29 |
list.condense(); |
| 30 |
System.err.println(list); |
| 31 |
} |
| 32 |
|
| 33 |
public LongList(){this(256);} |
| 34 |
|
| 35 |
public LongList(int initial){ |
| 36 |
assert(initial>0) : initial; |
| 37 |
array=KillSwitch.allocLong1D(initial); |
| 38 |
} |
| 39 |
|
| 40 |
public void clear(){size=0;} |
| 41 |
public void clearFull(){ |
| 42 |
Arrays.fill(array, 0); |
| 43 |
size=0; |
| 44 |
} |
| 45 |
|
| 46 |
public final void set(int loc, long value){ |
| 47 |
if(loc>=array.length){ |
| 48 |
resize(loc*2L+1); |
| 49 |
} |
| 50 |
array[loc]=value; |
| 51 |
size=max(size, loc+1); |
| 52 |
} |
| 53 |
|
| 54 |
public final void setLast(long value){ |
| 55 |
assert(size>0); |
| 56 |
array[size-1]=value; |
| 57 |
} |
| 58 |
|
| 59 |
public final void increment(int loc, long value){ |
| 60 |
if(loc>=array.length){ |
| 61 |
resize(loc*2L+1); |
| 62 |
} |
| 63 |
array[loc]+=value; |
| 64 |
size=max(size, loc+1); |
| 65 |
} |
| 66 |
|
| 67 |
public final void increment(int loc){ |
| 68 |
increment(loc, 1); |
| 69 |
} |
| 70 |
|
| 71 |
public final void incrementBy(LongList b){ |
| 72 |
for(int i=b.size-1; i>=0; i--){ |
| 73 |
increment(i, b.get(i)); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public final void incrementBy(long[] b){ |
| 78 |
for(int i=b.length-1; i>=0; i--){ |
| 79 |
increment(i, b[i]); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public final void append(LongList b){ |
| 84 |
for(int i=0; i<b.size; i++){ |
| 85 |
add(b.get(i)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public final void append(long[] b){ |
| 90 |
for(int i=0; i<b.length; i++){ |
| 91 |
add(b[i]); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public final long get(int loc){ |
| 96 |
return(loc>=size ? 0 : array[loc]); |
| 97 |
} |
| 98 |
|
| 99 |
public final void add(long x){ |
| 100 |
if(size>=array.length){ |
| 101 |
resize(size*2L+1); |
| 102 |
} |
| 103 |
array[size]=x; |
| 104 |
size++; |
| 105 |
} |
| 106 |
|
| 107 |
public boolean contains(long x) { |
| 108 |
for(int i=0; i<size; i++) { |
| 109 |
if(array[i]==x) {return true;} |
| 110 |
} |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
public void addAll(LongList list2) { |
| 115 |
final long[] array2=list2.array; |
| 116 |
final int size2=list2.size; |
| 117 |
for(int i=0; i<size2; i++){add(array2[i]);} |
| 118 |
} |
| 119 |
|
| 120 |
private final void resize(final long size2){ |
| 121 |
assert(size2>size) : size+", "+size2; |
| 122 |
final int size3=(int)Tools.min(Shared.MAX_ARRAY_LEN, size2); |
| 123 |
assert(size3>size) : "Overflow: "+size+", "+size2+" -> "+size3; |
| 124 |
array=KillSwitch.copyOf(array, size3); |
| 125 |
} |
| 126 |
|
| 127 |
public final LongList shrink(){ |
| 128 |
if(size==array.length){return this;} |
| 129 |
array=KillSwitch.copyOf(array, size); |
| 130 |
return this; |
| 131 |
} |
| 132 |
|
| 133 |
public final double stdev(){ |
| 134 |
if(size<2){return 0;} |
| 135 |
double sum=sum(); |
| 136 |
double avg=sum/size; |
| 137 |
double sumdev2=0; |
| 138 |
for(int i=0; i<size; i++){ |
| 139 |
long x=array[i]; |
| 140 |
double dev=avg-x; |
| 141 |
sumdev2+=(dev*dev); |
| 142 |
} |
| 143 |
return Math.sqrt(sumdev2/size); |
| 144 |
} |
| 145 |
|
| 146 |
public final double avgDif(final double x){ |
| 147 |
double sum=0; |
| 148 |
for(int i=0; i<size; i++){ |
| 149 |
sum+=Tools.absdif(x, array[i]); |
| 150 |
} |
| 151 |
return sum/(Tools.max(1, size)); |
| 152 |
} |
| 153 |
|
| 154 |
public final double rmsDif(final double x){ |
| 155 |
double sum=0; |
| 156 |
for(int i=0; i<size; i++){ |
| 157 |
double dif=Tools.absdif(x, array[i]); |
| 158 |
sum+=dif*dif; |
| 159 |
} |
| 160 |
return Math.sqrt(sum/(Tools.max(1, size))); |
| 161 |
} |
| 162 |
|
| 163 |
public final long sumLong(){ |
| 164 |
long sum=0; |
| 165 |
for(int i=0; i<size; i++){ |
| 166 |
sum+=array[i]; |
| 167 |
} |
| 168 |
return sum; |
| 169 |
} |
| 170 |
|
| 171 |
public final long sumHist(){ |
| 172 |
long sum=0; |
| 173 |
for(int i=0; i<size; i++){ |
| 174 |
sum+=array[i]*i; |
| 175 |
} |
| 176 |
return sum; |
| 177 |
} |
| 178 |
|
| 179 |
public final double sum(){ |
| 180 |
double sum=0; |
| 181 |
for(int i=0; i<size; i++){ |
| 182 |
sum+=array[i]; |
| 183 |
} |
| 184 |
return sum; |
| 185 |
} |
| 186 |
|
| 187 |
public final double mean(){ |
| 188 |
return size<1 ? 0 : sum()/size; |
| 189 |
} |
| 190 |
|
| 191 |
public final double meanHist(){ |
| 192 |
return size<1 ? 0 : sumHist()/sum(); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
public final double harmonicMean(){ |
| 197 |
double sum=0; |
| 198 |
int count=0; |
| 199 |
for(int i=0; i<size; i++){ |
| 200 |
if(array[i]>0){ |
| 201 |
sum+=1.0/array[i]; |
| 202 |
count++; |
| 203 |
} |
| 204 |
} |
| 205 |
double avg=sum/Tools.max(1, count); |
| 206 |
return 1.0/avg; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
public final double geometricMean(){ |
| 211 |
double sum=0; |
| 212 |
int count=0; |
| 213 |
for(int i=0; i<size; i++){ |
| 214 |
if(array[i]>0){ |
| 215 |
sum+=Math.log(array[i]); |
| 216 |
count++; |
| 217 |
} |
| 218 |
} |
| 219 |
double avg=sum/Tools.max(1, count); |
| 220 |
return Math.exp(avg); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
public final double medianWeightedAverage(){ |
| 225 |
if(size<1){return 0;} |
| 226 |
int half=size/2; |
| 227 |
long count=0; |
| 228 |
double sum=0; |
| 229 |
for(int i=0, j=size-1; i<half; i++, j--){ |
| 230 |
int mult=i+1; |
| 231 |
double incr=(array[i]+array[j])*mult; |
| 232 |
sum+=incr; |
| 233 |
count+=2*mult; |
| 234 |
} |
| 235 |
if((size&1)==1){ |
| 236 |
int mult=half+1; |
| 237 |
double incr=(array[half])*mult; |
| 238 |
sum+=incr; |
| 239 |
count+=2*mult; |
| 240 |
} |
| 241 |
return sum/count; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
public final long median(){ |
| 246 |
if(size<1){return 0;} |
| 247 |
int idx=percentileIndex(0.5); |
| 248 |
return array[idx]; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
public final long min(){ |
| 253 |
if(size<1){return 0;} |
| 254 |
long x=array[0]; |
| 255 |
for(int i=1; i<size; i++){ |
| 256 |
x=Tools.min(x, array[i]); |
| 257 |
} |
| 258 |
return x; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
public final long max(){ |
| 263 |
if(size<1){return 0;} |
| 264 |
long x=array[0]; |
| 265 |
for(int i=1; i<size; i++){ |
| 266 |
x=Tools.max(x, array[i]); |
| 267 |
} |
| 268 |
return x; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
public final long mode(){ |
| 273 |
if(size<1){return 0;} |
| 274 |
assert(sorted()); |
| 275 |
int streak=1, bestStreak=0; |
| 276 |
long prev=array[0]; |
| 277 |
long best=prev; |
| 278 |
for(int i=0; i<size; i++){ |
| 279 |
long x=array[i]; |
| 280 |
if(x==prev){streak++;} |
| 281 |
else{ |
| 282 |
if(streak>bestStreak){ |
| 283 |
bestStreak=streak; |
| 284 |
best=prev; |
| 285 |
} |
| 286 |
streak=1; |
| 287 |
prev=x; |
| 288 |
} |
| 289 |
} |
| 290 |
if(streak>bestStreak){ |
| 291 |
bestStreak=streak; |
| 292 |
best=prev; |
| 293 |
} |
| 294 |
return best; |
| 295 |
} |
| 296 |
|
| 297 |
public long percentile(double fraction){ |
| 298 |
if(size<1){return 0;} |
| 299 |
int idx=percentileIndex(fraction); |
| 300 |
return array[idx]; |
| 301 |
} |
| 302 |
|
| 303 |
public int percentileIndex(double fraction){ |
| 304 |
if(size<2){return size-1;} |
| 305 |
assert(sorted()); |
| 306 |
double target=(sum()*fraction); |
| 307 |
double sum=0; |
| 308 |
for(int i=0; i<size; i++){ |
| 309 |
sum+=array[i]; |
| 310 |
if(sum>=target){ |
| 311 |
return i; |
| 312 |
} |
| 313 |
} |
| 314 |
return size-1; |
| 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 |
public final void shrinkToUnique(){ |
| 347 |
condense(); |
| 348 |
shrink(); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public final void condense(){ |
| 354 |
if(size<=1){return;} |
| 355 |
|
| 356 |
int i=0, j=1; |
| 357 |
for(; j<size && array[i]<array[j]; i++, j++){} |
| 358 |
|
| 359 |
int dupes=0; |
| 360 |
for(; j<size; j++){ |
| 361 |
long a=array[i], b=array[j]; |
| 362 |
assert(a<=b) : "Unsorted: "+i+", "+j+", "+a+", "+b; |
| 363 |
if(b>a){ |
| 364 |
i++; |
| 365 |
array[i]=b; |
| 366 |
}else{ |
| 367 |
|
| 368 |
dupes++; |
| 369 |
assert(a==b); |
| 370 |
} |
| 371 |
} |
| 372 |
assert(dupes==(size-(i+1))); |
| 373 |
assert(size>=(i+1)); |
| 374 |
size=i+1; |
| 375 |
} |
| 376 |
|
| 377 |
public void removeElementAt(int i) { |
| 378 |
for(int j=i+1; j<size; i++, j++) { |
| 379 |
array[i]=array[j]; |
| 380 |
} |
| 381 |
size--; |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public String toString(){ |
| 386 |
return toStringListView(); |
| 387 |
} |
| 388 |
|
| 389 |
public String toStringSetView(){ |
| 390 |
StringBuilder sb=new StringBuilder(); |
| 391 |
sb.append('['); |
| 392 |
String comma=""; |
| 393 |
for(int i=0; i<size; i++){ |
| 394 |
if(array[i]!=0){ |
| 395 |
sb.append(comma+"("+i+", "+array[i]+")"); |
| 396 |
comma=", "; |
| 397 |
} |
| 398 |
} |
| 399 |
sb.append(']'); |
| 400 |
return sb.toString(); |
| 401 |
} |
| 402 |
|
| 403 |
public String toStringListView(){ |
| 404 |
StringBuilder sb=new StringBuilder(); |
| 405 |
sb.append('['); |
| 406 |
String comma=""; |
| 407 |
for(int i=0; i<size; i++){ |
| 408 |
sb.append(comma+array[i]); |
| 409 |
comma=", "; |
| 410 |
} |
| 411 |
sb.append(']'); |
| 412 |
return sb.toString(); |
| 413 |
} |
| 414 |
|
| 415 |
public long[] toArray(){ |
| 416 |
long[] x=KillSwitch.allocLong1D(size); |
| 417 |
for(int i=0; i<x.length; i++){ |
| 418 |
x[i]=array[i]; |
| 419 |
} |
| 420 |
return x; |
| 421 |
} |
| 422 |
|
| 423 |
public void sort() { |
| 424 |
if(size>1){Shared.sort(array, 0, size);} |
| 425 |
} |
| 426 |
|
| 427 |
public void sortSerial() { |
| 428 |
if(size>1){Arrays.sort(array, 0, size);} |
| 429 |
} |
| 430 |
|
| 431 |
public void reverse() { |
| 432 |
if(size>1){Tools.reverseInPlace(array, 0, size);} |
| 433 |
} |
| 434 |
|
| 435 |
public boolean sorted(){ |
| 436 |
for(int i=1; i<size; i++){ |
| 437 |
if(array[i]<array[i-1]){return false;} |
| 438 |
} |
| 439 |
return true; |
| 440 |
} |
| 441 |
|
| 442 |
public int size() { |
| 443 |
return size; |
| 444 |
} |
| 445 |
|
| 446 |
public int capacity() { |
| 447 |
return array.length; |
| 448 |
} |
| 449 |
|
| 450 |
public int freeSpace() { |
| 451 |
return array.length-size; |
| 452 |
} |
| 453 |
|
| 454 |
public boolean isEmpty() { |
| 455 |
return size==0; |
| 456 |
} |
| 457 |
|
| 458 |
public int findIndex(long x) { |
| 459 |
for(int i=0; i<size; i++) { |
| 460 |
if(array[i]==x) {return i;} |
| 461 |
} |
| 462 |
return -1; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
public int findIndexAfter(long x) { |
| 467 |
for(int i=0; i<size; i++) { |
| 468 |
if(array[i]>x) {return i;} |
| 469 |
} |
| 470 |
return size; |
| 471 |
} |
| 472 |
|
| 473 |
public void capHist(final int max) { |
| 474 |
if(size<=max+1) {return;} |
| 475 |
|
| 476 |
long sum=0; |
| 477 |
for(int i=size-1; i>=max; i--) { |
| 478 |
sum+=array[i]; |
| 479 |
} |
| 480 |
array[max]=sum; |
| 481 |
size=max+1; |
| 482 |
} |
| 483 |
|
| 484 |
private static final long min(long x, long y){return x<y ? x : y;} |
| 485 |
private static final long max(long x, long y){return x>y ? x : y;} |
| 486 |
|
| 487 |
private static final int min(int x, int y){return x<y ? x : y;} |
| 488 |
private static final int max(int x, int y){return x>y ? x : y;} |
| 489 |
|
| 490 |
public long[] array; |
| 491 |
|
| 492 |
public int size=0; |
| 493 |
|
| 494 |
} |