Search in sources :

Example 6 with IntObjectHashMap

use of org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap 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 7 with IntObjectHashMap

use of org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap in project narchy by automenta.

the class Polygon method searchMonotones.

/**
 * searching all monotone pieces;
 * C++ code: unsigned - nexte
 *
 * @return success
 */
public boolean searchMonotones() {
    int loop = 0;
    IntObjectHashMap<Linebase> edges = new IntObjectHashMap(_edges);
    ArrayList poly;
    int[] edgesKeys;
    int i;
    int it;
    Linebase itEdge;
    Pointbase startp, endp;
    Linebase next;
    int nexte;
    while (edges.size() > _diagonals.size()) {
        loop++;
        // typedef list<unsigned int> Monopoly;
        poly = new ArrayList();
        edgesKeys = edges.keySet().toSortedArray();
        it = edgesKeys[0];
        itEdge = edges.get(it);
        // Pointbase* startp=startp=it.second.endPoint(0); // ??? startp=startp :-O
        startp = itEdge.endPoint(0);
        next = itEdge;
        poly.add(startp.id);
        if (_debug) {
        // writeToLog("Searching for loops:" + loop + '\n');
        // writeToLog("vertex index:" + startp.id + ' ');
        }
        for (; ; ) {
            endp = next.endPoint(1);
            if (next.type() != Poly2TriUtils.INSERT) {
                edges.remove(next.id());
                getSetFromStartAdjEdgeMap(next.endPoint(0).id).remove(next.id());
            }
            if (endp == startp)
                break;
            poly.add(endp.id);
            // writeToLog(endp.id + " ");
            nexte = selectNextEdge(next);
            if (nexte == 0) {
                System.out.println("Please check your input polygon:\n");
                System.out.println("1)orientations?\n2)with duplicated points?\n3)is a simple one?\n");
                System.out.println("poly2tri stopped.\n");
                return false;
            }
            next = edges.get(nexte);
            if (!(next.endPoint(0).equals(endp)))
                next.reverse();
        }
        // writeToLog("\nloop closed!\n\n");
        _mpolys.add(poly);
    }
    return true;
}
Also used : IntObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap) ArrayList(java.util.ArrayList)

Example 8 with IntObjectHashMap

use of org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap in project neo4j by neo4j.

the class SchemaComplianceCheckerTest method readPropertyValues.

private MutableIntObjectMap<Value> readPropertyValues(long nodeId) throws Exception {
    try (SafePropertyChainReader reader = new SafePropertyChainReader(context().withoutReporting(), CursorContext.NULL)) {
        NodeRecord node = loadNode(nodeId);
        MutableIntObjectMap<Value> values = new IntObjectHashMap<>();
        reader.read(values, node, reporter::forNode, CursorContext.NULL);
        return values;
    }
}
Also used : NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) IntObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap) Value(org.neo4j.values.storable.Value) Values.pointValue(org.neo4j.values.storable.Values.pointValue) TextValue(org.neo4j.values.storable.TextValue) Values.stringValue(org.neo4j.values.storable.Values.stringValue) PointValue(org.neo4j.values.storable.PointValue) Values.intValue(org.neo4j.values.storable.Values.intValue)

Aggregations

IntObjectHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap)8 Value (org.neo4j.values.storable.Value)5 CursorContext (org.neo4j.io.pagecache.context.CursorContext)4 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)4 ProgressListener (org.neo4j.internal.helpers.progress.ProgressListener)3 MutableIntSet (org.eclipse.collections.api.set.primitive.MutableIntSet)2 CacheAccess (org.neo4j.consistency.checking.cache.CacheAccess)2 EntityTokenRange (org.neo4j.kernel.impl.index.schema.EntityTokenRange)2 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)2 RelationshipRecord (org.neo4j.kernel.impl.store.record.RelationshipRecord)2 PointValue (org.neo4j.values.storable.PointValue)2 TextValue (org.neo4j.values.storable.TextValue)2 Values.intValue (org.neo4j.values.storable.Values.intValue)2 Values.pointValue (org.neo4j.values.storable.Values.pointValue)2 Values.stringValue (org.neo4j.values.storable.Values.stringValue)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1 FasterList (jcog.list.FasterList)1 Term (nars.term.Term)1