Search in sources :

Example 26 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project RoaringBitmap by RoaringBitmap.

the class CompressionResults method testAlternating.

public static void testAlternating() {
    System.out.println("Alternating case... universe = [0," + universe_size + ")");
    RoaringBitmap r = new RoaringBitmap();
    for (int i = 1; i < universe_size; i++) {
        if (i % 2 == 0)
            r.add(i);
    }
    System.out.println("Adding all even values in the universe");
    System.out.println("As a bitmap it would look like 01010101... ");
    System.out.println("Bits used per value = " + F.format(r.getSizeInBytes() * 8.0 / r.getCardinality()));
    r.runOptimize();
    System.out.println("Bits used per value after run optimize = " + F.format(r.getSizeInBytes() * 8.0 / r.getCardinality()));
    System.out.println("An uncompressed bitset might use " + F.format(universe_size * 1.0 / r.getCardinality()) + " bits per value set");
    System.out.println();
}
Also used : RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Example 27 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method flip.

/**
 * Add the value if it is not already present, otherwise remove it.
 *
 * @param x long value
 */
public void flip(final long x) {
    int high = RoaringIntPacking.high(x);
    BitmapDataProvider lowBitmap = highToBitmap.get(high);
    if (lowBitmap == null) {
        // The value is not added: add it without any flip specific code
        addLong(x);
    } else {
        int low = RoaringIntPacking.low(x);
        // TODO Is it relevant to calling .flip with a cast?
        if (lowBitmap instanceof RoaringBitmap) {
            ((RoaringBitmap) lowBitmap).flip(low);
        } else if (lowBitmap instanceof MutableRoaringBitmap) {
            ((MutableRoaringBitmap) lowBitmap).flip(low);
        } else {
            // Fallback to a manual flip
            if (lowBitmap.contains(low)) {
                lowBitmap.remove(low);
            } else {
                lowBitmap.add(low);
            }
        }
    }
    invalidateAboveHigh(high);
}
Also used : MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap) BitmapDataProvider(org.roaringbitmap.BitmapDataProvider) RoaringBitmap(org.roaringbitmap.RoaringBitmap) MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap)

Example 28 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project RoaringBitmap by RoaringBitmap.

the class Roaring64NavigableMap method add.

/**
 * Add to the current bitmap all longs in [rangeStart,rangeEnd).
 *
 * @param rangeStart inclusive beginning of range
 * @param rangeEnd exclusive ending of range
 */
public void add(final long rangeStart, final long rangeEnd) {
    int startHigh = high(rangeStart);
    int startLow = low(rangeStart);
    int endHigh = high(rangeEnd);
    int endLow = low(rangeEnd);
    for (int high = startHigh; high <= endHigh; high++) {
        final int currentStartLow;
        if (startHigh == high) {
            // The whole range starts in this bucket
            currentStartLow = startLow;
        } else {
            // Add the bucket from the beginning
            currentStartLow = 0;
        }
        long startLowAsLong = Util.toUnsignedLong(currentStartLow);
        final long endLowAsLong;
        if (endHigh == high) {
            // The whole range ends in this bucket
            endLowAsLong = Util.toUnsignedLong(endLow);
        } else {
            // Add the bucket until the end: we have a +1 as, in RoaringBitmap.add(long,long), the end
            // is excluded
            endLowAsLong = Util.toUnsignedLong(-1) + 1;
        }
        if (endLowAsLong > startLowAsLong) {
            // Initialize the bitmap only if there is access data to write
            BitmapDataProvider bitmap = highToBitmap.get(high);
            if (bitmap == null) {
                bitmap = new MutableRoaringBitmap();
                pushBitmapForHigh(high, bitmap);
            }
            if (bitmap instanceof RoaringBitmap) {
                ((RoaringBitmap) bitmap).add(startLowAsLong, endLowAsLong);
            } else if (bitmap instanceof MutableRoaringBitmap) {
                ((MutableRoaringBitmap) bitmap).add(startLowAsLong, endLowAsLong);
            } else {
                throw new UnsupportedOperationException("TODO. Not for " + bitmap.getClass());
            }
        }
    }
    invalidateAboveHigh(startHigh);
}
Also used : MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap) BitmapDataProvider(org.roaringbitmap.BitmapDataProvider) RoaringBitmap(org.roaringbitmap.RoaringBitmap) MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap)

Example 29 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project narchy by automenta.

the class Revision method mergeTemporal.

@Nullable
public static Task mergeTemporal(NAR nar, long start, long end, FasterList<TaskRegion> tt) {
    // filter the task set:
    // if there are any exact matches to the interval, remove any others
    RoaringBitmap oob = new RoaringBitmap();
    for (int i = 0, ttSize = tt.size(); i < ttSize; i++) {
        TaskRegion x = tt.get(i);
        if (x == null || !x.intersects(start, end))
            oob.add(i);
    }
    int numRemoved = oob.getCardinality();
    if (numRemoved != 0 && numRemoved != tt.size()) {
        IntIterator ii = oob.getReverseIntIterator();
        while (ii.hasNext()) {
            tt.remove(ii.next());
        }
    }
    return mergeTemporal(nar, tt.array(), tt.size());
}
Also used : IntIterator(org.roaringbitmap.IntIterator) RoaringBitmap(org.roaringbitmap.RoaringBitmap) TaskRegion(nars.task.util.TaskRegion) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with RoaringBitmap

use of org.roaringbitmap.RoaringBitmap in project narchy by automenta.

the class Conj method term.

private Term term(long when, Object what, IntPredicate validator) {
    if (what == null)
        return null;
    final RoaringBitmap rb;
    final byte[] b;
    int n;
    if (what instanceof byte[]) {
        b = (byte[]) what;
        rb = null;
        n = indexOfZeroTerminated(b, (byte) 0);
        if (n == 1) {
            // simplest case
            return sub(b[0], null, validator);
        }
    } else {
        rb = (RoaringBitmap) what;
        b = null;
        n = rb.getCardinality();
    }
    final boolean[] negatives = { false };
    TreeSet<Term> t = new TreeSet();
    if (b != null) {
        for (byte x : b) {
            if (x == 0)
                // done
                break;
            t.add(sub(x, negatives, validator));
        }
    } else {
        rb.forEach((int termIndex) -> {
            t.add(sub(termIndex, negatives, validator));
        });
    }
    if (negatives[0] && n > 1) {
        // annihilate common terms inside and outside of disjunction
        // ex:
        // -X &&  ( X ||  Y)
        // -X && -(-X && -Y)  |-   -X && Y
        Iterator<Term> oo = t.iterator();
        List<Term> csa = null;
        while (oo.hasNext()) {
            Term x = oo.next();
            if (x.hasAll(NEG.bit | CONJ.bit)) {
                if (x.op() == NEG) {
                    Term x0 = x.sub(0);
                    if (x0.op() == CONJ && CONJ.commute(x0.dt(), x0.subs())) {
                        // DISJUNCTION
                        Term disj = x.unneg();
                        SortedSet<Term> disjSubs = disj.subterms().toSetSorted();
                        // factor out occurrences of the disj's contents outside the disjunction, so remove from inside it
                        if (disjSubs.removeAll(t)) {
                            // reconstruct disj if changed
                            oo.remove();
                            if (!disjSubs.isEmpty()) {
                                if (csa == null)
                                    csa = new FasterList(1);
                                csa.add(CONJ.the(disj.dt(), sorted(disjSubs)).neg());
                            }
                        }
                    }
                }
            }
        }
        if (csa != null)
            t.addAll(csa);
    }
    int ts = t.size();
    switch(ts) {
        case 0:
            // throw new RuntimeException("fault");
            return True;
        case 1:
            return t.first();
        default:
            return Op.instance(CONJ, when == ETERNAL ? DTERNAL : 0, sorted(t));
    }
}
Also used : FasterList(jcog.list.FasterList) Term(nars.term.Term) RoaringBitmap(org.roaringbitmap.RoaringBitmap)

Aggregations

RoaringBitmap (org.roaringbitmap.RoaringBitmap)85 Benchmark (org.openjdk.jmh.annotations.Benchmark)14 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)9 DataOutputStream (java.io.DataOutputStream)8 MutableRoaringBitmap (org.roaringbitmap.buffer.MutableRoaringBitmap)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 DataInputStream (java.io.DataInputStream)7 IOException (java.io.IOException)7 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteBuffer (java.nio.ByteBuffer)6 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)6 BitmapDataProvider (org.roaringbitmap.BitmapDataProvider)6 ByteString (com.google.protobuf.ByteString)5 Setup (org.openjdk.jmh.annotations.Setup)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 FasterList (jcog.list.FasterList)4 Term (nars.term.Term)4 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)4