use of org.eclipse.collections.api.set.MutableSet in project eclipse-collections by eclipse.
the class AbstractMemoryEfficientMutableSetTestCase method groupByEach.
@Test
public void groupByEach() {
MutableSet<Integer> set = this.classUnderTest().collect(Integer::valueOf);
MutableMultimap<Integer, Integer> expected = UnifiedSetMultimap.newMultimap();
set.forEach(Procedures.cast(value -> expected.putAll(-value, Interval.fromTo(value, set.size()))));
Multimap<Integer, Integer> actual = set.groupByEach(new NegativeIntervalFunction());
Assert.assertEquals(expected, actual);
Multimap<Integer, Integer> actualWithTarget = set.groupByEach(new NegativeIntervalFunction(), UnifiedSetMultimap.newMultimap());
Assert.assertEquals(expected, actualWithTarget);
}
use of org.eclipse.collections.api.set.MutableSet in project eclipse-collections by eclipse.
the class AbstractMutableSetTestCase method forEach.
@Override
@Test
public void forEach() {
super.forEach();
int size = MORE_COLLISIONS.size();
for (int i = 1; i < size; i++) {
MutableSet<Integer> set = this.newWith();
set.addAll(MORE_COLLISIONS.subList(0, i));
MutableSet<Integer> result = UnifiedSet.newSet();
set.forEach(CollectionAddProcedure.on(result));
Assert.assertEquals(set, result);
}
// 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();
set.forEach(Procedures.cast(each -> counter.increment()));
Assert.assertEquals(1, counter.getCount());
}
use of org.eclipse.collections.api.set.MutableSet in project narchy by automenta.
the class KIFInput method impl.
// private Variable nextVar(Op v) {
// return $.v(v, nextVar());
// }
// private final AtomicInteger serial = new AtomicInteger(0);
// private String nextVar() {
// return Integer.toString(Math.abs(serial.incrementAndGet()), 36);
// }
// public final Set<Twin<Term>> impl = new HashSet();
public Term impl(Term a, Term b, boolean implOrEquiv) {
// reduce as implication first
Term tmp = IMPL.the(a, b);
if (tmp.unneg().op() != IMPL) {
logger.warn("un-impl: {} ==> {} ", a, b);
return null;
}
tmp = tmp.unneg();
a = tmp.sub(0);
b = tmp.sub(1);
MutableSet<Term> aVars = new VarOnlySet();
if (a instanceof Compound)
((Compound) a).recurseTermsToSet(Op.VariableBits, aVars, true);
else if (a.op().var)
aVars.add(a);
MutableSet<Term> bVars = new VarOnlySet();
if (b instanceof Compound)
((Compound) b).recurseTermsToSet(Op.VariableBits, bVars, true);
else if (b.op().var)
bVars.add(b);
Map<Term, Term> remap = new HashMap();
MutableSet<Term> common = aVars.intersect(bVars);
if (!common.isEmpty()) {
common.forEach(t -> {
Variable u = $.v(Op.VAR_INDEP, // Op.VAR_PATTERN,
t.toString().substring(1));
if (!t.equals(u))
remap.put(t, u);
});
}
for (MutableSet<Term> ab : new MutableSet[] { aVars, bVars }) {
ab.forEach(aa -> {
if (aa.op() == VAR_INDEP && !common.contains(aa)) {
remap.put(aa, $.v(Op.VAR_DEP, aa.toString().substring(1)));
}
});
}
if (!remap.isEmpty()) {
a = a.replace(remap);
if (a == null)
throw new NullPointerException("transform failure");
b = b.replace(remap);
if (b == null)
throw new NullPointerException("transform failure");
}
try {
return implOrEquiv ? IMPL.the(a, b) : equi(a, b);
} catch (Exception ignore) {
ignore.printStackTrace();
}
return null;
}
use of org.eclipse.collections.api.set.MutableSet in project narchy by automenta.
the class PrologToNAL method N.
private static nars.term.Term N(alice.tuprolog.Term t) {
if (t instanceof alice.tuprolog.Term) {
Struct s = (Struct) t;
String name = s.name();
switch(name) {
case ":-":
assert (s.subs() == 2);
// reverse, prolog is backwards
nars.term.Term pre = N(s.sub(1));
nars.term.Term post = N(s.sub(0));
// convert to implication first, then promote variables on the resulting pre/post
Term impl = $.impl(pre, post);
pre = impl.sub(0);
post = impl.sub(1);
if (pre.varQuery() > 0 && post.varQuery() > 0) {
MutableSet<nars.term.var.Variable> prev = new UnifiedSet();
pre.recurseTerms(Termlike::hasVarQuery, (a) -> {
if (a.op() == Op.VAR_QUERY)
prev.add((Variable) a);
return true;
}, null);
MutableSet<nars.term.var.Variable> posv = new UnifiedSet();
post.recurseTerms(Termlike::hasVarQuery, (a) -> {
if (a.op() == Op.VAR_QUERY)
posv.add((Variable) a);
return true;
}, null);
MutableSet<nars.term.var.Variable> common = prev.intersect(posv);
int cs = common.size();
if (cs > 0) {
Map<nars.term.Term, nars.term.Term> x = new UnifiedMap(cs);
for (nars.term.var.Variable c : common) {
x.put(c, $.varIndep(c.toString().substring(1)));
}
impl = impl.replace(x);
}
}
return impl;
case ",":
return CONJ.the(N(s.sub(0)), N(s.sub(1)));
default:
nars.term.Term atom = $.the(name);
int arity = s.subs();
if (arity == 0) {
return atom;
} else {
return $.inh($.p((nars.term.Term[]) Util.map(0, arity, i -> N(s.sub(i)), nars.term.Term[]::new)), atom);
}
}
} else if (t instanceof Var) {
return $.varQuery(((Var) t).name());
// throw new RuntimeException(t + " untranslated");
} else if (t instanceof NumberTerm.Int) {
return $.the(((NumberTerm.Int) t).intValue());
} else {
throw new TODO(t + " (" + t.getClass() + ") untranslatable");
}
}
use of org.eclipse.collections.api.set.MutableSet in project eclipse-collections by eclipse.
the class DoubletonSetTest method groupByEach.
@Override
@Test
public void groupByEach() {
super.groupByEach();
MutableSet<Integer> set = Sets.fixedSize.of(1, 2);
MutableMultimap<Integer, Integer> expected = UnifiedSetMultimap.newMultimap();
set.forEach(Procedures.cast(value -> expected.putAll(-value, Interval.fromTo(value, set.size()))));
Multimap<Integer, Integer> actual = set.groupByEach(new NegativeIntervalFunction());
Assert.assertEquals(expected, actual);
Multimap<Integer, Integer> actualWithTarget = set.groupByEach(new NegativeIntervalFunction(), UnifiedSetMultimap.newMultimap());
Assert.assertEquals(expected, actualWithTarget);
}
Aggregations