|
| 1 | +package org.fastfilter.quotient; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +public class InsertOrderTest { |
| 6 | + public static void main(String... args) { |
| 7 | + for(int size = 1_000_000; size <= 100_000_000; size *= 10) { |
| 8 | + long[] keys = new long[size]; |
| 9 | + for(int i=0; i<size; i++) { |
| 10 | + // guaranteed unique |
| 11 | + keys[i] = hash64(i, 0); |
| 12 | + } |
| 13 | + System.out.print(size + " unsorted keys:"); |
| 14 | + for (double fillRatePercent = 99; fillRatePercent > 50; fillRatePercent--) { |
| 15 | + boolean success = test(keys, fillRatePercent / 100.0); |
| 16 | + if (success) { |
| 17 | + System.out.println(" success at fill rate " + fillRatePercent + "%"); |
| 18 | + break; |
| 19 | + } |
| 20 | + } |
| 21 | + Arrays.sort(keys); |
| 22 | + System.out.print(size + " sorted keys:"); |
| 23 | + for (double fillRatePercent = 99; fillRatePercent > 50; fillRatePercent--) { |
| 24 | + boolean success = test(keys, fillRatePercent / 100.0); |
| 25 | + if (success) { |
| 26 | + System.out.println(" success at fill rate " + fillRatePercent + "%"); |
| 27 | + break; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static long hash64(long x, long seed) { |
| 34 | + x += seed; |
| 35 | + x = (x ^ (x >>> 33)) * 0xff51afd7ed558ccdL; |
| 36 | + x = (x ^ (x >>> 33)) * 0xc4ceb9fe1a85ec53L; |
| 37 | + x = x ^ (x >>> 33); |
| 38 | + return x; |
| 39 | + } |
| 40 | + |
| 41 | + public static int reduce(int hash, int n) { |
| 42 | + // http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ |
| 43 | + return (int) (((hash & 0xffffffffL) * n) >>> 32); |
| 44 | + } |
| 45 | + |
| 46 | + static boolean test(long[] keys, double fillRate) { |
| 47 | + // System.out.println("fillRate " + fillRate); |
| 48 | + int slotsPerBlock = 48; |
| 49 | + int slots = (int) (keys.length / fillRate); |
| 50 | + int blocks = (slots + slotsPerBlock) / slotsPerBlock; |
| 51 | + slots = blocks * slotsPerBlock; |
| 52 | + |
| 53 | + int[] used = new int[blocks]; |
| 54 | + for (int i = 0; i < keys.length; i++) { |
| 55 | +// if ((i % (keys.length / 10)) == 0) { |
| 56 | +// for (int j = 0; j < used.length; j += 10) { |
| 57 | +// System.out.print(used[j] / 10); |
| 58 | +// } |
| 59 | +// System.out.println(); |
| 60 | +// } |
| 61 | + long k = keys[i]; |
| 62 | + int index = reduce((int) k, blocks); |
| 63 | + int index2 = reduce((int) (k >> 32), blocks); |
| 64 | + if (used[index] < used[index2]) { |
| 65 | + used[index]++; |
| 66 | + if (used[index] > slotsPerBlock) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + } else { |
| 70 | + used[index2]++; |
| 71 | + if (used[index2] > slotsPerBlock) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return true; |
| 77 | + } |
| 78 | +} |
0 commit comments