Search in sources :

Example 6 with MutableList

use of org.eclipse.collections.api.list.MutableList in project narchy by automenta.

the class DynamicTruthBeliefTable method matchDT.

/**
 * returns an appropriate dt for the root term
 * of beliefs held in the table.  returns 0 if no other value can
 * be computed.
 */
protected int matchDT(long start, long end, boolean commutive, NAR nar) {
    int s = size();
    if (s == 0)
        return 0;
    int dur = nar.dur();
    IntFloatHashMap dtEvi = new IntFloatHashMap(s);
    forEachTask(t -> {
        int tdt = t.dt();
        if (tdt != DTERNAL) {
            if (tdt == XTERNAL)
                throw new RuntimeException("XTERNAL should not be present in " + t);
            if ((t.term().subs() > 2) == commutive)
                // maybe evi
                dtEvi.addToValue(tdt, t.evi(start, end, dur));
        }
    });
    int n = dtEvi.size();
    if (n == 0) {
        return 0;
    } else {
        MutableList<IntFloatPair> ll = dtEvi.keyValuesView().toList();
        int selected = n != 1 ? Roulette.decideRoulette(ll.size(), (i) -> ll.get(i).getTwo(), nar.random()) : 0;
        return ll.get(selected).getOne();
    }
}
Also used : CONJ(nars.Op.CONJ) DTERNAL(nars.time.Tense.DTERNAL) ArrayUtils(org.apache.commons.lang3.ArrayUtils) MutableList(org.eclipse.collections.api.list.MutableList) IntFloatPair(org.eclipse.collections.api.tuple.primitive.IntFloatPair) Roulette(jcog.decide.Roulette) Nullable(org.jetbrains.annotations.Nullable) Task(nars.Task) Truth(nars.truth.Truth) IntFloatHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntFloatHashMap) NAR(nars.NAR) Op(nars.Op) TemporalBeliefTable(nars.table.TemporalBeliefTable) Bool(nars.term.atom.Bool) XTERNAL(nars.time.Tense.XTERNAL) Term(nars.term.Term) IntFloatHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntFloatHashMap) IntFloatPair(org.eclipse.collections.api.tuple.primitive.IntFloatPair)

Example 7 with MutableList

use of org.eclipse.collections.api.list.MutableList 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)

Example 8 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class ParallelIterateTest method sumByBigDecimal.

@Test
public void sumByBigDecimal() {
    MutableList<BigDecimal> list = Interval.oneTo(100000).collect(BigDecimal::new).toList().shuffleThis();
    MutableMap<String, BigDecimal> sumByCount = ParallelIterate.sumByBigDecimal(list, EVEN_OR_ODD_BD, bd -> new BigDecimal(1L));
    Assert.assertEquals(BigDecimal.valueOf(50000L), sumByCount.get("Even"));
    Assert.assertEquals(BigDecimal.valueOf(50000L), sumByCount.get("Odd"));
    MutableMap<String, BigDecimal> sumByValue = ParallelIterate.sumByBigDecimal(list, EVEN_OR_ODD_BD, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list, EVEN_OR_ODD_BD, bd -> bd), sumByValue);
    MutableMap<Integer, BigDecimal> sumByValue2 = ParallelIterate.sumByBigDecimal(list, bd -> bd.intValue() % 1000, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list, bd -> bd.intValue() % 1000, bd -> bd), sumByValue2);
    MutableList<BigDecimal> list2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY).collect(BigDecimal::new).toList();
    MutableMap<String, BigDecimal> sumByValue3 = ParallelIterate.sumByBigDecimal(list2, EVEN_OR_ODD_BD, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list2, EVEN_OR_ODD_BD, bd -> bd), sumByValue3);
    MutableMap<Integer, BigDecimal> sumByValue4 = ParallelIterate.sumByBigDecimal(list2, bd -> bd.intValue() % 1000, bd -> bd);
    Assert.assertEquals(Iterate.sumByBigDecimal(list2, bd -> bd.intValue() % 1000, bd -> bd), sumByValue4);
    Interval small = Interval.oneTo(11);
    MutableMap<String, BigDecimal> smallSumByCount = ParallelIterate.sumByBigDecimal(small, EVEN_OR_ODD, i -> BigDecimal.valueOf(1L));
    Assert.assertEquals(new BigDecimal(5), smallSumByCount.get("Even"));
    Assert.assertEquals(new BigDecimal(6), smallSumByCount.get("Odd"));
}
Also used : Arrays(java.util.Arrays) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Multimap(org.eclipse.collections.api.multimap.Multimap) CompositeFastList(org.eclipse.collections.impl.list.mutable.CompositeFastList) HashingStrategies(org.eclipse.collections.impl.block.factory.HashingStrategies) Verify(org.eclipse.collections.impl.test.Verify) StringFunctions(org.eclipse.collections.impl.block.factory.StringFunctions) MutableList(org.eclipse.collections.api.list.MutableList) UnifiedSetWithHashingStrategy(org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy) BigDecimal(java.math.BigDecimal) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Functions(org.eclipse.collections.impl.block.factory.Functions) After(org.junit.After) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Interval(org.eclipse.collections.impl.list.Interval) BigInteger(java.math.BigInteger) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Collection(java.util.Collection) ObjectDoubleMap(org.eclipse.collections.api.map.primitive.ObjectDoubleMap) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Iterate(org.eclipse.collections.impl.utility.Iterate) Executors(java.util.concurrent.Executors) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) List(java.util.List) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) ObjectLongMap(org.eclipse.collections.api.map.primitive.ObjectLongMap) Lists(org.eclipse.collections.impl.factory.Lists) Function(org.eclipse.collections.api.block.function.Function) Bag(org.eclipse.collections.api.bag.Bag) Procedure(org.eclipse.collections.api.block.procedure.Procedure) ArrayListAdapter(org.eclipse.collections.impl.list.mutable.ArrayListAdapter) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) HashBagMultimap(org.eclipse.collections.impl.multimap.bag.HashBagMultimap) SynchronizedPutUnifiedSetMultimap(org.eclipse.collections.impl.multimap.set.SynchronizedPutUnifiedSetMultimap) Function2(org.eclipse.collections.api.block.function.Function2) SynchronizedPutHashBagMultimap(org.eclipse.collections.impl.multimap.bag.SynchronizedPutHashBagMultimap) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) Test(org.junit.Test) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) ImmutableList(org.eclipse.collections.api.list.ImmutableList) MapIterable(org.eclipse.collections.api.map.MapIterable) Assert(org.junit.Assert) Collections(java.util.Collections) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 9 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method removeIf.

@Override
@Test
public void removeIf() {
    super.removeIf();
    MutableSet<IntegerWithCast> set = this.newWith();
    MutableList<IntegerWithCast> collisions = COLLISIONS.collect(IntegerWithCast::new);
    set.addAll(collisions);
    collisions.reverseForEach(each -> {
        Assert.assertFalse(set.remove(null));
        Assert.assertTrue(set.remove(each));
        Assert.assertFalse(set.remove(each));
        Assert.assertFalse(set.remove(null));
        Assert.assertFalse(set.remove(new IntegerWithCast(COLLISION_10)));
    });
    Assert.assertEquals(UnifiedSet.<IntegerWithCast>newSet(), set);
    collisions.forEach(Procedures.cast(each -> {
        MutableSet<IntegerWithCast> set2 = this.newWith();
        set2.addAll(collisions);
        Assert.assertFalse(set2.remove(null));
        Assert.assertTrue(set2.remove(each));
        Assert.assertFalse(set2.remove(each));
        Assert.assertFalse(set2.remove(null));
        Assert.assertFalse(set2.remove(new IntegerWithCast(COLLISION_10)));
    }));
    // remove the second-to-last item in a fully populated single chain to cause the last item to move
    MutableSet<Integer> set3 = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Assert.assertTrue(set3.remove(COLLISION_3));
    Assert.assertEquals(UnifiedSet.newSetWith(COLLISION_1, COLLISION_2, COLLISION_4), set3);
    Assert.assertTrue(set3.remove(COLLISION_2));
    Assert.assertEquals(UnifiedSet.newSetWith(COLLISION_1, COLLISION_4), set3);
    // search a chain for a non-existent element
    MutableSet<Integer> chain = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4);
    Assert.assertFalse(chain.remove(COLLISION_5));
    // search a deep chain for a non-existent element
    MutableSet<Integer> deepChain = this.newWith(COLLISION_1, COLLISION_2, COLLISION_3, COLLISION_4, COLLISION_5, COLLISION_6, COLLISION_7);
    Assert.assertFalse(deepChain.remove(COLLISION_8));
    // search for a non-existent element
    MutableSet<Integer> empty = this.newWith();
    Assert.assertFalse(empty.remove(COLLISION_1));
}
Also used : Arrays(java.util.Arrays) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Function(org.eclipse.collections.api.block.function.Function) UnsortedSetIterable(org.eclipse.collections.api.set.UnsortedSetIterable) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) AbstractCollectionTestCase(org.eclipse.collections.impl.collection.mutable.AbstractCollectionTestCase) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) TreeBag(org.eclipse.collections.impl.bag.sorted.mutable.TreeBag) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Counter(org.eclipse.collections.impl.Counter) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) MutableSortedBag(org.eclipse.collections.api.bag.sorted.MutableSortedBag) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Iterables.iSet(org.eclipse.collections.impl.factory.Iterables.iSet) Lists(org.eclipse.collections.impl.factory.Lists) Assert(org.junit.Assert) Collections(java.util.Collections) MutableSet(org.eclipse.collections.api.set.MutableSet) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) Test(org.junit.Test)

Example 10 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class AbstractMutableSetTestCase method tap.

@Override
@Test
public void tap() {
    super.tap();
    int size = MORE_COLLISIONS.size();
    for (int i = 1; i < size; i++) {
        MutableList<Integer> tapResult = Lists.mutable.of();
        MutableSet<Integer> set = this.newWith();
        set.addAll(MORE_COLLISIONS.subList(0, i));
        Assert.assertSame(set, set.tap(tapResult::add));
        Assert.assertEquals(set.toList(), tapResult);
    }
    // test iterating on a bucket with only one element
    MutableSet<Integer> set = this.newWith(COLLISION_1, COLLISION_2);
    set.remove(COLLISION_2);
    Counter counter = new Counter();
    Assert.assertSame(set, set.tap(x -> counter.increment()));
    Assert.assertEquals(1, counter.getCount());
}
Also used : Arrays(java.util.Arrays) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) Function(org.eclipse.collections.api.block.function.Function) UnsortedSetIterable(org.eclipse.collections.api.set.UnsortedSetIterable) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) AbstractCollectionTestCase(org.eclipse.collections.impl.collection.mutable.AbstractCollectionTestCase) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) IntegerWithCast(org.eclipse.collections.impl.IntegerWithCast) TreeBag(org.eclipse.collections.impl.bag.sorted.mutable.TreeBag) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Counter(org.eclipse.collections.impl.Counter) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) MutableSortedBag(org.eclipse.collections.api.bag.sorted.MutableSortedBag) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Iterables.iSet(org.eclipse.collections.impl.factory.Iterables.iSet) Lists(org.eclipse.collections.impl.factory.Lists) Assert(org.junit.Assert) Collections(java.util.Collections) Counter(org.eclipse.collections.impl.Counter) Test(org.junit.Test)

Aggregations

MutableList (org.eclipse.collections.api.list.MutableList)11 Predicates (org.eclipse.collections.impl.block.factory.Predicates)8 Lists (org.eclipse.collections.impl.factory.Lists)8 Interval (org.eclipse.collections.impl.list.Interval)8 Verify (org.eclipse.collections.impl.test.Verify)8 Assert (org.junit.Assert)8 Test (org.junit.Test)8 Function (org.eclipse.collections.api.block.function.Function)7 Collections (java.util.Collections)6 RichIterable (org.eclipse.collections.api.RichIterable)6 Procedures (org.eclipse.collections.impl.block.factory.Procedures)6 Arrays (java.util.Arrays)5 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 ExecutorService (java.util.concurrent.ExecutorService)5 LazyIterable (org.eclipse.collections.api.LazyIterable)5 Predicates2 (org.eclipse.collections.impl.block.factory.Predicates2)5 CollectionAddProcedure (org.eclipse.collections.impl.block.procedure.CollectionAddProcedure)5 ArrayList (java.util.ArrayList)4 Executors (java.util.concurrent.Executors)4