Search in sources :

Example 1 with ObjectBytePair

use of org.eclipse.collections.api.tuple.primitive.ObjectBytePair in project narchy by automenta.

the class FastCompound method get.

public static FastCompound get(Op o, int subs, Iterable<Term> subterms) {
    ObjectByteHashMap<Term> atoms = new ObjectByteHashMap();
    DynBytes shadow = new DynBytes(256);
    // UncheckedBytes shadow = new UncheckedBytes(Bytes.wrapForWrite(new byte[256]));
    shadow.writeUnsignedByte(o.ordinal());
    shadow.writeUnsignedByte(subs);
    final byte[] numAtoms = { 0 };
    ByteFunction0 nextUniqueAtom = () -> numAtoms[0]++;
    int structure = o.bit, hashCode = 1;
    byte volume = 1;
    for (Term x : subterms) {
        x.recurseTerms((child) -> {
            shadow.writeUnsignedByte((byte) child.op().ordinal());
            if (child.op().atomic) {
                int aid = atoms.getIfAbsentPut(child, nextUniqueAtom);
                shadow.writeUnsignedByte((byte) aid);
            } else {
                shadow.writeUnsignedByte(child.subs());
            // TODO use last bit of the subs byte to indicate presence or absence of subsequent 'dt' value (32 bits)
            }
        });
        structure |= x.structure();
        hashCode = Util.hashCombine(hashCode, x.hashCode());
        volume += x.volume();
    }
    hashCode = Util.hashCombine(hashCode, o.id);
    assert (volume < 127);
    // TODO calculate normalized by the encountered sequence of variable id's - whether it is monotonically increasing by 1 each time the max value does increase or something
    boolean normalized = false;
    // TODO sort atoms to canonicalize its dictionary for sharing with other terms
    FastCompound y;
    // {
    // byte[][] a = new byte[atoms.size()][];
    // for (ObjectBytePair<Term> p : atoms.keyValuesView()) {
    // a[p.getTwo()] = IO.termToBytes(p.getOne());
    // }
    // y = new FastCompoundSerializedAtoms(a, shadow.toByteArray(), x.hashCode(), x.hashCodeSubTerms(), (byte) x.volume(), x.isNormalized());
    // }
    {
        Term[] a = new Term[atoms.size()];
        for (ObjectBytePair<Term> p : atoms.keyValuesView()) {
            a[p.getTwo()] = p.getOne();
        }
        y = new FastCompoundInstancedAtoms(a, shadow.toByteArray(), structure, hashCode, volume, normalized);
    }
    return y;
}
Also used : DynBytes(jcog.data.byt.DynBytes) ObjectBytePair(org.eclipse.collections.api.tuple.primitive.ObjectBytePair) ByteFunction0(org.eclipse.collections.api.block.function.primitive.ByteFunction0) Term(nars.term.Term) ObjectByteHashMap(org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap)

Aggregations

DynBytes (jcog.data.byt.DynBytes)1 Term (nars.term.Term)1 ByteFunction0 (org.eclipse.collections.api.block.function.primitive.ByteFunction0)1 ObjectBytePair (org.eclipse.collections.api.tuple.primitive.ObjectBytePair)1 ObjectByteHashMap (org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap)1