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();
}
}
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;
}
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"));
}
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));
}
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());
}
Aggregations