Search in sources :

Example 1 with MutableSet

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);
}
Also used : NegativeIntervalFunction(org.eclipse.collections.impl.block.function.NegativeIntervalFunction) Function(org.eclipse.collections.api.block.function.Function) Multimap(org.eclipse.collections.api.multimap.Multimap) UnsortedSetIterable(org.eclipse.collections.api.set.UnsortedSetIterable) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Verify(org.eclipse.collections.impl.test.Verify) MutableBag(org.eclipse.collections.api.bag.MutableBag) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) UnmodifiableMutableSet(org.eclipse.collections.impl.set.mutable.UnmodifiableMutableSet) LazyIterable(org.eclipse.collections.api.LazyIterable) UnifiedSetMultimap(org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Interval(org.eclipse.collections.impl.list.Interval) MutableSetMultimap(org.eclipse.collections.api.multimap.set.MutableSetMultimap) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) Comparators(org.eclipse.collections.impl.block.factory.Comparators) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Iterator(java.util.Iterator) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) SynchronizedMutableSet(org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet) Bags(org.eclipse.collections.impl.factory.Bags) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) FixedSizeSet(org.eclipse.collections.api.set.FixedSizeSet) List(java.util.List) Lists(org.eclipse.collections.impl.factory.Lists) ImmutableSet(org.eclipse.collections.api.set.ImmutableSet) Assert(org.junit.Assert) Collections(java.util.Collections) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) NegativeIntervalFunction(org.eclipse.collections.impl.block.function.NegativeIntervalFunction) Test(org.junit.Test)

Example 2 with MutableSet

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

Example 3 with MutableSet

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;
}
Also used : MutableSet(org.eclipse.collections.api.set.MutableSet) Variable(nars.term.var.Variable) IntObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap) Compound(nars.term.Compound) Term(nars.term.Term) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with MutableSet

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");
    }
}
Also used : Struct(alice.tuprolog.Struct) NumberTerm(alice.tuprolog.NumberTerm) Iterables(com.google.common.collect.Iterables) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) CONJ(nars.Op.CONJ) Var(alice.tuprolog.Var) nars.$(nars.$) Theory(alice.tuprolog.Theory) Util(jcog.Util) Variable(nars.term.var.Variable) MutableSet(org.eclipse.collections.api.set.MutableSet) TODO(jcog.TODO) Op(nars.Op) Termlike(nars.term.Termlike) Map(java.util.Map) Term(nars.term.Term) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) TODO(jcog.TODO) Variable(nars.term.var.Variable) NumberTerm(alice.tuprolog.NumberTerm) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Var(alice.tuprolog.Var) NumberTerm(alice.tuprolog.NumberTerm) Term(nars.term.Term) Struct(alice.tuprolog.Struct) Termlike(nars.term.Termlike) Variable(nars.term.var.Variable) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Term(nars.term.Term)

Example 5 with MutableSet

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);
}
Also used : NegativeIntervalFunction(org.eclipse.collections.impl.block.function.NegativeIntervalFunction) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) Procedures2(org.eclipse.collections.impl.block.factory.Procedures2) Multimap(org.eclipse.collections.api.multimap.Multimap) Twin(org.eclipse.collections.api.tuple.Twin) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterables.mSet(org.eclipse.collections.impl.factory.Iterables.mSet) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) UnifiedSetMultimap(org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap) MutableSet(org.eclipse.collections.api.set.MutableSet) Lists(org.eclipse.collections.impl.factory.Lists) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Sets(org.eclipse.collections.impl.factory.Sets) Tuples(org.eclipse.collections.impl.tuple.Tuples) Interval(org.eclipse.collections.impl.list.Interval) Assert(org.junit.Assert) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Before(org.junit.Before) NegativeIntervalFunction(org.eclipse.collections.impl.block.function.NegativeIntervalFunction) Test(org.junit.Test)

Aggregations

MutableSet (org.eclipse.collections.api.set.MutableSet)7 MutableList (org.eclipse.collections.api.list.MutableList)5 Procedures (org.eclipse.collections.impl.block.factory.Procedures)5 Lists (org.eclipse.collections.impl.factory.Lists)5 Interval (org.eclipse.collections.impl.list.Interval)5 FastList (org.eclipse.collections.impl.list.mutable.FastList)5 Verify (org.eclipse.collections.impl.test.Verify)5 Assert (org.junit.Assert)5 Test (org.junit.Test)5 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 NoSuchElementException (java.util.NoSuchElementException)4 LazyIterable (org.eclipse.collections.api.LazyIterable)4 RichIterable (org.eclipse.collections.api.RichIterable)4 Function (org.eclipse.collections.api.block.function.Function)4 UnsortedSetIterable (org.eclipse.collections.api.set.UnsortedSetIterable)4 Pair (org.eclipse.collections.api.tuple.Pair)4 Predicates (org.eclipse.collections.impl.block.factory.Predicates)4 Iterables.mList (org.eclipse.collections.impl.factory.Iterables.mList)4 Iterate (org.eclipse.collections.impl.utility.Iterate)4