use of org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap 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;
}
use of org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap in project narchy by automenta.
the class DepIndepVarIntroduction method introduce.
@Nullable
@Override
protected Term introduce(Term input, Term selected, byte order) {
if (selected.equals(Imdex))
return null;
Op inOp = input.op();
List<ByteList> paths = $.newArrayList(1);
int minPathLength = inOp.statement ? 2 : 0;
input.pathsTo(selected, (path, t) -> {
if (path.size() >= minPathLength)
paths.add(path.toImmutable());
// TODO may be able to terminate early if we know this is the last one
return true;
});
int pSize = paths.size();
// if (pSize == 0)
if (pSize <= 1)
return null;
// byte[][] paths = pp.toArray(new byte[pSize][]);
// //detect an invalid top-level indep var substitution
// if (inOp.statement) {
// Iterator<byte[]> pp = paths.iterator();
// while (pp.hasNext()) {
// byte[] p = pp.next();
// if (p.length < 2)
// pp.remove();;
// for (byte[] r : paths)
// if (r.length < 2)
// return null; //substitution would replace something at the top level of a statement}
// }
// }
// use the innermost common parent to decide the type of variable.
// in case there is no other common parent besides input itself, use that.
Term commonParent = input.commonParent(paths);
Op commonParentOp = commonParent.op();
// decide what kind of variable can be introduced according to the input operator
boolean depOrIndep;
switch(commonParentOp) {
case CONJ:
depOrIndep = true;
break;
case IMPL:
depOrIndep = false;
break;
default:
// ????
return null;
}
ObjectByteHashMap<Term> m = new ObjectByteHashMap<>(0);
for (int path = 0; path < pSize; path++) {
ByteList p = paths.get(path);
// root
Term t = null;
int pathLength = p.size();
for (int i = -1; i < pathLength - 1; /* dont include the selected term itself */
i++) {
t = (i == -1) ? input : t.sub(p.get(i));
Op o = t.op();
if (!depOrIndep && validIndepVarSuperterm(o)) {
byte inside = (byte) (1 << p.get(i + 1));
m.updateValue(t, inside, (previous) -> (byte) ((previous) | inside));
} else if (depOrIndep && validDepVarSuperterm(o)) {
m.addToValue(t, (byte) 1);
}
}
}
if (!depOrIndep) {
// at least one impl/equiv must have both sides covered
return (m.anySatisfy(b -> b == 0b11)) ? $.v(VAR_INDEP, order) : /*varIndep(order)*/
null;
} else {
// at least one conjunction must contain >=2 path instances
return m.anySatisfy(b -> b >= 2) ? $.v(VAR_DEP, order) : /* $.varDep(order) */
null;
}
}
Aggregations