Search in sources :

Example 1 with IntIterator

use of org.eclipse.collections.api.iterator.IntIterator in project narchy by automenta.

the class Polygon method selectNextEdge.

/**
 * find the next edge, for monotone polygon searching purpose;
 * for any given edge, find the next edge we should choose when searching for
 * monotone polygon pieces;
 * auxiliary function to find monotone polygon pieces
 * C++ code: return unsigned int, eid also unsigned, same for nexte_ccw, nexte_cw
 */
private int selectNextEdge(Linebase edge) {
    int eid = edge.endPoint(1).id;
    IntHashSet edges = getSetFromStartAdjEdgeMap(eid);
    int numEdges = edges.size();
    assert (numEdges != 0);
    int nexte = 0;
    if (numEdges == 1)
        nexte = (edges.intIterator().next());
    else {
        // int[] edgesKeys = edges.toSortedArray();
        int nexte_ccw = 0, nexte_cw = 0;
        // max min of cos(alfa)
        double max = -2.0, min = 2.0;
        Linebase iEdge;
        IntIterator iter = edges.toSortedList().intIterator();
        int it;
        while (iter.hasNext()) {
            it = iter.next();
            if (it == edge.id())
                continue;
            iEdge = getEdge(it);
            double[] A = { 0, 0 }, B = { 0, 0 }, C = { 0, 0 };
            A[0] = edge.endPoint(0).x;
            A[1] = edge.endPoint(0).y;
            B[0] = edge.endPoint(1).x;
            B[1] = edge.endPoint(1).y;
            if (!edge.endPoint(1).equals(iEdge.endPoint(0)))
                iEdge.reverse();
            C[0] = iEdge.endPoint(1).x;
            C[1] = iEdge.endPoint(1).y;
            double area = Poly2TriUtils.orient2d(A, B, C);
            double cosb = angleCosb(A, B, C);
            if (area > 0 && max < cosb) {
                nexte_ccw = it;
                max = cosb;
            } else if (min > cosb) {
                nexte_cw = it;
                min = cosb;
            }
        }
        nexte = (nexte_ccw != 0) ? nexte_ccw : nexte_cw;
    }
    return nexte;
}
Also used : MutableIntIterator(org.eclipse.collections.api.iterator.MutableIntIterator) IntIterator(org.eclipse.collections.api.iterator.IntIterator) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 2 with IntIterator

use of org.eclipse.collections.api.iterator.IntIterator in project eclipse-collections by eclipse.

the class IntInterval method zip.

@Override
public <T> ImmutableList<IntObjectPair<T>> zip(Iterable<T> iterable) {
    int size = this.size();
    int othersize = Iterate.sizeOf(iterable);
    MutableList<IntObjectPair<T>> target = Lists.mutable.withInitialCapacity(Math.min(size, othersize));
    IntIterator iterator = this.intIterator();
    Iterator<T> otherIterator = iterable.iterator();
    for (int i = 0; i < size && otherIterator.hasNext(); i++) {
        target.add(PrimitiveTuples.pair(iterator.next(), otherIterator.next()));
    }
    return target.toImmutable();
}
Also used : IntObjectPair(org.eclipse.collections.api.tuple.primitive.IntObjectPair) IntIterator(org.eclipse.collections.api.iterator.IntIterator)

Example 3 with IntIterator

use of org.eclipse.collections.api.iterator.IntIterator in project eclipse-collections by eclipse.

the class IntIntervalTest method iterator_throws.

@Test(expected = NoSuchElementException.class)
public void iterator_throws() {
    IntIterator iterator = this.intInterval.intIterator();
    while (iterator.hasNext()) {
        iterator.next();
    }
    iterator.next();
}
Also used : IntIterator(org.eclipse.collections.api.iterator.IntIterator) Test(org.junit.Test)

Example 4 with IntIterator

use of org.eclipse.collections.api.iterator.IntIterator in project eclipse-collections by eclipse.

the class IntIntervalTest method intervalIterator.

@Test
public void intervalIterator() {
    IntInterval zero = IntInterval.zero();
    IntIterator zeroIterator = zero.intIterator();
    Assert.assertTrue(zeroIterator.hasNext());
    Assert.assertEquals(0, zeroIterator.next());
    Assert.assertFalse(zeroIterator.hasNext());
    IntInterval oneToFive = IntInterval.oneTo(5);
    IntIterator oneToFiveIterator = oneToFive.intIterator();
    for (int i = 1; i < 6; i++) {
        Assert.assertTrue(oneToFiveIterator.hasNext());
        Assert.assertEquals(i, oneToFiveIterator.next());
    }
    Verify.assertThrows(NoSuchElementException.class, (Runnable) oneToFiveIterator::next);
    IntInterval threeToNegativeThree = IntInterval.fromTo(3, -3);
    IntIterator threeToNegativeThreeIterator = threeToNegativeThree.intIterator();
    for (int i = 3; i > -4; i--) {
        Assert.assertTrue(threeToNegativeThreeIterator.hasNext());
        Assert.assertEquals(i, threeToNegativeThreeIterator.next());
    }
    Verify.assertThrows(NoSuchElementException.class, (Runnable) threeToNegativeThreeIterator::next);
}
Also used : IntIterator(org.eclipse.collections.api.iterator.IntIterator) Test(org.junit.Test)

Example 5 with IntIterator

use of org.eclipse.collections.api.iterator.IntIterator in project narchy by automenta.

the class GraphMeter method multicast.

// --------------------------------------------------------------------
/**
 * Performs anti-entropy epidemic multicasting from node 0.
 * As a result the number of nodes that have been reached in cycle i
 * is put into <code>b[i]</code>.
 * The number of cycles performed is determined by <code>b.length</code>.
 * In each cycle each node contacts a random neighbour and exchanges
 * information. The simulation is generational: when a node contacts a neighbor
 * in cycle i, it sees their state as in cycle i-1, besides, all nodes update
 * their state at the same time point, synchronously.
 */
public static void multicast(Graph g, int[] b, Random r) {
    int[] c1 = new int[g.size()];
    int[] c2 = new int[g.size()];
    for (int i = 0; i < c1.length; ++i) c2[i] = c1[i] = WHITE;
    c2[0] = c1[0] = BLACK;
    int k = 0;
    for (int black = 1; k < b.length || black < g.size(); ++k) {
        for (int i = 0; i < c2.length; ++i) {
            MutableIntCollection neighbours = g.neighbors(i);
            IntIterator it = neighbours.intIterator();
            for (int j = r.nextInt(neighbours.size()); j > 0; --j) it.next();
            int randn = it.next();
            // push pull exchane with random neighbour
            if (// c2[i] is black too
            c1[i] == BLACK) {
                if (c2[randn] == WHITE)
                    ++black;
                c2[randn] = BLACK;
            } else if (c1[randn] == BLACK) {
                if (c2[i] == WHITE)
                    ++black;
                c2[i] = BLACK;
            }
        }
        System.arraycopy(c2, 0, c1, 0, c1.length);
        b[k] = black;
    }
    for (; k < b.length; ++k) b[k] = g.size();
}
Also used : MutableIntCollection(org.eclipse.collections.api.collection.primitive.MutableIntCollection) IntIterator(org.eclipse.collections.api.iterator.IntIterator)

Aggregations

IntIterator (org.eclipse.collections.api.iterator.IntIterator)10 IntIntPair (org.eclipse.collections.api.tuple.primitive.IntIntPair)3 Test (org.junit.Test)3 IntIterable (org.eclipse.collections.api.IntIterable)1 LazyIntIterable (org.eclipse.collections.api.LazyIntIterable)1 MutableIntCollection (org.eclipse.collections.api.collection.primitive.MutableIntCollection)1 MutableIntIterator (org.eclipse.collections.api.iterator.MutableIntIterator)1 MutableIntList (org.eclipse.collections.api.list.primitive.MutableIntList)1 IntObjectPair (org.eclipse.collections.api.tuple.primitive.IntObjectPair)1 ReverseIntIterable (org.eclipse.collections.impl.lazy.primitive.ReverseIntIterable)1 AbstractIntIterable (org.eclipse.collections.impl.primitive.AbstractIntIterable)1 IntHashSet (org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)1