Search in sources :

Example 11 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project eclipse-collections by eclipse.

the class IntLongMapProbeTest method testRandomGet.

private void testRandomGet(MutableIntLongMap intlongMap, int keyCount) {
    Random random = new Random(0x123456789ABCDL);
    MutableIntSet set = new IntHashSet(keyCount);
    while (set.size() < keyCount) {
        set.add(random.nextInt());
    }
    int[] randomNumbersForMap = set.toArray();
    this.shuffle(randomNumbersForMap, random);
    for (int i = 0; i < keyCount; i++) {
        intlongMap.put(randomNumbersForMap[i], (long) (randomNumbersForMap[i] * 10));
    }
    for (int i = 0; i < intlongMap.size(); i++) {
        Assert.assertEquals((long) (randomNumbersForMap[i] * 10), intlongMap.get(randomNumbersForMap[i]));
    }
}
Also used : MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) Random(java.util.Random) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 12 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class GraphMeter method weakly.

// --------------------------------------------------------------------
/**
 * Returns the weakly connected cluster indexes with size as a value.
 * Cluster membership can be seen from the content of the array {@link #color};
 * each node has the cluster index as color. The cluster indexes carry no
 * information; we guarantee only that different clusters have different indexes.
 */
public List<IntHashSet> weakly(Graph g) {
    int size = g.size();
    this.g = g;
    if (cluster == null)
        cluster = new IntHashSet();
    if (color == null || color.length < size)
        color = new int[size];
    for (int i = 0; i < size; ++i) color[i] = WHITE;
    int actCluster = 0;
    int j;
    for (int i = 0; i < size; ++i) {
        if (color[i] != WHITE)
            continue;
        cluster.clear();
        // dfs is recursive, for large graphs not ok
        bfs(i);
        --actCluster;
        for (j = 0; j < size; ++j) {
            int cj = color[j];
            if (cj == BLACK || cluster.contains(cj))
                color[j] = actCluster;
        }
    }
    if (actCluster == 0)
        return Collections.emptyList();
    actCluster = -actCluster;
    List<IntHashSet> x = new FasterList(actCluster);
    for (int i = 0; i < actCluster; i++) {
        x.add(new IntHashSet(1));
    }
    for (j = 0; j < size; ++j) {
        x.get(-color[j] - 1).add(j);
    }
    return x;
}
Also used : FasterList(jcog.list.FasterList) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 13 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class UndirectedGraph method neighbors.

// ---------------------------------------------------------------
/**
 * Uses sets as collection so does not support multiple edges now, even if
 * the underlying directed graph does.
 */
@Override
public IntHashSet neighbors(int i) {
    IntHashSet result = new IntHashSet(g.neighbors(i));
    final int max = g.size();
    for (int j = 0; j < max; ++j) {
        if (g.isEdge(j, i))
            result.add(j);
    }
    return result;
}
Also used : IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 14 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class AdjGraphTest method testGraph1.

@Test
public void testGraph1() {
    AdjGraph<String, String> g = new AdjGraph<>(true);
    assertEquals(1, g.eid(0, 1));
    assertEquals(4294967296L, g.eid(1, 0));
    assertEquals(4294967297L, g.eid(1, 1));
    int xIndex = g.addNode("x");
    assertEquals(xIndex, g.addNode("x"));
    g.addNode("y");
    assertTrue(g.setEdge("x", "y", "xy"));
    // duplicate
    assertFalse(g.setEdge("x", "y", "xy"));
    assertEquals("xy", g.edge("x", "y"));
    assertFalse(g.setEdge("x", "z", "xz"));
    g.setEdge("y", "x", "yx");
    // unconnected
    g.addNode("z");
    assertTrue(g.removeEdge("x", "y"));
    assertNull(g.edge("x", "y"));
    g.writeGML(System.out);
    GraphMeter m = new GraphMeter();
    List<IntHashSet> wc = m.weakly(g);
    // assertEquals("[[0, 1], [2]]", wc.toString());
    // TODO strongly should return a similar kind of result as wc
    IntIntHashMap tj = m.strongly(g);
    System.out.println(tj);
}
Also used : IntIntHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet) Test(org.junit.jupiter.api.Test)

Example 15 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class ArithmeticIntroduction method apply.

public static Term apply(Term x, @Nullable Anon anon, Random rng) {
    if ((anon == null && !x.hasAny(INT)) || x.complexity() < 3)
        return x;
    // find all unique integer subterms
    IntHashSet ints = new IntHashSet();
    x.recurseTerms((t) -> {
        Int it = null;
        if (t instanceof Anom) {
            Anom aa = ((Anom) t);
            Term ta = anon.get(aa);
            if (ta.op() == INT)
                it = ((Int) ta);
        } else if (t instanceof Int) {
            it = (Int) t;
        }
        if (it == null)
            return;
        ints.add((it.id));
    });
    // Set<Term> ints = ((Compound) x).recurseTermsToSet(INT);
    int ui = ints.size();
    if (ui <= 1)
        // nothing to do
        return x;
    // increasing so that relational comparisons can assume that 'a' < 'b'
    int[] ii = ints.toSortedArray();
    // potential mods to select from
    // FasterList<Supplier<Term[]>> mods = new FasterList(1);
    IntObjectHashMap<List<Supplier<Term[]>>> mods = new IntObjectHashMap(ii.length);
    Variable v = $.varDep("x");
    // test arithmetic relationships
    for (int a = 0; a < ui; a++) {
        int ia = ii[a];
        for (int b = a + 1; b < ui; b++) {
            int ib = ii[b];
            assert (ib > ia);
            if (ib - ia < ia && (ia != 0)) {
                mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("add", v, $.the(ib - ia)) });
                mods.getIfAbsentPut(ib, FasterList::new).add(() -> new Term[] { Int.the(ia), $.func("add", v, $.the(ia - ib)) });
            } else if ((ia != 0 && ia != 1) && (ib != 0 && ib != 1) && Util.equals(ib / ia, (((float) ib) / ia), Float.MIN_NORMAL)) {
                mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("mul", v, $.the(ib / ia)) });
            } else if (ia == -ib) {
                // negation (x * -1)
                mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("mul", v, $.the(-1)) });
                mods.getIfAbsentPut(ib, FasterList::new).add(() -> new Term[] { Int.the(ia), $.func("mul", v, $.the(-1)) });
            }
        }
    }
    if (mods.isEmpty())
        return x;
    // TODO fair select randomly if multiple of the same length
    RichIterable<IntObjectPair<List<Supplier<Term[]>>>> mkv = mods.keyValuesView();
    int ms = mkv.maxBy(e -> e.getTwo().size()).getTwo().size();
    mkv.reject(e -> e.getTwo().size() < ms);
    // convention: choose lowest base
    MutableList<IntObjectPair<List<Supplier<Term[]>>>> mmm = mkv.toSortedListBy(IntObjectPair::getOne);
    IntObjectPair<List<Supplier<Term[]>>> m = mmm.get(0);
    int base = m.getOne();
    Term baseTerm = Int.the(base);
    if (anon != null)
        baseTerm = anon.put(baseTerm);
    Term yy = x.replace(baseTerm, v);
    for (Supplier<Term[]> s : m.getTwo()) {
        Term[] mm = s.get();
        if (anon != null)
            mm[0] = anon.put(mm[0]);
        yy = yy.replace(mm[0], mm[1]);
    }
    Term y = CONJ.the(yy, SIM.the(baseTerm, v));
    if (y.op() != CONJ) {
        // something happened
        return null;
    }
    if (x.isNormalized()) {
        y = y.normalize();
    }
    return y;
}
Also used : Variable(nars.term.var.Variable) IntObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap) FasterList(jcog.list.FasterList) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet) Term(nars.term.Term) Int(nars.term.atom.Int) IntObjectPair(org.eclipse.collections.api.tuple.primitive.IntObjectPair) Anom(nars.term.anon.Anom) FasterList(jcog.list.FasterList) MutableList(org.eclipse.collections.api.list.MutableList) List(java.util.List) Supplier(java.util.function.Supplier)

Aggregations

IntHashSet (org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)22 MutableIntSet (org.eclipse.collections.api.set.primitive.MutableIntSet)8 Random (java.util.Random)4 FasterList (jcog.list.FasterList)2 BitSet (java.util.BitSet)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Supplier (java.util.function.Supplier)1 ChangedTextInput (nars.op.in.ChangedTextInput)1 Term (nars.term.Term)1 Anom (nars.term.anon.Anom)1 Int (nars.term.atom.Int)1 Variable (nars.term.var.Variable)1 IntIterator (org.eclipse.collections.api.iterator.IntIterator)1 MutableIntIterator (org.eclipse.collections.api.iterator.MutableIntIterator)1 MutableList (org.eclipse.collections.api.list.MutableList)1 IntObjectPair (org.eclipse.collections.api.tuple.primitive.IntObjectPair)1 IntIntHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap)1 IntObjectHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap)1 Test (org.junit.Test)1