Search in sources :

Example 6 with PeekableIntIterator

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

the class CountMinRoar method whileEachInt.

public void whileEachInt(IntIntPredicate each) {
    PeekableIntIterator s = set.getIntIterator();
    int next;
    while (s.hasNext() && each.accept(next = s.next(), count(next))) ;
}
Also used : PeekableIntIterator(org.roaringbitmap.PeekableIntIterator)

Example 7 with PeekableIntIterator

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

the class MatchConstraint method combineConstraints.

public static PrediTerm<Derivation> combineConstraints(AndCondition a) {
    RoaringBitmap constraints = new RoaringBitmap();
    @NotNull PrediTerm[] cond1 = a.cond;
    for (int i = 0, cl = cond1.length; i < cl; i++) {
        Term x = cond1[i];
        if (x instanceof MatchConstraint) {
            constraints.add(i);
        }
    }
    if (constraints.getCardinality() < 2) {
        return a;
    } else {
        // identify contiguous runs of constraints
        // inclusive
        List<IntIntPair> ranges = new FasterList<>(1);
        int start = -1, end = -1;
        PeekableIntIterator ii = constraints.getIntIterator();
        while (ii.hasNext()) {
            int next = ii.next();
            if (start == -1) {
                start = end = next;
            } else {
                if (next == end + 1) {
                    end++;
                } else {
                    if (end - start >= 1) {
                        // compile that range
                        ranges.add(pair(start, end));
                    }
                    // broken
                    start = -1;
                }
            }
        }
        if (end - start >= 1)
            ranges.add(pair(start, end));
        if (ranges.size() > 1)
            throw new TODO();
        IntIntPair rr = ranges.get(0);
        List<PrediTerm<Derivation>> l = new FasterList();
        int i;
        for (i = 0; i < start; i++) {
            l.add(a.cond[i]);
        }
        CompoundConstraint.the(Util.map(MatchConstraint.class::cast, MatchConstraint[]::new, ArrayUtils.subarray(a.cond, rr.getOne(), rr.getTwo() + 1))).forEach(l::add);
        i = end + 1;
        for (; i < a.cond.length; i++) {
            l.add(a.cond[i]);
        }
        return AndCondition.the((List) l);
    }
}
Also used : TODO(jcog.TODO) PrediTerm(nars.term.pred.PrediTerm) FasterList(jcog.list.FasterList) PeekableIntIterator(org.roaringbitmap.PeekableIntIterator) PrediTerm(nars.term.pred.PrediTerm) Term(nars.term.Term) Util(jcog.Util) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull) RoaringBitmap(org.roaringbitmap.RoaringBitmap) IntIntPair(org.eclipse.collections.api.tuple.primitive.IntIntPair)

Example 8 with PeekableIntIterator

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

the class TestRoaringBitmap method report128.

@Test
public void report128() {
    MutableRoaringBitmap bitmap = new MutableRoaringBitmap();
    bitmap.add(59798854);
    bitmap.add(91274955);
    bitmap.add(97569495);
    bitmap.add(101993170);
    PeekableIntIterator it = bitmap.getIntIterator();
    it.advanceIfNeeded(100620278);
    Assert.assertTrue(it.hasNext());
    Assert.assertEquals(101993170, it.next());
    Assert.assertFalse(it.hasNext());
}
Also used : PeekableIntIterator(org.roaringbitmap.PeekableIntIterator) Test(org.junit.Test)

Example 9 with PeekableIntIterator

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

the class TestIntIteratorFlyweight method testIterationFromBitmap.

@Test
public void testIterationFromBitmap() {
    final Random source = new Random(0xcb000a2b9b5bdfb6l);
    final int[] data = takeSortedAndDistinct(source, 450000);
    MutableRoaringBitmap bitmap = MutableRoaringBitmap.bitmapOf(data);
    BufferIntIteratorFlyweight iter = new BufferIntIteratorFlyweight(bitmap);
    Assert.assertEquals(iter.peekNext(), data[0]);
    Assert.assertEquals(iter.peekNext(), data[0]);
    BufferIntIteratorFlyweight iter2 = new BufferIntIteratorFlyweight(bitmap);
    PeekableIntIterator j = bitmap.getIntIterator();
    for (int k = 0; k < data.length; k += 3) {
        iter2.advanceIfNeeded(data[k]);
        iter2.advanceIfNeeded(data[k]);
        j.advanceIfNeeded(data[k]);
        j.advanceIfNeeded(data[k]);
        Assert.assertEquals(j.peekNext(), data[k]);
        Assert.assertEquals(iter2.peekNext(), data[k]);
    }
    BufferReverseIntIteratorFlyweight reverseIter = new BufferReverseIntIteratorFlyweight(bitmap);
    final List<Integer> intIteratorCopy = asList(iter);
    final List<Integer> reverseIntIteratorCopy = asList(reverseIter);
    Assert.assertEquals(bitmap.getCardinality(), intIteratorCopy.size());
    Assert.assertEquals(bitmap.getCardinality(), reverseIntIteratorCopy.size());
    Assert.assertEquals(Ints.asList(data), intIteratorCopy);
    Assert.assertEquals(Lists.reverse(Ints.asList(data)), reverseIntIteratorCopy);
}
Also used : Random(java.util.Random) PeekableIntIterator(org.roaringbitmap.PeekableIntIterator) Test(org.junit.Test)

Example 10 with PeekableIntIterator

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

the class TestIterators method testEmptySkips.

@Test
public void testEmptySkips() {
    MutableRoaringBitmap bitmap = new MutableRoaringBitmap();
    PeekableIntIterator it = bitmap.getIntIterator();
    it.advanceIfNeeded(0);
}
Also used : PeekableIntIterator(org.roaringbitmap.PeekableIntIterator) Test(org.junit.Test)

Aggregations

PeekableIntIterator (org.roaringbitmap.PeekableIntIterator)11 Test (org.junit.Test)8 Random (java.util.Random)3 TODO (jcog.TODO)1 Util (jcog.Util)1 FasterList (jcog.list.FasterList)1 Term (nars.term.Term)1 PrediTerm (nars.term.pred.PrediTerm)1 IntIntPair (org.eclipse.collections.api.tuple.primitive.IntIntPair)1 NotNull (org.jetbrains.annotations.NotNull)1 RoaringBitmap (org.roaringbitmap.RoaringBitmap)1