|
| 1 | +package tech.v3.dataset; |
| 2 | + |
| 3 | +import clojure.lang.RT; |
| 4 | +import clojure.lang.IDeref; |
| 5 | +import java.util.function.LongConsumer; |
| 6 | +import ham_fisted.ArrayLists; |
| 7 | +import org.roaringbitmap.RoaringBitmap; |
| 8 | + |
| 9 | + |
| 10 | +public class ByteValidity { |
| 11 | + public static int trimIndexes(int[] indexes, int nIndexes, long maxIdx) { |
| 12 | + while(nIndexes > 0) { |
| 13 | + if(Integer.toUnsignedLong(indexes[nIndexes-1]) >= maxIdx) |
| 14 | + --nIndexes; |
| 15 | + else |
| 16 | + break; |
| 17 | + } |
| 18 | + return nIndexes; |
| 19 | + } |
| 20 | + public static abstract class ValidityBase implements LongConsumer, IDeref { |
| 21 | + long nElems; |
| 22 | + int idx; |
| 23 | + int nIndexes; |
| 24 | + int[] indexes; |
| 25 | + public ValidityBase(long nElems, long maxIndexes) { |
| 26 | + this.nElems = nElems; |
| 27 | + indexes = new int[(int)maxIndexes]; |
| 28 | + nIndexes = 0; |
| 29 | + idx = 0; |
| 30 | + } |
| 31 | + } |
| 32 | + public static class ValidityIndexReducer extends ValidityBase { |
| 33 | + public ValidityIndexReducer(long nElems, long maxIndexes) { |
| 34 | + super(nElems, maxIndexes); |
| 35 | + } |
| 36 | + public void accept(long value) { |
| 37 | + if(value != 0) { |
| 38 | + int intVal = (int)value; |
| 39 | + int offset = idx * 8; |
| 40 | + if( (intVal & 1) == 1) indexes[nIndexes++] = offset; |
| 41 | + if( (intVal & 2) == 2) indexes[nIndexes++] = offset+1; |
| 42 | + if( (intVal & 4) == 4) indexes[nIndexes++] = offset+2; |
| 43 | + if( (intVal & 8) == 8) indexes[nIndexes++] = offset+3; |
| 44 | + if( (intVal & 16) == 16) indexes[nIndexes++] = offset+4; |
| 45 | + if( (intVal & 32) == 32) indexes[nIndexes++] = offset+5; |
| 46 | + if( (intVal & 64) == 64) indexes[nIndexes++] = offset+6; |
| 47 | + if( (intVal & 128) == 128) indexes[nIndexes++] = offset+7; |
| 48 | + } |
| 49 | + ++idx; |
| 50 | + } |
| 51 | + public Object deref() { |
| 52 | + return ArrayLists.toList(indexes).subList(0, trimIndexes(indexes, nIndexes, nElems)); } |
| 53 | + } |
| 54 | + public static class MissingIndexReducer extends ValidityBase { |
| 55 | + public MissingIndexReducer(long nElems, long maxIndexes) { |
| 56 | + super(nElems, maxIndexes); |
| 57 | + } |
| 58 | + public void accept(long value) { |
| 59 | + if(value != -1) { |
| 60 | + int intVal = (int)value; |
| 61 | + int offset = idx * 8; |
| 62 | + if( (intVal & 1) != 1) indexes[nIndexes++] = offset; |
| 63 | + if( (intVal & 2) != 2) indexes[nIndexes++] = offset+1; |
| 64 | + if( (intVal & 4) != 4) indexes[nIndexes++] = offset+2; |
| 65 | + if( (intVal & 8) != 8) indexes[nIndexes++] = offset+3; |
| 66 | + if( (intVal & 16) != 16) indexes[nIndexes++] = offset+4; |
| 67 | + if( (intVal & 32) != 32) indexes[nIndexes++] = offset+5; |
| 68 | + if( (intVal & 64) != 64) indexes[nIndexes++] = offset+6; |
| 69 | + if( (intVal & 128) != 128) indexes[nIndexes++] = offset+7; |
| 70 | + } |
| 71 | + ++idx; |
| 72 | + } |
| 73 | + public Object deref() { |
| 74 | + RoaringBitmap rb = new RoaringBitmap(); |
| 75 | + rb.addN(indexes, 0, trimIndexes(indexes, nIndexes, nElems)); |
| 76 | + return rb; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments